<?php
//v1.0.4 www.bcoos.net 2006/10 GPL
// www.bcoos.net 2006-09 1.0.2 GPL
//v1.6 www.exoops.info 2006/03 
// $Id: index.php,v 1.5 2003/03/19 07:02:49 half-dead Exp $
// ------------------------------------------------------------------------- //
//               E-Xoops: Content Management for the Masses                  //
//                       < http://www.e-xoops.com >                          //
// ------------------------------------------------------------------------- //
// 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");
$bcoosConfig['title'] .= " - "._MI_POLLS_NAME;
if ( !empty($_POST['poll_id']) ) {
	$poll_id = intval($_POST['poll_id']);
	} elseif (!empty($_GET['poll_id'])) {
		$poll_id = intval($_GET['poll_id']);
	}
if ( empty($poll_id) ) {
	if ($bcoosConfig['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($_GET['limit'])) ? intval($_GET['limit']) : 50;
	$start = (!empty($_GET['start'])) ? intval($_GET['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($_POST['option_id']) ) {
	$voted_polls = (!empty($_COOKIE['voted_polls'])) ? $_COOKIE['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($_POST['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($_POST['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", $bcoosConfig['title']);
				$xoopsMailer->assign("ADMINMAIL", $bcoosConfig['adminmail']);
				$xoopsMailer->assign("SITEURL", $bcoosConfig['xoops_url']."/");
				$xoopsMailer->setFromEmail($bcoosConfig['adminmail']);
				$xoopsMailer->setFromName($bcoosConfig['title']);
				$xoopsMailer->setSubject(sprintf(_PL_YOURPOLLAT, $author->uname(), $bcoosConfig['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");
	}
?>
