<?php
class Reply
{
	//var $now_address = '';
	function Reply()
	{
		$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.'/bbs/">'.$getbbsconfig["bbsname"].'</a>';
		
	}

	
	function infolist()
	{
		//公司简介
		$idval 		 = $this -> input -> req["id"];
		$page_num_val  = $this -> input -> req["page_num"];
		$page_size = 20 ;
		
		//var_dump($strArray );
		
		if( $page_num_val > 1 )
		{
			$page_num  = $page_num_val;
			$limit_num = ceil( ( $page_num_val -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,d.usd_nick FROM ".$this -> db -> prefix."reply AS r , ".$this -> db -> prefix."userdetail as d    WHERE d.use_id = r.use_id and  r.rep_enabled = 1  and r.thr_id = ".$idval." GROUP BY r.rep_id ORDER BY  r.rep_addtime DESC ,  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 = '/bbs/index.php?/thread/infolist/'.$idval, $page_num  , $page_size  , $result_count = $infocount["CNUM"] );
		
		$this -> view -> assign('page_str',$page_str );
		
		
		$this -> view -> assign('info', $info );


		$sqlinfo = " SELECT thr_id, thr_title,thr_desc,thr_addtime, thr_list  FROM  ".$this -> db -> prefix."thread  WHERE thr_enabled = 1 and thr_id = ".$idval."  ";
		
		$threadInfo = $this -> db -> select_one($sqlinfo);
		
		$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.'/bbs/index.php?/thread/infolist/'.$threadInfo["thr_id"].'/1">'.$threadInfo["thr_title"].'</a>';
		
		$this -> view ->assign('nowaddress', $this -> nowaddress );
		
		//$comInfo = array();
		$this -> view ->assign('threadInfo', $threadInfo);
		
		$this -> view -> display('thread_tpl.html');
		
		
	}
	
	function add()
	{
		$idval 	= $this -> input -> req["id"];
		
		$GLOBALS['WebSe'] -> isLoginUser( $getUrl = BASE_WEBURL.'?/reply.html&type=add&id='.$idval );
		
		
		$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.'?/forum-'.$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( 'rep_desc','','Basic','','510' );	//加载文本编辑器
		
		$this -> view -> display('replyadd_tpl.html');
		
	}
	
	function add_info()
	{
		
		$rep_descval 	= $_REQUEST["rep_desc"];

		$thr_idval 		= $GLOBALS['Reque'] -> funpost("thr_id");
		$rep_uptimeval 	= $rep_addtimeval = time();
		
		$rep_ipval 		= $this -> helper -> get_ip();
		$use_idval 		= $_SESSION["webUser"]["use_id"];
		
		$rep_enabledval = 1;
		
		$insertsql ="INSERT INTO `".$this -> db -> prefix."reply` ( `rep_desc` , `rep_addtime` ,`rep_uptime` , `use_id`, `rep_ip`,`thr_id` ,`rep_enabled`) VALUES (   '$rep_descval', '$rep_addtimeval', '$rep_uptimeval', '$use_idval','$rep_ipval','$thr_idval','$rep_enabledval')"; 
		
		$this -> db -> query_sql( $insertsql );
		
		$upthread = "UPDATE   ".$this -> db -> prefix."thread  SET thr_replycount = thr_replycount + 1 WHERE  thr_enabled = 1 and thr_id = ".$thr_idval." ";
		$this -> db -> query_sql( $upthread );
		
		//添加积分
		$updatethread = "UPDATE  `".$this -> db -> prefix."userdetail` SET usd_points = usd_points + 2 ,usd_rank = usd_rank + 2 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.'?/thread-'.$thr_idval.'.html');
		$this -> view -> display('suggestion.tpl');
	}
	
	
	function del()
	{
		$idval 		= $_REQUEST["id"];
		$thr_idval 	= $_REQUEST["thr_id"];
		
		//var_dump($_SESSION["webUser"]["use_id"]);
		
		$delthread = "DELETE FROM `".$this -> db -> prefix."reply` WHERE `rep_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 - 2 ,usd_rank = usd_rank - 2 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.'?/thread-'.$thr_idval.'.html');
		$this -> view -> display('suggestion.tpl');
		
		//$this -> view -> display('suggestion.tpl');
	}
	

	function editinfo()
	{
		
		$idval 		= $this -> input -> req["id"];
		$thr_idval 	= $this -> input -> req["thr_id"];
		
		$GLOBALS['WebSe'] -> isLoginUser( $getUrl = BASE_WEBURL.'?/reply-'.$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);
		
		
		$sql_queryre = " SELECT  * FROM  ".$this -> db -> prefix."reply  WHERE rep_id  = ".$idval." GROUP BY rep_id ORDER BY rep_id desc  ";
		
		$infore 	  = $this -> db ->  select_one($sql_queryre);

		$this -> nowaddress .= ' > <a href="'.BASE_WEBURL.'?/thread-'.$thr_idval.'.html">'.$info["for_name"].'</a>';
		
		$this -> view -> assign('nowaddress',$this -> nowaddress);
		
		$this -> view -> assign('infore',$infore);
		$this -> view -> assign('info',$info);
		
		$this -> view -> assign('idval',$idval);
		$this -> view -> assign('thr_idval',$thr_idval);
		
		$this -> helper -> load_ckeditor( 'rep_desc',$infore["rep_desc"],'Basic','','510' );	//加载文本编辑器
		
		$this -> view -> display('replyedit_tpl.html');
	}
	
	function edit()
	{
		
		$idval 		= $this -> input -> req["id"];
		$thr_idval 	= $this -> input -> req["thr_id"];
		$rep_desc 	= $_REQUEST["rep_desc"];
		
		$GLOBALS['WebSe'] -> isLoginUser( $getUrl = BASE_WEBURL.'?/thread-'.$thr_idval.'.html&page=1' );
		
		$updatethread = "UPDATE  `".$this -> db -> prefix."reply` SET rep_desc = '".$rep_desc."'  WHERE `rep_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-'.$thr_idval.'.html&page=1');
		$this -> view -> display('suggestion.tpl');

	}
	

}

$Reply = new Reply();

if($GLOBALS['Webur'] -> getval["type"]=='list')
{
	$Reply -> infolist();
}else if($GLOBALS['Webur'] -> getval["type"]=='add')
{
	$Reply -> add();
}else if($GLOBALS['Webur'] -> getval["type"]=='add_info')
{
	$Reply -> add_info();
}else if($GLOBALS['Webur'] -> getval["type"]=='edit')
{
	$Reply -> edit();
}else if($GLOBALS['Webur'] -> getval["type"]=='editinfo')
{
	$Reply -> editinfo();
}else if($GLOBALS['Webur'] -> getval["type"]=='del')
{
	$Reply -> del();
}else if(intval($GLOBALS['Webur'] -> getval["idval"]) != '')
{
	$Reply -> infolist();
}else{
	$this -> error('404');
}

//$this -> error('404');
?>