<?php
if (!defined('HUCART_COM')) exit('HuCart.COM No direct script access allowed');
class Message
{
	function Message()
	{
		//$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 ( f.mes_title LIKE  '%".$keywordval."%' )";  
		}

		$listval  = $this -> input ->  get["listval"];
		$listtype = $this -> input ->  get["listtype"];

		$orderbyval = "";
		if($listval!='' and $listtype =='DESC' )
		{
			//$endArray["listtype"] = 'DESC';
			//$endArray["listval"]  = $listval;
			$orderbyval = $listval ."  DESC ";
			$listhtml = '';	//反向排序参数
			
		}elseif($listval!='' )
		{
			$orderbyval = $listval;
			$listhtml = 'DESC';
			
			//$endArray["listtype"] = '';
			//$endArray["listval"]  = $listval;

		}else{
			//$endArray["listtype"] = 'DESC';
			//$endArray["listval"]  = 'mes_addtime';
			$orderbyval = " f.mes_addtime DESC , f.`mes_id` DESC ";	
		}


		$sql_query = " SELECT f.mes_id,f.tus_id, f.mes_title,f.mes_type,f.mes_status,f.mes_content,f.mes_addtime, u.use_name ,u.use_email ,uf.use_name as ufuse_name FROM ".$this -> db -> prefix."message as f left join ".$this -> db -> prefix."user as u on f.tus_id = u.use_id left join ".$this -> db -> prefix."user as uf on f.fus_id = uf.use_id   WHERE 1   $whereadd  ORDER BY $orderbyval LIMIT  $limit_num , $page_size ";
		
		$info = $this -> db -> select_info($sql_query);
		
		//var_dump($info);
		
		$sql_query = " SELECT count(1) as CNUM  FROM  ".$this -> db -> prefix."message as f  WHERE 1  $whereadd ORDER BY f.`mes_id`  ";
		
		$infocount = $this -> db -> select_one($sql_query);

		//var_dump($info);
		
		$page_str = $this -> page ->get_admin_page( $url = '?load=message&act=info_list' , $page_num  , $page_size  , $result_count = $infocount["CNUM"] , $endArray );
		
		
		$this -> view -> assign('listval',$listval);
		$this -> view -> assign('listtype',$listtype);

		$this -> view -> assign('keywordval',$keywordval);
		
		$this -> view -> assign('page_str',$page_str);
		$this -> view -> assign('info',$info);
		$this -> view -> display('message.tpl');
		
	}
	
	/*
	function add(  )
	{
		$this -> view ->  caching = true;
		$this -> view ->  cache_lifetime = 86400 ;  //60 *  60 * 24

		$this -> view -> assign('act_url',ADMIN_WEBURL."/index.php?load=message&act=add_info");
		$this -> view -> display('adminadd.tpl');
	}
	*/
	

	function del( )
	{
		$infoid = $this -> input ->  get["id"];
		
		$insertip 	= "  DELETE FROM `".$this -> db -> prefix."message` WHERE `mes_id` in ( $infoid )  ";
		$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=message&act=info_list");
		$this -> view ->  display('suggestion.tpl');
	}
	
	function info(  )
	{
		$infoid = $this -> input ->  get["id"];
		//SELECT  f.*,r.mes_content as remes_content  FROM message as fm left join message as rm on r.mes_parentid = f.mes_id  WHERE f.parent_id = 0  and  f.mes_id =
		$sql_query = " SELECT f.mes_id,f.tus_id, f.mes_title,f.mes_type,f.mes_status,f.mes_content,f.mes_addtime, u.use_name ,u.use_email ,uf.use_name as ufuse_name FROM ".$this -> db -> prefix."message as f left join ".$this -> db -> prefix."user as u on f.tus_id = u.use_id left join ".$this -> db -> prefix."user as uf on f.fus_id = uf.use_id   WHERE f.mes_id =  ".$infoid;
		$info = $this -> db -> select_one($sql_query);
		$this -> helper -> load_ckeditor( 'mes_content',$info["mes_content"],'Basic' );	//加载文本编辑器
		$this -> view -> assign('act_url',ADMIN_WEBURL."/index.php?load=message&act=edit_info");
		$this -> view ->  assign('info',$info);
		$this -> view ->  display('messageedit.tpl');
	
	}
	
	
	function edit_info( )
	{	
	
		$mes_idval 		= $this -> input ->  funpost("mes_id");
		$mes_titleval 	= $this -> input ->  funpost("mes_title");
		
		$mes_contentval = trim($_REQUEST["mes_content"]);
		
		
		//$_SESSION["emplyeeUser"]["use_id"] =='' or  $_SESSION["webAdmin"]["admin_name"]
		
		$addsetUp = '';
		
		if($mes_titleval !='' and $mes_contentval !='' )
		{
			$updatedata = "UPDATE `".$this -> db -> prefix."message` SET  mes_content = '".$mes_contentval."' , mes_title = '".$mes_titleval."'WHERE   mes_id = ".$mes_idval;
			
			$this -> db -> query_sql( $updatedata );
			
			$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=message&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=message&act=info&id=".$mes_idval);
			$this -> view ->  display('suggestion.tpl');	
		}
		
	}
	
	function add(  )
	{
		$this -> view ->  caching = true;
		$this -> view ->  cache_lifetime = 86400 ;  //60 *  60 * 24

		$this -> helper -> load_ckeditor( 'mes_content','','Basic' );	//加载文本编辑器
		
		$this -> view -> assign('act_url',ADMIN_WEBURL."/index.php?load=message&act=add_info");
		$this -> view -> display('messageadd.tpl');
		
	}
	
	function add_info()
	{
		
		$mes_titleval 		= $this -> input ->  funpost("mes_title");
		$tuse_nameval 		= $this -> input ->  funpost("tuse_name");
		$mes_contentval 	= trim($_REQUEST["mes_content"]);
 
		
		if( $mes_titleval !='' and $tuse_nameval !='' and $mes_contentval !='' )
		{
			$sql_query = " SELECT use_id  FROM  ".$this -> db -> prefix."user  WHERE use_name = '".$tuse_nameval."' ORDER BY `use_id` ";
			$info = $this -> db -> select_one($sql_query);
			
			if( $info["use_id"] > 0 )
			{
				$fus_idval = $_SESSION["emplyeeUser"]["use_id"];
				$tus_idval = $info["use_id"];
				$mes_addtimeval 	= time();
				
				$insertsql =" INSERT INTO `".$this -> db -> prefix."message` (`mes_title` , `mes_content` , `mes_type` ,`tus_id` ,`fus_id` , `mes_addtime` ) VALUES (  '$mes_titleval',  '$mes_contentval', '$mes_typeval','$tus_idval','$fus_idval', '$mes_addtimeval')";
				
				$this -> db -> query_sql($insertsql);
				$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=message&act=info_list");
				$this -> view ->  display('suggestion.tpl');	
			}else{
				
				$this -> view ->  assign('info_title',$_LANG['bd_nousererror']);
				$this -> view ->  assign('URL_TIME',URL_TIME);
				$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=message&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=message&act=add");
			$this -> view ->  display('suggestion.tpl');		}
			//var_dump($_REQUEST);
	}
		
}

//


$Message = new Message();

?>