<?php
/*
===========================用户控制器
^^^^^^^^^^^^^^^^^^成员
------添加(on_insert)
------修改(on_update)
------删除(on_del)

*/
class cls_tab_sys_config extends cls_base_table
{
	private $obj_db;
	private $config=array();
	private $perms=array();
	//构造函数
	function __construct()
	{
		global $obj_db;
		parent::__construct();
		$this->obj_db=$obj_db;
		$this->config["php_path"]=WEB_ROOT."/".DATA_DIR."/config/";
		$this->perms=array();
	}
	/*添加
	arr_fields:关健字数组
	*/
	function _insert($arr_fields)
	{
		$arr_return=parent::on_insert(DB_PRE."sys_config",$arr_fields);
		return $arr_return;
	}
	function _update($arr_fields,$msg_where)
	{
		$arr_return=parent::on_update(DB_PRE."sys_config",$arr_fields,$msg_where);
		return $arr_return;
	}
	function _del($msg_where)
	{
		$str_msginfo=parent::on_del(DB_PRE."sys_config",$msg_where);
		if($str_msginfo=="") self::on_refresh();
		return $str_msginfo;
	}
	function on_save($arr_fields=array())
	{
		$arr_return=array("err"=>"");
		(!isset($arr_fields["config_id"]))? $arr_fields["config_id"]=0:$arr_fields["config_id"]=intval($arr_fields["config_id"]);
		$lng_id=$arr_fields["config_id"];
		unset($arr_fields["config_id"]);//释放id
		$arr_fields["config_updatetime"]=TIME;
		if(is_array($arr_fields["config_val"])) $arr_fields["config_val"]=implode("|",$arr_fields["config_val"]);
			if($lng_id<1){
			$arr_fields["config_addtime"]=TIME;
			$arr_return=self::_insert($arr_fields);
		}else{
			$str_where="config_id='".$lng_id."'";
			$arr_return=self::_update($arr_fields,$str_where);
			$arr_return["id"]=$lng_id;
		}
		if($arr_return["err"]=="") self::on_refresh();
		return $arr_return;
	}
	function on_saveall($arr_all=array())
	{
		$arr_return=array("err"=>"");
		foreach($arr_all as $item){
			$arr_fields=$item;
			(!isset($arr_fields["config_id"]))? $arr_fields["config_id"]=0:$arr_fields["config_id"]=intval($arr_fields["config_id"]);
			$lng_id=$arr_fields["config_id"];
			unset($arr_fields["config_id"]);//释放id
			$arr_fields["config_updatetime"]=TIME;
			if(is_array($arr_fields["config_val"])) $arr_fields["config_val"]=implode("|",$arr_fields["config_val"]);
			if($lng_id>1){
				$str_where="config_id='".$lng_id."'";
				$arr_return=self::_update($arr_fields,$str_where);
			}
		}
		if($arr_return["err"]=="") self::on_refresh();
		return $arr_return;
	}

	function on_del($arr_id)
	{
		$arr_return=array("err"=>"");
		$str_id=get_arr_id($arr_id);
		if($str_id==""){
			$arr_return["err"]="没有指定要删除的记录！";
			return $arr_return;
		}
		$arr_return=self::_del("config_id in(".$str_id.")");
		return $arr_return;
	}
	function on_refresh()
	{
		$obj_result = $this->obj_db->query("SELECT * FROM ".DB_PRE."sys_config"." a inner join ".DB_PRE."sys_model b on a.config_model_id=b.model_id order by config_model_id,config_id");
		while($obj_rs = $this->obj_db->fetch_array($obj_result))
		{
			$obj_rs["config_val"]=str_replace("'","\'",$obj_rs["config_val"]);
			if(!isset($arr_config["id_".$obj_rs["config_model_id"]])){
				$arr_config["id_".$obj_rs["config_model_id"]]["list"]="<?php".chr(10).'$cfg_'.$obj_rs["model_regkey"].'=array('.chr(10);
				$arr_config["id_".$obj_rs["config_model_id"]]["name"]=$obj_rs["model_regkey"];
			}
			if($obj_rs["config_type"]=="array"){
				$obj_rs["config_val"]=str_replace("|",chr(10),$obj_rs["config_val"]);
				$arr_x=explode(chr(10),$obj_rs["config_val"]);
				$str_x="'".implode("','",$arr_x)."'";
				$str_x=str_replace("=&gt;","'=>'",$str_x);
				$str_x=str_replace(chr(13),"",$str_x);
				$arr_config["id_".$obj_rs["config_model_id"]]["list"].="'".$obj_rs["config_name"]."'=>array(".$str_x."),".chr(10);
			}else if($obj_rs["config_type"]=="bool"||$obj_rs["config_type"]=="int"){
				$arr_config["id_".$obj_rs["config_model_id"]]["list"].="'".$obj_rs["config_name"]."'=>".format_number($obj_rs["config_val"]).",".chr(10);
			}else{
				$arr_config["id_".$obj_rs["config_model_id"]]["list"].="'".$obj_rs["config_name"]."'=>'".$obj_rs["config_val"]."',".chr(10);
			}
		}
		foreach($arr_config as $item){
			$str_val=$item["list"].");".chr(10)."?>";
			file_create($this->config["php_path"]."config.".$item["name"].".php",$str_val,1);
		}
	}
	function get_config($msg_v)
	{
		if(isset($this->config[$msg_v])) return $this->config[$msg_v];
	}
	function get_config_model_regkey_default()
	{
		return $this->config["dim_default"];
	}
	function get_config_list($msgval)
	{
		$arr_val=explode(chr(13).chr(10),$msgval);
		$arr_return=array();
		foreach($arr_val as $item)
		{
			$arr_x=explode(":",$item);
			if(count($arr_x)<2) $arr_x[1]=$arr_x[0];
			$arr_return[$arr_x[0]]=$arr_x[1];
		}
		return $arr_return;
	}
	function get_val($msg_mod,$msg_var){
		$str_return="";
		if(isset($this->perms[$msg_mod])){
			if(isset($this->perms[$msg_mod][$msg_var])) $str_return=$this->perms[$msg_mod][$msg_var];
		}else{
			include_once $this->config["php_path"]."config.".$msg_mod.".php";
			$str_var='cfg_'.$msg_mod;
			$this->perms[$msg_mod]=$$str_var;
			if(isset($this->perms[$msg_mod][$msg_var])) $str_return=$this->perms[$msg_mod][$msg_var];
		}
		return $str_return;
	}
	function get_model($msg_mod){
		if(!isset($this->perms[$msg_mod])){
			include_once $this->config["php_path"]."config.".$msg_mod.".php";
			$str_var="cfg_".$msg_mod;
			$this->perms[$msg_mod]=$$str_var;
		}
		return $this->perms[$msg_mod];
	}
}
?>