<?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("class/class.newsstory.php");

$item_id = (!empty($item_id)) ? intval($item_id) : 0;
$storyid = (!empty($storyid)) ? intval($storyid) : 0;
if ( empty($storyid) && empty($item_id) ) {
	redirect_header("index.php",2,_NW_NOSTORY);
	exit();
}
include(XOOPS_ROOT_PATH."/modules/".$xoopsModule->dirname()."/cache/config.php");
$myts =& MyTextSanitizer::getInstance();
// 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) ) {
	$artcomment = new XoopsComments($xoopsDB->prefix("comments"),$comment_id);
} else {
	$artcomment = new XoopsComments($xoopsDB->prefix("comments"));
}
$item_id = (!empty($storyid)) ? $storyid : $item_id;
$artcomment->setVar("item_id", $item_id);
if( $artcomment->getVar("pid") == 0 ){
	$article = new NewsStory($item_id);
	if ( $article->published() == 0 || $article->published() > time() ) {
		redirect_header("index.php",2,_NW_NOSTORY);
		exit();
	}
	include_once(XOOPS_ROOT_PATH."/header.php");
	if ( file_exists(XOOPS_ROOT_PATH."/themes/".$xoopsTheme['thename']."/themenews.php") ) {
		include(XOOPS_ROOT_PATH."/themes/".$xoopsTheme['thename']."/themenews.php");
	}
	$article->updateCounter();
	$datetime = formatTimestamp($article->created());
	$title = $article->textlink()."&nbsp;:&nbsp;".$article->title();
	$hometext = $article->hometext();
	$bodytext = $article->bodytext();
	if($bodytext == "") { 
    		$bodytext = $hometext;
	} else { 
    		$bodytext = "$hometext<br /><br />$bodytext";
	}
	if ( $article->uname() ) {
		$poster = "<a href='".XOOPS_URL."/userinfo.php?uid=".$article->uid()."'>".$article->uname()."</a>";
	} else {
		$poster = $xoopsConfig['anonymous'];
	}
	echo "<table width='100%' border=0><tr><td valign=top>";
	$adminlink = "&nbsp;";
	if ( $xoopsUser ) {
		if ( $xoopsUser->isAdmin($xoopsModule->mid()) ) {					$adminlink = $article->adminlink();
		}
	}
	$imglink = "";
	if ( $article->topicdisplay() ) {
		$imglink = $article->imglink();
	}
	themenews($poster, $datetime, $title, $article->counter(), $bodytext, $imglink, $adminlink);
	echo "</td><td>&nbsp;</td><td valign='top' width='200'>";
	$boxtitle = ""._NW_RELATEDLINKS."";
	$boxstuff = "";
	$boxstuff .= "<strong><big>&middot;</big></strong>&nbsp;<a href='index.php?storytopic=".$article->topicid()."'>";
	$boxstuff .= sprintf(_NW_MOREABOUT,$article->topic_title());
	$boxstuff .= "</a><br />";
	// if the poster is not anonymous, show link to other articles by this poster
	if ( $article->uname() ) {
		$boxstuff .= "<strong><big>&middot;</big></strong>&nbsp;<a href='".XOOPS_URL."/userinfo.php?uid=".$article->uid()."'>";
		$boxstuff .= sprintf(_NW_NEWSBY,$article->uname());
		$boxstuff .= "</a>";
	}
	$boxstuff .= "<br /><br /><hr noshade='noshade' width='95%' size='1' /><div><b>";
	$boxstuff .= sprintf(_NW_MOSTREAD,$article->topic_title());
	$boxstuff .= "</b><br />";
	$result2=$xoopsDB->query("SELECT storyid, title FROM ".$xoopsDB->prefix("stories")." WHERE topicid=".$article->topicid()." AND published <= ".time()." ORDER BY counter DESC",1,0);
	list($topstory, $ttitle) = $xoopsDB->fetchRow($result2);
	$ttitle = $myts->makeTboxData4Show($ttitle);
	$boxstuff .= "<a href='".$PHP_SELF."?item_id=$topstory'>$ttitle</a><br /><br />";
	$boxstuff .= "<b>";
	$boxstuff .= sprintf(_NW_LASTNEWS,$article->topic_title());
	$boxstuff .= "</b><br />";
	$result2=$xoopsDB->query("SELECT storyid, title FROM ".$xoopsDB->prefix("stories")." WHERE topicid=".$article->topicid()." AND published <= ".time()." ORDER BY published DESC",1,0);
	list($topstory, $ttitle) = $xoopsDB->fetchRow($result2);
	$ttitle = $myts->makeTboxData4Show($ttitle);
	$boxstuff .= "<a href='".$PHP_SELF."?item_id=$topstory'>$ttitle</a></div>";
	//$boxstuff .= "<br /><br /><b>"._NW_RATETHISART."</b><br /><form action='rate.php' method='post'><select name='rating'><option value='0'>"._NW_SELECTRATE."</option><option value='5'>5</option><option value='4'>4</option><option value='3'>3</option><option value='2'>2</option><option value='1'>1</option></select><input type='hidden' value='".$storyid."' /><input type='submit' value='"._GO."' /></form><br /><br />";
	$boxstuff .= "<div style='text-align:right'>";
	$boxstuff .= "<a href='print.php?storyid=$item_id'><img src='images/print.gif' border='0' alt='"._NW_PRINTERFRIENDLY."' width='15' height='11' /></a>&nbsp;&nbsp;";
	$boxstuff .= "<a target='_top' href='mailto:?subject=".sprintf(_NW_INTARTICLE,$xoopsConfig['sitename'])."&body=".sprintf(_NW_INTARTFOUND,$xoopsConfig['sitename']).":  ".XOOPS_URL."/modules/".$xoopsModule->dirname()."/article.php?item_id=".$article->storyid()."'><img src='images/friend.gif' border='0' alt='"._NW_SENDSTORY."' width='15' height='11' /></a>";
	$boxstuff .= "</div>";
	//themesidebox($boxtitle, $boxstuff);
	themecenterposts($boxtitle, $boxstuff);
	echo "</td></tr></table>";
}
$orderby = ($order == 1) ? "date DESC" : "date ASC";
if ( $mode == "flat" ) {
	$criteria = array("item_id=".$item_id."");
	$commentsArray = $artcomment->getAllComments($criteria, true, $orderby);
} elseif ( $mode=="thread" ) {
	$criteria = array("item_id=".$item_id."", "pid=".$artcomment->getVar("pid")."");
	$commentsArray = $artcomment->getAllComments($criteria, true, $orderby);
} else {
	$commentsArray = "";
}

include_once(XOOPS_ROOT_PATH."/header.php");
OpenTable();
// print navbar
$artcomment->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" ) {
		$artcomment->showThreadHead("100%");
		$count = 0;
		foreach($commentsArray as $ele){
			if ( !($count % 2) ) {
				$color_num = 1;
			} else {
				$color_num = 2;
			}
			$ele->showThreadPost($order, $mode, $adminview, $color_num);
			$count++;
		}
		$artcomment->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");
?>