<?php

/********************************************************************************
	- MemHT Portal -
	
	Copyright (C) 2007-2008 by Miltenovik Manojlo
	http://www.memht.com
	
	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 opinion) 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, see <http://www.gnu.org/licenses/> (GPLv2)
	or write to the Free Software Foundation, Inc., 51 Franklin Street,
	Fifth Floor, Boston, MA02110-1301, USA.
		
********************************************************************************/

if (!defined("_LOAD_PAGE_")) {
	die("<table style='padding: 2px; border: 1px solid #999; background-color: #EEE; font-family: Verdana; font-size: 10px;' align='center'><tr><td><b>Error:</b> This file cannot be opened directly!</td></tr></table>");
}

$op = (isset($_REQUEST['op'])) ? inCode($_REQUEST['op']) : "" ;
$id = (isset($_REQUEST['id'])) ? intval($_REQUEST['id']) : 0 ;
$ok = (isset($_GET['ok'])) ? inCode($_GET['ok']) : false ;
$fields = (isset($_POST['fields'])) ? intval($_POST['fields']) : 2 ;
$question = (isset($_POST['question'])) ? inCode($_POST['question']) : "" ;
$comments = (isset($_POST['comments'])) ? intval($_POST['comments']) : 0 ;
$enabled = (isset($_POST['enabled'])) ? intval($_POST['enabled']) : 0 ;

$pg = (isset($_GET['pg'])) ? intval($_GET['pg']) : 1 ;
$ofsppg = 20; //Items per page
$ofsbgn = ($pg*$ofsppg)-$ofsppg;

function sondList($ofsbgn,$ofsppg,$pg) {
	global $dblink;

	$n = 0;
	echo "<table width='100%' align='center' cellspacing='1' cellpadding='0' class='std_nicetable'>";
	echo "<thead>\n";
	echo "<tr><td>"._QUESTION_."</td><td width='1'>&nbsp;</td></tr>\n";
	echo "</thead>\n";
	echo "<tbody>\n";
	if ($result = $dblink->get_list("SELECT * FROM memht_survey ORDER BY id DESC LIMIT $ofsbgn,$ofsppg")) {
		foreach ($result as $row) {
			$id = intval($row['id']);
			$question = outCode($row['question']);
			$enabled = intval($row['enabled']);
			
			$off = ($enabled!="1") ? "<img src='images/off.gif' alt='"._OFF_."' title='"._OFF_."'>" : "" ;
			$class = (($n++%2)!=0) ? "hlight" : "clean" ;
			
			echo "<tr><td class='$class'><a href='admin.php?page=surveys&op=showSurvey&id=$id'><b>$question</b></a></td><td class='$class' nowrap><a href='admin.php?page=surveys&op=editSurvey&id=$id' title='"._MODIFY_."'><img src='images/edit.gif' alt='Edit' border='0'></a> <a href='admin.php?page=surveys&op=deleteSurvey&id=$id' title='"._DELETE_."'><img src='images/delete.gif' alt='Delete' border='0'></a> $off</td></tr>\n";
		}
	} else {
		echo "<tr><td colspan='2' align='center' id='errorText' class='clean'><b>"._NOSURVEY_."</b></td></tr>";
	}
	echo "</tbody>\n";
	echo "</table>";
	
	//Pages
	include_once("inc/class/paginationSystem.class.php");
	$ps = new paginationSystem();
	$ps->items = $ofsppg;
	$ps->actpg = $pg;
	$ps->query = "SELECT id FROM memht_survey";
	$ps->url = "admin.php?page=surveys&op=showSurveys&pg={{N}}";
	$ps->show();
}

function showSond($id) {
	global $dblink,$siteConfig;
	
	$row = $dblink->get_row("SELECT * FROM memht_survey WHERE id='$id'");
	$question = outCode($row['question']);
	$comments = intval($row['usecomments']);
			
	$row = $dblink->get_row("SELECT SUM(votes) AS tot FROM memht_survey_data WHERE surveyid='$id'");
	$tot = intval($row['tot']);
			
	echo "<table width='100%' border='0' class='small'>\n";
	echo "<tr><td colspan='2' class='box' align='center'><b>$question</b><br>("._TOTVOTES_.": $tot)</td></tr>\n";
	echo "<tr><td align='right'>\n";
					
	$result = $dblink->get_list("SELECT * FROM memht_survey_data WHERE surveyid='$id' ORDER BY id");
	foreach ($result as $row) {
		$aid = intval($row['id']);
		$answer = outCode($row['answer']);
		$votes = intval($row['votes']);
		
		@$perc = ($votes/$tot)*100;
		$width = $perc*3;
		
		echo "<tr><td><b>$answer</b>";
		echo "<div id='info'><img src='templates/".$siteConfig['template']."/images/bar_sx.gif' border='0' align='texttop' title='$votes (".round($perc,1)."%)'><img src='templates/".$siteConfig['template']."/images/bar_centrale.gif' border='0' align='texttop' title='$votes (".round($perc,1)."%)' width='$width' height='12'><img src='templates/".$siteConfig['template']."/images/bar_dx.gif' border='0' align='texttop' title='$votes (".round($perc,1)."%)'> ".round($perc,1)."%<br><br></div>\n";
		echo "</td></tr>\n";
	}
	echo "<tr><td colspan='2' align='center' class='box'><a href='admin.php?page=surveys&op=editSurvey&id=$id' title='"._MODIFY_."'>"._MODIFY_."</a> | <a href='admin.php?page=surveys&op=deleteSurvey&id=$id' title='"._DELETE_."'>"._DELETE_."</a></td></tr>\n";
	echo "</table>\n";
}

function addSond($question,$fields,$comments,$enabled,$ok=false) {
	global $dblink,$tzNOW;
	
	if ($fields<2) { $fields = 2; }
	
	if (!$ok) {
		echo "<table width='100%' align='center' cellspacing='0' cellpadding='1'>\n";
		echo "<form name='form_fields' method='post' action='admin.php?page=surveys&op=addSurvey'>";
		echo "<tr><td></td><td><input type='text' name='fields' value='$fields' size='3' maxlength='2'> <input type='submit' name='Submit' value='"._FIELDS_."'></td></tr>\n";
		echo "</tr>\n";
		echo "</form>\n";
		
		echo "<form name='form_sond' method='post' action='admin.php?page=surveys&op=addSurvey&ok=true'>";
		echo "<tr><td width='20%'><b>"._QUESTION_."</b></td><td><input type='text' name='question' size='40' maxlength='255'></td></tr>\n";
		for ($i=1;$i<=$fields;$i++) {
			echo "<tr><td><b>"._ANSWER_."</b> $i</td><td><input type='text' name='answer_$i' size='40' maxlength='255'></td></tr>\n";
		}
		
		echo "<tr><td><b>"._COMMENTSENABLED_."</b></dh><td>";
			echo "<select name='comments'>\n";
				echo "<option value='1' selected>"._YES_."</option>\n";
				echo "<option value='0'>"._NO_."</option>\n";
			echo "</select></td></tr>\n";
		
		echo "<tr><td><b>"._ENABLED_."</b></dh><td>";
			echo "<select name='enabled'>\n";
				echo "<option value='1' selected>"._YES_."</option>\n";
				echo "<option value='0'>"._NO_."</option>\n";
			echo "</select></td></tr>\n";
		
		echo "<input type='hidden' name='fields' value='$fields'>\n";
		echo "<tr><td colspan='2'><input type='submit' name='Submit' value='"._ADD_."'></td></tr>\n";
			
		echo "</form>\n";
		echo "</table>\n";
	} else {	
		$save = true;
		if ($question=="") { $save = false; }
		
		for ($i=1;$i<=$fields;$i++) {
			if ($_POST['answer_'.$i]=="") { $save = false; }
		}
		
		if ($save) {
			$dblink->query("INSERT INTO memht_survey (id,question,date,usecomments,enabled) VALUES (null,'$question',$tzNOW,'$comments','$enabled')");
			
			$row = $dblink->get_row("SELECT id FROM memht_survey ORDER BY id DESC LIMIT 1");
			$lastid = intval($row['id']);
			
			for ($i=1;$i<=$fields;$i++) {
				$dblink->query("INSERT INTO memht_survey_data (id,surveyid,answer) VALUES (null,'$lastid','".inCode($_POST['answer_'.$i])."')");
			}
			
			$dblink->query("DELETE FROM memht_survey_log WHERE surveyid<'$lastid'");
			echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=surveys&op=showSurvey&id=$lastid'>";
		} else {
			echo "<div align='center' id='errorText'><b>"._FIELDINVALID_."</b></div>";
		}
	}
}

function editSond($id,$question,$fields,$comments,$enabled,$ok=false) {
	global $dblink,$tzNOW;
	
	$id = intval($id);
	
	$row = $dblink->get_row("SELECT * FROM memht_survey WHERE id='$id'");
	$dbfields = $dblink->get_num("SELECT id FROM memht_survey_data WHERE surveyid='$id'");
	
	if (!isset($_GET['check']) AND !isset($_GET['ok'])) { $fields = $dbfields; }
	if ($fields<2) { $fields = 2; }
	
	if (!$ok) {
		$question = outCode($row['question']);
		$comments = intval($row['usecomments']);
		$enabled = intval($row['enabled']);
	
		echo "<table width='100%' align='center' cellspacing='0' cellpadding='1'>\n";
		echo "<form name='form_fields' method='post' action='admin.php?page=surveys&op=editSurvey&check=1&id=$id'>";
		echo "<tr><td></td><td><input type='text' name='fields' value='$fields' size='3' maxlength='2'> <input type='submit' name='Submit' value='"._FIELDS_."'></td></tr>\n";
		echo "</tr>\n";
		echo "</form>\n";
		
		echo "<form name='form_sond' method='post' action='admin.php?page=surveys&op=editSurvey&id=$id&ok=true'>";
		echo "<tr><td width='20%'><b>"._QUESTION_."</b></td><td><input type='text' name='question' value=\"$question\" size='40' maxlength='255'></td></tr>\n";
		
		$i = 0;
		$result = $dblink->get_list("SELECT * FROM memht_survey_data WHERE surveyid='$id' ORDER BY id LIMIT $fields");
		foreach ($result as $row) {
			$aid = intval($row['id']);
			$answer = outCode($row['answer']);
			
			$i++;
			echo "<tr><td><b>"._ANSWER_."</b> $i</td><td><input type='text' name='answer_$i' value=\"$answer\" size='40' maxlength='255'></td></tr>\n";
			echo "<input type='hidden' name='field_$i' value='$aid'>\n";
		}
		while ($i<$fields) {
			$i++;
			echo "<tr><td><b>"._ANSWER_."</b> $i</td><td><input type='text' name='answer_$i' size='40' maxlength='255'></td></tr>\n";
		}
		if ($dbfields>$i) {
			$num = $dbfields-$i;
			$result = $dblink->get_list("SELECT * FROM memht_survey_data WHERE surveyid='$id' ORDER BY id LIMIT $i,$num");
			foreach ($result as $row) {
				$aid = intval($row['id']);
				$i++;
				echo "<input type='hidden' name='delete_$i' value='$aid'>\n";
			}
		}
		
		echo "<tr><td><b>"._COMMENTSENABLED_."</b></dh><td>";
			echo "<select name='comments'>\n";
				if ($comments==1) {
					echo "<option value='1' selected>"._YES_."</option>\n";
					echo "<option value='0'>"._NO_."</option>\n";
				} else {
					echo "<option value='1'>"._YES_."</option>\n";
					echo "<option value='0' selected>"._NO_."</option>\n";
				}
			echo "</select></td></tr>\n";
		
		echo "<tr><td><b>"._ENABLED_."</b></dh><td>";
			echo "<select name='enabled'>\n";
				if ($enabled==1) {
					echo "<option value='1' selected>"._YES_."</option>\n";
					echo "<option value='0'>"._NO_."</option>\n";
				} else {
					echo "<option value='1'>"._YES_."</option>\n";
					echo "<option value='0' selected>"._NO_."</option>\n";
				}
			echo "</select></td></tr>\n";
		
		echo "<input type='hidden' name='fields' value='$fields'>\n";
		echo "<tr><td colspan='2'><input type='submit' name='Submit' value='"._MODIFY_."'></td></tr>\n";
			
		echo "</form>\n";
		echo "</table>\n";
	} else {	
		$save = true;
		if ($question=="") { $save = false; }
		
		$i = 0;
		while ($i<$fields AND $i<$dbfields) {
			$i++;
			if ($_POST['answer_'.$i]=="") { $save = false; }
		}
		while ($i<$fields) {
			$i++;
			if ($_POST['answer_'.$i]=="") { $save = false; }
		}
		
		if ($save) {
			$dblink->query("UPDATE memht_survey SET question='$question',usecomments='$comments',enabled='$enabled' WHERE id='$id'");
			
			$i = 0;
			while ($i<$fields AND $i<$dbfields) {
				$i++;
				$dblink->query("UPDATE memht_survey_data SET answer='".inCode($_POST['answer_'.$i])."' WHERE id='".intval($_POST['field_'.$i])."'");
			}
			while ($i<$fields) {
				$i++;
				$dblink->query("INSERT INTO memht_survey_data (id,surveyid,answer) VALUES (null,'$id','".inCode($_POST['answer_'.$i])."')");
			}
			if ($dbfields>$i) {
				while ($i<$dbfields) {
					$i++;
					$dblink->query("DELETE FROM memht_survey_data WHERE id='".intval($_POST['delete_'.$i])."'");
				}
			}
			echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=surveys&op=showSurvey&id=$id'>";
		} else {
			echo "<div align='center' id='errorText'><b>"._FIELDINVALID_."</b></div>";
		}
	}
}

function deleteSond($id,$ok=false) {
	global $dblink;
	
	if ($ok) {
		$dblink->query("DELETE FROM memht_survey WHERE id='$id'");
		$dblink->query("DELETE FROM memht_survey_data WHERE surveyid='$id'");
		$dblink->query("DELETE FROM memht_survey_comments WHERE surveyid='$id'");
		$dblink->query("DELETE FROM memht_survey_log WHERE surveyid='$id'");
		
		echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=surveys'>";		
	} else {
		echo "<div align='center'><b>"._SUREDELETESURVEY_."</b><br><a href='admin.php?page=surveys&op=deleteSurvey&id=$id&ok=true' title='"._YES_."'>"._YES_."</a> - <a href='admin.php?page=surveys' title='"._NO_."'>"._NO_."</a></div>";
	}
}

require_once("admin/inc/inc_header.php");
	admin_page_title($page);
															
	openTable();
		echo "<div align='center' class='box'>";
			echo "<a href='admin.php?page=surveys' title='"._LIST_."'><img src='admin/icons/surveys.png' border='0' title='"._LIST_."' align='top'> "._LIST_."</a>";
			echo " - <a href='admin.php?page=surveys&op=addSurvey' title='"._ADD_."'><img src='admin/icons/add.png' border='0' title='"._ADD_."' align='top'> "._ADD_."</a>";
		echo "</div>";
	closeTable();
	
	openTable();
	
		switch($op) {
			case "addSurvey":
				addSond($question,$fields,$comments,$enabled,$ok);
			break;
			
			case "showSurvey":
				showSond($id);
			break;
			
			case "editSurvey":
				editSond($id,$question,$fields,$comments,$enabled,$ok);
			break;
			
			case "deleteSurvey":
				deleteSond($id,$ok);
			break;
		
			case "showSurveys":
			default:
				sondList($ofsbgn,$ofsppg,$pg);
			break;
		}
	
	closeTable();
require_once("admin/inc/inc_footer.php");

?>