<?php

#############################################################################
#                              FAgenda
# FAgenda is a website agenda designed for and integrated in Site@School.
# (http://siteatschool.sourceforge.net)
#
# About FAgenda:
# Original idea: Dirk Schouten and Fred Stuurman
# Coding: Fred Stuurman (fc.stuurman@hccnet.nl)
#############################################################################
# agenda.php  last changed 07/09/2006, S@S version: 2.4.10                                       
#             by Fred Stuurman (fc.stuurman@hccnet.nl)
#$Id: agenda.php,v 1.2 2006/10/15 19:08:53 fstuurman Exp $
############################################################################
#                        LICENSE INFORMATION
# Copyright (C) 2004 Fred Stuurman (fc.stuurman@hccnet.nl)
#
# FAgenda is an integrated part of the Site@School Content Management System.
# FAgenda may not be modified, taken out of Site@School and/or distributed 
# without the written permission of the author.
# 
# FAgenda is free to use for Educational use and may be used free-of-charge 
# by members of eucational institutions, but must be purchased by all others.
# Contact the author, F. Stuurman (fc.stuurman@hccnet.nl), for 
# non-educational use or adapting FAgenda to your requirements.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# FITNESS FOR A PARTICULAR PURPOSE.
#
#                   DO NOT REMOVE THIS COPYRIGHT NOTICE
##############################################################################
define('IN_SAS', true);

if (IsSet ($_POST[a]))
{
	$a = $_POST[a];
}
include_once ("agenda_common.inc.php"); //execute common code

$tpl = new TemplatePower("agenda_view.html");
$tpl->prepare();

fill_time_slots(); //fill the timeslots text
$agenda_edit = fill_agenda_list(); //get the different agenda's

$tpl->assign("year_prev", $y_prev);
$tpl->assign("year_next", $y_next);
$tpl->assign("month", $month);
$tpl->assign("year", $year);
$tpl->assign("cur_date", $lang_week[$week_day] . " " . $d . " " . $lang_month[$month] . " " . $year);
$tpl->assign("pa_link", $pa_link . "index.php");

for ($i = 1; $i < 13; $i++)
{ //fill the month's link bar
	$k = $i + $month -1; //set current month as first entry
	if ($k > 12)
	{ //if larger as 12 start counting at 1
		$k = $i + $month -13;
		$year_link = $y_next;
	}
	else
	{
		$year_link = $year;
	}
	$m = substr($lang_month[$k], 0, 3); //get the first three letters of month
	$tpl->assign("m$i", "<a style=\"color: $months_font_color\" href='agenda.php?date=1-$k-$year_link&a=$agenda_edit' class='navigationtext'>$m</a>");
}
$tpl->assign("year_next", "<a style=\"color: $months_font_color\" href=\"agenda.php?date=1-1-$y_next&a=$agenda_edit\">$y_next&nbsp;&gt;&gt;</a>");
$tpl->assign("year_prev", "<a style=\"color: $months_font_color\" href=\"agenda.php?date=1-1-$y_prev&a=$agenda_edit\">&lt;&lt;&nbsp;$y_prev</a>");

if ($d +7 > $nr)
{ //fill the week forward link
	$tpl->assign("day_forward", $d +7 - $nr);
	$tpl->assign("month_forward", $m_next);

	if (($m_next == 1) and ($m_next < $month)) //next month is in next year
	{
		$year_temp = $year +1;
		$tpl->assign("year_forward", $year_temp . "&a=" . $agenda_edit);
	}
	else
	{
		$tpl->assign("year_forward", $year . "&a=" . $agenda_edit);
	}
}
else
{
	$tpl->assign("day_forward", $d +7);
	$tpl->assign("month_forward", $month);
	$tpl->assign("year_forward", $year . "&a=" . $agenda_edit);
}

if ($d -7 < 1)
{ //fill the week backward link
	$tpl->assign("day_back", $nr_prev + $d -7);
	$tpl->assign("month_back", $m_prev);
	if (($m_prev == 12) and ($m_next > $month)) //next month is in previous year
	{
		$year_temp = $year -1;
		$tpl->assign("year_back", $year_temp . "&a=" . $agenda_edit);
	}
	else
	{
		$tpl->assign("year_back", $year . "&a=" . $agenda_edit);
	}

}
else
{
	$tpl->assign("day_back", $d -7);
	$tpl->assign("month_back", $month);
	$tpl->assign("year_back", $year . "&a=" . $agenda_edit);
}

foreach ($a as $key => $value)
{
	if ($value == "")
	{
		unset ($a[$key]); //get rid of the empty ones
	}
}

if (count($a) == 1)
{
	$single_agenda = "1";
	$a_sel = array_pop($a);
}

for ($i = 1; $i < 6; $i++) //fill the week numbers
{
	if ($start_week + $i -2 == 52)
	{
		$start_week = 2 - $i;
	} //reset to week 1 if larger that 52
	$w = $start_week + $i -1;

	if ($single_agenda == 1)
	{ //single agenda number
		$tpl->assign("w$i", "<a href=\"agenda_week.php?a=$a_sel&week=$w&year=$year\" target=\"_blank\">$w</a>");
	}
	else
	{
		$tpl->assign("w$i", $w);
	}
}

for ($i = 1; $i < 36; $i++) //fill the day numbers and set colors
{ // set days and weekend colors
	if (($i == 6) or ($i == 7) or ($i == 13) or ($i == 14) or ($i == 20) or ($i == 21) or ($i == 27) or ($i == 28) or ($i == 34) or ($i == 35))
	{
		$tpl->assign("color$i", $weekend_color);
	}
	else
	{
		$tpl->assign("color$i", $day_color);
	} //set color for the day  
	if ($i + $day -1 < 1)
	{ //days of previous month
		$day_number = $nr_prev -abs($day) + $i -1;
		if ($i == 1)
		{
			if ($m_prev == 12)
			{ //previous month is dec so we need previous year
				$query_date_first = "$y_prev-$m_prev-$day_number"; //this is the first date of the 5 weeks period
			}
			else
			{
				$query_date_first = "$year-$m_prev-$day_number";
			}
		} //this is the first date of the 5 weeks period
		$week_day = date("w", mktime(12, 0, 0, $m_prev, $d, $y_prev));
		$tpl->assign("$i", "<a href=\"agenda_edit.php?a=$agenda_edit&date=$d-$month-$year&date_edit=$y_prev-$m_prev-$day_number\">$day_number</a>");
		$tpl->assign("curdate$i", "document.getElementById('date').innerHTML='$lang_week[$week_day] $day_number $lang_month[$m_prev] $y_prev';");
		if (($day_number == date("j")) and ($m_prev == date("n")) and ($y_prev == date("Y")))
		{
			$tpl->assign("color$i", $active_day_color);
		}
	}

	if (($i + $day -1 < $nr +1) and ($i + $day -1 > 0))
	{ //days of this month
		$day_number = $day + $i -1;
		if ($i == 1)
		{
			$query_date_first = "$year-$month-$day_number";
		} //this is the first date of the 5 weeks period
		if ($i == 35)
		{
			$query_date_last = "$year-$month-$day_number";
		} //this is the last date of the 5 weeks period
		$week_day = date("w", mktime(12, 0, 0, $month, $day_number, $year));
		$tpl->assign("$i", "<a href=\"agenda_edit.php?a=$agenda_edit&date=$d-$month-$year&date_edit=$year-$month-$day_number\">$day_number</a>");
		$tpl->assign("curdate$i", "document.getElementById('date').innerHTML='$lang_week[$week_day] $day_number $lang_month[$month] $year';");
		if (($day_number == date("j")) and ($month == date("n")) and ($year == date("Y")))
		{
			$tpl->assign("color$i", $active_day_color);
		}
	}

	if ($i + $day -1 > $nr)
	{ //days of next month
		$day_number = $i - $nr + $day -1;
		if (($m_next == 1) and ($m_next < $month)) //next month is in next year
		{
			$y_next = $year +1;
		}
		else
		{
			$y_next = $year;
		}

		if ($day_number > $nr_next)
		{ //we have a third month
			$day_number = $day_number - $nr_next;
			$m_next = $m_next +1;
			if ($m_next = 13)
			{
				$m_next = 1;
			}
		}
		if ($i == 35)
		{
			$query_date_last = "$y_next-$m_next-$day_number";
		} //this is the last date of the 5 weeks period
		$week_day = date("w", mktime(12, 0, 0, $m_next, $day_number, $y_next));
		$tpl->assign("$i", "<a href=\"agenda_edit.php?a=$agenda_edit&date=$d-$month-$year&date_edit=$y_next-$m_next-$day_number\">$day_number</a>");
		$tpl->assign("curdate$i", "document.getElementById('date').innerHTML='$lang_week[$week_day] $day_number $lang_month[$m_next] $y_next';");
		if (($day_number == date("j")) and ($m_next == date("n")) and ($y_next == date("Y")))
		{
			$tpl->assign("color$i", $active_day_color);
		}
	}

}
//fill the timeslots from the mysql table

//if(count($a) > 1) 
if ($single_agenda != 1)
{ //we have multiple agenda's
	$where_clause = "";
	foreach ($a as $value)
	{
		if ($value != "")
		{
			$where_clause .= " OR agenda = $value"; //create where clause of agenda id's
		}
	}
	$where_clause = substr($where_clause, 3); //strip the first OR
	$query = "SELECT adate, timeslot, descr, agenda FROM $table_agenda_events WHERE adate >= '$query_date_first' AND adate <= '$query_date_last' AND ($where_clause) ORDER BY adate";
	$result = mysql_query($query);
	//print $query;
}
else //we have 1 agenda
	{
	//$single_agenda = "1" ;
	//   $a_sel = array_pop($a);                                            //get agenda number
	$query = "SELECT adate, timeslot, descr, agenda FROM $table_agenda_events WHERE adate >= '$query_date_first' AND adate <= '$query_date_last' AND agenda = '$a_sel' ORDER BY adate";
	$result = mysql_query($query);
}
//print $query;
$numRows = mysql_num_rows($result);
if ($numRows > 0)
{
	$first_day_num = date("z", strtotime($query_date_first)); //get the daynumber of the begin date
	$first = 1;
	while ($time_list = mysql_fetch_array($result)) //fill the description from table
	{

		if (strtotime($time_list[0]) == strtotime("$year-$month-$d")) //date is current date, fill the time slot
		{

			if ($single_agenda == "1")
			{
				$tpl->assign("app" . $time_list[1], "<table cellpadding='0' cellspacing='0' width='100%'><tr bgcolor='$busy_color'><td>&nbsp;" . $time_list[2] . "</td></tr></table>");
			}
			else
			{
				$tpl->assign("app" . $time_list[1], "<table cellpadding='0' cellspacing='0' width='100%'><tr bgcolor='$busy_color'><td>&nbsp;</td></tr></table>");
			}
		}
		$actual_day_num = date("z", strtotime($time_list[0])) + 1;
		$k = $actual_day_num - $first_day_num; //calculate the number of the day in the 5 weeks

		if ($k < 1)
		{ //if negative we are in the previous or next year 
			if ($month == 12)
			{
				$k = $k +date("z", strtotime("$year-12-31")) + 1; //add the number of days of next year
			}
			else
			{
				$k = $k +date("z", strtotime("$y_prev-12-31")) + 1; //add the number of days of previous year
			}
		}
		if ($first == 1) //process the very first record
		{
			if ($single_agenda == "1") //fill timeslot with text
			{
				$temp = "document.getElementById('loc$time_list[1]').innerHTML='&nbsp;" . addslashes($time_list[2]) . "';";
			}
			else //fill time slot with blank
				{
				$temp = "document.getElementById('loc$time_list[1]').innerHTML='&nbsp;';";
			}
			$temp .= "document.getElementById('loc$time_list[1]').style.backgroundColor = '$busy_color';";
			$first++;
		}
		if ($k > $k_prev) //up to the next day
		{
			$tpl->assign("dag$k_prev", $temp); //set timeslot text for the day in the template
			if ($single_agenda == "1") //fill timeslot with text
			{
				$temp = "document.getElementById('loc$time_list[1]').innerHTML='&nbsp;" . addslashes($time_list[2]) . "';";
			}
			else //fill time slot with blank
				{
				$temp = "document.getElementById('loc$time_list[1]').innerHTML='&nbsp;';";
			}
			$temp .= "document.getElementById('loc$time_list[1]').style.backgroundColor = '$busy_color';";
		}
		if ($k == $k_prev)
		{
			if ($single_agenda == "1") //fill timeslot with text
			{
				$temp .= "document.getElementById('loc$time_list[1]').innerHTML='&nbsp;" . addslashes($time_list[2]) . "';";
			}
			else //fill time slot with blank
				{
				$temp .= "document.getElementById('loc$time_list[1]').innerHTML='&nbsp;';";
			}
			$temp .= "document.getElementById('loc$time_list[1]').style.backgroundColor = '$busy_color';";
		}
		$k_prev = $k;
	}
	$tpl->assign("dag$k_prev", $temp); //process the very last record
}
$tpl->printToScreen();
?>
