<?php
function b_BopComments_newbb($maxtopic, $module_mid, $blockmode=0, $rss=0)	{
//----------------------------------------------
// if you want to show Private forum's Comments.
// please $private_bb = true;
// if you not want to show praivate forum's Comments.
// please $private_bb = false;

	$private_bb = TRUE;
//----------------------------------------------
// View mode (thread or flat) get from user details
	
	$userSetting = TRUE;
//----------------------------------------------
	
	global $xoopsUser;
	
	$comment = array();
	$xoopsDB =& Database::getInstance();
	if ( $private_bb == TRUE && $xoopsUser )	{
		$uid = $xoopsUser->uid();
		$pu_SQL = "SELECT forum_id FROM ".
				$xoopsDB->prefix("bb_forum_access").
				" WHERE user_id=".$uid;
		if ( !$pu_query = $xoopsDB->query($pu_SQL) ) echo "Error! (newbb)";
		while ($pu_result= $xoopsDB->fetchArray($pu_query))	{
			$forum[] = $pu_result['forum_id'];
		}
	}
	if ( !count($forum)==0 )
	{
		if ($rss)
			$SQL = "SELECT t.topic_id, t.topic_title, t.topic_views, t.topic_replies, t.forum_id, f.forum_name, p.post_time, p.uid, p.post_id as post_id, pt.post_text,p.nohtml,p.nosmiley FROM (".
				$xoopsDB->prefix("bb_topics")." t LEFT JOIN ".
				$xoopsDB->prefix("bb_forums")." f ON t.forum_id=f.forum_id) LEFT JOIN ".
				$xoopsDB->prefix("bb_posts")." p ON t.topic_last_post_id=p.post_id ".
				"LEFT JOIN ".$xoopsDB->prefix("bb_posts_text")." pt ON pt.post_id=p.post_id ".
				"WHERE (f.forum_type <> 1) OR (f.forum_id IN (".implode(", ", $forum).") AND t.forum_id IN (".implode(", ", $forum).") AND f.forum_type=1) ".
				"ORDER BY t.topic_time DESC";
		else
			$SQL = "SELECT t.topic_id, t.topic_title, t.topic_views, t.topic_replies, t.forum_id, f.forum_name, p.post_time, p.uid, p.post_id as post_id FROM (".
				$xoopsDB->prefix("bb_topics")." t LEFT JOIN ".
				$xoopsDB->prefix("bb_forums")." f ON t.forum_id=f.forum_id) LEFT JOIN ".
				$xoopsDB->prefix("bb_posts")." p ON t.topic_last_post_id=p.post_id ".
				"WHERE (f.forum_type <> 1) OR (f.forum_id IN (".implode(", ", $forum).") AND t.forum_id IN (".implode(", ", $forum).") AND f.forum_type=1) ".
				"ORDER BY t.topic_time DESC";
	} else {
		if ($rss)
			$SQL = "SELECT t.topic_id, t.topic_views, t.topic_replies, t.forum_id, f.forum_name, p.post_time, p.uid, p.post_id as post_id, p.subject as topic_title, pt.post_text,p.nohtml,p.nosmiley  FROM (".
				$xoopsDB->prefix("bb_topics")." t LEFT JOIN ".
				$xoopsDB->prefix("bb_forums")." f ON t.forum_id=f.forum_id) LEFT JOIN ".
				$xoopsDB->prefix("bb_posts")." p ON t.topic_last_post_id=p.post_id ".
				"LEFT JOIN ".$xoopsDB->prefix("bb_posts_text")." pt ON pt.post_id=p.post_id ".
				"WHERE (f.forum_type <> 1) ".
				"ORDER BY t.topic_time DESC";
		else
			$SQL = "SELECT t.topic_id, t.topic_views, t.topic_replies, t.forum_id, f.forum_name, p.post_time, p.uid, p.post_id as post_id, p.subject as topic_title FROM (".
				$xoopsDB->prefix("bb_topics")." t LEFT JOIN ".
				$xoopsDB->prefix("bb_forums")." f ON t.forum_id=f.forum_id) LEFT JOIN ".
				$xoopsDB->prefix("bb_posts")." p ON t.topic_last_post_id=p.post_id ".
				"WHERE (f.forum_type <> 1) ".
				"ORDER BY t.topic_time DESC";
	}
	if ( !$query = $xoopsDB->query($SQL, $maxtopic, 0) ) echo "Error! (newbb)<br />$SQL";
	
	$myts =& MyTextSanitizer::getInstance();
	
	while ( $result = $xoopsDB->fetchArray($query) )	{
		$key = BopComments_KeyCheck($comment, $result['post_time']);
		
		$comment[$key]['moduleimg'] = XOOPS_URL."/modules/BopComments/plugin/image/newbb.png";
		$comment[$key]['modulelink'] = XOOPS_URL."/modules/newbb/index.php";
		//$comment[$key]['placelink'] = XOOPS_URL."/modules/newbb/viewforum.php?forum=".$result['forum_id'];
		$comment[$key]['placelink'] = XOOPS_URL."/modules/newbb/viewforum.forum+".$result['forum_id'].".htm";
		$comment[$key]['place'] = $result['forum_name'];
		
		//$comment[$key]['topiclink'] = XOOPS_URL."/modules/newbb/viewtopic.php?forum=".$result['forum_id']."&amp;topic_id=".$result['topic_id']."&amp;post_id=".$result['post_id']."#forumpost".$result['post_id'];
		$comment[$key]['topiclink'] = XOOPS_URL."/modules/newbb/viewtopic.forum+".$result['forum_id']."+topic_id+".$result['topic_id']."+post_id+".$result['post_id'].".htm#forumpost".$result['post_id'];
		//$comment[$key]['topiclink'] = XOOPS_URL."/newbb+viewtopic.forum+".$result['forum_id']."+topic_id+".$result['topic_id']."+post_id+".$result['post_id'].".htm#forumpost".$result['post_id'];
		
		$comment[$key]['topic'] = $result['topic_title'];
		$comment[$key]['user'] = $result['uid'];
		$comment[$key]['read'] = $result['topic_views'];
		$comment[$key]['reply'] = $result['topic_replies'];
		$comment[$key]['date'] = $result['post_time'];
		$context =strip_tags($myts->displayTarea($result['post_text'],$result['nohtml'],$result['nosmiley'],1));
		if ($rss && function_exists('xoops_make_context'))
			$comment[$key]['context'] = xoops_make_context($context);
	}
	return $comment;
}
?>