<?php

#############################################################################
#           			                   Site@School
# Site@School is a website management system designed for primary schools.
#
# About Site@School:
# Comissioned by: City District 'de Baarsjes', Amsterdam, Netherlands
# Original idea, coding (version 1.0) and design : Mark de Haan (mark@starwave.nl)
# Ideas, improvements, expansions (version 1.1 - 2.3): Fred Stuurman 
# Current coder & maintainer: Fred Stuurman (fc.stuurman@hccnet.nl)
############################################################################
# con_alerts.inc.php: last changed 26/06/2004  version 2.3
#                       by Fred Stuurman (fc.stuurman@hccnet.nl)
#$Id: con_alerts.inc.php,v 1.1 2006/10/03 08:33:55 fstuurman Exp $
############################################################################
#                        LICENSE INFORMATION
# Copyright (C) 2002, 2003, 2004 Municipality of Amsterdam, City District
#  'de Baarsjes', Amsterdam, the Netherlands.
#
# 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.email.value == "") 
    {
    alert( "<?php echo $sas_lang['input_error'] ?>" );
    form.email.focus();
    return false ;
    }
    if (form.name.value == "") 
    {
    alert( "<?php echo $sas_lang['input_error'] ?>" );
    form.name.focus();
    return false ;
    }
  return true ;
}
</script>
<?php


if (IsSet ($_GET['deletealert']))
{
	$deletealert = $_GET['deletealert'];
}
if (IsSet ($_GET['savealert']))
{
	$savealert = $_GET['savealert'];
}
if (IsSet ($_GET['newalert']))
{
	$newalert = $_GET['newalert'];
}
if (IsSet ($_GET['alert']))
{
	$alert = $_GET['alert'];
}
if (IsSet ($_GET['config']))
{
	$config = $_GET['config'];
}

if (IsSet ($_POST['sections']))
{
	$sections = $_POST['sections'];
}
if (IsSet ($_POST['email']))
{
	$email = $_POST['email'];
}
if (IsSet ($_POST['name']))
{
	$name = $_POST['name'];
}
if (IsSet ($_POST['send_alerts']))
{
	$send_alerts = $_POST['send_alerts'];
}
if (IsSet ($_POST['send_alerts_per_section']))
{
	$send_alerts_per_section = $_POST['send_alerts_per_section'];
}

$table_alerts = $dbprefix . "alerts";
$table_prot = $dbprefix . "prot_sections";

if (IsSet ($deletealert))
{
	mysql_query("DELETE from $table_alerts WHERE id = '$deletealert'");
}

if (IsSet ($savealert))
{
	if ($savealert == "config")
	{
		mysql_query("UPDATE $table_configuration SET config_value='$send_alerts' where config_key='send_alerts'");
		mysql_query("UPDATE $table_configuration SET config_value='$send_alerts_per_section' where config_key='send_alerts_per_section'");
	}

	if (IsSet ($sections))
	{
		$sections = str_replace(" ", "", $sections); //strip blank from input
		$section_array = explode(",", $sections); //create an array
		$error = 0;
		$msg = $sas_lang['sections_not_found'];
		foreach ($section_array as $value) //check if sections exist.
		{
			if (substr($value, 0, 1) == "P")
			{
				$table_sections = $dbprefix . "prot_sections";
				$value = str_replace("P", "", $value);
			}
			$result = mysql_query("SELECT id FROM $table_sections WHERE id = '$value'");
			$num_rows = mysql_num_rows($result);
			if ($num_rows == 0)
			{
				$error = 1;
				$msg .= $value . " ";
			}
		}
	}

	if ($savealert == "new")
	{
		$query = mysql_query("SELECT email FROM $table_alerts WHERE email = '$email'");
		$results = mysql_num_rows($query);
		if ($results != '0')
		{
			print "<script language=\"Javascript\">
						   <!--
						   alert (\"" . $sas_lang['alert_email_exists'] . "\");
						   window.location = 'index.php?option=configuration&suboption=alerts&newalert=1' ;	
						   //-->
						   </script>";
		}
		else
		{
			if ($error == 0)
			{
				mysql_query("INSERT INTO $table_alerts (id, email, sections, name) VALUES (NULL, '$email', '$sections', '$name')");
			}
			else
			{
				print "<span class=\"error\"><br>" . $msg . "<br><br></span>";
			}
		}
	}
	else
	{
		mysql_query("UPDATE $table_alerts SET name='$name' WHERE id='$savealert'");
		mysql_query("UPDATE $table_alerts SET email='$email' WHERE id='$savealert'");
		if ($error == 0)
		{
			mysql_query("UPDATE $table_alerts SET sections='$sections' WHERE id='$savealert'");
		}
		else
		{
			print "<span class=\"error\">" . $msg . "</span>";
		}
	}
}
include ("interface/header.inc.php");
include ("interface/menubar.inc.php");
include ("interface/menustart.inc.php");
print "<span class=heading>" . $sas_lang['filemanager_menu'] . "</span><br><br>";
print "<span class=link><a href=index.php?option=configuration&suboption=alerts>" . $sas_lang['manage'] . "</a><p>
       <a href=index.php?option=configuration&suboption=alerts&config=yes>" . $sas_lang['configuration'] . "</a><br></span>";
include ("interface/menuend.inc.php");
include ("interface/pagestart.inc.php");

if (substr("$access", 9, 1) == "0") //bit 10 off the accessrights
{
	print "<span class=\"heading\">" . $sas_lang['noaccess'] . "<br></span>";
	print "<span class=\"text\">" . $sas_lang['noaccess_introduction'] . "</span><br><br><br>";
	include ("interface/pageend.inc.php");
	include ("interface/footer.inc.php");
	exit;
}

print "<span class=\"heading\">" . $sas_lang['alerts'] . ":<p></span>";

if (IsSet ($newalert))
{
	$query2 = "SELECT id, name FROM $table_sections ORDER BY sort";
	$result2 = mysql_query($query2) or die_script($query2, mysql_error());
	$query3 = "SELECT id, name FROM $table_prot ORDER BY sort";
	$result3 = mysql_query($query3) or die_script($query3, mysql_error());
	print "<table border=\"0\">
				<tr><td valign=\"top\"><table border=\"0\">
				<tr><td colspan=\"2\"><span class=\"text\">" . $sas_lang['alert_add'] . ":</span></td></tr>
				<tr><td>&nbsp;</td></tr>";
	print "<FORM METHOD=\"POST\" NAME=\"form\" ACTION=\"index.php?option=configuration&suboption=alerts&savealert=new\" onsubmit=\"return checkinput(this);\">";
	print "<tr><td><span class=text><b>" . $sas_lang['fullname'] . ": (*)</b></span></td><td><input tabindex=\"1\" type=\"text\" name=\"name\" size=\"40\" class=\"textfield\">&nbsp;</td></tr>
				<tr><td colspan=\"2\">&nbsp;</td></tr>";
	print "<tr><td><span class=text><b>" . $sas_lang['alert_sections'] . ":</b></span></td><td><input tabindex=\"2\" type=\"text\" name=\"sections\" size=\"40\" class=\"textfield\">&nbsp;</td></tr><td colspan=\"2\">&nbsp;</td></tr>";
	print "<tr><td><span class=text><b>" . $sas_lang['email'] . ": (*)</b></span></td><td><input tabindex=\"3\" type=\"text\" name=\"email\" size=\"60\" class=\"textfield\">&nbsp;</td></tr>
				<tr>";
	print "<tr><td>&nbsp;</td><td><span class=text>" . $sas_lang['required_fields'] . "</span></td></tr>";
	print "</table></td><td><table border=\"0\"><tr><td>&nbsp;</td><td><span class=text><b>S@S " . $sas_lang['section'] . ":</b></span></td></tr>";
	while ($section_list = mysql_fetch_array($result2))
	{
		print "<tr><td><span class=text>$section_list[0]</span></td><td><span class=text>$section_list[1]</span></td></tr>";
	}
	print "<tr><td>&nbsp;</td><td><span class=text><b>" . $sas_lang['protected_area'] . " " . $sas_lang['section'] . ":</b></span></td></tr>";
	while ($section_list = mysql_fetch_array($result3))
	{
		print "<tr><td><span class=text>P$section_list[0]</span></td><td><span class=text>$section_list[1]</span></td></tr>";
	}
	print "</table></td></tr></table>";
	print "\n<br><input type=\"button\" value=\"&nbsp;&nbsp;&nbsp;" . $sas_lang['goback'] . "\" name=\"backbutton\" onClick=\"javascript:history.go(-1)\" class=\"backbutton\">";
	print "&nbsp;<input tabindex=\"6\" type=\"submit\" value=\"&nbsp;&nbsp;&nbsp;" . $sas_lang['save'] . "\" name=\"savebutton\" class=\"savebutton\">";
	print "</form>";

	exit;
}

if (IsSet ($alert))
{
	$query = "SELECT * FROM $table_alerts where id='$alert'"; // getting the alert info 
	$result = mysql_query($query) or die_script($query, mysql_error());
	$alert_list = mysql_fetch_array($result);

	$query2 = "SELECT id, name FROM $table_sections ";
	$result2 = mysql_query($query2) or die_script($query2, mysql_error());

	$query3 = "SELECT id, name FROM $table_prot ORDER BY sort";
	$result3 = mysql_query($query3) or die_script($query3, mysql_error());

	print "<table border=\"0\">
				<tr><td valign=\"top\"><table border=\"0\">
				<tr><td colspan=\"2\"><span class=\"text\">" . $sas_lang['alert_change'] . ":</span></td></tr>
				<tr><td>&nbsp;</td></tr>";
	print "<FORM METHOD=\"POST\" NAME=\"form\" ACTION=\"index.php?option=configuration&suboption=alerts&savealert=$alert\" onsubmit=\"return checkinput(this);\">";
	print "<tr><td><span class=text><b>" . $sas_lang['fullname'] . ": (*)</span></td><td><input tabindex=\"1\" type=\"text\" name=\"name\" size=\"40\" value=\"$alert_list[3]\" class=\"textfield\">&nbsp;</td></tr>
				<tr><td colspan=\"2\">&nbsp;</td></tr>";
	print "<tr><td><span class=text><b>" . $sas_lang['alert_sections'] . ":</b></span></td><td><input tabindex=\"2\" type=\"text\" name=\"sections\" size=\"20\" value=\"$alert_list[2]\" class=\"textfield\">&nbsp;</td></tr><tr><td colspan=\"2\">&nbsp;</td></tr>";
	print "<tr><td><span class=text><b>" . $sas_lang['email'] . ": (*)</span></td><td><input tabindex=\"3\" type=\"text\" name=\"email\" size=\"60\" value=\"$alert_list[1]\" class=\"textfield\">&nbsp;</td></tr>";
	print "<tr><td>&nbsp;</td><td><span class=text>" . $sas_lang['required_fields'] . "</span></td></tr>";
	print "</table></td><td><table border=\"0\"><tr><td>&nbsp;</td><td><span class=text><b>S@S " . $sas_lang['section'] . ":</b></span></td></tr>";
	while ($section_list = mysql_fetch_array($result2))
	{
		print "<tr><td><span class=text>$section_list[0]</span></td><td><span class=text>$section_list[1]</span></td></tr>";
	}
	print "<tr><td>&nbsp;</td><td><span class=text><b>" . $sas_lang['protected_area'] . " " . $sas_lang['section'] . ":</b></span></td></tr>";
	while ($section_list = mysql_fetch_array($result3))
	{
		print "<tr><td><span class=text>P$section_list[0]</span></td><td><span class=text>$section_list[1]</span></td></tr>";
	}
	print "</table></td></tr></table>";

	print "\n<br><input type=\"button\" value=\"&nbsp;&nbsp;&nbsp;" . $sas_lang['goback'] . "\" name=\"backbutton\" 
				onClick=\"javascript:history.go(-1)\" class=\"backbutton\">";
	print "&nbsp;<input tabindex=\"6\" type=\"submit\" value=\"&nbsp;&nbsp;&nbsp;" . $sas_lang['save'] . "\" name=\"savebutton\" class=\"savebutton\">";
	print "</form>";
	exit;
}

if ($config == "yes")
{
	$send_alerts = $sas_config['send_alerts'];
	$send_alerts_per_section = $sas_config['send_alerts_per_section'];

	print "<table border=\"0\" width=\"60%\">";
	print "<tr><td colspan=\"2\"><span class=\"text\">" . $sas_lang['configuration'] . ":</span></td></tr>
				<tr><td>&nbsp;</td></tr>";
	print "<FORM METHOD=\"POST\" NAME=\"form\" ACTION=\"index.php?option=configuration&suboption=alerts&savealert=config\">";
	print "<tr><td><span class=text><b>" . $sas_lang['alerts_send'] . "</b></span></td><td><span class=text>" . $sas_lang['yes'] . "</span>";
	if ($send_alerts == "1")
	{
		print "<input type=\"radio\" value=\"1\" name=\"send_alerts\" checked>";
		print "<span class=text>&nbsp;&nbsp;&nbsp; " . $sas_lang['no'] . "</span>";
		print "<input type=\"radio\" name=\"send_alerts\" value=\"0\"></td></tr>";
	}
	else
	{
		print "<input type=\"radio\" value=\"1\" name=\"send_alerts\">";
		print "<span class=text>&nbsp;&nbsp;&nbsp; " . $sas_lang['no'] . "</span>";
		print "<input type=\"radio\" name=\"send_alerts\" value=\"0\" checked></td></tr>";
	}
	print "<tr><td><span class=text><b>" . $sas_lang['alerts_per_section'] . "</b></span></td><td><span class=text>" . $sas_lang['yes'] . "</span>";
	if ($send_alerts_per_section == "1")
	{
		print "<input type=\"radio\" value=\"1\" name=\"send_alerts_per_section\" checked>";
		print "<span class=text>&nbsp;&nbsp;&nbsp; " . $sas_lang['no'] . "</span>";
		print "<input type=\"radio\" name=\"send_alerts_per_section\" value=\"0\"></td></tr>";
	}
	else
	{
		print "<input type=\"radio\" value=\"1\" name=\"send_alerts_per_section\">";
		print "<span class=text>&nbsp;&nbsp;&nbsp; " . $sas_lang['no'] . "</span>";
		print "<input type=\"radio\" name=\"send_alerts_per_section\" value=\"0\" checked></td></tr>";
	}
	print "</table><p>";
	print "\n<br><input type=\"button\" value=\"&nbsp;&nbsp;&nbsp;" . $sas_lang['goback'] . "\" name=\"backbutton\" 
			onClick=\"javascript:history.go(-1)\" class=\"backbutton\">";
	print "&nbsp;<input tabindex=\"6\" type=\"submit\" value=\"&nbsp;&nbsp;&nbsp;" . $sas_lang['save'] . "\" name=\"savebutton\" class=\"savebutton\">";
	print "</form>";
}
else
{
	$query = "SELECT * FROM $table_alerts "; // getting the alert info 
	$result = mysql_query($query) or die_script($query, mysql_error());

	print "<span class=\"text\">" . $sas_lang['alert_intro'] . "</span><br><br><br>";
	print "<table border=\"0\" cellpadding=\"2\">
	       <tr><td colspan=\"2\">&nbsp;</td>
	       <td><span class=\"text\"><b>" . $sas_lang['fullname'] . ":&nbsp;&nbsp;</b></span></td>
	       <td><span class=\"text\"><b>" . $sas_lang['email'] . ":</b></span></td>
	       <td><span class=\"text\"><b>" . $sas_lang['section'] . ":</b></span></td></tr>";

	while ($alert_list = mysql_fetch_array($result))
	{
		$alert_sections = explode(",", $alert_list[2]);
		print "<tr>
				 <td><a href=\"index.php?option=configuration&suboption=alerts&deletealert=$alert_list[0]\" 
				 onClick=\"return confirm('" . $sas_lang['deleteuser_confirmation'] . "')\">
				 <img border=\"0\" src=\"graphics/delete.gif\" alt=\"" . $sas_lang['delete'] . "\" width=\"16\" height=\"16\"></td>
				 <td><a href=\"index.php?option=configuration&suboption=alerts&alert=$alert_list[0]\">
				 <img border=\"0\" src=\"graphics/edit.gif\" alt=\"" . $sas_lang['edit'] . "\" width=\"16\" height=\"16\"></a></td>
				 <td><span class=link><a href=\"index.php?option=configuration&suboption=alerts&alert=$alert_list[0]\">$alert_list[3]</span></a></td>
				 <td><span class=link>$alert_list[1]</span></td>
				 <td><span class=link>";
		$i = 0;

		while ($i < count($alert_sections)) //loop through the sections
		{
			if (strlen($alert_sections[$i]) > 0) //is string not empty?
			{
				if (substr($alert_sections[$i], 0, 1) == "P")
				{
					$id = substr($alert_sections[$i], 1, 1);
					$query2 = "SELECT name FROM $table_prot where id = '$id'";
					$result2 = mysql_query($query2) or die_script($query2, mysql_error()); //get the section name prot area
				}
				else
				{
					$query2 = "SELECT name FROM $table_sections where id = $alert_sections[$i]";
					$result2 = mysql_query($query2) or die_script($query2, mysql_error()); //get the section name
				}
				if (mysql_num_rows($result2) == 1)
				{
					$section_name = mysql_result($result2, 0);
					print "[$section_name] &nbsp;";
				}
			}
			next($alert_sections);
			$i++;
		}

		print "</span></td></tr>";
	}
	print "</table>";
	print "<p><img src=graphics/folder_closed.gif alt=\"" . $sas_lang['alert_add'] . "\">&nbsp;
	       <a href=index.php?option=configuration&suboption=alerts&newalert=1><span class=link>" . $sas_lang['alert_add'] . "</a></span>";
	print "<p><br><input type=\"button\" value=\"&nbsp;&nbsp;&nbsp;" . $sas_lang['goback'] . "\" name=\"backbutton\" 
			onClick=\"javascript:history.go(-1)\" class=\"backbutton\">";
}
?>