<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');
class Make_nav extends Controller
{
	function Make_nav()
	{
		parent::Controller();
		check_is_login();
		$this->load->database();
	}
	function index()
	{
		$this->load->config('top_nav_config');
		$data = array(
			'top_nav' => $this->config->item('top_nav'),
			'catalog'	=> $this->_parse_catalog(0)
		);
		$this->load->view(TPL_FOLDER."make_nav",$data);
	}
	
	function _parse_catalog($id)
	{
		static $catalog_arr=array();
		$this->db->where('parent_id',$id);
		$this->db->order_by('seqorder','asc');
		$this->db->order_by('id','desc');
		$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_nav()
	{
		$nav = $this->input->post('nav');
		if( ! is_array($nav))
		{
			$tx_msg="<li>导航不能为空.</li>";
			echo_msg($tx_msg,'','no');
		}
		$str = '$config["top_nav"] = array(';
		$t = '';
		$i = 1;
		foreach($nav as $v)
		{
			$t = explode('|',$v);
			if(count($nav) == $i) $str .= 'array("title"=>"'.$t[0].'","cid"=>"'.$t[1].'")';
			else $str .= 'array("title"=>"'.$t[0].'","cid"=>"'.$t[1].'"),';
			$i++;
		}
		$str .= ');';
		$this->load->helper('file');
		$string = read_file(APPPATH.'config/top_nav_config'.EXT);
		$reg = '/\$config\[\"top_nav\"\][^\r\n]+;/is';
		preg_match_all( $reg ,$string , $out  );
		if(isset($out[0][0]))
		{
			$string = str_replace($out[0][0],$str,$string);
			write_file(APPPATH.'config/top_nav_config'.EXT, $string);
			$tx_msg="<li>导航修改成功.</li>";
			echo_msg($tx_msg,'','yes');
		}
		else
		{
			$tx_msg="<li>导航修改失败.</li>";
			echo_msg($tx_msg,'','no');
		}
	}
}
?>