<?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)
############################################################################
# show_form.php: last changed 05/10/2006  version 2.4.10
#                  by Fred Stuurman (fc.stuurman@hccnet.nl)
#$Id: show_form.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");
}

//$sendemail = "" ;
if ((IsSet ($_GET['sendemail'])) AND ($_GET['sendemail'] == "yes"))
{
	$sendemail = $_GET['sendemail'];
}

load_language($cmsdir . '/', 'sn_form'); //load language file

//set the forms table names
$table_forms = $dbprefix . "m_forms";
$table_forms_config = $dbprefix . "m_forms_config";

$query = "SELECT email FROM $table_forms_config WHERE form_name = '$pagetitle'"; // getting the email adress
$result = mysql_query($query) or die_script($query, mysql_error());
$num_rows = mysql_num_rows($result);
if ($num_rows == 0)
{
	print $sas_lang['forms_module_form_page_error'];
	//no form with this pagename found.
}
else
{
	$email = mysql_result($result, 0);
	unset ($query);
	unset ($result);

	$query = "SELECT id, welcome FROM $table_forms_config WHERE form_name = '$pagetitle'";
	$result = mysql_query($query) or die_script($query, mysql_error());
	$welcome_text = mysql_result($result, 0, 'welcome');
	//$send_copy = mysql_result($result, 0, 'send_copy');
	$form_id = mysql_result($result, 0, 'id');
	unset ($query);
	unset ($result);

	if ($sendemail == "yes")
	{
		if (IsSet ($_POST['forms_input_list']))
		{
			$forms_input_list = $_POST['forms_input_list'];
		}

		$query = "SELECT email_text, email, email_subject, thank_you FROM $table_forms_config WHERE form_name = '$pagetitle'";
		$result = mysql_query($query) or die_script($query, mysql_error());
		$email_text = mysql_result($result, 0, email_text);
		$email = mysql_result($result, 0, email);
		$email_subject = mysql_result($result, 0, email_subject);
		$thank_you_text = mysql_result($result, 0, thank_you);
		unset ($query);
		unset ($result);

		//$query = "SELECT name, length, id FROM $table_forms WHERE form_id = '$form_id' ORDER BY sort";        
		//$result = mysql_query($query) or die_script($query,mysql_error());
		//$i = 0 ;

		$message .= "<html><body><br>$email_text<p>";
		foreach ($forms_input_list as $key => $value)
		{
			$query = "SELECT name, length, id FROM $table_forms WHERE form_id = '$form_id' AND id = '$key'";
			$result = mysql_query($query) or die_script($query, mysql_error());
			$field_name = mysql_result($result, 0, name);
			$message .= $field_name . "<br><b>";
			$user_input = htmlspecialchars(strip_tags($forms_input_list[$key])); //remove html tags
			$message .= stripslashes($user_input) . "</b><p>";
		}

		$message .= str_repeat("-", 75);
		$message .= "</body></html>";
		$message = wordwrap($message);
		require ("$cmsdir/core/class.phpmailer.php"); //use the phpmailer class
		$mail = new PHPMailer();
		$mail->From = $sas_config['email']; //site email address
		$mail->FromName = $sitename;
		$mail->Subject = $email_subject;
		$mail->Body = $message;
		$mail->IsHTML(true);
		$mail->AddAddress($email); //add email address owner form.

		$mail->Send();
		print $thank_you_text;
		print $message;
		print "<p><form><input type=\"button\" value=\"$sas_lang[goback]\" name=\"backbutton\" 
								onClick=\"parent.location='index.php?section=$section&page=$page'\" class=\"backbutton\"></form>";
	}
	else
	{

		$query = "SELECT name, required, length, id FROM $table_forms WHERE form_id = '$form_id' ORDER BY sort";
		$result = mysql_query($query) or die_script($query, mysql_error()); //get the fields where input is required!

		print "<script language=\"JavaScript\" type=\"text/javascript\">
					function checkinput(form)
					{";
		$i = 0;
		while ($forms_list = mysql_fetch_array($result))
		{
			if (($forms_list[1] == 1) AND (substr($forms_list[2], 0, 1) != "S"))
			{
				print "if (document.getElementById($forms_list[3]).value == '') {";
				print "alert('$sas_lang[forms_module_input_required_msg] $forms_list[0]');";
				print "document.getElementById($forms_list[3]).focus();";
				print "return false ;}\n ";
			}

			if (($forms_list[1] == 1) AND ($forms_list[2] == "S0"))
			{

				print "if (document.getElementById($forms_list[3]).selectedIndex== 0) {";
				print "alert('$sas_lang[forms_module_input_required_msg] $forms_list[0]');";
				print "document.getElementById($forms_list[3]).focus();";
				print "return false ;} \n";
			}
			$i++;
		}

		print "  return true ;
					}
					</script>";

		print "<center><b>$welcome_text</b></center><p>";
		$count = 0;
		$query = "SELECT name, length, required, id FROM $table_forms WHERE form_id = '$form_id' ORDER BY sort";
		$result = mysql_query($query) or die_script($query, mysql_error());

		print "<form name=\"sas_form\" method=\"POST\" action=\"index.php?section=$section&page=$page&sendemail=yes\" onsubmit=\"return checkinput(this);\">
						   <table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" style=\"border-collapse: collapse\" bordercolor=\"#00000\" width=\"550\">";
		$i = 0;
		$j = 0;
		while ($forms_list = mysql_fetch_array($result))
		{
			if ($forms_list[2] == 1)
			{
				$required = "(*)"; //field is required display (*)
				$count++;
			}
			else
			{
				$required = "";
			}

			print "<tr>
										<td>";
			if (substr($forms_list[1], 0, 1) == "S") //do we have a selection field?
			{
				if (substr($forms_list[1], 0, 2) == "S0") //is this the first one ?
				{
					$form_id = $forms_list[3]; //save the id for the selection statement
					$select_required = $required;
				}
				$select_list[$j] = $forms_list[0]; //put field in array
				$j++;
				if (substr($forms_list[1], 0, 2) == "SE") //is this the last one ?
				{
					$title = array_shift($select_list);

					print "<b>$title $select_required</b><br>";
					$ind = $i - $j;
					print "<select name=\"forms_input_list[$form_id]\" id=\"$form_id\">";
					foreach ($select_list as $key => $value)
					{
						print "<option value=\"$value\">$value</option>";
					}
					print "</select><tr><td height=\"5\"></td></tr>\n";
					$j = 0;
					unset ($select_list); //destoy array
				}
			}
			else
			{
				print "<b>$forms_list[0] $required</b>";
				print "</td></tr>\n<tr><td>";
			}

			if ($forms_list[1] == "0") //length=0 no input field just text.
			{
				print "</td>";
			}
			elseif ($forms_list[1] == "1")
			{
				print $sas_lang['yes'] . "<input type=\"radio\" value=\"" . $sas_lang['yes'] . "\" id=\"$forms_list[3]\" name=\"forms_input_list[$forms_list[3]]\">&nbsp;" .
				$sas_lang['no'] . "<input type=\"radio\" value=\"" . $sas_lang['no'] . "\" name=\"forms_input_list[$forms_list[3]]\" checked>\n";
			}
			elseif ((intval($forms_list[1]) < 80) AND (intval($forms_list[1]) > 0))
			{
				print "<INPUT size=\"$forms_list[1]\" name=\"forms_input_list[$forms_list[3]]\" id=\"$forms_list[3]\"></td></tr>\n";
			}
			elseif ((intval($forms_list[1]) > 80) AND (intval($forms_list[1]) > 0))
			{
				$rows = ceil(intval($forms_list[1]) / 80);
				print "<TEXTAREA name=\"forms_input_list[$forms_list[3]]\" rows=\"$rows\" cols=\"70\"  id=\"$forms_list[3]\"></TEXTAREA></td></tr>\n";
			}
			if (substr($forms_list[1], 0, 1) != "S")
			{
				print "<tr>
													  <td height=\"5\"></td>
											   </tr>";
			}
			$i++;
		}

		if ($count > 0) //do we have required fields?
		{
			print "<tr><td colspan=\"3\"><font size=\"1\">" . $sas_lang['required_fields'] . "</font></td></tr>";
		}
		print "<tr><td colspan=\"3\" align=\"center\"><INPUT type=submit value=$sas_lang[forms_module_send] name=B1>
						   </td></tr></table></form><br>";
	}
}
?>