<?php
if (!defined('HUCART_COM')) exit('HuCart.COM No direct script access allowed');
class Thread
{
	function Thread()
	{
		//$GLOBALS['WebSe'] -> isOnLine();
		//$this -> view -> assign('act_url',ADMIN_WEBURL."/index.php?/login/actlogin");
		
	}
	
	
	function info_list(  )
	{
		$page_size = 20 ;		$page_num_val 	= $this -> input ->  req["page_num"];
		
		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 ;
		}
		
		if($this -> input ->  req["keyword"] !='')
		{   
			$keywordval 	= $this -> input ->  req["keyword"];
			$keywordval  	= urldecode($keywordval); 
		}
		if($keywordval!='')
		{ 	
			//$endArray["keyword"] =$keywordval;
			
			$endurl 	   .= '&keyword='.urlencode($keywordval);
			$whereadd .= " AND ( thr_title LIKE  '%".$keywordval."%' )";  
		}
		
		$for_idval 		= $this -> input ->  req["for_id"];
		
		if($for_idval!='' and $for_idval !='A' )
		{ 
			
			//$endArray["for_id"] = $for_idval;
			 
			$whereadd .= " AND ( t.for_id  = ".$for_idval."  )";

		 }else{
		 
		 	$endurl 	   .= '&for_id=A'; 	
		 }
		
		
		Thread::getforum();
		
		$sql_query = " SELECT thr_id, thr_title, thr_file, thr_list ,f.for_name,thr_enabled FROM  ".$this -> db -> prefix."thread as t left join ".$this -> db -> prefix."forum as f on t.for_id = f.for_id   WHERE 1  $whereadd GROUP BY thr_id ORDER BY `thr_id` LIMIT  $limit_num , $page_size ";
		
		$info = $this -> db -> select_info($sql_query);
		
		$sql_query = " SELECT count(1) as LNUM  FROM  ".$this -> db -> prefix."thread as t  WHERE 1  $whereadd  ORDER BY `thr_id`  ";
		
		$infocount = $this -> db -> select_one($sql_query);
		//var_dump();
		$page_str = $this -> page ->get_admin_page( $url = '?load=thread&act=info_list', $page_num  , $page_size  , $result_count = $infocount["LNUM"], $endArray);
		
		$this -> view -> assign('for_idval',$for_idval);
		$this -> view -> assign('keywordval',$keywordval);
		
		
		$this -> view -> assign('page_str',$page_str);
		$this -> view -> assign('info',$info);
		$this -> view -> display('thread.tpl');
		
	}
	
	function add(  )
	{
		//$this -> view ->  caching = true;
		///$this -> view ->  cache_lifetime = 86400 ;  //60 *  60 * 24
		
		$sql_query = " SELECT for_id, for_name,  for_list FROM  ".$this -> db -> prefix."forum  WHERE 1 GROUP BY for_id ORDER BY  for_list , `for_id` LIMIT  100 ";
		
		$infoforum = $this -> db -> select_info($sql_query);
		
		$this -> view -> assign('infoforum',$infoforum);
		
		
 		$this -> helper -> load_ckeditor( 'thr_desc' ,'','Basic' );	//加载文本编辑器  具体描述

		$this -> view -> display('threadadd.tpl');
		
		
	}
	
	function add_info()
	{
		$thr_titleval 	= $this -> input ->  funpost("thr_title");
		$for_idval 		= $this -> input ->  funpost("for_id");
		$thr_descval 	= $this -> input ->  funpost("thr_desc");
		$thr_fileval 	= $this -> input ->  funpost("thr_file");
		$thr_listval 	= $this -> input ->  funpost("thr_list");
		$thr_enabledval  = $this -> input ->  funpost("thr_enabled");
		
		if($thr_titleval and $thr_descval)
		{
			$sql_query = " SELECT count(1) as LNUM  FROM  ".$this -> db -> prefix."thread  WHERE thr_title = '".$thr_titleval."' and for_id = '".$for_idval."'  ORDER BY `thr_id` ";
			
			$info = $this -> db -> select_one($sql_query);
			
			if( $info["LNUM"] < 1 )
			{
				$insertip =" INSERT INTO `".$this -> db -> prefix."thread` (`thr_title` , `for_id` ,  `thr_desc` , `thr_file` , `thr_list` , `thr_enabled` ) VALUES (  '$thr_titleval', '$for_idval','$thr_descval', '$thr_fileval', '$thr_listval', '$thr_enabledval')";
				
				$this -> db -> query_sql($insertip);
				$this -> view ->  assign('info_title',$this->lang['add_success']);
				$this -> view ->  assign('URL_TIME',URL_TIME);
				$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=thread&act=info_list");
				$this -> view ->  display('suggestion.tpl');	
			}else{
				
				$this -> view ->  assign('info_title',$this->lang['err_info_exist']);
				$this -> view ->  assign('URL_TIME',URL_TIME);
				$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=thread&act=add");
				$this -> view ->  display('suggestion.tpl');	
			}
			
		}else{
			
			$this -> view ->  assign('info_title',$this->lang['err_info_missing']);
			$this -> view ->  assign('URL_TIME',URL_TIME);
			$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=thread&act=add");
			$this -> view ->  display('suggestion.tpl');		}
			//var_dump($_REQUEST);
	}
	
	function del( )
	{
		$infoid = $this -> input ->  get["id"];
		$insertip = " DELETE FROM `".$this -> db -> prefix."thread` WHERE `thread`.`thr_id` in ( $indel ) ";
		
		$this -> db -> query_sql($insertip);
		$this -> view ->  assign('info_title',$this->lang['del_success']);
		$this -> view ->  assign('URL_TIME',URL_TIME);
		$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=thread&act=info_list");
		$this -> view ->  display('suggestion.tpl');
	}
	
	function info(  )
	{
		$infoid = $this -> input ->  req["id"];
		
		$sql_query = " SELECT *  FROM  ".$this -> db -> prefix."thread  WHERE thr_id = '".$infoid."' ORDER BY `thr_id` ";
		$info = $this -> db -> select_one($sql_query);
		//var_dump($info);
		$this -> view ->  assign('info',$info);
		
		Thread::getforum();
		
		$this -> view -> assign('asInfo',$asInfo);
		
		$this -> helper -> load_ckeditor( 'thr_desc' ,$info["thr_desc"],'Basic' );	//加载文本编辑器  具体描述
		
		$this -> view ->  display('threadedit.tpl');
	
	}
	
	
	function getforum()
	{
		$sql_query = " SELECT for_id, for_name,  for_list  FROM  ".$this -> db -> prefix."forum  WHERE 1 GROUP BY for_id ORDER BY  for_list , `for_id` LIMIT  100 ";
		
		$infoforum = $this -> db -> select_info($sql_query);
		
		$this -> view -> assign('infoforum',$infoforum);
	}
	
	
	
	function edit_info( )
	{	
	
		$thr_idval 		= $this -> input ->  funpost("thr_id");
		$thr_titleval 	= $this -> input ->  funpost("thr_title");
		$for_idval 		= $this -> input ->  funpost("for_id");
		$thr_descval 	= $this -> input ->  funpost("thr_desc");
		$thr_fileval 	= $this -> input ->  funpost("thr_file");
		$thr_listval 	= $this -> input ->  funpost("thr_list");
		$thr_enabledval  = $this -> input ->  funpost("thr_enabled");
		
		if($thr_titleval !='' and $thr_descval !='' and $thr_idval !='')
		{
			$updatethread = "UPDATE `".$this -> db -> prefix."thread` SET  thr_title = '".$thr_titleval."' , thr_desc = '".$thr_descval."' , thr_file = '".$thr_fileval."', thr_list = '".$thr_listval."',thr_enabled = '".$thr_enabledval."'  WHERE  thr_id = '".$thr_idval."' ";
			
			$this -> db -> query_sql( $updatethread );
			
			$this -> view ->  assign('info_title',$this->lang['edit_success']);
			$this -> view ->  assign('URL_TIME',URL_TIME);
			$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=thread&act=info_list");
			$this -> view ->  display('suggestion.tpl');
				
		}else{
		
			$this -> view ->  assign('info_title',$this->lang['err_info_missing']);
			$this -> view ->  assign('URL_TIME',URL_TIME);
			$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=thread&act=info&id=".$thr_idval);
			$this -> view ->  display('suggestion.tpl');	
		}
		
	}
		
}

//


$Thread = new Thread();

?>