<?php

#############################################################################
#                        Site@Service Calendar module
# Site@Service is a website management system designed for schools.
# This is a calendar module which can be used in Site@Service.
#
# Idea and design by Marinus Witteveen (marinus.witteveen@xs4all.nl)
# Altered for compatibility with S@S by Fred Stuurman (fc.stuurman@hccnet.nl)
#$Id: calendar.php,v 1.2 2006/10/15 19:08:54 fstuurman Exp $
############################################################################
# calendar.php: last changed 18/09/2005  version 1.0.4
#                  by Fred Stuurman (fc.stuurman@hccnet.nl)
#$Id: calendar.php,v 1.2 2006/10/15 19:08:54 fstuurman Exp $
############################################################################
#                        LICENSE INFORMATION
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
##############################################################################
if (!defined('IN_SAS'))
{
	die("Hacking attempt");
}
if (IsSet ($_GET[year]))
{
	$year = intval($_GET[year]);
}
else
{
	$year = date("Y");
}
if (IsSet ($_GET[month]))
{
	$month = intval($_GET[month]);
}
else
{
	$month = date("n");
}

load_language($cmsdir . '/', 'sn_calendar'); //load language file

$query = "SELECT dbtables FROM $table_modules WHERE location='sn_calendar'";
$result = mysql_query($query) or die_script($query, mysql_error());
$moduletables = mysql_result($result, 0);
unset ($query);
unset ($result);
$moduletables = explode(",", $moduletables);
$event_table = "$moduletables[0]";
$calendars_table = "$moduletables[1]";

$query = "SELECT id, options, title FROM $calendars_table WHERE name = '$pagetitle'";
$result = mysql_query($query) or die_script($query, mysql_error());
$num_rows = mysql_num_rows($result);
if ($num_rows == 0)
{
	print $sas_lang['calendar_page_error'];
}
else
{
	$cal_id = mysql_result($result, 0, 0);
	$calendar_options = mysql_result($result, 0, 1);
	$cal_title = mysql_result($result, 0, 2);
	unset ($query);
	unset ($result);
	$calendar_options = explode(",", $calendar_options);
	$header_color = $calendar_options[0];
	$day_color = $calendar_options[1];
	$active_day_color = $calendar_options[2];
	$table_bgcolor = $calendar_options[3];
	$day_color_today = $calendar_options[4];
	$font_color = $calendar_options[5];
	$table_width = $calendar_options[6];
	$cell_height = $calendar_options[7];

	$cell_height2 = $cell_height / 2;

	$d = date("j");
	$m = date("n");
	$y = date("Y");

	$query = "SELECT description FROM $event_table WHERE month='$month' AND year='$year' AND calendar='$cal_id'";
	$result = mysql_query($query) or die_script($query, mysql_error());
	if (mysql_num_rows($result) > 0)
	{
		$details = mysql_result($result, 0);
	}
	else
	{
		$details = "";
	}

	$arr_details = explode("\n", $details);
	$calender_item = array ();

	for ($i = 0; $i < count($arr_details); $i++)
	{
		$entry = explode("_", $arr_details[$i]);

		if ((strpos($entry[0], '-') == 1) OR (strpos($entry[0], '-') == 2)) //do we have string like 11-15_ then we have multiple days
		{
			$first = substr($entry[0], 0, strpos($entry[0], '-'));
			$last = substr($entry[0], strpos($entry[0], '-') + 1);

			for ($k = $first; $k < $last +1; $k++)
			{ //loop through the given period.
				$calender_item[$k] = $entry[1];
			}
		}
		else
		{
			if (array_key_exists($entry[0], $calender_item)) //lets see if we have this day already in our array
			{
				if (strpos($calender_item[$entry[0]], ',') == FALSE) //do we have a comma in first entry
				{
					$calender_item[$entry[0]] = trim($calender_item[$entry[0]]) . "<br>" . $entry[1];
				} //add it to the existing entry.
				else
				{ //break first entry inparts
					$cell_part1 = substr($calender_item[$entry[0]], 0, strpos($calender_item[$entry[0]], ','));
					$popup_part1 = substr($calender_item[$entry[0]], strpos($calender_item[$entry[0]], ',') + 1, -1);
					if (strpos($entry[1], ',') == FALSE) //do we have a comma in second entry
					{
						$cell_part2 = $entry[1];
						$popup_part2 = "";
					}
					else
					{
						$cell_part2 = substr($entry[1], 0, strpos($entry[1], ',')); //break second entry in parts    
						$popup_part2 = substr($entry[1], strpos($entry[1], ',') + 1, -1);
					}
					$calender_item[$entry[0]] = $cell_part1 . "<br>" . $cell_part2 . "," . $popup_part1 . "<br>" . $popup_part2 . " ";
				}
			}
			else
			{
				$calender_item[$entry[0]] = $entry[1]; //single day entry
			}
		}
	}

	$n_month = $month +1;
	if ($n_month > 12)
	{
		$n_month = 1;
		$n_year = $year +1;
	}
	else
	{
		$n_year = $year;
	}
	$p_month = $month -1;
	if ($p_month < 1)
	{
		$p_month = 12;
		$p_year = $year -1;
	}
	else
	{
		$p_year = $year;
	}

	print "<script type=\"text/javascript\" src=\"$cmsdir/modules/sn_calendar/popup.js\"></script>";

	if ($cal_title != "")
	{
		print $cal_title;
	}
?>
<DIV ID="d11" style="visibility:hidden;position:absolute;z-index:200">HELLO</DIV> 
<table width="<?php echo $table_width ?>" border="0" cellspacing="0" cellpadding="0" align="center" bgcolor="<?php echo $table_bgcolor ?>">
<tr height="20" bgcolor="<?php echo $header_color ?>">
<td><a href="index.php?page=<?php echo $page ?>&section=<?php echo $section ?>&month=<?php echo $p_month ?>&year=<?php echo $p_year ?>"><b><font color="<?php echo $font_color ?>"><<<<<</a></font></td>
<td colspan="4"><div align="center"><b><font color="<?php echo $font_color ?>"><?php print $sas_lang[month.$month]." ".$year ?></font></b></div></td>
<td align="right"><a href="index.php?page=<?php echo $page ?>&section=<?php echo $section ?>&month=<?php echo $n_month ?>&year=<?php echo $n_year ?>"><b><font color="<?php echo $font_color ?>">>>>>></a></font></td>

</tr></table>
<table width="<?php echo $table_width ?>" border="0" cellspacing="1" cellpadding="0" align="center" bgcolor="<?php echo $table_bgcolor ?>">

  <tr bgcolor="<?php echo $header_color ?>" align="center" valign="middle">
    <td width="16%" height="20">
      <div align="center"><b><font color="<?php echo $font_color ?>"><?php echo $sas_lang['calendar_module_monday'] ?></font></b></div>
    </td>
    <td width="16%" height="20">
      <div align="center"><b><font color="<?php echo $font_color ?>"><?php echo $sas_lang['calendar_module_tuesday'] ?></font></b></div>
    </td>
    <td width="16%" height="20">
      <div align="center"><b><font color="<?php echo $font_color ?>"><?php echo $sas_lang['calendar_module_wednesday'] ?></font></b></div>
    </td>
    <td width="16%" height="20">
      <div align="center"><b><font color="<?php echo $font_color ?>"><?php echo $sas_lang['calendar_module_thursday'] ?></font></b></div>
    </td>
    <td width="16%" height="20">
      <div align="center"><b><font color="<?php echo $font_color ?>"><?php echo $sas_lang['calendar_module_friday'] ?></font></b></div>
    </td>
    <td width="16%" height="20">
      <div align="center"><b><font color="<?php echo $font_color ?>"><?php echo $sas_lang['calendar_module_weekend'] ?></font></b></div>
    </td>
  </tr>

<?php

	$firstMonthDay = getdate(mktime(0, 0, 0, $month, 1, $year));
	$lastMonthDay = getdate(mktime(0, 0, 0, $month +1, 0, $year));

	$wday = $firstMonthDay["wday"];
	$lwday = $lastMonthDay["mday"];

	if ($wday == 0)
	{
		$start = -6; //first day is a sunday
	}
	else
	{
		$start = 1 - $wday;
	}

	if ((($start == -6) or (($start == -5) AND $lwday > 30)) and ($lwday > 29))
	{
		$cells = 42;
	}
	else
	{
		$cells = 36;
	}

	//print $cells." ".$start." ".$lwday;
	$firstday = getdate(mktime(0, 0, 0, $month, $start, $year));
	for ($i = 0; $i < $cells; $i++)
	{
		$nextday = getdate(mktime(0, 0, 0, $month, $start + $i, $year));
		if ($nextday["mon"] == $month)
		{
			$calendar[$i] = $nextday["mday"];
			$event[$i] = $calender_item[$start + $i];
		}
		else
		{
			$calendar[$i] = "";
			$event[$i] = "";
		}
	}

	$i = 1;
	while ($i < $cells)
	{

		$j = 0;
		echo "<tr>\n";
		while ($j < 5)
		{
			if (($d == $calendar[$i]) and ($m == $month) and ($y == $year))
			{
				$bg1 = $day_color_today; //set color for today
				$bg2 = $day_color_today;
			}
			else
			{
				$bg1 = $active_day_color;
				$bg2 = $day_color;
			}

			if ($event[$i] != "")
			{
				$popup_text = addslashes(substr($event[$i], 0, -1)); //strip \n at the end
				$cell_text = substr($event[$i], 0, strpos($event[$i], ',')); //get portion till first comma 
				if (strpos($event[$i], ',') == FALSE)
				{
					$cell_text = $event[$i]; //if no comma , use whole entry
				}
				else
				{
					$popup_text = substr($event[$i], strpos($event[$i], ',') + 1, -1);
				}
				$popup_text = str_replace("'","&rsquo;",$popup_text); //replace single quote with &rsquo;
				
				echo "<td ONMOUSEOVER=\"popup('" . $popup_text . "','#FFFFFF');\" ONMOUSEOUT=\"removeBox();\"
				          height=\"" . $cell_height . "\" valign=\"top\" align=\"center\" rowspan=\"2\" 
				          bgcolor=\"$bg1\"><b>" . $calendar[$i] . "</b><br>" . $cell_text . "</td>";
			}
			else
			{
				echo "<td height=\"" . $cell_height . "\" valign=\"top\" align=\"center\" rowspan=\"2\" 
				          bgcolor=\"$bg2\"><b>" . $calendar[$i] . "<br></td>";
			}
			$i++;
			$j++;
		}
		if (($d == $calendar[$i]) and ($m == $month) and ($y == $year))
		{
			$bg1 = $day_color_today;
			$bg2 = $day_color_today;
		}
		else
		{
			$bg1 = $active_day_color;
			$bg2 = $day_color;
		}

		if ($event[$i] != "")
		{
			$popup_text = addslashes(substr($event[$i], 0, -1)); //strip \n at the end
			$cell_text = substr($event[$i], 0, strpos($event[$i], ','));
			if (strpos($event[$i], ',') == FALSE)
			{
				$cell_text = $event[$i];
			}
			else
			{
				$popup_text = substr($event[$i], strpos($event[$i], ',') + 1, -1);
			}
			echo "<td ONMOUSEOVER=\"popup('" . $popup_text . "','#FFFFFF');\" ONMOUSEOUT=\"removeBox();\"
			          height=\"" . $cell_height2 . "\" valign=\"top\" align=\"center\" 
			          bgcolor=\"$bg1\"><b>" . $calendar[$i] . "</b><br>" . $cell_text . "</td></tr>";
		}
		else
		{
			echo "<td height=\"" . $cell_height2 . "\" valign=\"top\" align=\"center\" bgcolor=\"$bg2\"><b>" . $calendar[$i] . "<br></td></tr>";
		}
		$i++;

		if (($d == $calendar[$i]) and ($m == $month) and ($y == $year))
		{
			$bg1 = $day_color_today;
			$bg2 = $day_color_today;
		}
		else
		{
			$bg1 = $active_day_color;
			$bg2 = $day_color;
		}

		if ($event[$i] != "")
		{
			$popup_text = addslashes(substr($event[$i], 0, -1)); //strip \n at the end 
			$cell_text = substr($event[$i], 0, strpos($event[$i], ','));
			if (strpos($event[$i], ',') == FALSE)
			{
				$cell_text = $event[$i];
			}
			else
			{
				$popup_text = substr($event[$i], strpos($event[$i], ',') + 1, -1);
			}
			echo "<tr><td ONMOUSEOVER=\"popup('" . $popup_text . "','#FFFFFF');\" ONMOUSEOUT=\"removeBox();\"
			          height=\"" . $cell_height2 . "\" valign=\"top\" align=\"center\" bgcolor=\"$bg1\"><b>" . $calendar[$i] . "</b><br>" . $cell_text . "</td></tr>";
		}
		else
		{
			echo "<tr><td height=\"" . $cell_height2 . "\" valign=\"top\" align=\"center\" bgcolor=\"$bg2\"><b>" . $calendar[$i] . "</b><br></td></tr>";
		}
		$i++;
	}
?>  
</table>
<table width="<?php echo $table_width ?>" border="0" cellspacing="0" cellpadding="0" align="center"><tr bgcolor="<?php echo $header_color ?>">
<td><a href="index.php?page=<?php echo $page ?>&section=<?php echo $section ?>&month=1&year=<?php echo $year ?>"><b><font color="<?php echo $font_color ?>"><?php echo substr($sas_lang[month1],0,3);?></a></font></b></td>
<td><a href="index.php?page=<?php echo $page ?>&section=<?php echo $section ?>&month=2&year=<?php echo $year ?>"><b><font color="<?php echo $font_color ?>"><?php echo substr($sas_lang[month2],0,3);?></a></font></b></td>
<td><a href="index.php?page=<?php echo $page ?>&section=<?php echo $section ?>&month=3&year=<?php echo $year ?>"><b><font color="<?php echo $font_color ?>"><?php echo substr($sas_lang[month3],0,3);?></a></font></b></td>
<td><a href="index.php?page=<?php echo $page ?>&section=<?php echo $section ?>&month=4&year=<?php echo $year ?>"><b><font color="<?php echo $font_color ?>"><?php echo substr($sas_lang[month4],0,3);?></a></font></b></td>
<td><a href="index.php?page=<?php echo $page ?>&section=<?php echo $section ?>&month=5&year=<?php echo $year ?>"><b><font color="<?php echo $font_color ?>"><?php echo substr($sas_lang[month5],0,3);?></a></font><b></td>
<td><a href="index.php?page=<?php echo $page ?>&section=<?php echo $section ?>&month=6&year=<?php echo $year ?>"><b><font color="<?php echo $font_color ?>"><?php echo substr($sas_lang[month6],0,3);?></a></font></b></td>
<td><a href="index.php?page=<?php echo $page ?>&section=<?php echo $section ?>&month=7&year=<?php echo $year ?>"><b><font color="<?php echo $font_color ?>"><?php echo substr($sas_lang[month7],0,3);?></a></font></b></td>
<td><a href="index.php?page=<?php echo $page ?>&section=<?php echo $section ?>&month=8&year=<?php echo $year ?>"><b><font color="<?php echo $font_color ?>"><?php echo substr($sas_lang[month8],0,3);?></a></font></b></td>
<td><a href="index.php?page=<?php echo $page ?>&section=<?php echo $section ?>&month=9&year=<?php echo $year ?>"><b><font color="<?php echo $font_color ?>"><?php echo substr($sas_lang[month9],0,3);?></a></font></b></td>
<td><a href="index.php?page=<?php echo $page ?>&section=<?php echo $section ?>&month=10&year=<?php echo $year ?>"><b><font color="<?php echo $font_color ?>"><?php echo substr($sas_lang[month10],0,3);?></a></font></b></td>
<td><a href="index.php?page=<?php echo $page ?>&section=<?php echo $section ?>&month=11&year=<?php echo $year ?>"><b><font color="<?php echo $font_color ?>"><?php echo substr($sas_lang[month11],0,3);?></a></font></b></td>
<td><a href="index.php?page=<?php echo $page ?>&section=<?php echo $section ?>&month=12&year=<?php echo $year ?>"><b><font color="<?php echo $font_color ?>"><?php echo substr($sas_lang[month12],0,3);?></a></font></b></td>
</tr></table>
<?php

}
?>
<script type="text/javascript">initPopup();</script>
