<?php
class Thread
{
	//var $nowaddress = '';
	function Thread()
	{
		$getmenu = $GLOBALS['Helpe']->get_menu();
		$GLOBALS['Templ']->assign('getmenu',$getmenu);
		
		$getbbsconfig = $GLOBALS['Helpe']->get_bbsconfig();
		$GLOBALS['Templ']-> assign('getbbsconfig',$getbbsconfig);
		
		$getlink = $GLOBALS['Helpe']->get_link();
		$GLOBALS['Templ']->assign('getlink',$getlink);

		$getuserlink = $GLOBALS['WebSe']->getuserlink();
		$GLOBALS['Templ']->assign('getuserlink',$getuserlink);

		$getads = $GLOBALS['Helpe'] -> get_ads( $instr = '1,2,3,4,5,6,7,8,9,10,11' );
		$GLOBALS['Templ'] -> assign('getads',$getads);
		
		$this -> nowaddress = '<a href="'.BASE_WEBURL.'/">首页</a> > <a href="'.BASE_WEBURL.'/bbs/">'.$getbbsconfig["bbsname"].'</a>';
		
	}

	
	function infolist()
	{
		//公司简介
		$idval 		 = $GLOBALS['Reque'] -> reqval["id"];
		$pageNum  	 = $GLOBALS['Reque'] -> reqval["pageNum"];
		$pageSize 	 = 10 ;
		
		//var_dump($strArray );
		
		if( $pageNum > 1 )
		{
			//$pageNum  = $pageNum;
			$LIMITnum = ceil( ( $pageNum -1 ) * $pageSize );
		}else{
			$pageNum  = 1;
			$LIMITnum = 0 ;
		}

		$sqlQuery = " SELECT r.rep_id, r.rep_desc ,r.rep_addtime, r.rep_list,r.use_id FROM ".SQL_PREFIX."reply AS r WHERE   r.rep_enabled = 1  and r.thr_id = ".$idval." GROUP BY r.rep_id ORDER BY  r.rep_addtime ,  r.rep_list , r.rep_id desc  LIMIT  $LIMITnum , $pageSize ";
		
		$info 		= $GLOBALS['MySql']  ->  selectInfo($sqlQuery);




		$sqlQuery 	= " SELECT count(1) as CNUM  FROM  ".SQL_PREFIX."reply  WHERE rep_enabled = 1 and thr_id = ".$idval."  ORDER BY `rep_id`  ";
		
		
		$infocount 	= $GLOBALS['MySql']  ->  selectOne($sqlQuery);
		
		$PageStr = $GLOBALS['Page'] -> buildPageStr( $url = BASE_WEBURL.'/bbs/?load=thread&act=infolist&id='.$idval, $pageNum  , $pageSize  , $resultCount = $infocount["CNUM"] );
		
		$GLOBALS['Templ'] -> assign('PageStr',$PageStr );
 
		$GLOBALS['Templ'] -> assign('info', $info );
 
		$sqlinfo = " SELECT thr_id, thr_title,thr_desc,thr_addtime,thr_replycount,thr_count,use_id FROM  ".SQL_PREFIX."thread  WHERE thr_enabled = 1 and thr_id = ".$idval."  ";
		
		$threadInfo = $GLOBALS['MySql'] -> selectOne($sqlinfo);
		
		$threadInfo["thr_desc"] =  stripslashes($threadInfo["thr_desc"]);
 
		
		$upthread = "UPDATE   ".SQL_PREFIX."thread  SET thr_count = thr_count + 1 WHERE  thr_enabled = 1 and thr_id = ".$idval." ";
		
		$GLOBALS['MySql'] -> querySql( $upthread );
		
		$this -> nowaddress .= ' > <a href="'.BASE_WEBURL.'/bbs/?load=thread&act=infolist&id='.$threadInfo["thr_id"].'&pageNum=1">'.$threadInfo["thr_title"].'</a>';
		
		
		//查询用户信息
		$useridstr = $threadInfo["use_id"];
		foreach((array)$info as $ikey => $ival)
		{
			$useridstr .= 	",".$ival["use_id"];
		}
		
		$sqlQueryuser = "SELECT  use_id ,usd_sex,usd_nick, usd_image,usd_points,usd_rank FROM ".SQL_PREFIX."userdetail WHERE use_id in ( $useridstr ) ";
		$infoUser 		= $GLOBALS['MySql']  ->  selectInfo($sqlQueryuser);
		
		
		$infoUserArray = array();
		
		foreach((array)$infoUser as $ukey => $uval)
		{
			$infoUserArray[$uval["use_id"]]["usd_sex"] 		= $uval["usd_sex"];
			$infoUserArray[$uval["use_id"]]["usd_nick"] 	= $uval["usd_nick"];
			$infoUserArray[$uval["use_id"]]["usd_image"] 	= $uval["usd_image"];
			$infoUserArray[$uval["use_id"]]["usd_points"] 	= $uval["usd_points"];
			$infoUserArray[$uval["use_id"]]["usd_rank"] 	= $GLOBALS['Helpe'] -> get_rank( $uval["usd_rank"] , $type = 'all' );

		}
		
		$webUser = $_SESSION["webUser"];
		
		$GLOBALS['Templ']->assign('webUser', $webUser);
		
		$GLOBALS['Templ']->assign('infoUser', $infoUserArray);
		
		$GLOBALS['Templ']->assign('nowaddress', $this -> nowaddress );
		
		//$comInfo = array();
		$GLOBALS['Templ']->assign('threadInfo', $threadInfo);
		
		$GLOBALS['Templ']->assign('pageNum', $pageNum);
		
		$GLOBALS['Templ']-> display('thread_tpl.html');
		
		
	}
	
	function add()
	{
		$idval 	= $GLOBALS['Reque'] -> reqval["id"];
		
		$GLOBALS['WebSe'] -> isLoginUser( $getUrl = BASE_WEBURL.'/bbs/index.php?load=thread&act=add&id='.$idval );
		
		
		$sqlQuery = " SELECT  * FROM  ".SQL_PREFIX."forum  WHERE for_id = ".$idval." GROUP BY for_id ORDER BY for_id desc  ";
		
		$info 	  = $GLOBALS['MySql']  ->  selectOne($sqlQuery);

		$this -> nowaddress .= ' > <a href="'.BASE_WEBURL.'/bbs/index.php?load=forum&act=infolist&id='.$idval.'">'.$info["for_name"].'</a>';
		
		$GLOBALS['Templ'] -> assign('nowaddress',$this -> nowaddress);

		$GLOBALS['Templ'] -> assign('info',$info);
		
		$GLOBALS['Templ'] -> assign('idval',$idval);
		
		$GLOBALS['Helpe'] -> load_ckeditor( $textName = 'thr_desc','','Basic','','510' );	//加载文本编辑器
		
		$GLOBALS['Templ'] -> display('threadadd_tpl.html');
		
	}
	
	
	function addinfo()
	{
		
		$thr_titleval 	= $GLOBALS['Reque'] -> funpost("thr_title");
		$thr_descval  	= $_REQUEST["thr_desc"];
		$for_idval 		= $GLOBALS['Reque'] -> funpost("for_id");
		$thr_uptimeval 	= $thr_addtimeval = time();
		
		$thr_ipval 		= $GLOBALS['Helpe'] -> getip();
		$use_idval 		= $_SESSION["webUser"]["use_id"];
		
		$thr_enabledval = 1;
		$insertsql ="INSERT INTO `".SQL_PREFIX."thread` (`thr_title` , `thr_desc` , `thr_addtime` ,`thr_uptime` , `use_id`, `thr_ip`,`for_id` ,`thr_enabled`) VALUES (  '$thr_titleval', '$thr_descval', '$thr_addtimeval', '$thr_uptimeval', '$use_idval','$thr_ipval','$for_idval','$thr_enabledval')"; 
		
		$GLOBALS['MySql'] -> querySql( $insertsql );
		
		
		///bbs/index.php?load=forum&act=infolist&id=1
		$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_addsusse']);
		$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
		$GLOBALS['Templ'] -> assign('geturl',BASE_WEBURL.'/bbs/index.php?load=forum&act=infolist&id='.$for_idval);
		$GLOBALS['Templ'] -> display('suggestion_tpl.html');
	}
	
	
}


$Thread = new Thread();
?>