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

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

$agenda = array_values($a); //new agenda array starting with key 0
$num = count($agenda); //number of agenda's

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

if (IsSet ($_POST[date_new]))
{
	$date_new = $_POST[date_new];
}

for ($i = 1; $i < count($date_new) + 1; $i++) //loop trough the array of timeslots                                         
{
	if ($date_new[$i] != "") //something is filled in
	{
		if (!get_magic_quotes_gpc())
		{
			$date_new[$i] = addslashes($date_new[$i]); //add slashes if needed
		}

		for ($k = 0; $k < $num; $k++) //check in every agenda if record exists
		{
			$query = "SELECT id FROM $table_agenda_events WHERE adate = '$save_date' AND agenda = '$agenda[$k]' AND timeslot = '$i'";
			$result = mysql_query($query);
			$numRows = mysql_num_rows($result);
			//print $query."<br>" ;
			if ($numRows > 0)
			{
				$result = mysql_query("UPDATE $table_agenda_events SET descr = '$date_new[$i]' WHERE adate = '$save_date' AND timeslot = '$i' AND agenda = '$agenda[$k]'");
				if (mysql_errno() != 0)
				{
					echo mysql_errno() . ": " . mysql_error() . "<br>";
				}
			}
			else
			{ //we have to insert a new one
				// print "INSERT INTO agenda (id , adate , timeslot, descr, agenda) VALUES('', '$save_date', '$i', '$date_new[$i]', '$agenda[$k]')" ;                                                                 //we have a record, update it

				$result = mysql_query("INSERT INTO $table_agenda_events (id , adate , timeslot, descr, agenda) VALUES('', '$save_date', '$i', '$date_new[$i]', '$agenda[$k]')");
				if (mysql_errno() != 0)
				{
					echo mysql_errno() . ": " . mysql_error() . "<br>";
				}
			}
		}
	}
	else //empty slot , see if have to delete
		{
		if ($num == 1) //only delete when we have 1 agenda
		{
			mysql_query("DELETE FROM $table_agenda_events WHERE adate = '$save_date' AND agenda = '$agenda[0]' AND timeslot = '$i'");
		}
	}
}
Header("Location: agenda.php?date=$date&a=$_GET[a]");
?>