<?php
class Forum
{
	var $now_address = '';
	function Forum()
	{
		$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);

		$getads = $this -> helper -> get_ads( $instr = '1,2,3,4,5,6,7,8,9,10,11' );
		$this -> view -> assign('getads',$getads);
		
		$getuserlink = $GLOBALS['WebSe']->getuserlink();
		//var_dump($getuserlink);
		$this -> view ->assign('getuserlink',$getuserlink);

		$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_val = $this -> input -> req["page_num"];

		$page_size = 30 ;
		
		//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 t.thr_id, t.thr_title,t.thr_addtime, t.thr_list, t.thr_replycount, t.thr_count ,d.usd_nick 	FROM ".$this -> db -> prefix."thread AS t , ".$this -> db -> prefix."userdetail as d    WHERE d.use_id = t.use_id and  t.thr_enabled = 1  and for_id = ".$idval." GROUP BY t.thr_id ORDER BY t.thr_uptime DESC,  t.thr_addtime DESC,  t.thr_list , t.thr_id desc  LIMIT  $limit_num , $page_size ";
		
		$info 		= $this -> db ->  select_info($sql_query);
		
 
		 foreach((array)$info as $key => $val )
		{
			$info[$key]["get_url"] = BASE_WEBURL."/?thread-".$val["thr_id"].".html";	
		}
 
		
		$sql_query 	= " SELECT count(1) as CNUM  FROM  ".$this -> db -> prefix."thread  WHERE thr_enabled = 1 and for_id = ".$idval." and use_id <> '' ORDER BY `for_id`  ";
		
		$infocount 	= $this -> db ->  select_one($sql_query);
		

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

		$this -> view -> assign('page_str',$page_str);
		
		$this -> view -> assign('info',$info);
		
		if($idval > 0){
			$sqlinfo = " SELECT for_id, for_name, for_desc, for_image 	for_list  FROM  ".$this -> db -> prefix."forum  WHERE for_enabled = 1  and  for_id = $idval LIMIT 150  ";
		}else{
			$sqlinfo = " SELECT for_id, for_name, for_desc, for_image 	for_list  FROM  ".$this -> db -> prefix."forum  WHERE for_enabled = 1  LIMIT 150  ";
		}
		$forumInfo = $this -> db -> select_one($sqlinfo);

		$upthread = "UPDATE   ".$this -> db -> prefix."forum  SET for_count = for_count + 1 WHERE  for_enabled = 1 and for_id = ".$idval." ";
		
		$this -> db -> query_sql( $upthread );

		$this -> nowaddress .= ' > <a href="'.BASE_WEBURL.'?/forum-'.$forumInfo["for_id"].'.html" >'.$forumInfo["for_name"].'</a>';
		$forumInfo["get_url"] = BASE_WEBURL.'?/forum-'.$forumInfo["for_id"].'.html';

		$this -> view ->assign('forumInfo',$forumInfo);
		$this -> view ->assign('nowaddress',$this -> nowaddress );
		
		$this -> view -> display('forum_tpl.html');

	}
	
	function Index()
	{
		$getmenu 	 = $this -> helper -> get_menu();

		$getads 	 = $this -> helper -> get_ads( $instr = '1,2,3,4,5,6,7,8,9,10,11' );

		$forumInfo 	 = $this -> foruminfo();

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

		$this -> view ->assign('getuserlink',$getuserlink);
		
		$this -> view -> assign('getmenu',$getmenu);
		$this -> view -> assign('getads',$getads);

		
		$this -> view -> assign('forumInfo',$forumInfo);
		
		$this -> view -> assign('threadInfo',$threadInfo);

		$this -> view -> display('forumindex_tpl.html');
	}

	function foruminfo()
	{
	
	
		$webtitle = '论坛中心';
		//处理你现在的位置
		$now_address = '<a href="'.BASE_WEBURL.'/">首页</a> > <a href="'.BASE_WEBURL.'?/forum.html">论坛中心</a>';
	
		$this -> view -> assign('webtitle',$webtitle);
		
		$this -> view -> assign('now_address',$now_address);
	
		$sqlinfo = " SELECT for_id, for_name, for_desc, for_image 	for_list  FROM  ".$this -> db -> prefix."forum   WHERE for_enabled = 1  LIMIT 150  ";
		
		$info = $this -> db -> select_info($sqlinfo);

		foreach((array)$info as $key => $val )
		{
			$info[$key]["threadinfo"] = $this -> threadinfo( $val["for_id"] );
			$info[$key]["get_url"] 	  = BASE_WEBURL."/?forum-".$val["for_id"].".html";
		}

		return $info;
	}
	
	
	function threadinfo( $forumid = '')
	{

		if($forumid !='')
		{
			$sql = " SELECT t.thr_id, t.thr_title,t.thr_addtime, t.thr_list, t.thr_replycount, t.thr_count ,d.usd_nick 	FROM ".$this -> db -> prefix."thread AS t , ".$this -> db -> prefix."userdetail as d    WHERE d.use_id = t.use_id and  t.thr_enabled = 1  and for_id = $forumid GROUP BY t.thr_id ORDER BY  t.thr_addtime DESC,  t.thr_list , t.thr_id desc  LIMIT 10 ";
			$info = $this -> db -> select_info($sql);


			foreach((array)$info as $key => $val )
			{
			 
				$info[$key]["get_url"] 	  = BASE_WEBURL."/?thread-".$val["thr_id"].".html";
			}
			return $info;
		}
	}
	

}


$Forum = new Forum();

if($GLOBALS['Webur'] -> getval["act"]=='list')
{
	$Forum -> infolist();
}else if(intval($GLOBALS['Webur'] -> getval["idval"]) != '')
{
	$Forum -> infolist();
}else{
	$Forum -> index();
}
//$this -> error('404');
?>