<?php
// $Id: viewforum.php,v 1.2 2004/07/19 20:34:53 Farsus Exp $
// -------------------------------------------------------------------------//
//			       RUNCMS
//
//	 Relaible - Unique - Nocost &  Simplicity & ease off use
//                       < http://www.runcms.org >
// -------------------------------------------------------------------------//
// Based on:
// phpBB: http://www.phpBB.com
// License type: GPL
// ------------------------------------------------------------------------- //

include_once("header.php");

$forum = intval($forum);

if ($newbbConfig['wol_enabled'])
{
	include_once($bbPath['path']."class/class.whosonline.php");
	$whosonline = new WhosOnline($forum);
}

function show_subforums($forum_id)
{
	global $db, $myts, $bbWidth, $xoopsUser, $xoopsConfig, $last_visit, $bbImage, $xoopsModule, $bbPath, $bbTable;
	$sql = "
		SELECT f.*, u.uname, u.uid, p.topic_id, p.post_time, p.subject, p.icon
		FROM ".$bbTable['forums']." f
		LEFT JOIN ".$bbTable['posts']." p
		ON p.post_id = f.forum_last_post_id
		LEFT JOIN ".$db->prefix("users")." u
		ON u.uid = p.uid $limit_forums
		WHERE f.parent_forum = $forum_id
		ORDER BY f.cat_id, f.forum_order";
	if ($result = $db->query($sql))
	{
		$num_subforums = $db->num_rows($result);
		if($num_subforums > 0)
		{
?>
<table border="0" width="<?php echo $bbWidth?>" cellpadding="0" cellspacing="0" align="center" valign="top"><tr>
<td class="bg2">
<table border="0" cellpadding="4" cellspacing="1" width="100%"><tr class="bg3" align="center">
<td>&nbsp;</td>
<td nowrap="nowrap" align="left"><b><?php echo _MD_SUBFORUM;?></b></td>
<td nowrap="nowrap"><b><?php echo _MD_TOPICS;?></b></td>
<td nowrap="nowrap"><b><?php echo _MD_POSTS;?></b></td>
<td nowrap="nowrap"><b><?php echo _MD_LASTPOST;?></b></td>
<td nowrap="nowrap"><b><?php echo _MD_MODERATOR;?></b></td>
</tr>
<?php

			while ($row = $db->fetch_array($result))
			{
				$last_post = '';

				$permissions = get_forum_auth($row['forum_id']);
				if ($permissions['can_view'] == 0)
				{
						continue;
				}

				if ($row['post_time'])
				{
				 	$last_post .= formatTimestamp($row['post_time'], "m");
				}

				if (empty($last_post)) {
					$last_post = _MD_NOPOSTS;
				}

				$last_post_datetime = $row['post_time'];
				echo "<tr align='left' valign='top'>";
				if ( ( ($last_post_datetime > $last_visit) && ($last_post != _MD_NOPOSTS)  ) &&
				!isset($HTTP_COOKIE_VARS['bbtopic_time'][$row['topic_id']]) ) {
				echo "<td class='bg3' align='center' valign='middle' width='5%'><img src='".$bbImage['newposts']."' alt='' /></td>";
				} else {
					echo "<td class='bg3' align='center' valign='middle' width='5%'><img src='".$bbImage['folder']."' alt='' /></td>";
				}
				$name         = $myts->makeTboxData4Show($row['forum_name']);
				$total_posts  = $row["forum_posts"];
				$total_topics = $row["forum_topics"];
				$myts->setType('admin');
				$desc         = $myts->makeTareaData4Show($row['forum_desc'], 1, 1, 1);
				$forum_link   = $bbPath['url']."viewforum.php?forum=".$row["forum_id"];
				if ($xoopsUser && $xoopsUser->isAdmin($xoopsModule->mid())) {
					$admin_link = "<a href='admin/forum_manager.php?op=edit_forum&forum_id=".$row["forum_id"]."'><img src='".$bbImage['editicon']."' alt='"._EDIT."' border='0'></a>";
				}
				echo "
				<td class='bg1' onmouseover='this.className=\"bg3\"; this.style.cursor=\"hand\";' onmouseout='this.className=\"bg1\";' onclick='window.location.href=\"$forum_link\";'>
				$admin_link <a href='$forum_link'><b>$name</b></a><br />$desc</td>
				<td class='bg3' width='5%' align='center' valign='middle'>$total_topics</td>
				<td class='bg1' width='5%' align='center' valign='middle'>$total_posts</td>
				<td class='bg3' width='15%' align='center' valign='middle' onmouseover='this.className=\"bg1\";' onmouseout='this.className=\"bg3\";'>$last_post</td>
				<td class='bg1' width='5%' align='center' valign='middle' onmouseover='this.className=\"bg3\";' onmouseout='this.className=\"bg1\";'>";
				list_moderators($row['forum_id']);
				echo "</td></tr>";
			}
?>
</table></td></tr></table>
<br>
<?php
		}
	}
}

if ($forum == -1 || !isset($forum))
{
	redirect_header("index.php", 2, _MD_ERRORFORUM);
	exit();
}

$sql = "SELECT * FROM ".$bbTable['forums']." WHERE forum_id=$forum";
if (!$result = $db->query($sql)) {
	redirect_header("index.php", 2, _MD_ERRORCONNECT);
	exit();
}

if (!$forumdata = $db->fetch_array($result)) {
	redirect_header("index.php", 2, _MD_ERROREXIST);
	exit();
}

$forum_name = $myts->makeTboxData4Show($forumdata['forum_name']);

$permissions = get_forum_auth($forumdata['forum_id']);
if ( $permissions['can_view'] == 0 ) {
	redirect_header("index.php", 2, _MD_NORIGHTTOACCESS);
	exit();
}

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

echo "<table border='0' width='$bbWidth' cellpadding='5' align='center'>";
echo "<tr><td align='left'><img src='".$bbImage['f_open']."' alt='/' />&nbsp;&nbsp;<a href='".$bbPath['url']."index.php'>";
echo _MD_FORUMINDEX."</a>";
if($forumdata['parent_forum'] == 0)
{
	echo "</a><br />&nbsp;&nbsp;&nbsp;<img src='".$bbImage['f_close']."' alt='/' />&nbsp;&nbsp;<b>".$myts->makeTboxData4Show($forumdata['forum_name'])."</b><br />( ". _MD_MODERATEDBY .":";
	list_moderators($forum);
}
else
{
	$q = "select forum_name from ".$bbTable['forums']." WHERE forum_id=".$forumdata['parent_forum'];
	$row = $db->fetch_array($db->query($q));
	echo "<br />&nbsp;&nbsp;&nbsp;<img src='".$bbImage['f_open']."' alt='/' />&nbsp;&nbsp;<a href='".$bbPath['url']."viewforum.php?forum=".$forumdata['parent_forum']."'>".$myts->makeTboxData4Show($row['forum_name'])."</a>";
	echo "<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src='".$bbImage['f_close']."' alt='/' />&nbsp;&nbsp;<b>".$myts->makeTboxData4Show($forumdata['forum_name'])."</b><br />( ". _MD_MODERATEDBY .":";
	list_moderators($forum);
}
echo " )</td></tr></table>";

if (!isset($sortname) || $sortname == "") $sortname = "p.post_time";
if (!isset($sortorder) || $sortorder == "") $sortorder = "DESC";
if (!isset($sortdays) || $sortdays == "") $sortdays = "100";

show_subforums($forumdata['forum_id']);

?>

<table border="0" cellpadding="1" cellspacing="0" align="center" valign="top" width="<?php echo $bbWidth;?>">
<tr>
<?php
echo "<td align='right'>";
if ( $permissions['can_post'] == 1 )
{
	if ($forumdata['allow_polls'] == 1 && $permissions['can_addpoll'])
	{
		echo "<a href='newtopic.php?forum=$forum&addpoll=1'><img src='".$bbImage['newpoll']."'></a>&nbsp;";
	}
	echo "<a href='newtopic.php?forum=$forum'><img src='".$bbImage['newthread']."'></a>";
}
else
{
	echo "<b>"._MD_POST_DENIED."</b>";
}
echo "&nbsp;&nbsp;</td>";
?>
</tr>
</table>
<?php
$startdate = ( time() - (86400 * $sortdays) );

if ( !isset($start) || $start=="" ) $start = 0;
$start = intval($start);
$sql = "
	SELECT
	t.*,
	u.uname,
	u2.uname AS last_poster,
	u2.uid AS last_poster_uid,
	p.post_time AS last_post_time,
	p.icon
	FROM
	".$bbTable['topics']." t
	LEFT JOIN ".$db->prefix("users")." u
	ON u.uid=t.topic_poster
	LEFT JOIN ".$bbTable['posts']." p
	ON p.post_id=t.topic_last_post_id
	LEFT JOIN ".$db->prefix("users")." u2
	ON u2.uid=p.uid
	WHERE t.forum_id=$forum
	AND (p.post_time > '$startdate' OR t.topic_sticky=1)
	ORDER BY
	topic_sticky DESC, ".$sortname." ".$sortorder."";

if ( !$result = $db->query($sql, $forumdata['topics_per_page'], $start) ) {
	error_die("</table></td></tr></table><h4>"._MD_ERROROCCURED."</h4><hr />"._MD_COULDNOTQUERY."");
}

$topics_start = $start;
$pageNav = '';
$sql = "SELECT COUNT(*) FROM ".$bbTable['topics']." WHERE forum_id=$forum AND (topic_time > $startdate OR topic_sticky = 1)";
if ( !$r = $db->query($sql) )
{
	error_die(_MD_COULDNOTQUERY."</table></td></tr></table>");
}
list($all_topics) = $db->fetch_row($r);
$count = 1;
$next = $topics_start + $forumdata['topics_per_page'];
$prev = $topics_start - $forumdata['topics_per_page'];
if ( $all_topics > $forumdata['topics_per_page'] ) {
	$pageNav .=  "<div align=right><b>"._MD_GOTOPAGE."&nbsp;";
	if ( $prev >= 0) {
		$pageNav.= "<a href=\"viewforum.php?forum=$forum&start=$prev&sortname=$sortname&sortorder=$sortorder&sortdays=$sortdays\">"._MD_PREVPAGE."</a>&nbsp;";
	}
	for ( $x = 0; $x < $all_topics; $x++ ) {
		if ( !($x % $forumdata['topics_per_page']) ) {
			if( $x == $topics_start ) {
				$pageNav .= "$count\n";
			} else {
				$pageNav .= "<a href=\"viewforum.php?forum=$forum&start=$x&sortname=$sortname&sortorder=$sortorder&sortdays=$sortdays\">$count</a>\n";
	 			if ( !($count % 10)) {
					$pageNav .= "<br />";
				}
			}
			$count++;
		}
	}
	if ( $next < $all_topics ) {
		$pageNav .= "&nbsp;<a href=\"viewforum.php?forum=$forum&start=$next&sortname=$sortname&sortorder=$sortorder&sortdays=$sortdays\">"._MD_NEXTPAGE."</a>";
	}
	$pageNav.= "</b></div><br />";
}
echo $pageNav;
?>
<table border="0" cellpadding="0" cellspacing="0" align="center" valign="top" width="<?php echo $bbWidth;?>"><tr><td class="bg2">
<table border="0" cellpadding="4" cellspacing="1" width="100%">
<tr class="bg3" align="left">
<td>&nbsp;</td>
<td>&nbsp;</td>
<td width="100%" nowrap="nowrap"><b><a href="viewforum.php?forum=<?php echo $forum;?>&sortname=t.topic_title&sortdays=<?php echo $sortdays;?>&sortorder=<?php if ($sortname=="t.topic_title" && $sortorder=="DESC") { echo "ASC"; } else { echo "DESC"; }?>"><?php echo _MD_TOPIC;?></a></B></TD>
<td align="center" nowrap="nowrap"><b><a href="viewforum.php?forum=<?php echo $forum;?>&sortname=t.topic_replies&sortdays=<?php echo $sortdays;?>&sortorder=<?php if ($sortname=="t.topic_replies" && $sortorder=="DESC") { echo "ASC"; } else { echo "DESC"; }?>"><?php echo _MD_REPLIES?></a></b></td>
<td align="center" nowrap="nowrap"><b><a href="viewforum.php?forum=<?php echo $forum;?>&sortname=u.uname&sortdays=<?php echo $sortdays;?>&sortorder=<?php if ($sortname=="u.uname" && $sortorder=="DESC") { echo "ASC"; } else { echo "DESC"; }?>"><?php echo _MD_POSTER;?></a></b></td>
<td align="center" nowrap="nowrap"><b><a href="viewforum.php?forum=<?php echo $forum;?>&sortname=t.topic_views&sortdays=<?php echo $sortdays;?>&sortorder=<?php if ($sortname=="t.topic_views" && $sortorder=="DESC") { echo "ASC"; } else { echo "DESC"; }?>"><?php echo _MD_VIEWS;?></a></b></td>
<td align="center" nowrap="nowrap"><b><a href="viewforum.php?forum=<?php echo $forum;?>&sortname=p.post_time&sortdays=<?php echo $sortdays;?>&sortorder=<?php if ($sortname=="p.post_time" && $sortorder=="DESC") { echo "ASC"; } else { echo "DESC"; }?>"><?php echo _MD_DATE;?></a></b></td>
</tr>

<?php
while ($myrow = $db->fetch_array($result)) {
	echo"<tr>";
	if (!$myrow["last_poster"]) {
		$myrow["last_poster"] = $xoopsConfig['anonymous'];
	}

	$last_post  = formatTimestamp($myrow['last_post_time'], "m");
	$last_post .= "<br />"._MD_BY;
	if ( !empty($myrow['last_poster_uid']) ) {
		$last_post .= " <a href='".XOOPS_URL."/userinfo.php?uid=".$myrow['last_poster_uid']."'>".$myts->makeTboxData4Show($myrow['last_poster'])."</a>";
		} else {
			$last_post .= " ".$myts->makeTboxData4Show($myrow['last_poster']);
		}

	if ($myrow['topic_sticky'] == 1)
	{
                // If topic is sticky
		$image = $bbImage['sticky'];
	}
	elseif ($myrow['topic_status'] == 1)
	{
                // if topic is locked
		$image = $bbImage['locked'];
	}
	else
	{
                // topic not sticky and not locked
		if ( $myrow['topic_replies'] >= $forumdata['hot_threshold'])
		{
                        // topic is hot
                        $image = $bbImage['hot_folder'];
			if ( $myrow['last_post_time'] > $last_visit )
			{
                                // topic is hot, check if have new post
				if ( !isset($bbtopic_time[$myrow['topic_id']]) || ($bbtopic_time[$myrow['topic_id']] < $myrow['last_post_time'] ) )
				{
                                        // topic is hot, have new post
					$image = $bbImage['hot_newposts'];
				}
				}
		 	}
		else
		{
                        // topic not hot
                        $image = $bbImage['folder'];
			if( $myrow['last_post_time'] > $last_visit )
			{
                                // check if have new post
				if ( !isset($bbtopic_time[$myrow['topic_id']]) || ($bbtopic_time[$myrow['topic_id']] < $myrow['last_post_time'] ) )
				{
                                        // have new post
					$image = $bbImage['newposts'];
				}
			}
		}
	}
	echo "<td class='bg3' align='center'><img src='$image' /></td>";
	$topic_title = $myts->makeTboxData4Show($myrow['topic_title']);
	$pagination = "";
	$start = "";
	$topiclink = "viewtopic.php?topic_id=".$myrow['topic_id']."&amp;forum=".$forum."";
	if ( $myrow['topic_replies'] + 1 > $forumdata['posts_per_page']) {
		$pagination .= "&nbsp;&nbsp;&nbsp;(<img src=\"".$bbImage['posticon']."\" /> ";
		$pagenr      = 1;
		$skippages   = 0;
		for ($x = 0; $x < $myrow['topic_replies'] + 1; $x += $forumdata['posts_per_page']) {
			$lastpage = (($x + $forumdata['posts_per_page']) >= $myrow['topic_replies'] + 1);
			if ($lastpage) {
				$start = "&start=$x";
				} else {
					if ( $x != 0 ) {
						$start = "&start=$x";
					}
				}
			if ( $pagenr > 3 && $skippages != 1 && !$lastpage ) {
				$pagination .= ", ... ";
				$skippages = 1;
			}
			if ($skippages != 1 || $lastpage) {
				if ($x!=0) $pagination .= ", ";
				$pagination .= "<a href='$topiclink$start'>$pagenr</a>";
			}
			$pagenr++;
		}
		$pagination .= ")";
	}
	if ($myrow['icon']) {
		echo "<td class='bg1' align='center'><img src=\"".XOOPS_URL."/images/subject/".$myrow['icon']."\" /></td>\n";
	} else {
		echo "<td class='bg1' align='center'>&nbsp;</td>";
	}
	echo "<td id='topic".$myrow['topic_id']."' class='bg3'";
	echo " onmouseover='this.className=\"bg1\"; this.style.cursor=\"hand\";' onclick='window.location.href=\"" . $topiclink . "\"' onmouseout='this.className=\"bg3\";'";
	echo "> <a href='$topiclink'>$topic_title</a>$pagination";
	echo "</td>
	<td class='bg1' align='center' valign='middle'>".$myrow['topic_replies']."</td>
	<td class='bg3' align='center' valign='middle' nowrap='nowrap' onmouseover='this.className=\"bg1\";' onmouseout='this.className=\"bg3\";'>";
	if ( $myrow['topic_poster'] != 0 && $myrow['uname'] ) {
		echo "<a href='".XOOPS_URL."/userinfo.php?uid=".$myrow['topic_poster']."'>".$myrow['uname']."</a>";
	} else {
		echo $xoopsConfig['anonymous'];
	}
	echo "
	</td>
	<td class='bg1' align='center' valign='middle'>".$myrow['topic_views']."</td>
	<td class='bg3' align='right' valign='middle' nowrap='nowrap'>$last_post</td></tr>";
}


?>
<tr><td align="left" colspan=2  class='bg1'>
<form action="viewforum.php" method="get">
<input type="hidden" name="forum" value="<?php echo $forum;?>">
<b><?php echo _MD_SORTBY;?></b>&nbsp;&nbsp;
</td>
<td align="left" colspan=5  class='bg1'>
<select class="select" name="sortname">
<option value="t.topic_title"<?php if($sortname=="t.topic_title") echo " selected";?>><?php echo _MD_TOPICTITLE;?></option>
<option value="p.post_time"<?php if($sortname=="p.post_time") echo " selected";?>><?php echo _MD_LASTPOSTTIME;?></option>
<option value="t.topic_replies"<?php if($sortname=="t.topic_replies") echo " selected";?>><?php echo _MD_NUMBERREPLIES;?></option>
<option value="t.topic_views"<?php if($sortname=="t.topic_views") echo " selected";?>><?php echo _MD_NUMBERVIEWS;?></option>
<option value="u.uname"<?php if($sortname=="u.uname") echo " selected";?>><?php echo _MD_TOPICPOSTER;?></option>
</select>

<select class="select" name="sortorder">
<option value="ASC"<?php if($sortorder=="ASC") echo " selected";?>><?php echo _MD_ASCENDING;?></option>
<option value="DESC"<?php if($sortorder=="DESC") echo " selected";?>><?php echo _MD_DESCENDING;?></option>
</select>

<select class="select" name="sortdays">
<option value="1"<?php if($sortdays=="1") echo " selected";?>><?php echo _MD_LASTDAY;?></option>
<option value="2"<?php if($sortdays=="2") echo " selected";?>><?php printf(_MD_FROMLASTDAYS,2);?></option>
<option value="5"<?php if($sortdays=="5") echo " selected";?>><?php printf(_MD_FROMLASTDAYS,5);?></option>
<option value="10"<?php if($sortdays=="10") echo " selected";?>><?php printf(_MD_FROMLASTDAYS,10);?></option>
<option value="20"<?php if($sortdays=="20") echo " selected";?>><?php printf(_MD_FROMLASTDAYS,20);?></option>
<option value="30"<?php if($sortdays=="30") echo " selected";?>><?php printf(_MD_FROMLASTDAYS,30);?></option>
<option value="45"<?php if($sortdays=="45") echo " selected";?>><?php printf(_MD_FROMLASTDAYS,45);?></option>
<option value="60"<?php if($sortdays=="60") echo " selected";?>><?php printf(_MD_FROMLASTDAYS,60);?></option>
<option value="75"<?php if($sortdays=="75") echo " selected";?>><?php printf(_MD_FROMLASTDAYS,75);?></option>
<option value="100"<?php if($sortdays=="100") echo " selected";?>><?php printf(_MD_FROMLASTDAYS,100);?></option>
<option value="365"<?php if($sortdays=="365") echo " selected";?>><?php echo _MD_THELASTYEAR;?></option>
<option value="1000"<?php if($sortdays=="1000") echo " selected";?>><?php echo _MD_BEGINNING;?></option>
</select>
<input type="submit" class="button" name="refresh" value="<?php echo _GO;?>">
</form>
</nobr>
</td></tr>
</table></td></tr></table><br>
<?php
echo $pageNav;
if ($newbbConfig['wol_enabled'])
{
	echo $whosonline->show_online_list();
}
?>

<table align="center" border="0" width="<?php echo $bbWidth;?>"><tr>
<td valign="top">
<table width="100%">
<tr>
<td><img src="<?php echo $bbImage['newposts'];?>" align=middle> = <?php echo _MD_NEWPOSTS;?></td>
<td><img src="<?php echo $bbImage['hot_newposts'];?>" align=middle> = <?php printf(_MD_MORETHAN,$forumdata['hot_threshold']);?></td>
</tr>
<tr>
<td><img src="<?php echo $bbImage['folder'];?>" align=middle> = <?php echo _MD_NONEWPOSTS?></td>
<td><img src="<?php echo $bbImage['hot_folder'];?>" align=middle> = <?php printf(_MD_MORETHAN,$forumdata['hot_threshold']);?></td>
</tr>
<tr>
<td><img src="<?php echo $bbImage['locked'];?>" align=middle> = <?php echo _MD_TOPICLOCKED;?></td>
<td><img src="<?php echo $bbImage['sticky'];?>" align=middle> = <?php echo _MD_TOPICSTICKY;?></td>
</tr>
</table>
</td>
<td align="right">

<?php
if ( $permissions['can_post'] == 1 )
{
	if ($forumdata['allow_polls'] == 1 && $permissions['can_addpoll'] == 1)
	{
		echo "<a href='newtopic.php?forum=$forum&addpoll=1'><img src='".$bbImage['newpoll']."'></a>&nbsp;";
	}
	echo "<a href='newtopic.php?forum=$forum'><img src='".$bbImage['newthread']."'></a>";
}
echo "&nbsp;&nbsp;<br>";
make_jumpbox();
echo "<table><tr><td align=left>";
echo $permissions['can_post'] ? _MD_CAN_POST : _MD_CANNOT_POST;
echo $permissions['can_reply'] ? _MD_CAN_REPLY : _MD_CANNOT_REPLY;
echo $permissions['can_edit'] ? _MD_CAN_EDIT : _MD_CANNOT_EDIT;
echo $permissions['can_delete'] ? _MD_CAN_DELETE : _MD_CANNOT_DELETE;
echo $permissions['can_addpoll'] ? _MD_CAN_ADDPOLL : _MD_CANNOT_ADDPOLL;
echo $permissions['can_vote'] ? _MD_CAN_VOTE : _MD_CANNOT_VOTE;
echo $permissions['can_attach'] ? _MD_CAN_ATTACH : _MD_CANNOT_ATTACH;
echo "</td></tr></table>";
?>
</td>
</tr></table>
<?php
if ($newbbConfig['rss_enable'] == 1) {
	echo "<div align='right'><a href='./cache/forum.xml' target='_blank'><img src='./images/xml.gif' border='0' vspace='2'></a></div>";
}
CloseTable();
include_once(XOOPS_ROOT_PATH."/footer.php");
?>