<?php
// $Id: index.php,v 1.1 2004/07/11 10:29:11 Farsus Exp $
// -------------------------------------------------------------------------//
//			       RUNCMS                                                   //
//                                                                          //
//	 reliable - Unique - Nocost &  Simplicity & ease off use                //
//                       < http://www.runcms.org >                          //
// -------------------------------------------------------------------------//
// Original Author: Kazumi Ono
// Author Website : http://www.mywebaddons.com/ , http://www.myweb.ne.jp
// License Type   : GPL: See /manual/LICENSES/GPL.txt
// ------------------------------------------------------------------------- //

include_once("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_once(XOOPS_ROOT_PATH."/header.php");
		make_cblock();
		echo "<br />";
		} else {
			$xoopsOption['show_rblock'] =0;
			include_once(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>
	<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."'>"._PREV."</a>";
	}

	echo "</td><td align='right'>";

	if ( $polls_count > $limit ) {
		echo "<a href='index.php?start=".($start+$limit)."&amp;limit=".$limit."'>"._NEXT."</a>";
	}

	echo "</td></tr></table>";
	CloseTable();
	include_once(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'] : array();
	$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")) ) {
					cookie("voted_polls[$poll_id]", 1, 31536000);
					$msg = _PL_ALREADYVOTED;
					} else {
						$poll->vote($HTTP_POST_VARS['option_id'], _REMOTE_ADDR, $xoopsUser->getVar("uid"));
						$poll->updateCount();
						cookie("voted_polls[$poll_id]", 1, 31536000);
						$msg = _PL_THANKSFORVOTE;
					}
				} else {
					if ( XoopsPollLog::hasVoted($poll_id, _REMOTE_ADDR) ) {
						cookie("voted_polls[$poll_id]", 1, 31536000);
						$msg = _PL_ALREADYVOTED;
						} else {
							$poll->vote($HTTP_POST_VARS['option_id'], _REMOTE_ADDR);
							$poll->updateCount();
							cookie("voted_polls[$poll_id]", 1, 31536000);
							$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/");
				$xoopsMailer->setTemplate("mail_results.tpl");
				$author = new XoopsUser($poll->getVar("user_id"));
				$xoopsMailer->setToUsers($poll->getVar("uid"));
				$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", $meta['title']);
				$xoopsMailer->assign("ADMINMAIL", $xoopsConfig['adminmail']);
				$xoopsMailer->assign("SITEURL", $xoopsConfig['xoops_url']."/");
				$xoopsMailer->setFromEmail($xoopsConfig['adminmail']);
				$xoopsMailer->setFromName($meta['title']);
				$xoopsMailer->setSubject(sprintf(_PL_YOURPOLLAT, $author->uname(), $meta['title']));
				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_once(XOOPS_ROOT_PATH."/header.php");
		OpenTable();
		echo "<div align='center'>";
		$poll     = new XoopsPoll($poll_id);
		$renderer = new XoopsPollRenderer($poll);
		echo $renderer->renderForm('160');
		echo "</div>";
		CloseTable();
		include_once(XOOPS_ROOT_PATH."/footer.php");
	}
?>
