<?php
//v1.0.4 www.bcoos.net 2006/10 GPL
//v1.2 www.exoops.info 2006/03 
// $Id: viewtopic.php,v 1.11 2003/07/05 17:48:06 half-dead Exp $
// ------------------------------------------------------------------------- //
//               E-Xoops: Content Management for the Masses                  //
//                       < http://www.e-xoops.com >                          //
// ------------------------------------------------------------------------- //
// Based on:
// phpBB: http://www.phpBB.com
// License type: GPL
// ------------------------------------------------------------------------- //
include_once("header.php");
$forum    = intval($forum);
$topic_id = intval($topic_id);
if ( empty($forum) ) {
	redirect_header('index.php', 2, _MD_ERRORFORUM);
	exit();
	} elseif ( empty($topic_id) ) {
		redirect_header('viewforum.php?forum='.$forum.'', 2, _MD_ERRORTOPIC);
		exit();
	}
include_once("class/class.forumposts.php");
$sql = "
	SELECT
	t.topic_title,
	t.topic_status,
	t.topic_sticky,
	f.forum_name,
	f.forum_access,
	f.forum_type,
	f.allow_html,
	f.allow_sig,
	f.posts_per_page,
	f.hot_threshold,
	f.topics_per_page
	FROM
	".$db->prefix("bb_topics")." t
	LEFT JOIN ".$db->prefix("bb_forums")." f ON f.forum_id = t.forum_id
	WHERE
	t.topic_id = $topic_id
	AND
	t.forum_id = $forum";
if ( !$result = $db->query($sql) ) {
	$error = "<h4>"._ERROROCCURED."</h4><hr />"._MD_COULDNOTQUERY;
	redirect_header("index.php", 2, $error);
	exit();
}
if ( !$forumdata = $db->fetchArray($result) ) {
	redirect_header("index.php", 2, _MD_FORUMNOEXIST);
	exit();
}
if ( $forumdata['forum_type'] == 1 ) {
	$accesserror = 0;
	if ( $xoopsUser ) {
		if ( !$xoopsUser->isAdmin($xoopsModule->mid()) ) {
			if ( !check_priv_forum_auth($xoopsUser->uid(), $forum, FALSE) ) {
				$accesserror = 1;
			}
		}
	} else {
		$accesserror = 1;
	}
	if ( $accesserror == 1 ) {
		redirect_header("index.php", 2, _MD_NORIGHTTOACCESS);
		exit();
	}
}
$can_post = 0;
if ( $forumdata['forum_access'] == 1 ) {
	if ( $xoopsUser ) {
		$can_post = 1;
	}
	} elseif ( $forumdata['forum_access'] == 2 ) {
		$can_post = 1;
		} else {
			if ( $xoopsUser ) {
				if ( $xoopsUser->isAdmin || is_moderator($forum, $xoopsUser->uid()) ) {
					$can_post = 1;
				}
			}
		}
$total = get_total_posts($topic_id, "topic");
if ( $total > $forumdata['posts_per_page'] ) {
	$times = 0;
	for ($x=0; $x<$total; $x+=$forumdata['posts_per_page']) {
		$times++;
	}
	$pages = $times;
}
$forumdata['topic_title'] = $myts->makeTboxData4Show($forumdata['topic_title']);
cookie("bbtopic_time[$topic_id]", time());
$bcoosConfig['title'] = $forumdata['topic_title'];
include_once(XOOPS_ROOT_PATH."/header.php");
OpenTable();
echo "<table border='0' width='$bbWidth' cellpadding='5' align='center'>";
$total_forum = get_total_posts($forum, 'forum');
echo "<tr><td align='left'><img src='".$bbImage['f_open']."' alt='/' />&nbsp;&nbsp;<a href='".$bbUrl['root']."index.php'>";
echo _MD_FORUMINDEX;
echo "</a><br />&nbsp;&nbsp;&nbsp;<img src='".$bbImage['f_open']."' alt='/' />&nbsp;&nbsp;<a href='". $bbUrl['root']."viewforum.php?forum=$forum&amp;$total_forum'>".$myts->makeTboxData4Show($forumdata['forum_name'])."</a>";
echo "<br /><img src='".$bbImage['f_content']."' alt='/' />&nbsp;&nbsp;<b>";
echo $forumdata['topic_title'];
echo "</b></td><td align='right'>";
if ( $can_post ) {
	echo "<input type='button' class='button' value='"._MD_POSTNEW."' onclick='location.href=\"newtopic.php?forum=".$forum."\"' />";
	} else {
		echo "<a href='".XOOPS_URL."/register.php'>"._MD_REGTOPOST."</a>";
	}
echo "</td></tr>";
echo "</table>";
if ( isset($post_id) && $post_id != "" ) {
	$forumpost = new ForumPosts($post_id);
	} else {
		$forumpost = new ForumPosts();
	}
if ($xoopsUser) {
	if ( !isset($order) ) {
		$order = $xoopsUser->getVar("uorder");
	}
	if (!isset($viewmode) && $xoopsUser->getVar("umode") != "0") {
		$viewmode = $xoopsUser->getVar("umode");
	}
	} else {
		if ( !isset($order) ) {
			$order = $bcoosConfig['com_order'];
		}
		if ( !isset($viewmode) && ($bcoosConfig['com_mode'] != "0") ) {
			$viewmode = $bcoosConfig['com_mode'];
		}
	}
if ($order == 1) {
	$qorder = "post_time DESC";
	} else {
		$qorder = "post_time ASC";
	}
$forumpost->setOrder($qorder);
if ( !isset($post_id) || $post_id == "" ) {
	$pid=0;
	$forumpost->setTopicId($topic_id);
	$forumpost->setParent($pid);
}
if ($viewmode == "thread") {
	$postsArray = $forumpost->getTopPosts();
	} else {
		$viewmode = "flat";
		if ( isset($start) ) {
			$postsArray = $forumpost->getAllPosts($forumdata['posts_per_page'], $start);
			} else {
				$postsArray = $forumpost->getAllPosts($forumdata['posts_per_page']);
			}
	}
include("include/navbar.php");
$adminview = 0;
if ($xoopsUser) {
	if ( $xoopsUser->isAdmin($xoopsModule->mid()) || is_moderator($forum, $xoopsUser->uid())) {
		$adminview = 1;
	}
}
if ($viewmode == "flat") {
	$count = 0;
	foreach ($postsArray as $obj) {
		if ( !($count % 2) ) {
			$row_color = 1;
			} else {
				$row_color = 2;
			}
		$forumpost->setType($obj->type);
		$obj->showPost($viewmode, $order, $can_post, $forumdata['topic_status'], $forumdata['allow_sig'], $adminview, $row_color);
		$count++;
	}
}
//if the mode is thread, show thread
if ($viewmode == "thread") {
	foreach ($postsArray as $obj) {
		$forumpost->setType($obj->type);
		$obj->showPost($viewmode, $order, $can_post, $forumdata['topic_status'], $forumdata['allow_sig'], $adminview);
		//if not in the top page, show links
		if ( $forumpost->parent() ) {
			echo "<div style='text-align:left'>";
			echo "&nbsp;<a href='./viewtopic.php?forum=$forum&amp;topic_id=$topic_id&amp;viewmode=$viewmode&amp;order=$order'>"._MD_TOP."</a>&nbsp;|&nbsp;<a href='"._PHP_SELF."?forum=$forum&amp;topic_id=$topic_id&amp;post_id=".$forumpost->parent()."&amp;viewmode=".$viewmode."&amp;order=".$order."#".$forumpost->parent()."'>"._PARENT."</a>";
			echo "</div>";
		}
		echo "<br />";
		$treeArray = $forumpost->getPostTree($obj->postid());
		if ( count($treeArray) > 0 ) {
			$obj->showTreeHead();
			$count = 0;
			foreach ($treeArray as $treeItem) {
				if ( !($count % 2) ) {
					$color_num = 1;
					} else {
						$color_num = 2;
					}
				$treeItem->showTreeItem($viewmode, $order, $color_num);
				$count++;
			}
			$obj->showTreeFoot();
		}
		echo "<br />";
	}
}
if ( !isset($refresh) && $viewmode != "thread" ) {
	$sql = "UPDATE ".$db->prefix("bb_topics")." SET topic_views=topic_views+1 WHERE topic_id=$topic_id";
	$db->query($sql);
}
if ( !$forumpost->parent() ) {
	include("include/navbar.php");
}
echo "<br /><table width='$bbWidth'><tr><td align='left' valign='top'>";
if ($can_post == 1) {
	echo "<input type='button' class='button' value='"._MD_POSTNEW."' onclick='location.href=\"newtopic.php?forum=".$forum."\"' />";
}
echo "</td><td align='right' valign='top'>";
make_jumpbox();
echo "</td></tr></table>";
if ($xoopsUser) {
	if ( $xoopsUser->isAdmin($xoopsModule->mid()) || is_moderator($forum, $xoopsUser->uid()) ) {
		if ( $forumdata['topic_status'] != 1 ) {
			echo "<a href='".$bbUrl['root']."topicmanager.php?mode=lock&amp;topic_id=$topic_id&amp;forum=$forum'><img src='".$bbImage['locktopic']."' alt='"._MD_LOCKTOPIC."' /></a> ";
			} else {
				echo "<a href='".$bbUrl['root']."topicmanager.php?mode=unlock&amp;topic_id=$topic_id&amp;forum=$forum'><img src='".$bbImage['unlocktopic']."' alt='"._MD_UNLOCKTOPIC."' /></a> ";
			}
		echo "<a href='".$bbUrl['root']."topicmanager.php?mode=move&amp;topic_id=$topic_id&amp;forum=$forum'><img src='".$bbImage['movetopic']."' alt='"._MD_MOVETOPIC."' /></a> ";
		echo "<a href='".$bbUrl['root']."topicmanager.php?mode=del&amp;topic_id=$topic_id&amp;forum=$forum'><img src='".$bbImage['deltopic']."' alt='"._MD_DELETETOPIC."' /></a>";
		if ( $forumdata['topic_sticky'] == 0 ) {
			echo "<a href='".$bbUrl['root']."topicmanager.php?mode=sticky&amp;topic_id=$topic_id&amp;forum=$forum'><img src='".$bbImage['sticky']."' alt='"._MD_STICKYTOPIC."' /></a>";
			} else {
				echo "<a href='".$bbUrl['root']."topicmanager.php?mode=unsticky&amp;topic_id=$topic_id&amp;forum=$forum'><img src='".$bbImage['unsticky']."' alt='"._MD_UNSTICKYTOPIC."' /></a>";
			}
	}
}
CloseTable();
include_once(XOOPS_ROOT_PATH."/footer.php");
?>
