<?php


#############################################################################
#                              Site@School Forms module
# Site@School is a website management system designed for schools.
# This is a forms module which can be used in Site@School.
#
# idea and coding by Fred Stuurman (fc.stuurman@hccnet.nl)
############################################################################
# admin.php: last changed 05/10/2005  version 2.4.10
#                  by Fred Stuurman (fc.stuurman@hccnet.nl)
#$Id: admin.php,v 1.2 2006/10/15 19:08:54 fstuurman Exp $
############################################################################
#                        LICENSE INFORMATION
# Copyright (C) 2002 - 2006 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.form_name.value == "") 
    {
    alert( "<?php echo $sas_lang[input_error] ?>" );
    form.form_name.focus();
    return false ;
    }
    if (form.form_email.value == "") 
    {
    alert( "<?php echo $sas_lang[input_error] ?>" );
    form.form_email.focus();
    return false ;
    }
    if (form.form_input_number.value == "") 
    {
    alert( "<?php echo $sas_lang[input_error] ?>" );
    form.form_input_number.focus();
    return false ;
    }
  return true ;
}
</script>
<?php


if (IsSet ($_GET[addform]))
{
	$addform = $_GET[addform];
}
if (IsSet ($_GET[addinput]))
{
	$addinput = $_GET[addinput];
}
if (IsSet ($_GET[editform]))
{
	$editform = $_GET[editform];
}
if (IsSet ($_GET[saveform]))
{
	$saveform = $_GET[saveform];
}
if (IsSet ($_GET[deleteform]))
{
	$deleteform = $_GET[deleteform];
}
if (IsSet ($_GET[deleteinput]))
{
	$deleteinput = $_GET[deleteinput];
}
if (IsSet ($_GET[insertinput]))
{
	$insertinput = $_GET[insertinput];
}

if (IsSet ($_POST[form_name]))
{
	$form_name = $_POST[form_name];
}
if (IsSet ($_POST[form_email]))
{
	$form_email = $_POST[form_email];
}
if (IsSet ($_POST[form_email_subject]))
{
	$form_email_subject = $_POST[form_email_subject];
}
if (IsSet ($_POST[form_email_text]))
{
	$form_email_text = $_POST[form_email_text];
}
if (IsSet ($_POST[form_id]))
{
	$form_id = $_POST[form_id];
}
if (IsSet ($_POST[form_input_length]))
{
	$form_input_length = $_POST[form_input_length];
}
if (IsSet ($_POST[form_input_order]))
{
	$form_input_order = $_POST[form_input_order];
}
if (IsSet ($_POST[form_input_req]))
{
	$form_input_req = $_POST[form_input_req];
}
if (IsSet ($_POST[form_input_name]))
{
	$form_input_name = $_POST[form_input_name];
}
if (IsSet ($_POST[form_input_number]))
{
	$form_input_number = $_POST[form_input_number];
}
if (IsSet ($_POST[form_thank_you_text]))
{
	$form_thank_you_text = $_POST[form_thank_you_text];
}
if (IsSet ($_POST[check]))
{
	$check = $_POST[check];
}

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

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

$query = "SELECT name 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);
unset ($query);
unset ($result);

$query = "SELECT dbtables FROM $table_modules WHERE location='sn_form'";
$result = mysql_query($query) or die_script($query, mysql_error());
$moduletables = mysql_result($result, 0);
unset ($query);
unset ($result);
$moduletables = explode(",", $moduletables);
$table_forms = "$moduletables[0]";
$table_forms_config = "$moduletables[1]";

if (IsSet ($deleteform))
{
	mysql_query("DELETE from $table_forms WHERE form_id = '$deleteform'");
	mysql_query("DELETE from $table_forms_config WHERE id = '$deleteform'");
}

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

if (IsSet ($insertinput))
{
	$query1 = "SELECT sort, form_id FROM $table_forms WHERE id = '$insertinput'";
	$result1 = mysql_query($query1) or die_script($query1, mysql_error());
	$form_sort = mysql_result($result1, 0, sort);
	$form_id = mysql_result($result1, 0, form_id);
	mysql_query("INSERT INTO $table_forms VALUES ('', '', '30', '$form_id', '$form_sort','0')");
}

if (IsSet ($addinput))
{
	$query1 = mysql_query("SELECT sort FROM $table_forms WHERE form_id = '$addinput' ORDER BY sort DESC");
	$result1 = mysql_num_rows($query1);

	if ($result1 != '0')
	{
		$number = mysql_result($result1, 0) + 1;
	}
	else
	{
		$number = 1;
	}

	mysql_query("INSERT INTO $table_forms VALUES ('', '', '30', '$addinput', '$number','0')");
}

if (IsSet ($saveform))
{
	if ($saveform == "new")
	{
		$query = mysql_query("SELECT form_name FROM $table_forms_config WHERE form_name = '$form_name'");
		$results = mysql_num_rows($query);
		if ($results != '0')
		{
			print "<span class=\"error\"><br>$sas_lang[forms_module_form_name_exists]<br><br></span>";
		}
		else
		{
			mysql_query("INSERT INTO $table_forms_config VALUES ('', '$form_welcome', '$form_email_text', '$form_name', '$form_email', '$form_email_subject', '$check', '$form_thank_you_text')");
			$result1 = mysql_query("SELECT id FROM $table_forms_config WHERE form_name = '$form_name'");
			$form_id = mysql_result($result1, 0);
			for ($i = 1; $i < $form_input_number +1; $i++)
			{
				mysql_query("INSERT INTO $table_forms VALUES ('', '', '30', '$form_id', '$i','0')");
			}
		}

	}
	else
	{
		mysql_query("UPDATE $table_forms_config SET form_name='$form_name' WHERE id='$saveform'");
		mysql_query("UPDATE $table_forms_config SET email='$form_email' WHERE id='$saveform'");
		mysql_query("UPDATE $table_forms_config SET email_subject='$form_email_subject' WHERE id='$saveform'");
		mysql_query("UPDATE $table_forms_config SET email_text='$form_email_text' WHERE id='$saveform'");
		mysql_query("UPDATE $table_forms_config SET thank_you='$form_thank_you_text' WHERE id='$saveform'");
		mysql_query("UPDATE $table_forms_config SET welcome='$form_welcome' WHERE id='$saveform'");
		mysql_query("UPDATE $table_forms_config SET send_copy='$check' WHERE id='$saveform'");
		$i = 0;
		if (count($form_id > 0))
		{
			foreach ($form_id as $value)
			{
				mysql_query("UPDATE $table_forms SET name='$form_input_name[$i]' WHERE id='$value'");
				mysql_query("UPDATE $table_forms SET length='$form_input_length[$i]' WHERE id='$value'");
				mysql_query("UPDATE $table_forms SET sort='$form_input_order[$i]' WHERE id='$value'");
				mysql_query("UPDATE $table_forms SET required='$form_input_req[$i]' WHERE id='$value'");
				$i++;
			}
		}
	}
}

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\">$modulename</span>";

if (IsSet ($addform))
{
	print "<span class=\"text\"><b>:&nbsp;$sas_lang[forms_module_add]</b></center><br></span>";
	print "<FORM METHOD=\"POST\" enctype=\"multipart/form-data\" ACTION=\"index.php?option=modulemanager&module=$module&saveform=new\" 
		       onsubmit=\"return checkinput(this);\"><br><table>\n";
	print "<TR>
		          <TD vAlign=top><font size=\"2\"><b>$sas_lang[forms_module_name]:&nbsp; (*)</b></font></TD>
		        </TR>
		       <TR>
		          <TD><INPUT class=\"textfield\" size=\"50\" name=\"form_name\"><br>&nbsp;</TD>
		        </TR>
		        <TR>
		          <TD vAlign=top><font size=\"2\"><b>$sas_lang[forms_module_welcome]:&nbsp;</b></font></TD>
		        </TR>
		       <TR>  
		          <TD><TEXTAREA cols=80 rows=\"3\" name=\"form_welcome\"></TEXTAREA><br>&nbsp;</TD>
		        </TR>
		        <TR>
		          <TD vAlign=top><font size=\"2\"><b>$sas_lang[forms_module_email]:&nbsp; (*)</b></font></TD>
		        </TR>
		       <TR>  
		          <TD><INPUT class=\"textfield\" size=\"50\" name=\"form_email\"><br>&nbsp;</TD>
		        </TR>
		        <TR>
		          <TD vAlign=top><font size=\"2\"><b>$sas_lang[forms_module_email_subject]:&nbsp;</b></font></TD>
		        </TR>
		       <TR>  
		          <TD><INPUT class=\"textfield\" size=\"50\" name=\"form_email_subject\"><br>&nbsp;</TD>
		        </TR>
		        <TR>
		          <TD vAlign=top><font size=\"2\"><b>$sas_lang[forms_module_email_text]:&nbsp;</b></font></TD>
		        </TR>
		       <TR>  
		          <TD><TEXTAREA cols=70 rows=\"1\" name=\"form_email_text\"></TEXTAREA><br>&nbsp;</TD>
		        </TR>
		        <TR>
		          <TD vAlign=top><font size=\"2\"><b>$sas_lang[forms_module_thank_you_text]:&nbsp;</b></font></TD>
		        </TR>
		       <TR>  
		          <TD><TEXTAREA cols=70 rows=\"1\" name=\"form_thank_you_text\"></TEXTAREA><br>&nbsp;</TD>
		        </TR>
		        <TR>
		          <TD vAlign=top><font size=\"2\"><b>$sas_lang[forms_module_number_input_fields]: (*)</b></font></TD>
		        </TR>
		       <TR>  
		          <TD><INPUT class=\"textfield\" size=\"4\" name=\"form_input_number\">&nbsp;&nbsp;<span class=text>$sas_lang[required_fields]</span><br>&nbsp;</TD>
		        </TR>";

	print "</table>
		       <p><input type=\"button\" value=\"&nbsp;&nbsp;&nbsp;$sas_lang[goback]\" name=\"backbutton\"
		       onClick=\"javascript:parent.location='index.php?option=modulemanager&module=$module';\" class=\"backbutton\">";
	print "&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");
	exit;
}

if (IsSet ($editform))
{
	$query2 = "SELECT id, form_name, email, email_subject, email_text, welcome, send_copy, thank_you FROM $table_forms_config WHERE id='$editform'";
	$result2 = mysql_query($query2) or die_script($query2, mysql_error());
	$form_list = mysql_fetch_array($result2);
	$query3 = "SELECT id, name, length, sort, required FROM $table_forms WHERE form_id='$form_list[0]' ORDER BY sort";
	$result3 = mysql_query($query3) or die_script($query3, mysql_error());

	print "<span class=\"text\"><b>:&nbsp;$sas_lang[forms_module_edit]</b><p></span>";
	print "<FORM METHOD=\"POST\" enctype=\"multipart/form-data\" ACTION=\"index.php?option=modulemanager&module=$module&saveform=$editform\">
		       <table cellspacing=\"0\" cellpadding=\"0\">";

	print "<TR>
		          <TD vAlign=top><font size=\"2\"><b>$sas_lang[forms_module_name]:&nbsp;</b></font></TD>
		       </TR>
		       <TR>
		          <TD><INPUT class=\"textfield\" size=\"50\" name=\"form_name\" value=\"$form_list[1]\"><br>&nbsp;</TD>
		        </TR>
		        <TR>
		          <TD vAlign=top><font size=\"2\"><b>$sas_lang[forms_module_welcome]:&nbsp;</b></font></TD>
		        </TR>
		       <TR>
		          <TD><TEXTAREA cols=80 rows=\"3\" name=\"form_welcome\">$form_list[5]</TEXTAREA><br>&nbsp;</TD>
		        </TR>
		        <TR>
		          <TD vAlign=top><font size=\"2\"><b>$sas_lang[forms_module_email]:&nbsp;</b></font></TD>
		        </TR>
		       <TR>
		          <TD><INPUT class=\"textfield\" size=\"50\" name=\"form_email\" value=\"$form_list[2]\"><br>&nbsp;</TD>
		        </TR>
		        <TR>
		          <TD vAlign=top><font size=\"2\"><b>$sas_lang[forms_module_email_subject]:&nbsp;</b></font></TD>
		        </TR>
		       <TR>
		          <TD><INPUT class=\"textfield\" size=\"50\" name=\"form_email_subject\" value=\"$form_list[3]\"><br>&nbsp;</TD>
		        </TR>
		        <TR>
		          <TD vAlign=top><font size=\"2\"><b>$sas_lang[forms_module_email_text]:&nbsp;</b></font></TD>
		        </TR>
		        
		       <TR>
		          <TD><TEXTAREA cols=80 rows=\"3\" name=\"form_email_text\">$form_list[4]</TEXTAREA><br>&nbsp;</TD>
		        </TR>
		        <TR>
		          <TD vAlign=top><font size=\"2\"><b>$sas_lang[forms_module_thank_you_text]:&nbsp;</b></font></TD>
		        </TR>
		        <TR>  
		          <TD><TEXTAREA cols=70 rows=\"1\" name=\"form_thank_you_text\">$form_list[7]</TEXTAREA><br>&nbsp;</TD>
		        </TR>
		        <TR>";
	
	print "   </TR>
		        </table><table cellspacing=\"0\" cellpadding=\"0\">
		        <tr> 
		            <td colspan=\"2\">&nbsp;</td>
		            <td><font size=\"2\"><b>$sas_lang[forms_module_input_name]:&nbsp;</b></font></td>
		            <TD>&nbsp;&nbsp;</td>
		            <td><font size=\"2\"><b>$sas_lang[forms_module_input_length]:&nbsp;</b></font></td>
		            <TD>&nbsp;&nbsp;&nbsp;</td>
		            <td><font size=\"2\"><b>$sas_lang[forms_module_input_order]:&nbsp;</b></font></td>
		            <TD>&nbsp;</td>
		            <td><font size=\"2\"><b>$sas_lang[forms_module_input_required]:&nbsp;</b></font></td>
		            <tr>";
	$i = 0;
	while ($form_field_list = mysql_fetch_array($result3))
	{
		print "<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[forms_module_delete_confirm_input]'))
				          parent.location='index.php?option=modulemanager&module=$module&editform=$editform&deleteinput=$form_field_list[0]';\">
				          <img border=\"0\" src=\"graphics/insert.gif\" alt=\"" . $sas_lang['edit'] . "\" title=\"" . $sas_lang['edit'] . "\" width=\"16\" height=\"16\" 
				          onClick=\"parent.location='index.php?option=modulemanager&module=$module&editform=$editform&insertinput=$form_field_list[0]'\">
				          <br>&nbsp;</td>
				          <TD>&nbsp;</td>
				          <TD><INPUT class=\"textfield\" size=\"50\" name=\"form_input_name[$i]\" value=\"$form_field_list[1]\"><br>&nbsp;</TD>
				          <TD>&nbsp;</td>
				          <TD><INPUT class=\"textfield\" size=\"5\" name=\"form_input_length[$i]\" value=\"$form_field_list[2]\"><br>&nbsp;</TD>
				          <TD>&nbsp;&nbsp;&nbsp;</td>
				          <TD align=\"right\"><INPUT class=\"textfield\" size=\"5\" name=\"form_input_order[$i]\" value=\"$form_field_list[3]\"><br>&nbsp;</TD>
				          <TD>&nbsp;</td>";
		if ($form_field_list[4] == 0)
		{
			print "<TD align=\"right\"><input type=\"checkbox\" name=\"form_input_req[$i]\" value=\"1\"><br>&nbsp;</TD>";
		}
		else
		{
			print "<TD align=\"right\"><input type=\"checkbox\" name=\"form_input_req[$i]\" value=\"1\" checked><br>&nbsp;</TD>";
		}
		print "<INPUT type=\"hidden\" size=5 name=form_id[$i] value=\"$form_field_list[0]\"></TR>";
		$i++;
	}
	print "</table>";

	print "<p><input type=\"button\" value=\"&nbsp;&nbsp;&nbsp;$sas_lang[goback]\" name=\"backbutton\"
		onClick=\"javascript:parent.location='index.php?option=modulemanager&module=$module';\" class=\"backbutton\">";

	print "&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");
	exit;
}

$query = "SELECT id, form_name FROM $table_forms_config";
$result = mysql_query($query) or die_script($query, mysql_error());
if (mysql_num_rows($result) > 0)
{
	while ($form_list = mysql_fetch_array($result))
	{
		print "</center><p><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[forms_module_delete_confirm]'))
				          parent.location='index.php?option=modulemanager&module=$module&deleteform=$form_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&editform=$form_list[0]'\">
				          </td>
				          <td><span class=link><a href=\"index.php?option=modulemanager&module=$module&editform=$form_list[0]\">$form_list[1]</a></span></td>
				          </tr></table>";
	}
}
print "<p><img border=\"0\" src=\"graphics/insert.gif\">
       <span class=\"link\"><a href=\"index.php?option=modulemanager&module=$module&addform=1\">$sas_lang[forms_module_add]</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");
?>
