<?php
/*---------------------------------------------------+
| PHP-Fusion 6 Content Management System
+----------------------------------------------------+
| Copyright (c) 2005 Nick Jones
| http://www.php-fusion.co.uk/
+----------------------------------------------------+
| Released under the terms & conditions of v2 of the
| GNU General Public License. For details refer to
| the included gpl.txt file or visit http://gnu.org
+----------------------------------------------------*/
require_once "../../maincore.php";
require_once THEMES."templates/admin_header.php";

include INFUSIONS."ip_poll_panel/infusion_db.php";

if (file_exists(INFUSIONS."ip_poll_panel/locale/".$settings['locale'].".php")) {
	include INFUSIONS."ip_poll_panel/locale/".$settings['locale'].".php";
} else {
	include INFUSIONS."ip_poll_panel/locale/English.php";
}

if (!checkrights("IPP") || !defined("iAUTH") || $_GET['aid'] != iAUTH) { redirect("../../index.php"); }
if (isset($_GET['poll_id']) && !isnum($_GET['poll_id'])) redirect("../../index.php");

if (isset($_POST['save'])) {
	$poll_title = stripinput($_POST['poll_title']);
	foreach($_POST['poll_option'] as $key => $value) {
		$poll_option[$key] = stripinput($_POST['poll_option'][$key]);
	}
	if (isset($_GET['poll_id'])) {
		if (!isNum($_GET['poll_id'])) { header("Location: ".FUSION_SELF); exit; }
		$ended = (isset($_POST['close']) ? time() : 0);
		$result = dbquery("UPDATE ".DB_IP_POLLS." SET poll_title='$poll_title', poll_opt_0='".$_POST['poll_option'][0]."', poll_opt_1='".$_POST['poll_option'][1]."', poll_opt_2='".(isset($_POST['poll_option'][2]) ? $_POST['poll_option'][2] : "")."', poll_opt_3='".(isset($_POST['poll_option'][3]) ? $_POST['poll_option'][3] : "")."', poll_opt_4='".(isset($_POST['poll_option'][4]) ? $_POST['poll_option'][4] : "")."', poll_opt_5='".(isset($_POST['poll_option'][5]) ? $_POST['poll_option'][5] : "")."', poll_opt_6='".(isset($_POST['poll_option'][6]) ? $_POST['poll_option'][6] : "")."', poll_opt_7='".(isset($_POST['poll_option'][7]) ? $_POST['poll_option'][7] : "")."', poll_opt_8='".(isset($_POST['poll_option'][8]) ? $_POST['poll_option'][8] : "")."', poll_opt_9='".(isset($_POST['poll_option'][9]) ? $_POST['poll_option'][9] : "")."', poll_ended='$ended' WHERE poll_id='".$_GET['poll_id']."'");
		opentable($locale['PLAN_400']);
		echo "<center><br>\n".$locale['PLAN_401']."<br /><br />\n<a href='".FUSION_SELF.$aidlink."'>".$locale['PLAN_405']."</a><br><br>\n</center>\n";
		closetable();
	} else {
	
		$result = dbquery("UPDATE ".DB_IP_POLLS." SET poll_ended='".time()."' WHERE poll_ended='0'");
		$result = dbquery("INSERT INTO ".DB_IP_POLLS." VALUES('', '$poll_title', '".$_POST['poll_option'][0]."', '".$_POST['poll_option'][1]."', '".(isset($_POST['poll_option'][2]) ? $_POST['poll_option'][2] : "")."', '".(isset($_POST['poll_option'][3]) ? $_POST['poll_option'][3] : "")."', '".(isset($_POST['poll_option'][4]) ? $_POST['poll_option'][4] : "")."', '".(isset($_POST['poll_option'][5]) ? $_POST['poll_option'][5] : "")."', '".(isset($_POST['poll_option'][6]) ? $_POST['poll_option'][6] : "")."', '".(isset($_POST['poll_option'][7]) ? $_POST['poll_option'][7] : "")."', '".(isset($_POST['poll_option'][8]) ? $_POST['poll_option'][8] : "")."', '".(isset($_POST['poll_option'][9]) ? $_POST['poll_option'][9] : "")."', '', '', '', '', '', '', '', '', '', '', '', '".time()."', '0')");
		opentable($locale['PLAN_400']);
		echo "<center><br>\n".$locale['PLAN_402']."<br><br>\n<a href='".FUSION_SELF.$aidlink."'>".$locale['PLAN_405']."</a><br><br>\n</center>\n";
		closetable();
	}
} else if (isset($_POST['delete'])) {
	$result = dbquery("SELECT * FROM ".DB_IP_POLLS." WHERE poll_id='".$_POST['poll_id']."'");
	if (dbrows($result) != 0) $result = dbquery("DELETE FROM ".DB_IP_POLLS." WHERE poll_id='".$_POST['poll_id']."'");
	opentable($locale['PLAN_403']);
	echo "<center><br>\n".$locale['PLAN_404']."<br><br>\n<a href='".FUSION_SELF.$aidlink."'>".$locale['PLAN_405']."</a><br><br>\n</center>\n";
	closetable();
} else {
	if (isset($_POST['preview'])) {
		$poll = ""; $i = 0;
		$poll_title = stripinput($_POST['poll_title']);
		while ($i < count($_POST['poll_option'])) {
			$poll_option[$i] = stripinput($_POST['poll_option'][$i]);
			$poll .= "<input type='checkbox' name='option[$i]'> $poll_option[$i]<br><br>\n";
			$i++;
		}
		$opt_count = (isset($_POST['opt_count']) && $_POST['opt_count'] != 10 ? count($poll_option) : $_POST['opt_count']);
		opentable($locale['PLAN_410']);
		echo "<table align='center' width='280' cellpadding='0' cellspacing='0' class='tbl'>
<tr>
<td>$poll_title<br><br>
$poll</td>
</tr>
<tr>
<td align='center'><input type='button' name='blank' value='".$locale['PLAN_411']."' class='button' style='width:70px'></td>
</tr>
</table>\n";
		closetable();
		tablebreak();
	}
	$editlist = "";
	$result = dbquery("SELECT * FROM ".DB_IP_POLLS." ORDER BY poll_id DESC");
	if (dbrows($result) != 0) {
		while ($data = dbarray($result)) {
			$editlist .= "<option value='".$data['poll_id']."'>".$data['poll_title']."</option>\n";
		}
		opentable($locale['PLAN_420']);
		echo "<form name='editform' method='post' action='".FUSION_SELF.$aidlink."'>
<center>
<select name='poll_id' class='textbox' style='width:200px;'>
$editlist</select>
<input type='submit' name='edit' value='".$locale['PLAN_421']."' class='button'>
<input type='submit' name='delete' value='".$locale['PLAN_422']."' class='button'>
</center>
</form>\n";
		closetable();
		tablebreak();
	}
	if (isset($_POST['edit'])) {
		//if (!isNum($poll_id)) { header("Location: ".FUSION_SELF); exit; }
		$data = dbarray(dbquery("SELECT * FROM ".DB_IP_POLLS." WHERE poll_id='".$_POST['poll_id']."'"));
		$poll_title = $data['poll_title'];
		for ($i=0; $i<=9; $i++) {
			if ($data["poll_opt_".$i]) $poll_option[$i] = $data["poll_opt_".$i];
		}
		$opt_count = count($poll_option);
		$poll_started = $data['poll_started'];
		$poll_ended = $data['poll_ended'];
	}
	if (isset($_POST['addoption'])) {
		$poll_title = stripinput($_POST['poll_title']);
		foreach($_POST['poll_option'] as $key => $value) {
			$poll_option[$key] = stripinput($_POST['poll_option'][$key]);
		}
		$opt_count = ($_POST['opt_count'] != 10 ? count($poll_option) + 1 : $_POST['opt_count']);
	}
	$i = 0; $opt = 1;
	$poll_title = isset($poll_title) ? $poll_title : "";
	$opt_count = isset($opt_count) ? $opt_count : 2;
	if (isset($poll_id)) $poll_ended = isset($_POST['poll_ended']) ? $_POST['poll_ended'] : 0;
	opentable((isset($_POST['poll_id']) ? $locale['PLAN_431'] : $locale['PLAN_430']));
	echo "<form name='pollform' method='post' action='".FUSION_SELF.$aidlink.(isset($_POST['poll_id']) ? "&poll_id=".$_POST['poll_id']."&poll_ended=$poll_ended" : "")."'>
<table align='center' width='280' cellpadding='0' cellspacing='0' class='tbl'>
<tr>
<td width='80'>".$locale['PLAN_433']."</td><td><input type='text' name='poll_title' value='$poll_title' class='textbox' style='width:200px'></td>
</tr>
<tr>\n";
	while ($i != $opt_count) {
		$poll_opt = isset($poll_option[$i]) ? $poll_option[$i] : "";
		echo "<tr>\n<td width='80'>".$locale['PLAN_434']."$opt</td>\n";
		echo "<td><input type='text' name='poll_option[$i]' value='$poll_opt' class='textbox' style='width:200px'></td>\n</tr>\n";
		$i++; $opt++;
	}
	echo "</table>
<table align='center' width='280' cellpadding='0' cellspacing='0'>
<tr>
<td align='center'><br>\n";
	if (isset($_POST['poll_id']) && $poll_ended == 0) {
		echo "<input type='checkbox' name='close' value='yes'>".$locale['PLAN_435']."<br><br>\n";
	}
	if (!isset($_POST['poll_id']) || isset($_POST['poll_id']) && $poll_ended == 0) {
		echo "<input type='hidden' name='opt_count' value='$opt_count'>
<input type='submit' name='addoption' value='".$locale['PLAN_438']."' class='button'>
<input type='submit' name='preview' value='".$locale['PLAN_439']."' class='button'>
<input type='submit' name='save' value='".$locale['PLAN_440']."' class='button'>\n";
	} else {
		echo $locale['PLAN_436'].showdate("shortdate", $poll_started)."<br>\n";
		echo $locale['PLAN_437'].showdate("shortdate", $poll_ended)."<br>\n";
	}
	echo "</td>\n</tr>\n</table>\n</form>\n";
	closetable();
}

require_once THEMES."templates/footer.php";
?>