<?php
// ------------------------------------------------------------------------- //
//                XOOPS - PHP Content Management System                      //
//                       <http://www.xoops.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."/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(XOOPS_ROOT_PATH."/header.php");
// set comment mode if not set
if ( !isset($mode) || $mode == "" || ($mode != "nocomments" && $mode != "thread" && $mode != "flat") ) {
	if ( $xoopsUser ) {
		$mode = $xoopsUser->getVar("umode");
	} else {
		$mode = $xoopsConfig['com_mode'];
	}
}
// set comment order if not set
if ( !isset($order) ) {
	if ( $xoopsUser ) {
		$order = $xoopsUser->getVar("uorder");
	} else {
		$order = $xoopsConfig['com_order'];
	}
}
$order = intval($order);
if ( !empty($comment_id) ){
	$pollcomment = new XoopsComments($xoopsDB->prefix("xoopspollcomments"),$comment_id);
} else {
	$pollcomment = new XoopsComments($xoopsDB->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);
	$renderer->renderResults();
    	CloseTable();
	echo "<br />";
}

$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" ) {
		$pollcomment->showThreadHead();
		$count = 0;
		foreach ( $commentsArray as $ele ) {
			if ( !($count % 2) ) {
				$color_num = 1;
			} else {
				$color_num = 2;
			}
			$ele->showThreadPost($order, $mode, $adminview, $color_num);
			$count++;
		}
		$pollcomment->showThreadFoot();
	}
	if ( $mode == "thread" ) {
		foreach ( $commentsArray as $ele ) {
			$ele->showThreadHead();
			$ele->showThreadPost($order, $mode, $adminview);
			$ele->showThreadFoot();
			//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='".$PHP_SELF."?item_id=".$ele->getVar("item_id")."&amp;mode=".$mode."&amp;order=".$order."'>"._TOP."</a>&nbsp;|&nbsp;";
				echo "<a href='".$PHP_SELF."?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();
include(XOOPS_ROOT_PATH."/footer.php");
?>