<?php
// ------------------------------------------------------------------------- //
//                XOOPS - PHP Content Management System                      //
//                       <http://www.xoops.org/>                             //
// ------------------------------------------------------------------------- //
// Based on:								     //
// myPHPNUKE Web Portal System - http://myphpnuke.com/	  		     //
// PHP-NUKE Web Portal System - http://phpnuke.org/	  		     //
// Thatware - http://thatware.org/					     //
// ------------------------------------------------------------------------- //
//  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 //
// ------------------------------------------------------------------------- //
include("header.php");
include_once(XOOPS_ROOT_PATH."/modules/xoopspoll/class/xoopspoll.php");
include_once(XOOPS_ROOT_PATH."/modules/xoopspoll/class/xoopspolloption.php");
include_once(XOOPS_ROOT_PATH."/modules/xoopspoll/class/xoopspolllog.php");
include_once(XOOPS_ROOT_PATH."/modules/xoopspoll/class/xoopspollrenderer.php");

if ( !empty($HTTP_POST_VARS['poll_id']) ) {
	$poll_id = intval($HTTP_POST_VARS['poll_id']);
} elseif (!empty($HTTP_GET_VARS['poll_id'])) {
	$poll_id = intval($HTTP_GET_VARS['poll_id']);
}

if ( empty($poll_id) ) {
	if($xoopsConfig['startpage'] == "xoopspoll"){
		$xoopsOption['show_rblock'] =1;
		include(XOOPS_ROOT_PATH."/header.php");
		make_cblock();
		echo "<br />";
	}else{
		$xoopsOption['show_rblock'] =0;
		include(XOOPS_ROOT_PATH."/header.php");
	}
	$limit = (!empty($HTTP_GET_VARS['limit'])) ? intval($HTTP_GET_VARS['limit']) : 50;
	$start = (!empty($HTTP_GET_VARS['start'])) ? intval($HTTP_GET_VARS['start']) : 0;
	OpenTable();
	echo "<h4>"._PL_POLLSLIST."</h4>";
	// add 1 to $limit to know whether there are more polls
	$polls_arr = XoopsPoll::getAll(array(), true, "weight ASC, end_time DESC", $limit+1, $start);
	$polls_count = count($polls_arr);
	$max = ( $polls_count > $limit ) ? $limit : $polls_count;
	echo "<table border='0' cellpadding='0' cellspacing='0' valign='top' width='100%'><tr><td class='bg2'>
	<table width='100%' border='0' cellpadding='4' cellspacing='1'>
	<tr class='bg3'><td><b>"._PL_POLLQUESTION."</b></td><td align='center'><b>"._PL_VOTERS."</b></td><td align='center'><b>"._PL_VOTES."</b></td><td align='center'><b>"._PL_EXPIRATION."</b></td><td>&nbsp;</td></tr>";
	for ( $i = 0; $i < $max; $i++ ) {
		if ( $polls_arr[$i]->getVar("end_time") > time() ) {
			$end = formatTimestamp($polls_arr[$i]->getVar("end_time"),"m");
		} else {
			$end = "<span style='color:#ff0000;'>"._PL_EXPIRED."</span>";
		}
		echo "<tr class='bg1'><td><a href='index.php?poll_id=".$polls_arr[$i]->getVar("poll_id")."'>".$polls_arr[$i]->getVar("question")."</a></td><td align='center'>".$polls_arr[$i]->getVar("voters")."</td><td align='center'>".$polls_arr[$i]->getVar("votes")."</td><td align='center'>".$end."</td><td align='right'><a href='pollresults.php?poll_id=".$polls_arr[$i]->getVar("poll_id")."'>"._PL_RESULTS."</a></td></tr>";
	}
	echo "</table></td></tr></table>";
	echo "<table width='100%'><tr><td align='left'>";
	if ( $start > 0 ) {
		$prev_start = ($start - $limit > 0) ? $start - $limit : 0;
		echo "<a href='index.php?start=".$prev_start."&amp;limit=".$limit."'>"._PL_PREV."</a>";
	}
	echo "</td><td align='right'>";
	if ( $polls_count > $limit ) {
		echo "<a href='index.php?start=".($start+$limit)."&amp;limit=".$limit."'>"._PL_NEXT."</a>";
	}
	echo "</td></tr></table>";
	CloseTable();
	include (XOOPS_ROOT_PATH."/footer.php");
} elseif ( !empty($HTTP_POST_VARS['option_id']) ) {
	$voted_polls = (!empty($HTTP_COOKIE_VARS['voted_polls'])) ? $HTTP_COOKIE_VARS['voted_polls'] : "";
	$mail_author = false;
	$poll = new XoopsPoll($poll_id);
	if ( !$poll->hasExpired() ) {
		if ( empty($voted_polls[$poll_id]) ) {
			if ( $xoopsUser ) {
				if ( XoopsPollLog::hasVoted($poll_id, $REMOTE_ADDR, $xoopsUser->getVar("uid")) ) {
					setcookie("voted_polls[$poll_id]", 1, 0);
					$msg = _PL_ALREADYVOTED;
				} else {
					$poll->vote(intval($HTTP_POST_VARS['option_id']), $REMOTE_ADDR, $xoopsUser->getVar("uid"));
					$poll->updateCount();
					setcookie("voted_polls[$poll_id]", 1, 0);
					$msg = _PL_THANKSFORVOTE;
				}
			} else {
				if ( XoopsPollLog::hasVoted($poll_id, $REMOTE_ADDR) ) {
					setcookie("voted_polls[$poll_id]", 1, 0);
					$msg = _PL_ALREADYVOTED;
				} else {
					$poll->vote(intval($HTTP_POST_VARS['option_id']), $REMOTE_ADDR);
					$poll->updateCount();
					setcookie("voted_polls[$poll_id]", 1, 0);
					$msg = _PL_THANKSFORVOTE;
				}
			}
		} else {
			$msg = _PL_ALREADYVOTED;
		}
	} else {
		$msg = _PL_SORRYEXPIRED;
		if ( $poll->getVar("mail_status") != POLL_MAILED ) {
			$xoopsMailer =& getMailer();
			$xoopsMailer->useMail();
			$xoopsMailer->setTemplateDir(XOOPS_ROOT_PATH."/modules/xoopspoll/language/".$xoopsConfig['language']."/mail_template/");
			$xoopsMailer->setTemplate("mail_results.tpl");
			$author = new XoopsUser($poll->getVar("user_id"));
			$xoopsMailer->setToUsers($author);
			$xoopsMailer->assign("POLL_QUESTION", $poll->getVar("question"));
			$xoopsMailer->assign("POLL_START", formatTimestamp($poll->getVar("start_time"), "l", $author->timezone()));
			$xoopsMailer->assign("POLL_END", formatTimestamp($poll->getVar("end_time"), "l", $author->timezone()));
			$xoopsMailer->assign("POLL_VOTES", $poll->getVar("votes"));
			$xoopsMailer->assign("POLL_VOTERS", $poll->getVar("voters"));
			$xoopsMailer->assign("POLL_ID", $poll->getVar("poll_id"));
			$xoopsMailer->assign("SITENAME", $xoopsConfig['sitename']);
			$xoopsMailer->assign("ADMINMAIL", $xoopsConfig['adminmail']);
			$xoopsMailer->assign("SITEURL", $xoopsConfig['xoops_url']."/");

			$xoopsMailer->setFromEmail($xoopsConfig['adminmail']);
			$xoopsMailer->setFromName($xoopsConfig['sitename']);
			$xoopsMailer->setSubject(sprintf(_PL_YOURPOLLAT,$author->uname(),$xoopsConfig['sitename']));
			if ( $xoopsMailer->send() != false ) {
				$poll->setVar("mail_status", POLL_MAILED);
				$poll->store();
			}
		}
	}
	redirect_header(XOOPS_URL."/modules/xoopspoll/pollresults.php?poll_id=$poll_id", 1, $msg);
	exit();
} elseif ( !empty($poll_id) ) {
	include(XOOPS_ROOT_PATH."/header.php");
	OpenTable();
	$poll = new XoopsPoll($poll_id);
	$renderer = new XoopsPollRenderer($poll);
	echo $renderer->renderForm();
	CloseTable();
	include (XOOPS_ROOT_PATH."/footer.php");
}
?>