<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');
class Shop_catalog extends Controller
{
	
	function Shop_catalog()
	{
		parent::Controller();
		check_is_login();
		$this->load->database();
		$this->load->model("shop_admin/Shop_catalog_model");
		$this->load->library('form_validation');
	}
	
	function index()
	{
		$data=array(
			'catalog_list'=>$this->Shop_catalog_model->get_records()
		);
		$this->load->view(TPL_FOLDER."Shop_catalog_list",$data);
	}
	
	function make_shop_cache()
	{
		$this->db->select('cat_name,cid');
		$this->db->where('parent_id',0);
		$this->db->order_by('seqorder','ASC');
		$query = $this->db->get('shop_catalog');
		$catalog = array();
		if($query->num_rows() > 0)
		{
			$catalog = $query->result_array();
		}
		$str = '$config["shop_catalog"] = ';
		$str .= var_export($catalog,true);
		$str .= ';';
		$this->load->helper('file');
		$string = read_file(APPPATH.'config/shop_catalog_config'.EXT);
		$reg = '/\$config\[\"shop_catalog\"\].+;/is';
		preg_match_all( $reg ,$string , $out  );
		if(isset($out[0][0]))
		{
			$string = str_replace($out[0][0],$str,$string);
			write_file(APPPATH.'config/shop_catalog_config'.EXT, $string);
			$tx_msg="<li>店铺类目缓存生成成功.</li>";
			echo_msg($tx_msg,'','yes');
		}
		else
		{
			$tx_msg="<li>店铺类目缓存生成失败.</li>";
			echo_msg($tx_msg,'','no');
		}
	}
	
	function sort_record()
	{
		$this->form_validation->set_rules("rd_id","排序项","required");
		if($this->form_validation->run()==FALSE)
		{
			$tx_msg=validation_errors();
			echo_msg($tx_msg);
		}

		if($this->Shop_catalog_model->sort_record())
		{
			
			$tx_msg="<li>记录排序成功.</li>";
			echo_msg($tx_msg,'','yes');
		}
		else
		{
			$tx_msg="<li>记录排序失败.</li>";
			echo_msg($tx_msg);
		}
	}
	
	function del_record()
	{
		$this->form_validation->set_rules("rd_id","删除的记录","required");
		if($this->form_validation->run()==FALSE)
		{
			$tx_msg=validation_errors();
			echo_msg($tx_msg);
		}
		if($this->Shop_catalog_model->del_record())
		{
			
			$tx_msg="<li>记录删除成功.</li>";
			echo_msg($tx_msg,'','yes');
		}
		else
		{
			$tx_msg="<li>记录删除失败.</li>";
			echo_msg($tx_msg);
		}
	}
}
?>