<?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)
############################################################################
# mod_start_inc.php: last changed 12/05/2006  2.4                                     
#                    by Fred Stuurman (fc.stuurman@hccnet.nl)
#$Id: modulemanager.inc.php,v 1.1 2006/10/03 08:33:55 fstuurman Exp $
############################################################################
#                        LICENSE INFORMATION
# Copyright (C) 2002 - 2005 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");
}
if (IsSet ($_GET['module']))
{
	$module = $_GET['module'];
}
if (IsSet ($_GET['modoption']))
{
	$modoption = $_GET['modoption'];
}
else
{
	$modoption = "";
}
if (IsSet ($_GET['module_number']))
{
	$module_number = $_GET['module_number'];
}
if (IsSet ($_POST['module_number']))
{
	$module_number = $_POST['module_number'];
}
if (IsSet ($_POST['modulename']))
{
	$modulename = sql_addslashes($_POST['modulename']);
}
if (IsSet ($_POST['moduleoptions']))
{
	$moduleoptions = $_POST['moduleoptions'];
}

if ($modoption == "save")
{
	$query = "SELECT options FROM $table_modules WHERE id='$module_number'";
	$result = mysql_query($query) OR die_script($query, mysql_error());
	$moduleoptions_old = mysql_result($result, 0);
	$moduleoptions_old = explode(",", mysql_result($result, 0));
	unset ($query);
	unset ($result);
	mysql_query("UPDATE $table_modules SET name='$modulename' WHERE id='$module_number'");
	unset ($module);
	$i = 0;
	while ($i < count($moduleoptions_old))
	{
		$moduleoption = $moduleoption . $moduleoptions_old[$i] . ",";
		$i++;
		$moduleoption = $moduleoption . $moduleoptions[$i] . ",";
		$i++;
	}

	$moduleoption = substr($moduleoption, 0, strlen($moduleoption) - 1);
	//print $moduleoption ;
	mysql_query("UPDATE $table_modules SET options='$moduleoption' WHERE id='$module_number'");
}

if ($modoption == "edit")
{
	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 FROM $table_modules WHERE id='$module_number'";
	$result = mysql_query($query) or die_script($query, mysql_error());
	$modulename = mysql_result($result, 0);
	unset ($query);
	unset ($result);
	$query = "SELECT options FROM $table_modules WHERE id='$module_number'";
	$result = mysql_query($query) or die_script($query, mysql_error());
	$moduleoptions = mysql_result($result, 0);
	$moduleoptions = explode(",", mysql_result($result, 0));
	unset ($query);
	unset ($result);
	print "<span class=\"heading\">" . $sas_lang['modulemanager'] . "<br></span>";
	print "<span class=\"text\">" . $sas_lang['modulemanager_introduction'] . "</span>";
	print "<FORM METHOD=\"POST\" ACTION=\"index.php?option=modulemanager&modoption=save\">";
	print "<p><br><table border=0><tr><td align=\"right\" width=\"120\"><span class=\"text\">" . $sas_lang['module_name'] . "</span></td>
	  <td><input tabindex=\"1\" type=text size=60 class=\"textfield\" name=\"modulename\" value=\"$modulename\"></td></tr>
	  </tr><tr><td colspan=\"2\">&nbsp;</td></tr><input type=hidden name=\"module_number\" value=\"$module_number\">";
	print "<tr><td>&nbsp;</td>
	         <td><span class=\"text\">" . $sas_lang['module_script'] . "</span></td></tr>";
	$i = 0;
	while ($i < count($moduleoptions))
	{
		print "<tr><td align=\"right\"><span class=\"text\">$moduleoptions[$i]:</span></td>";
		$i++;
		print "<td><input tabindex=\"2\" type=text size=60 class=\"textfield\" name=\"moduleoptions[$i]\" value=\"$moduleoptions[$i]\"></td></tr>";
		$i++;
	}
	print "</table><input type=hidden name=\"number\" value=\"$i\">";
	print "<br><br><input type=\"button\" value=\"&nbsp;&nbsp;&nbsp;" . $sas_lang['cancel'] . "\" name=\"backbutton\" onClick=\"javascript:parent.location='index.php?option=modulemanager';\" class=\"backbutton\">";
	print "\n&nbsp;<input type=\"submit\" value=\"&nbsp;&nbsp;&nbsp;" . $sas_lang['save'] . "\" name=\"savebutton\" class=\"savebutton\">";
	print "</span></form>";
	include ("interface/pageend.inc.php");
	include ("interface/footer.inc.php");
}
else
{

	if (IsSet ($module))
	{
		$m_access = substr($access, strrpos($access, ',') + 1); //access bits module after comma

		$query = "SELECT id,name FROM $table_modules"; //get all installed modules id's               
		$result = mysql_query($query) or die_script($query, mysql_error());
		$i = 0;
		while ($module_list = mysql_fetch_array($result))
		{
			if ($module_list[0] == $module)
			{ //get the access bit number
				$access_bit = $i;
			}
			$i++;
		}
		if (substr("$m_access", $access_bit, 1) == "0")
		{
			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");
			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;
		}

		$query = "SELECT admin, location FROM $table_modules WHERE id='$module'"; // getting the name of admin script
		$result = mysql_query($query) or die_script($query, mysql_error());
		$adminpage = mysql_result($result, 0, 0);
		$location = mysql_result($result, 0, 1);
		unset ($query);
		unset ($result);
		include ("modules/$location/$adminpage");
		exit;
	}

	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");

	if (substr("$access", 2, 1) == "0") //bit 3 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['modulemanager'] . "<br></span>";
	print "<span class=\"text\">" . $sas_lang['modulemanager_introduction'] . "</span><br><br>";
	print "<span class=\"text\">" . $sas_lang['installed_modules'] . "</span><br><br>";
	print "<table border=\"0\" cellpadding=\"2\" cellspacing=\"0\" style=\"border-collapse: collapse\" bordercolor=\"#111111\">";

	$query = "SELECT id, name FROM $table_modules";
	$result = mysql_query($query) or die_script($query, mysql_error());
	$column = 0;
	while ($module_list = mysql_fetch_array($result))
	{
		if ($column == 1)
		{
			print "<td>
								<img border=\"0\" src=\"graphics/module.gif\">
								</td>
								<td><a href=\"index.php?option=modulemanager&modoption=edit&module_number=$module_list[0]\">[" . $sas_lang['edit'] . "]</a></td>
								<td><span class=\"link\"><a href=\"index.php?option=modulemanager&module=$module_list[0]\">$module_list[1]</a></span>
								</td></tr>";
			$column = 0;
		}
		else
		{
			print "<tr>
								<td>
								<img border=\"0\" src=\"graphics/module.gif\">
								</td>
								<td><a href=\"index.php?option=modulemanager&modoption=edit&module_number=$module_list[0]\">[" . $sas_lang['edit'] . "]</a></td>
								<td><span class=\"link\"><a href=\"index.php?option=modulemanager&module=$module_list[0]\">$module_list[1]</a></span>
								</td>
								<td width=\"75\">&nbsp;</td>";
			$column = 1;
		}
	}

	print "</table>";
}

include ("interface/pageend.inc.php");
include ("interface/footer.inc.php");
?>
