<?php
class Thread
{
	//var $now_address = '';
	function Thread()
	{
		$getmenu = $this -> helper->get_menu();
		$this -> view ->assign('getmenu',$getmenu);
		
		$getbbsconfig = $this -> helper->get_bbsconfig();
		$this -> view -> assign('getbbsconfig',$getbbsconfig);
		
		$getlink = $this -> helper->get_link();
		$this -> view ->assign('getlink',$getlink);

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

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

	
	function infolist()
	{
		//公司简介
		$idval 		 = intval($GLOBALS['Webur'] -> getval["idval"]);
		$page_num  	 = $this -> input -> req["page_num"];
		
		$page_size 	 = 15 ;
		
		if( $page_num > 1 )
		{
			//$page_num  = $page_num;
			$limit_num = ceil( ( $page_num -1 ) * $page_size );
		}else{
			$page_num  = 1;
			$limit_num = 0 ;
		}

		$sql_query = " SELECT r.rep_id, r.rep_desc ,r.rep_addtime, r.rep_list,r.use_id FROM ".$this -> db -> 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  $limit_num , $page_size ";
		$info 		= $this -> db ->  select_info($sql_query);


		$sql_query 	= " SELECT count(1) as CNUM  FROM  ".$this -> db -> prefix."reply  WHERE rep_enabled = 1 and thr_id = ".$idval."  ORDER BY `rep_id`  ";
		$infocount 	= $this -> db ->  select_one($sql_query);
		

		$page_str = $GLOBALS['Page'] -> buildpage_str( $url = BASE_WEBURL.'?/thread-'.$idval.".html", $page_num  , $page_size  , $result_count = $infocount["CNUM"] ,$endArray );

		$this -> view -> assign('page_str',$page_str );
 
		$this -> view -> assign('info', $info );
 
		$sqlinfo = " SELECT thr_id, thr_title,thr_desc,thr_addtime,thr_replycount,thr_count,use_id,for_id FROM  ".$this -> db -> prefix."thread  WHERE thr_enabled = 1 and thr_id = ".$idval."  ";
		
		$threadInfo = $this -> db -> select_one($sqlinfo);
		
		$threadInfo["thr_desc"] =  stripslashes($threadInfo["thr_desc"]);
 
		
		$upthread = "UPDATE   ".$this -> db -> prefix."thread  SET thr_count = thr_count + 1 WHERE  thr_enabled = 1 and thr_id = ".$idval." ";
		
		$this -> db -> query_sql( $upthread );
 
		$this -> nowaddress .= ' > <a href="'.BASE_WEBURL.'?/thread-'.$threadInfo["thr_id"].'.html">'.$threadInfo["thr_title"].'</a>';
 
		//查询用户信息
		$useridstr = $threadInfo["use_id"];
		foreach((array)$info as $ikey => $ival)
		{
			$useridstr .= 	",".$ival["use_id"];
		}
		
		$sql_queryuser = "SELECT  use_id ,usd_sex,usd_nick, usd_image,usd_points,usd_rank FROM ".$this -> db -> prefix."userdetail WHERE use_id in ( $useridstr ) ";
		$infoUser 		= $this -> db ->  select_info($sql_queryuser);
		
		$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"] 	= $this -> helper -> get_rank( $uval["usd_rank"] , $type = 'all' );

		}
		
		$webUser = $_SESSION["webUser"];
		$webUser["usd_id"] = $_SESSION["webUser"]["use_id"];
		
		$this -> view ->assign('webUser', $webUser);
		
		$this -> view ->assign('infoUser', $infoUserArray);
		
		$this -> view ->assign('nowaddress', $this -> nowaddress );

		$this -> view ->assign('threadInfo', $threadInfo);
		
		$this -> view ->assign('page_num', $page_num);
		
		$this -> view -> display('thread_tpl.html');
		
		
	}
	
	function add()
	{
		$idval 	= $this -> input -> req["id"];
		

		$GLOBALS['WebSe'] -> isLoginUser( $getUrl = "&backurl=".urlencode(BASE_WEBURL."/?thread.html&type=add&id=".$idval) );
		
		
		$sql_query = " SELECT  * FROM  ".$this -> db -> prefix."forum  WHERE for_id = ".$idval." GROUP BY for_id ORDER BY for_id desc  ";
		$info 	  = $this -> db ->  select_one($sql_query);

		

		$this -> nowaddress .= ' > <a href="'.BASE_WEBURL.'?/forum.html&type=infolist&id='.$idval.'">'.$info["for_name"].'</a>';
		
		$this -> view -> assign('nowaddress',$this -> nowaddress);

		$this -> view -> assign('info',$info);
		
		$this -> view -> assign('idval',$idval);
		
		$this -> helper -> load_ckeditor( 'thr_desc','','Basic','','510' );	//加载文本编辑器
		
		$this -> view -> display('threadadd_tpl.html');
		
	}
	
	
	function add_info()
	{
		
		$thr_titleval 	= $GLOBALS['Reque'] -> funpost("thr_title");
		$thr_descval  	= $GLOBALS['Reque'] -> delContent($_REQUEST["thr_desc"]);
		$for_idval 		= $GLOBALS['Reque'] -> funpost("for_id");
		$thr_uptimeval 	= $thr_addtimeval = time();
		
		$thr_ipval 		= $this -> helper -> get_ip();
		$use_idval 		= $_SESSION["webUser"]["use_id"];
		
		$thr_enabledval = 1;
		$insertsql ="INSERT INTO `".$this -> db -> 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')"; 
		$this -> db -> query_sql( $insertsql );
		
		//添加积分
		$updatethread = "UPDATE  `".$this -> db -> prefix."userdetail` SET usd_points = usd_points + 5 ,usd_rank = usd_rank + 5 WHERE  use_id = ".$_SESSION["webUser"]["use_id"]." ";
		$this -> db -> query_sql( $updatethread );
		

		$this -> view -> assign('info_title',$_LANG['bd_addsusse']);
		$this -> view -> assign('URL_TIME',URL_TIME);
		$this -> view -> assign('get_url',BASE_WEBURL.'?/forum-'.$for_idval.'.html');
		$this -> view -> display('suggestion.tpl');
	}
	
	
	function del()
	{
		$idval 	= $this -> input -> req["id"];
 		$foridval  = $this -> input -> req["forid"];
		
		$onesql ="select count(1) AS RENUM FROM `".$this -> db -> prefix."reply` WHERE thr_id = ".$idval ; 
		$oneInfo = $this -> db -> select_one( $onesql );
		
 		if( $oneInfo["RENUM"] > 0 )
		{
 
			$this -> view -> assign('info_title',$_LANG['bd_nodelsusse']);
			$this -> view -> assign('URL_TIME',URL_TIME);
			$this -> view -> assign('get_url',BASE_WEBURL.'?/thread-'.$idval.'.html');
			$this -> view -> display('suggestion.tpl');
		}else{
		
			$delthread = "DELETE FROM `".$this -> db -> prefix."thread` WHERE `thr_id` = ".$idval." and use_id = ".$_SESSION["webUser"]["use_id"]." LIMIT 1 ";
			$this -> db -> query_sql( $delthread );
			
			//添加积分
			$updatethread = "UPDATE  `".$this -> db -> prefix."userdetail` SET usd_points = usd_points - 5 ,usd_rank = usd_rank - 5 WHERE  use_id = ".$_SESSION["webUser"]["use_id"]." ";
			$this -> db -> query_sql( $updatethread );
			
			$this -> view -> assign('info_title',$_LANG['bd_delsusse']);
			$this -> view -> assign('URL_TIME',URL_TIME);
			$this -> view -> assign('get_url',BASE_WEBURL.'?/forum-'.$foridval.'.html');
			$this -> view -> display('suggestion.tpl');
		}
	}
	
	function editinfo()
	{
		$idval 	= $this -> input -> req["id"];
		
		$GLOBALS['WebSe'] -> isLoginUser( $getUrl = BASE_WEBURL.'?/thread-'.$idval.'.html' );

		$sql_query = " SELECT  * FROM  ".$this -> db -> prefix."thread  WHERE thr_id = ".$idval." GROUP BY thr_id ORDER BY thr_id desc  ";
		
		$info 	  = $this -> db ->  select_one($sql_query);

		$this -> nowaddress .= ' > <a href="'.BASE_WEBURL.'?/thread-'.$idval.'.html">'.$info["for_name"].'</a>';
		
		$this -> view -> assign('nowaddress',$this -> nowaddress);

		$this -> view -> assign('info',$info);
		
		$this -> view -> assign('idval',$idval);
		
		$this -> helper -> load_ckeditor( 'thr_desc',$info["thr_desc"],'Basic','','510' );	//加载文本编辑器
		
		$this -> view -> display('threadedit_tpl.html');
	}
	
	function edit()
	{
		$idval 			= $this -> input -> req["thrid"];
		$thr_titleval 	= $this -> input -> req["thr_title"];
		$thr_descval	= $GLOBALS['Reque'] -> delContent($_REQUEST["thr_desc"]);
		
		$GLOBALS['WebSe'] -> isLoginUser( $getUrl = BASE_WEBURL.'?/thread-'.$idval.'.html' );
		
		$updatethread = "UPDATE  `".$this -> db -> prefix."thread` SET thr_title = '".$thr_titleval."' ,thr_desc = '".$thr_descval."'  WHERE `thr_id` = ".$idval." and use_id = ".$_SESSION["webUser"]["use_id"]."  ";
		
		$this -> db -> query_sql( $updatethread );
		
		$this -> view -> assign('info_title',$_LANG['bd_editsusse']);
		$this -> view -> assign('URL_TIME',URL_TIME);
		$this -> view -> assign('get_url',BASE_WEBURL.'?/thread-'.$idval.'.html');
		$this -> view -> display('suggestion.tpl');
	}
	
	
}


$Thread = new Thread();

if($GLOBALS['Webur'] -> getval["type"]=='add')
{
	$Thread -> add();
}else if($GLOBALS['Webur'] -> getval["type"] == 'add_info')
{
	$Thread -> add_info();
}else if($GLOBALS['Webur'] -> getval["type"] == 'edit')
{
	$Thread -> edit();
}else if($GLOBALS['Webur'] -> getval["type"] == 'editinfo')
{
	$Thread -> editinfo();
}else if($GLOBALS['Webur'] -> getval["type"] == 'del')
{
	$Thread -> del();
}else if(intval($GLOBALS['Webur'] -> getval["idval"]) != '')
{
	$Thread -> infolist();
}else{
	$Thread -> index();
}
?>