<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');
class Hot_keyword extends Controller
{
	function Hot_keyword()
	{
		parent::Controller();
		check_is_login();
		$this->load->database();
	}
	function index()
	{
		$this->load->config('hot_keyword_config');
		$data = array(
			'hot_keyword' => $this->config->item('hot_keyword'),
			'catalog'	=> $this->_parse_catalog(0)
		);
		$this->load->view(TPL_FOLDER."hot_keyword",$data);
	}
	
	function _parse_catalog($id)
	{
		static $catalog_arr=array();
		$this->db->where('parent_id',$id);
		$this->db->order_by('seqorder','asc');
		$query=$this->db->get("shop_industry_catalog");
		foreach($query->result() as $row)
		{
			$catalog_arr[]=$row;
			if($row->is_has_chd==1)
			{
				$this->_parse_catalog($row->id);
			}
		}
		return $catalog_arr;
	}
	
	function save_hot_keyword()
	{
		$hot_keyword = $this->input->post('hot_keyword');
		if( ! is_array($hot_keyword))
		{
			$tx_msg="<li>热门关键词不能为空.</li>";
			echo_msg($tx_msg,'','no');
		}
		$str = '$config["hot_keyword"] = array(';
		$t = '';
		$i = 1;
		foreach($hot_keyword as $v)
		{
			$t = explode('|',$v);
			if(count($hot_keyword) == $i) $str .= 'array("title"=>"'.$t[0].'","title_color"=>"'.$t[1].'","sword"=>"'.$t[2].'","cid"=>"'.$t[3].'")';
			else $str .= 'array("title"=>"'.$t[0].'","title_color"=>"'.$t[1].'","sword"=>"'.$t[2].'","cid"=>"'.$t[3].'"),';
			$i++;
		}
		$str .= ');';
		$this->load->helper('file');
		$string = read_file(APPPATH.'config/hot_keyword_config'.EXT);
		$reg = '/\$config\[\"hot_keyword\"\].+;/is';
		preg_match_all( $reg ,$string , $out  );
		if(isset($out[0][0]))
		{
			$string = str_replace($out[0][0],$str,$string);
			write_file(APPPATH.'config/hot_keyword_config'.EXT, $string);
			$tx_msg="<li>热门关键词修改成功.</li>";
			echo_msg($tx_msg,'','yes');
		}
		else
		{
			$tx_msg="<li>热门关键词修改失败.</li>";
			echo_msg($tx_msg,'','no');
		}
	}
}
?>