<?php
if (!defined('HUCART_COM'))  exit('HuCart.COM No direct script access allowed');
class Keyword extends HC_Controller
{
	//注意无须构造函数,使用了构造函数得重构父类 parent::__construct();
	function __construct()
	{
		parent::__construct();
		$this->load_lang('adminsys/keyword');
		$this -> load('admin'); 
		$this -> admin -> online_admin();
		$this -> load_db();
		$this -> load('helper'); //加载辅助类
		//$this -> load_model('art_sort'); //加载产品分类模型
		//$this -> load_model('brand'); 	 //加载品牌模型
		//$this -> load_model('art_type'); 	 //加载类型模型
		//$this-> lang['txt_select'];
	}
	
	function info_list(  )
	{
		$this -> load('page',$this->lang['page']);
		
		$page_num_val 	= $this -> input ->  req["page_num"];
		$keywordval 	= $this -> input ->  req["keyword"];
		
		if($this -> input ->  get["keyword"] !='')
		{   
			$keywordval 	= $this -> input ->  get["keyword"];
			$keywordval  	= urldecode($keywordval); 
		}
		if($keywordval!='')
		{ 	
			//$endArray["keyword"] =$keywordval;
			$whereadd .= " AND (  key_word LIKE  '%".$keywordval."%' )";  
		}
		
		//列表字段显示
		$show_arr = array('key_id' => 1,'key_word'=>0,'key_is_top'=>1,'key_count'=>1,'key_locked'=>0);
		$field_show = $this -> helper -> get_th_list( $show_arr , $list_val , $list_type , $lang_arr = $this->lang['field_list'] );

		
		$sql_query = " SELECT key_id,key_word, key_is_top, key_count,key_locked  FROM  ".$this -> db -> prefix."keyword  WHERE 1 $whereadd GROUP BY key_id ORDER BY `key_id` LIMIT ". $this -> page ->get_limit_val();
		
		$info 		= $this -> db -> select_info($sql_query);
		
		$sql_query 	= " SELECT count(1) as CNUM  FROM  ".$this -> db -> prefix."keyword  WHERE 1 $whereadd ORDER BY `key_id` ";
		$infocount  = $this -> db -> select_one($sql_query);

		$page_str = $this -> page ->get_admin_page( $infocount["CNUM"] );

		$this -> view -> assign('field_show',$field_show);
		
		$this -> view -> assign('keywordval',$keywordval);
		$this -> view -> assign('page_str',$page_str);
		$this -> view -> assign('info',$info);
		$this -> view -> display('keyword.tpl');
		
	}
	
	function add()
	{
		$this -> view -> assign( 'edit_submit', $this-> lang['add_submit']);
		$this -> view -> assign( 'txt_title', $this-> lang['add_title']);
		$this -> view -> assign('act_url',ADMIN_WEBURL."/index.php?load=keyword&act=add_info");
		$this -> view -> display('keyword_edit.tpl');
	}
	
	function add_info()
	{
		$key_wordval 		= $this -> input ->  funpost("key_word");
		$key_lockedval 		= $this -> input ->  funpost("key_locked");
		$key_is_top 		= $this -> input ->  funpost("key_is_top");
		
		if($key_wordval !='' )
		{
			$sql_query = " SELECT count(1) as CNUM  FROM  ".$this -> db -> prefix."keyword  WHERE key_word = '".$key_wordval."' ORDER BY `key_id` ";
			$info = $this -> db -> select_one($sql_query);
			
			if( $info["CNUM"] < 1 )
			{

				$insertip ="INSERT INTO `".$this -> db -> prefix."keyword` (`key_word` ,  `key_locked`,key_is_top  ) VALUES (  '$key_wordval', '$key_lockedval','$key_is_top')";
				
				$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=keyword&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=keyword&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=keyword&act=add");
			$this -> view ->  display('suggestion.tpl');		
		}

	}
	
	function del( )
	{
		$infoid = $this -> input ->  get["id"];
		$sqllock = " SELECT key_id ,key_locked  FROM  ".$this -> db -> prefix."keyword  WHERE key_id in ( $infoid )  ORDER BY key_id  ";
		$infoLock = $this -> db -> select_one($sqllock);
		  
		  
		   if( $infoLock["key_locked"] == 0 )
		   {
			
				$deletesql ="DELETE FROM `".$this -> db -> prefix."keyword` WHERE `key_id` in ( $infoid ) ";
				$this -> db -> query_sql($deletesql);
				
				$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=keyword&act=info_list");
				$this -> view ->  display('suggestion.tpl');
				
			}else{
			
				//被锁定不能删除
				$this -> view ->  assign('info_title',$_LANG['bd_lockerror'] );
				$this -> view ->  assign('URL_TIME',URL_TIME);
				$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=keyword&act=info_list");
				$this -> view ->  display('suggestion.tpl');

			}
			
 
		
	}
	
	function info()
	{
		$infoid = $this -> input ->  get["id"];
		
		$sql_query = " SELECT *  FROM  ".$this -> db -> prefix."keyword  WHERE key_id = '".$infoid."' ORDER BY `key_id` ";
		$info = $this -> db -> select_one($sql_query);
		
		$this -> view -> assign('act_url',ADMIN_WEBURL."/index.php?load=keyword&act=edit_info");
		
		$this -> view -> assign( 'edit_submit', $this-> lang['edit_submit']);
		$this -> view -> assign( 'txt_title', $this-> lang['edit_title']);
		
		$this -> view ->  assign('info',$info);
		$this -> view ->  display('keyword_edit.tpl');
	
	}
	
	
	function edit_info( )
	{	
	
		$key_idval 			= $this -> input ->  funpost("key_id");
		$key_wordval 		= $this -> input ->  funpost("key_word");
		$key_lockedval 		= $this -> input ->  funpost("key_locked");
		$key_is_top 		= $this -> input ->  funpost("key_is_top");

		
		if($key_idval !='' and $key_wordval !='' )
		{

			$updatesql = "UPDATE `".$this -> db -> prefix."keyword` SET  key_word = '".$key_wordval."' ,key_locked = '".$key_lockedval."'  , key_is_top = '".$key_is_top."' WHERE  key_id = '".$key_idval."' ";
			$this -> db -> query_sql( $updatesql );
			
			$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=keyword&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=keyword&act=info&id=".$key_idval);
			$this -> view ->  display('suggestion.tpl');
		}
		
	}
		
}

