<?php 
class Cfg_Controller extends Base_Controller {
	function __construct(){
		parent::__construct();
		$this->global	=	new Cfg_Model();
	}
	
	function index(){
		$this->view = new View("cfg/list");
		$this->view->list	=	$this->global->getGlobal()->result_array();
		$this->display();
	}
	
	function add(){
		if($this->input->post()){
			if($this->global->insertGlobal($this->input->post('cfg'))){
				$this->main->message="添加系统配置成功";
				$this->main->message_type="sccuess";
			}
			else{
				$this->main->message="添加系统配置失败";
				$this->main->message_type="sccuess";
			}
			$this->index();
		}
		else{
			$this->view	=	new View("cfg/add");
			$this->main->formAction="cfg/add";
			$this->main->is_validator=true;
			$this->display();
		}
	}
	function delete($cfg_id=false){
		if($this->input->post('select_id')){
			$arrid=$this->input->post('select_id');
		}
		else{
			$arrid[]=$cfg_id;
		}
		$this->global->deleteGlobal($arrid);
		$this->main->message="删除系统配置项成功";
		$this->main->message_type="success";
		$this->index();
	}
	function edit($cfg_id=false){
		if($this->input->post()){
			if($this->global->updateGlobal($this->input->post('cfg'),$this->input->post('cfg_id'))){
				$this->main->message="添加系统配置成功";
				$this->main->message_type="sccuess";
			}
			else{
				$this->main->message="添加系统配置失败";
				$this->main->message_type="sccuess";
			}
			$this->index();
		}
		else{
			//取得指定ID的数据
			$globalobj		=	$this->global->getGlobal($cfg_id)->current();
			$this->view		=	new View("cfg/add");
			$this->view->cfg_id		=	$cfg_id;
			$this->main->formAction	=	"cfg/edit";
			$this->view->globalobj	=	$globalobj;
			$this->main->is_validator=true;
			$this->display();
		}
	}
	function base(){
		if(@$this->input->post()){
			$baseCfg	=	$this->input->post("base");
			$this->global->updateBaseCfg($baseCfg);
			$this->main->message="修改网站配置成功";
			$this->main->message_type="success";
			unset($_POST);
			$this->base();
		}
		else{
			$this->view					=	new View("cfg/base");
			$this->view->base			=	$this->global->getBaseCfg();
			$this->main->formAction		=	"cfg/base";
			$this->display();
		}
	}
}