<?php

#############################################################################
#                              Site@Service Forms 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: admin.php,v 1.2 2006/10/15 19:08:54 fstuurman Exp $
############################################################################
# admin.php: last changed 06/09/2005  version 1.1.1
#                  by Fred Stuurman (fc.stuurman@hccnet.nl)
#$Id: admin.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");
}
?>
<script language="JavaScript" type="text/javascript">
function checkinput ( form )
{
    if (form.name.value == "") 
    {
      alert( "<?php echo $sas_lang[input_error] ?>" );
      form.name.focus();
      return false ;
    }
}
function View(color)                  // preview color
{ 
  document.getElementById("ColorPreview").style.backgroundColor = '#' + color;
  document.getElementById("ColorHex").value = '#' + color;
}

function Set(string)                   // select color
{
  var color = ValidateColor(string);
    if (color == null) 
    { 
    alert("Invalid color code: " + string);         // invalid color
    }
    else 
    {                                                                // valid color
      View(color);                          // show selected color
      __dlg_close(color);
    }
}

</script>

<?php

if (IsSet ($_GET['addmonth']))
{
	$addmonth = $_GET['addmonth'];
}
if (IsSet ($_GET['deletemonth']))
{
	$deletemonth = $_GET['deletemonth'];
}
if (IsSet ($_GET['editmonth']))
{
	$editmonth = $_GET['editmonth'];
}
if (IsSet ($_GET['savemonth']))
{
	$savemonth = $_GET['savemonth'];
}
if (IsSet ($_GET['addcal']))
{
	$addcal = $_GET['addcal'];
}
if (IsSet ($_GET['deletecal']))
{
	$deletecal = $_GET['deletecal'];
}
if (IsSet ($_GET['editcal']))
{
	$editcal = $_GET['editcal'];
}
if (IsSet ($_GET['savecal']))
{
	$savecal = $_GET['savecal'];
}
if (IsSet ($_GET['showcal']))
{
	$showcal = $_GET['showcal'];
}
if (IsSet ($_POST['name']))
{
	$name = $_POST['name'];
}
if (IsSet ($_POST['descr']))
{
	$descr = $_POST['descr'];
}
if (IsSet ($_POST['month']))
{
	$month = $_POST['month'];
}
if (IsSet ($_POST['year']))
{
	$year = $_POST['year'];
}
if (IsSet ($_POST['cal']))
{
	$cal = $_POST['cal'];
}
if (IsSet ($_POST['calendar_options']))
{
	$calendar_options = $_POST['calendar_options'];
}

if (!get_magic_quotes_gpc())
{
	$header = addslashes($_POST['header']);
}
else
{
	$header = $_POST['header'];
}

load_language('', 'sn_calendar'); //load language file

include ("interface/header.inc.php");
include ("interface/menubar.inc.php");
include ("interface/menustart.inc.php");
include ("interface/menuend.inc.php");
include ("interface/pagestart.inc.php");

$query = "SELECT name, dbtables FROM $table_modules where id=$module"; //get the module name                
$result = mysql_query($query) or die_script($query, mysql_error());
$modulename = mysql_result($result, 0, 0);
$moduletables = mysql_result($result, 0, 1);
unset ($query);
unset ($result);
$moduletables = explode(",", $moduletables);
$event_table = "$moduletables[0]";
$calendars_table = "$moduletables[1]";

if (IsSet ($savecal))
{
	$comma_separated = implode(",", $calendar_options);
	if ($savecal == "new")
	{
		mysql_query("INSERT INTO $calendars_table (id, name, options, title) 
		                   VALUES (NULL, '$name', '$comma_separated', '$header')");
	}
	else
	{
		mysql_query("UPDATE $calendars_table SET name='$name', 
		                   options='$comma_separated', 
		                   title='$header' 
		                   WHERE id='$savecal'");
	}
}

if (IsSet ($savemonth))
{
	$descr = str_replace("&nbsp;","",$descr)."&nbsp;"; //add a blank to prevent the last character missing in popup.
	if ($savemonth == "new")
	{
		mysql_query("INSERT INTO $event_table (id, month, year, description, calendar) 
						   VALUES (NULL, '$month', '$year', '$descr', '$cal')");
	}
	else
	{
		mysql_query("UPDATE $event_table SET description='$descr', 
						   year='$year', 
						   month='$month' 
						   WHERE id='$savemonth'");
	}
	$showcal = $cal;
	$query = "SELECT name FROM $calendars_table WHERE id='$showcal' ";
	$result = mysql_query($query);
	$cal_name = mysql_result($result, 0);
	// set the last update date for the page
	$date1 = date("Y-m-d H:i:s");
	mysql_query("UPDATE $table_pages SET lastupdate='$date1' 
	               WHERE name = '$cal_name' AND module = 'sn_calendar/calendar.php'");
}

if (IsSet ($deletemonth))
{
	mysql_query("DELETE FROM $event_table WHERE id='$deletemonth'");
}

if (IsSet ($deletecal))
{
	mysql_query("DELETE FROM $calendars_table WHERE id='$deletecal'");
	mysql_query("DELETE FROM $event_table WHERE calendar='$deletecal'");
}

print "<span class=\"heading\">$modulename<br></span>";
print "<span class=\"text\">" . $sas_lang['calendar_module_introduction'] . "</span><p>";

if (IsSet ($editcal))
{
	$query = "SELECT name, options, title FROM $calendars_table WHERE id=$editcal";
	$result = mysql_query($query) or die_script($query, mysql_error());
	$name = mysql_result($result, 0, 0);
	$calendar_options = mysql_result($result, 0, 1);
	$header = mysql_result($result, 0, 2);
	unset ($query);
	unset ($result);
	$calendar_options = explode(",", $calendar_options);

	print "<table border=\"0\">";
	print "<FORM METHOD=\"POST\" ACTION=\"index.php?option=modulemanager&module=$module&savecal=$editcal\">";
	print "<tr><td><span class=\"text\">" . $sas_lang['calendar_module_page_name'] . ": (*)</span></td>
	       <td colspan=\"3\"><input tabindex=\"1\" class=\"textfield\" size=\"50\" name=\"name\" value=\"$name\"></td></tr>";
	print "<tr><td><span class=\"text\">" . $sas_lang['calendar_module_title'] . ": </span></td>
	       <td colspan=\"3\"><textarea name=\"header\" cols=\"60\" rows=\"3\">$header</textarea></td></tr>";
	print "<tr><td><span class=\"text\">" . $sas_lang['calendar_header_bgcolor'] . ": </span></td>
	       <td><input tabindex=\"2\" type=text size=8 class=\"textfield\" name=\"calendar_options[0]\" value=\"$calendar_options[0]\"></td>
	       <td width=\"50\"><table bgcolor=\"$calendar_options[0]\" height=\"20\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" border=\"1\"><tr><td>&nbsp;</td></tr></table></td>
	       <td rowspan=\"8\" valign=\"top\"><table width=\"240\"><tr><td>";
	include ("interface/color_table.html");
	print "</td></tr></table></td></tr>";
	print "<tr><td><span class=\"text\">" . $sas_lang['calendar_day_color'] . ": </span></td>
	       <td><input tabindex=\"3\" type=text size=8 class=\"textfield\" name=\"calendar_options[1]\" value=\"$calendar_options[1]\"></td>
	       <td width=\"50\"><table bgcolor=\"$calendar_options[1]\" height=\"20\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" border=\"1\"><tr><td>&nbsp;</td></tr></table></td></tr>";
	print "<tr><td><span class=\"text\">" . $sas_lang['calendar_active_day_color'] . ": </td>
	       <td><input tabindex=\"4\" type=text size=8 class=\"textfield\" name=\"calendar_options[2]\" value=\"$calendar_options[2]\"></td>
	       <td width=\"50\"><table bgcolor=\"$calendar_options[2]\" height=\"20\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" border=\"1\"><tr><td>&nbsp;</td></tr></table></td></tr>";
	print "<tr><td><span class=\"text\">" . $sas_lang['calendar_table_bgcolor'] . ": </td>
	       <td><input tabindex=\"5\" type=text size=8 class=\"textfield\" name=\"calendar_options[3]\" value=\"$calendar_options[3]\"></td>
	       <td width=\"50\"><table bgcolor=\"$calendar_options[3]\" height=\"20\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" border=\"1\"><tr><td>&nbsp;</td></tr></table></td></tr>";
	print "<tr><td><span class=\"text\">" . $sas_lang['calendar_today_color'] . ": </td>
	       <td><input tabindex=\"6\" type=text size=8 class=\"textfield\" name=\"calendar_options[4]\" value=\"$calendar_options[4]\"></td>
	       <td width=\"50\"><table bgcolor=\"$calendar_options[4]\" height=\"20\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" border=\"1\"><tr><td>&nbsp;</td></tr></table></td></tr>";
	print "<tr><td><span class=\"text\">" . $sas_lang['calendar_font_color'] . ": </td>
	       <td><input tabindex=\"7\" type=text size=8 class=\"textfield\" name=\"calendar_options[5]\" value=\"$calendar_options[5]\"></td>
	       <td width=\"50\" bgcolor=\"$calendar_options[5]\"><table height=\"20\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" border=\"1\"><tr><td>&nbsp;</td></tr></table></td></tr>";
	print "<tr><td><span class=\"text\">" . $sas_lang['calendar_table_width'] . ": </span></td>
	       <td width=\"50\" colspan=\"2\"><input tabindex=\"8\" type=text size=4 class=\"textfield\" name=\"calendar_options[6]\" value=\"$calendar_options[6]\"></td></tr>";
	print "<tr><td><span class=\"text\">" . $sas_lang['calendar_cell_height'] . ": </span></td>
	       <td width=\"50\" colspan=\"2\"><input tabindex=\"9\" type=text size=4 class=\"textfield\" name=\"calendar_options[7]\" value=\"$calendar_options[7]\"></td></tr>";
	print "<tr><td colspan=\"4\">&nbsp;</td></tr>
	       <tr><td colspan=\"4\"><span class=text>$sas_lang[required_fields]</span></td>
	       </tr>";
	print "</table><br><input type=\"button\" value=\"&nbsp;&nbsp;&nbsp;$sas_lang[cancel]\" name=\"backbutton\" onClick=\"javascript:parent.location='index.php?option=modulemanager&module=$module';\" class=\"cancelbutton\">";
	print "&nbsp;<input type=\"submit\" value=\"&nbsp;&nbsp;&nbsp;$sas_lang[save]\" name=\"savebutton\" class=\"savebutton\"></form>";

	include ("interface/pageend.inc.php");
	include ("interface/footer.inc.php");
	exit;
}

if (IsSet ($editmonth))
{
	$current_year = date("Y");
	$query = "SELECT id, month, year, description, calendar FROM $event_table WHERE id=$editmonth";
	$result = mysql_query($query) or die_script($query, mysql_error());
	$month = mysql_result($result, 0, 1);
	$year = mysql_result($result, 0, 2);
	$descr = mysql_result($result, 0, 3);
	$cal = mysql_result($result, 0, 4);
	print "<FORM METHOD=\"POST\" ACTION=\"index.php?option=modulemanager&module=$module&savemonth=$editmonth\">";
	print "<table>
	       <tr><td><span class=\"text\">" . $sas_lang['calendar_module_month'] . ": </span><br>
	       <select size=\"1\" name=\"month\">\n";
	for ($i = 1; $i < 13; $i++)
	{
		if ($i == $month)
		{
			echo "<option selected value=\"$i\">$i</option>\n";
		}
		else
		{
			echo "<option value=\"$i\">$i</option>\n";
		}
	}
	print "<br></td></tr>
	       <tr><td><span class=\"text\">" . $sas_lang['calendar_module_year'] . ": </span><br>
	       <select size=\"1\" name=\"year\">\n";
	for ($i = $current_year; $i < $current_year +5; $i++)
	{
		if ($i == $year)
		{
			echo "<option selected value=\"$i\">$i</option>\n";
		}
		else
		{
			echo "<option value=\"$i\">$i</option>\n";
		}
	}
	$descr = htmlspecialchars($descr);
	print "<br></td></tr><tr><td>&nbsp;</td></tr>
	       <tr><td><span class=\"text\">" . $sas_lang['calendar_module_descr'] . ":</span><br>
	       <textarea name=\"descr\" cols=\"70\" rows=\"11\">$descr</textarea></td>
	       </tr></table>
	       <input type=\"hidden\" name=\"cal\" size=\"10\" value=\"$cal\"><br>";

	print "<input type=\"button\" value=\"&nbsp;&nbsp;&nbsp;$sas_lang[cancel]\" name=\"backbutton\" onClick=\"javascript:parent.location='index.php?option=modulemanager&module=$module';\" class=\"cancelbutton\">";
	print "&nbsp;<input type=\"submit\" value=\"&nbsp;&nbsp;&nbsp;$sas_lang[save]\" name=\"savebutton\" class=\"savebutton\"></form>";
	include ("interface/pageend.inc.php");
	include ("interface/footer.inc.php");
	exit;
}

if (IsSet ($addcal))
{
	print "<table border=\"0\">";
	print "<FORM METHOD=\"POST\" ACTION=\"index.php?option=modulemanager&module=$module&savecal=new\" onsubmit=\"return checkinput(this);\">";
	print "<tr><td><span class=\"text\">" . $sas_lang['calendar_module_page_name'] . "(*): </span></td>
	       <td colspan=\"3\"><input class=\"textfield\" size=\"50\" name=\"name\"></td></tr>";
	print "<tr><td><span class=\"text\">" . $sas_lang['calendar_module_title'] . ": </span></td>
	       <td colspan=\"3\"><textarea name=\"header\" cols=\"60\" rows=\"3\"></textarea></td></tr>";
	print "<tr><td><span class=\"text\">" . $sas_lang['calendar_header_bgcolor'] . ": </span></td>
	       <td><input tabindex=\"1\" type=text size=8 class=\"textfield\" name=\"calendar_options[0]\" value=\"#6699FF\"></td>
	       <td width=\"50\"><table bgcolor=\"#6699FF\" height=\"20\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" border=\"1\"><tr><td>&nbsp;</td></tr></table></td>
	       <td rowspan=\"8\" valign=\"top\"><table width=\"240\"><tr><td>";
	include ("interface/color_table.html");
	print "</td></tr></table></td></tr>";
	print "<tr><td><span class=\"text\">" . $sas_lang['calendar_day_color'] . ": </span></td>
	       <td><input tabindex=\"2\" type=text size=8 class=\"textfield\" name=\"calendar_options[1]\" value=\"#A2E0FF\"></td>
	       <td width=\"50\"><table bgcolor=\"#A2E0FF\" height=\"20\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" border=\"1\"><tr><td>&nbsp;</td></tr></table></td></tr>";
	print "<tr><td><span class=\"text\">" . $sas_lang['calendar_active_day_color'] . ": </td>
	       <td><input tabindex=\"3\" type=text size=8 class=\"textfield\" name=\"calendar_options[2]\" value=\"#A2E0FF\"></td>
	       <td width=\"50\"><table bgcolor=\"#A2E0FF\" height=\"20\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" border=\"1\"><tr><td>&nbsp;</td></tr></table></td></tr>";
	print "<tr><td><span class=\"text\">" . $sas_lang['calendar_table_bgcolor'] . ": </td>
	       <td><input tabindex=\"4\" type=text size=8 class=\"textfield\" name=\"calendar_options[3]\" value=\"#000000\"></td>
	       <td width=\"50\"><table bgcolor=\"#000000\" height=\"20\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" border=\"1\"><tr><td>&nbsp;</td></tr></table></td></tr>";
	print "<tr><td><span class=\"text\">" . $sas_lang['calendar_today_color'] . ": </td>
	       <td><input tabindex=\"4\" type=text size=8 class=\"textfield\" name=\"calendar_options[4]\" value=\"#FFFF00\"></td>
	       <td width=\"50\"><table bgcolor=\"#FFFF00\" height=\"20\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" border=\"1\"><tr><td>&nbsp;</td></tr></table></td></tr>";
	print "<tr><td><span class=\"text\">" . $sas_lang['calendar_font_color'] . ": </td>
	       <td><input tabindex=\"7\" type=text size=8 class=\"textfield\" name=\"calendar_options[5]\" value=\"#FFFFFF\"></td>
	       <td width=\"50\" bgcolor=\"#FFFFFF\"><table height=\"20\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" border=\"1\"><tr><td>&nbsp;</td></tr></table></td></tr>";
	print "<tr><td><span class=\"text\">" . $sas_lang['calendar_table_width'] . ": </span></td>
	       <td colspan=\"3\"><input tabindex=\"5\" type=text size=4 class=\"textfield\" name=\"calendar_options[6]\" value=\"582\"></td>
	       </tr>";
	print "<tr><td><span class=\"text\">" . $sas_lang['calendar_cell_height'] . ": </span></td>
	       <td colspan=\"3\"><input tabindex=\"6\" type=text size=4 class=\"textfield\" name=\"calendar_options[7]\" value=\"80\"></td>
	       </tr>";
	print "<tr><td colspan=\"4\">&nbsp;</td></tr>
	       <tr><td colspan=\"4\"><span class=text>$sas_lang[required_fields]</span></td></tr>";
	print "</table><br><input type=\"button\" value=\"&nbsp;&nbsp;&nbsp;$sas_lang[cancel]\" name=\"backbutton\" onClick=\"javascript:parent.location='index.php?option=modulemanager&module=$module';\" class=\"cancelbutton\">";
	print "&nbsp;<input type=\"submit\" value=\"&nbsp;&nbsp;&nbsp;$sas_lang[save]\" name=\"savebutton\" class=\"savebutton\"></form>";

	include ("interface/pageend.inc.php");
	include ("interface/footer.inc.php");
	exit;
}

if (IsSet ($addmonth))
{
	$current_year = date("Y");
	print "<FORM METHOD=\"POST\" enctype=\"multipart/form-data\" ACTION=\"index.php?option=modulemanager&module=$module&savemonth=new\">";
	print "<table>
	       <tr><td><span class=\"text\">" . $sas_lang['calendar_module_month'] . ": </span><br>
	       <select size=\"1\" name=\"month\">\n";
	for ($i = 1; $i < 13; $i++)
	{
		print "<option value=\"$i\">$i</option>\n";
	}
	print "<br></td></tr>
	       <tr><td><span class=\"text\">" . $sas_lang['calendar_module_year'] . ": </span><br>
	       <select size=\"1\" name=\"year\">\n";
	for ($i = $current_year; $i < $current_year +5; $i++)
	{
		print "<option value=\"$i\">$i</option>\n";
	}
	print "<br></td></tr><tr><td>&nbsp;</td></tr>
	       <tr><td><span class=\"text\">" . $sas_lang['calendar_module_descr'] . ":</span><br>
	       <textarea name=\"descr\" cols=\"70\" rows=\"11\"></textarea></td>
	       </tr></table>
	       <input type=\"hidden\" name=\"cal\" size=\"10\" value=\"$addmonth\">";

	print "<br><input type=\"button\" value=\"&nbsp;&nbsp;&nbsp;$sas_lang[cancel]\" name=\"backbutton\" onClick=\"javascript:parent.location='index.php?option=modulemanager&module=$module';\" class=\"cancelbutton\">";
	print "&nbsp;<input type=\"submit\" value=\"&nbsp;&nbsp;&nbsp;$sas_lang[save]\" name=\"savebutton\" class=\"savebutton\"></form>";

	include ("interface/pageend.inc.php");
	include ("interface/footer.inc.php");
	exit;
}

$query = "SELECT id, name FROM $calendars_table";
$result = mysql_query($query) or die_script($query, mysql_error());
if (mysql_num_rows($result) > 0)
{
	while ($cal_list = mysql_fetch_array($result))
	{
		print "<br><table>
		          <tr>
		          <td><img border=\"0\" src=\"graphics/delete.gif\" alt=\"" . $sas_lang['delete'] . "\" title=\"" . $sas_lang['delete'] . "\" width=\"16\" height=\"16\" onClick=\"if(confirm('$sas_lang[calendar_module_delete_cal_confirm]'))
		          parent.location='index.php?option=modulemanager&module=$module&deletecal=$cal_list[0]';\">
		          <img border=\"0\" src=\"graphics/edit.gif\" alt=\"" . $sas_lang['edit'] . "\" title=\"" . $sas_lang['edit'] . "\" width=\"16\" height=\"16\" onClick=\"parent.location='index.php?option=modulemanager&module=$module&editcal=$cal_list[0]'\">
		          </td>
		          <td><span class=link><a href=\"index.php?option=modulemanager&module=$module&showcal=$cal_list[0]\">$cal_list[1]</a></span></td>
		          </tr>";

		if ($showcal == $cal_list[0])
		{
			print "<tr><td>&nbsp;</td><td>";
			$query2 = "SELECT id, month, year FROM $event_table WHERE calendar = $cal_list[0] ORDER BY year,month"; // getting the categories
			$result2 = mysql_query($query2) or die_script($query2, mysql_error());
			if (mysql_num_rows($result2) > 0)
			{
				while ($event_list = mysql_fetch_array($result2))
				{
					print "<br><table>
								<tr>
								<td><img border=\"0\" src=\"graphics/delete.gif\" alt=\"" . $sas_lang['delete'] . "\" width=\"16\" height=\"16\" 
								onClick=\"if(confirm('" . $sas_lang['calendar_module_delete_confirm'] . "')) parent.location='index.php?option=modulemanager&module=$module&deletemonth=$event_list[0]';\">
								<img border=\"0\" src=\"graphics/edit.gif\" alt=\"" . $sas_lang['edit'] . "\" width=\"16\" height=\"16\" 
								onClick=\"parent.location='index.php?option=modulemanager&module=$module&editmonth=$event_list[0]'\">
								</td>
								<td><span class=link><a href=\"index.php?option=modulemanager&module=$module&editmonth=$event_list[0]\">$event_list[1] - $event_list[2]</a></span></td>
								</tr></table>";
				}
			}
			print "<br><img border=\"0\" src=\"graphics/document.gif\">
						 <span class=\"link\"><a href=\"index.php?option=modulemanager&module=$module&addmonth=$cal_list[0]\">" . $sas_lang['calendar_module_add_month'] . "</a></span>";

			print "</td></tr>";
		}
		print "</table>";
	}
}
print "<br><img border=\"0\" src=\"graphics/document.gif\">
       <span class=\"link\"><a href=\"index.php?option=modulemanager&module=$module&addcal=1\">" . $sas_lang['calendar_module_add_cal'] . "</a></span>";

print "\n<p><input type=\"button\" value=\"&nbsp;&nbsp;&nbsp;" . $sas_lang['goback'] . "\" name=\"backbutton\" 
       onClick=\"javascript:parent.location='index.php?option=modulemanager';\" class=\"backbutton\">";
include ("interface/pageend.inc.php");
include ("interface/footer.inc.php");
?>
