<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');
class Cat_url extends Controller
{
	function Cat_url()
	{
		parent::Controller();
		check_is_login();
		$this->load->database();
	}
	
	function index()
	{
		$this->load->config('cat_url_config');
		$data = array(
			'cat_url' => $this->config->item('cat_url'),
			'catalog'	=> $this->_parse_catalog()
		);
		$this->load->view(TPL_FOLDER."cat_url",$data);
	}
	
	function _parse_catalog()
	{
		$this->db->where('parent_id',0);
		$this->db->order_by('seqorder','asc');
		$query=$this->db->get("shop_industry_catalog");
		return $query->result();
	}
	
	function save_cat_url()
	{
		$cat_url = $this->input->post('cat_url');
		if( ! is_array($cat_url))
		{
			$tx_msg="<li>类目名称不能为空.</li>";
			echo_msg($tx_msg,'','no');
		}
		$str = '$config["cat_url"] = array(';
		$t = '';
		$i = 1;
		foreach($cat_url as $v)
		{
			$t = explode('|',$v);
			if(count($cat_url) == $i) $str .= 'array("title"=>"'.$t[0].'","title_color"=>"'.$t[1].'","url"=>"'.$t[2].'")';
			else $str .= 'array("title"=>"'.$t[0].'","title_color"=>"'.$t[1].'","url"=>"'.$t[2].'"),';
			$i++;
		}
		$str .= ');';
		$this->load->helper('file');
		$string = read_file(APPPATH.'config/cat_url_config'.EXT);
		$reg = '/\$config\[\"cat_url\"\].+;/is';
		preg_match_all( $reg ,$string , $out  );
		if(isset($out[0][0]))
		{
			$string = str_replace($out[0][0],$str,$string);
			write_file(APPPATH.'config/cat_url_config'.EXT, $string);
			$tx_msg="<li>类目名称修改成功.</li>";
			echo_msg($tx_msg,'','yes');
		}
		else
		{
			$tx_msg="<li>类目名称修改失败.</li>";
			echo_msg($tx_msg,'','no');
		}
	}
	
	function get_cat_url()
	{
		$cid = $this->input->post('cid');
		$this->load->config('top_config');
		$this->load->library('TopClient','','top');
		$this->load->library('top/TaobaokeCaturlGetRequest','','req');
		$this->req->setCid($cid);
		$this->req->setPid($this->config->item('pid'));
		$resp = $this->top->execute($this->req);
		if($resp)
		{
			if(isset($resp['taobaoke_item']['taobaoke_cat_click_url']))
			echo $resp['taobaoke_item']['taobaoke_cat_click_url'];
			else echo '0';
		}
		else
		{
			echo '0';
		}
	}
}
?>