<?php
// $Id: pollresults.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
// ------------------------------------------------------------------------- //
$results_page = 1;

include_once("header.php");
include_once(XOOPS_ROOT_PATH."/class/xoopscomments.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");

$item_id = (!empty($item_id)) ? intval($item_id) : 0;
$poll_id = (!empty($poll_id)) ? intval($poll_id) : 0;

if ( empty($item_id) && empty($poll_id) ) {
	redirect_header("index.php", 0);
	exit();
}

include_once(XOOPS_ROOT_PATH."/header.php");

// set comment mode if not set
if ( !isset($mode) || $mode == "" || ($mode != 0 && $mode != "thread" && $mode != "flat") ) {
	if ( $xoopsUser ) {
		$mode = $xoopsUser->getVar("umode");
		} else {
			$mode = $xoopsConfig['com_mode'];
		}
}


if ( !empty($comment_id) ) {
	$pollcomment = new XoopsComments($db->prefix("xoopspollcomments"), $comment_id);
	} else {
		$pollcomment = new XoopsComments($db->prefix("xoopspollcomments"));
	}

$item_id = (!empty($poll_id)) ? $poll_id : $item_id;
$pollcomment->setVar("item_id", $item_id);
$parentid = $pollcomment->getVar("pid");

if ( empty($parentid) ) {
	OpenTable();
	$poll     = new XoopsPoll($item_id);
	$renderer = new XoopsPollRenderer($poll);
	echo $renderer->renderResults();
	CloseTable();
	echo "<br />";
}

//---------------------------------------------------------------------------------------//
// Comments start here
//---------------------------------------------------------------------------------------//
// set comment order if not set
if ( !isset($order) ) {
	if ( $xoopsUser ) {
		$order = $xoopsUser->getVar("uorder");
		} else {
			$order = $xoopsConfig['com_order'];
		}
}
$order = intval($order);

$orderby = ($order == 1) ? "date DESC" : "date ASC";
if ( $mode == "flat" ) {
	$criteria = array("item_id=".$item_id."");
	$commentsArray = $pollcomment->getAllComments($criteria, true, $orderby);
	} elseif ( $mode == "thread" ) {
		$criteria = array("item_id=".$item_id."", "pid=".$pollcomment->getVar("pid")."");
		$commentsArray = $pollcomment->getAllComments($criteria, true, $orderby);
		} else {
			$commentsArray = "";
		}

OpenTable();
$pollcomment->printNavBar($item_id, $mode, $order);

// Now, show comments
if ( is_array($commentsArray) && count($commentsArray) ) {
	if ( $xoopsUser && $xoopsUser->isAdmin($xoopsModule->mid()) ) {
		$adminview = 1;
		} else {
			$adminview = 0;
		}
	if ( $mode == "flat" ) {
		$count = 0;
		foreach ( $commentsArray as $ele ) {
			if ( !($count % 2) ) {
				$color_num = 1;
				} else {
					$color_num = 2;
				}
			$ele->showThreadPost($order, $mode, $adminview, $color_num);
			$count++;
		}
	}
	if ( $mode == "thread" ) {
		foreach ( $commentsArray as $ele ) {
			$ele->showThreadPost($order, $mode, $adminview);
			//show thread tree
			//if not in the top page, show links to parent and top comment
			if ( $ele->getVar("pid") != 0 ) {
				echo "<div style='text-align:left'>";
				echo "&nbsp;<a href='./pollresults.php?item_id=".$ele->getVar("item_id")."&amp;mode=".$mode."&amp;order=".$order."'>"._TOP."</a>&nbsp;|&nbsp;";
				echo "<a href='./pollresults.php?item_id=".$ele->getVar("item_id")."&amp;comment_id=".$ele->getVar("pid")."&amp;mode=".$mode."&amp;order=".$order."#".$ele->getVar("pid")."'>"._PARENT."</a>";
				echo "</div>";
			}
			echo "<br />";
			$treeArray = $ele->getCommentTree();
			if ( count($treeArray) >0 ) {
				$ele->showTreeHead();
				$count = 0;
				foreach ( $treeArray as $treeItem ) {
					if ( !($count % 2) ) {
						$color_num = 1;
						} else {
							$color_num = 2;
						}
					$treeItem->showTreeItem($order, $mode, $color_num);
					$count++;
				}
				$ele->showTreeFoot();
			}
		}
		echo "<br />";
	}
}

CloseTable();
//---------------------------------------------------------------------------------------//
// Comments stop here
//---------------------------------------------------------------------------------------//

include_once(XOOPS_ROOT_PATH."/footer.php");
?>
