<?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_edit.php last changed 07/09/2006, S@S version: 2.4.10                                   
#             by Fred Stuurman (fc.stuurman@hccnet.nl)
#$Id: agenda_edit.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 ($_GET[a]))
{
	$a = explode(",", $_GET[a]);
}
if (IsSet ($_GET[date_edit]))
{
	$edit_date = explode("-", $_GET[date_edit]);
}
if (IsSet ($_GET[date]))
{
	$date = htmlspecialchars(strip_tags($_GET[date]));
}

include_once ("agenda_common.inc.php"); //execute common code

$tpl = new TemplatePower("agenda_edit.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", $edit_date[2] . " " . $lang_month[$edit_date[1]] . " " . $edit_date[0]);

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'>$m</a>");
}
$tpl->assign("year_next", "<a style=\"color: $months_font_color\" href=\"agenda.php?date=1-1-$y_next\">$y_next&nbsp;&gt;&gt;</a>");
$tpl->assign("year_prev", "<a style=\"color: $months_font_color\" href=\"agenda.php?date=1-1-$y_prev\">&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
	{
		$tpl->assign("year_forward", $year +1);
	}
	else
	{
		$tpl->assign("year_forward", $year);
	}
}
else
{
	$tpl->assign("day_forward", $d +7);
	$tpl->assign("month_forward", $month);
	$tpl->assign("year_forward", $year);
}

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
	{
		$tpl->assign("year_back", $year -1);
	}
	else
	{
		$tpl->assign("year_back", $year);
	}

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

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 than 52
	$tpl->assign("w$i", $start_week + $i -1);
}

for ($i = 1; $i < 36; $i++) //fill the day numbers and set 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
		$tpl->assign("$i", "<a href=\"agenda_edit.php?a=$_GET[a]&date=$d-$month-$year&date_edit=$y_prev-$m_prev-$day_number\">$day_number</a>");
		$tpl->assign("curdate$i", "document.getElementById('date').innerHTML='$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 = "$day_number-$month-$year";
		} //this is the first date of the 5 weeks period
		if ($i == 35)
		{
			$query_date_last = "$day_number-$month-$year";
		} //this is the last date of the 5 weeks period
		$tpl->assign("$i", "<a href=\"agenda_edit.php?a=$_GET[a]&date=$d-$month-$year&date_edit=$year-$month-$day_number\">$day_number</a>");
		$tpl->assign("curdate$i", "document.getElementById('date').innerHTML='$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 ($i == 35)
		{
			$query_date_last = "$day_number-$m_next-$y_next";
		} //this is the last date of the 5 weeks period
		$tpl->assign("$i", "<a href=\"agenda_edit.php?a=$_GET[a]&date=$d-$month-$year&date_edit=$y_next-$m_next-$day_number\">$day_number</a>");
		$tpl->assign("curdate$i", "document.getElementById('date').innerHTML='$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
$num = 0;
$where_clause = "";
for ($k = 0; $k < count($a); $k++)
{
	if ($a[$k] != "")
	{ //get the agenda numbers for the where clause
		$where_clause .= " OR agenda = $a[$k]";
		$num++;
	} //create where clause of agenda id's
}
$where_clause = substr($where_clause, 3);
$query = "SELECT adate, timeslot, descr, agenda FROM $table_agenda_events WHERE adate = '$_GET[date_edit]' AND ($where_clause) ORDER BY timeslot, agenda";
$result = mysql_query($query);
//print $query;
$i = 0;
while ($time_list = mysql_fetch_array($result)) //fill the description from table
{
	$slot[$i] = $time_list[1];
	$descr[$i] = $time_list[2];
	$i++;
}

$i = 0;
if ($num == 1) //we have one agenda to edit
{

	for ($k = 1; $k < 46; $k++) //loop trough the time slots and fill the data
	{
		if ($k == $slot[$i]) //slotnumber is equal
		{
			$value = $descr[$i];
			$i++;
		}
		else
		{
			$value = "";
		}
		if ($k == 45) //remarks field needs a bigger input field
		{
			$tpl->assign("app$k", "<input tabindex=\"$k\" style=\"HEIGHT:20px;\" type=text size=109 maxlength=109 name=\"date_new[$k]\" value=\"$value\">");
			//   $tpl->assign( "app$k", "<textarea tabindex=\"$k\" cols=\"82\" rows=\"3\" name=\"date_new[$k]\">$value</textarea>");

		}
		else
		{
			$tpl->assign("app$k", "<input tabindex=\"$k\" style=\"HEIGHT:20px;\" type=text size=50 maxlength=50 name=\"date_new[$k]\" value=\"$value\">");
		}
	}
}
else //we have multiple agenda's
	{
	for ($k = 1; $k < 46; $k++)
	{ //loop trough the time slots and fill the data
		$tpl->assign("app$k", "<input tabindex=\"$k\" style=\"HEIGHT:20px;\" type=text size=50 maxlength=50 name=\"date_new[$k]\">");
	}
	if (count($slot) > 0)
	{
		foreach ($slot as $key => $value)
		{
			$tpl->assign("app$value", "<table cellpadding='0' cellspacing='0' width='100%'><tr bgcolor='$busy_color_edit'><td>&nbsp;</td></tr></table>");
		}
	}
}
$tpl->assign("save_button", "<input TYPE=HIDDEN NAME=\"save_date\" VALUE=\"$_GET[date_edit]\"><input TYPE=IMAGE img src=\"edit.gif\" ALT=\"Submit Form\">");
$tpl->assign("agenda_action", "agenda_save.php?a=" . $_GET[a] . "&date=" . $_GET[date]);
$tpl->printToScreen();
?>
