<?php

#############################################################################
#                              FAgenda
# FAgenda is a website agenda designed for Site@School
# (http://siteatschool.sourceforge.net)
#
# About FAgenda:
# Original idea: Dirk Schouten and Fred Stuurman
# Coding: Fred Stuurman (fc.stuurman@hccnet.nl)
############################################################################
# FILENAME agenda_common.php last changed 07/09/2006, S@S version: 2.4.10                                       
#                            by Fred Stuurman (fc.stuurman@hccnet.nl)
#$Id: agenda_common.inc.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
##############################################################################
//common code
if (!defined('IN_SAS'))
{
	die("Hacking attempt");
}

include ("config.inc.php"); //connect to mysql database

include_once ("class.TemplatePower.inc.php"); //load template code

$language = "";
$query = "SELECT config_value FROM $table_configuration WHERE config_key='language'"; //get the language
$result = mysql_query($query);
$language = mysql_result($result, 0);

if (file_exists("../$cmsdir/languages/" . $language . "/sn_agenda/" . $language . ".php"))
{
	include ("../$cmsdir/languages/" . $language . "/sn_agenda/" . $language . ".php");
}
else
{
	include ("../$cmsdir/languages/EN/sn_agenda/EN.php");
}

for ($i = 1; $i < 13; $i++)
{ //create the months array
	$lang_month[$i] = $sas_lang[month . $i];
}
for ($i = 1; $i < 7; $i++)
{ //create the week days array
	$lang_week[$i] = $sas_lang[week . $i];
}
$lang_week[0] = $sas_lang[week0];

if (isset ($_GET['date']))
{ //is the date set otherwise use todays date
	$cur_date = explode("-", $_GET['date']);
	$d = $cur_date[0];
	$month = $cur_date[1];
	$year = $cur_date[2];
}
else
{
	$d = date("j");
	$month = date("n");
	$year = date("Y");
}

//calculate all kind of variables needed later on
$m_prev = $month -1; //previous month 
$y_next = $year +1; //next year
$y_prev = $year -1; //previous year

if ($m_prev < 1)
{
	$m_prev = 12; //previous month is in previous year
	$y_prev = $year -1;
	$nr_prev = date("t", mktime(12, 0, 0, $m_prev, 1, $y_prev)); //number of days previous month previous year
}
else
{
	$nr_prev = date("t", mktime(12, 0, 0, $m_prev, 1, $year)); //number of days previous month this year
}
$m_next = $month +1; //next month number
if ($m_next > 12)
{
	$m_next = 1; //next month is in next year
	$y_next = $year +1;
	$nr_next = date("t", mktime(12, 0, 0, $m_next, 1, $y_next)); //number of days next month previous year
}
else
{
	$nr_next = date("t", mktime(12, 0, 0, $m_next, 1, $year)); //number of days next month this year
}

$nr = date("t", mktime(12, 0, 0, $month, 1, $year)); //nr of days of the current month

$firstday = date("w", mktime(12, 0, 0, $month, $d, $year)); //what is the weekday of current date
if ($firstday == 0)
{
	$firstday = 7;
} //our current day is a sunday so we need 7 an not 0
$day = $d - $firstday +1; //what is the monday, this is our first day of the first week
$start_week = date("W", mktime(12, 0, 0, $month, $day, $year)); //what is first week number
$week_day = date("w", mktime(12, 0, 0, $month, $d, $year));

function fill_time_slots()
//fill the wording of the time slots from table
{
	global $tpl;
	global $table_configuration;
	global $timeslots_header_color;
	$query = "SELECT config_value FROM $table_configuration WHERE config_key LIKE 'agenda_time%' ORDER BY config_key";
	$result = mysql_query($query);
	$i = 1;
	while ($time_list = mysql_fetch_array($result)) //fill the time stamps from table
	{
		$tpl->assign("time$i", $time_list[0]);
		$i++;
	}
	$tpl->assign("timeslots_header_color", $timeslots_header_color);
}

function fill_agenda_list()
{
	global $tpl;
	global $d;
	global $month;
	global $year;
	global $a;
	global $sas_lang, $userid, $table_users;
	global $table_agenda_name;
	global $bgcolor, $months_bgcolor, $week_days_bgcolor, $week_number_bgcolor, $mouse_over_color;
	if (IsSet ($_POST[a]))
	{
		$a = $_POST[a];
	}
	if (IsSet ($_GET[a]))
	{
		$a = explode(",", $_GET[a]);
	}
	$query = "SELECT id, name, username FROM $table_agenda_name ORDER BY sort";
	$result = mysql_query($query);
	if (mysql_num_rows($result) == 0)
	{
		print "<font color=red><b>" . $sas_lang['agenda_empty_agenda_warning'] . "</b></font>";
		exit;
	}
	$i = 0;
	while ($row = mysql_fetch_array($result))
	{
		$agenda[$i] = $row[1];
		$agenda_id[$i] = $row[0];
		$agenda_user[$i] = $row[2];
		$i++;
	}
	//print_r($a);
	//print_r($agenda_id);
	$agenda_name = "";
	$agenda_result = "<form ACTION=\"agenda.php?date=$d-$month-$year\" METHOD=POST>";
	$agenda_result .= "<tr><td><b>" . $sas_lang['agenda_title'] . ":</b></td>
	                       <td><input type=\"button\" value=\"" . $sas_lang['agenda_search'] . "\" onclick=\"popup('search_dialog.php','popup',700,600);return false\"></td>
	                       <td align=\"right\"><input TYPE=SUBMIT VALUE=\"" . $sas_lang['view'] . "\"></td></tr>";

	if (count($a) == 0)
	{ //no agenda was chosen

		$query = "SELECT username FROM $table_users WHERE id = '$userid'"; //lets see if the user has an agenda
		$result = mysql_query($query);
		if (mysql_num_rows($result) == 1)
		{
			$username = mysql_result($result, 0, 0);
			unset ($query);
			unset ($result);
			$key = array_search($username, $agenda_user); //find the key of the agenda for this user
			$a[$key] = $agenda_id[$key]; //use this agenda for the default
			for ($key = 0; $key < $i; $key++)
			{
				$agenda_edit_id .= "$a[$key],"; //create comma delimited string of agenda id's
			}
			$agenda_edit_id = substr($agenda_edit_id, 0, -1); //strip last comma
		}
		else
		{
			$agenda_edit_id = $agenda_id[0]; //no agenda was set use the first in order
			$a[0] = $agenda_id[0];
		}
	}
	else //agenda was chosen
		{
		$agenda_edit_id = "";
		for ($k = 0; $k < $i; $k++)
		{
			$agenda_edit_id .= "$a[$k],"; //create comma delimited string of agenda id's
		}
		$agenda_edit_id = substr($agenda_edit_id, 0, -1); //strip last comma
	}
	//print $agenda_edit_id ;
	$i = 0;
	for ($k = 0; $k < count($agenda); $k++)
	{ //loop through the agenda's
		// print " a: $a[$k] agenda:$agenda_id[$k]";

		if ($agenda_id[$k] == $a[$k])
		{
			$checked = "checked";
		}
		else
		{
			$checked = "";
		}
		$a_new = str_repeat(",", $k) . $agenda_id[$k]; //make comma delimted string for the agenda link
		$agenda_result .= "<tr><td><a  style=\"text-decoration: none\" href=\"agenda.php?date=$d-$month-$year&a=$a_new\">$agenda[$k]</a></td>
		<td colspan=\"2\" align=\"right\"><input TYPE=CHECKBOX NAME=\"a[$k]\" $checked value=\"$agenda_id[$k]\"></td></tr>";
		if ((IsSet ($a[$k])) and ($a[$k] != ""))
		{
			$agenda_name = $agenda[$k];
			$i++;
		}

	}
	if ($i > 1)
	{
		$agenda_name = $sas_lang['multiple_agenda'];
	}
	$tpl->assign("agenda_name", $agenda_name);
	$tpl->assign("agenda_list", $agenda_result);
	$tpl->assign("today", "agenda.php?a=" . $agenda_edit_id);
	$tpl->assign("button_today", $sas_lang['today']);
	$tpl->assign("h1", $sas_lang['week_title1']); //set week titles
	$tpl->assign("h2", $sas_lang['week_title2']);
	$tpl->assign("h3", $sas_lang['week_title3']);
	$tpl->assign("h4", $sas_lang['week_title4']);
	$tpl->assign("h5", $sas_lang['week_title5']);
	$tpl->assign("h6", $sas_lang['week_title6']);
	$tpl->assign("h7", $sas_lang['week_title7']);
	$tpl->assign("h8", $sas_lang['week_title8']);
	$tpl->assign("bgcolor", $bgcolor); //set background color
	$tpl->assign("months_bgcolor", $months_bgcolor); //set months background color
	$tpl->assign("week_days_bgcolor", $week_days_bgcolor); //set weekdays background color
	$tpl->assign("week_number_bgcolor", $week_number_bgcolor); //set weeknumber background color
	$tpl->assign("mouse_over_color", $mouse_over_color); //set mouse over color for a day cell
	return $agenda_edit_id;
}
?>