<?php
/*
===========================注册模块控制器
*/
class cls_tab_temp_fun extends cls_base_table
{
	private $perms;
	//构造函数
	function __construct()
	{
		parent::__construct();
	}
	function __get($name){
		if(isset($this->perms["$name"])){
			return $this->perms["$name"];
		}
	}
	function __set($name,$value){
		if(isset($this->perms["$name"])){
			$this->perms["$name"]=$value;
		}else{
		}
	}
	//保存
	function on_save($arr_fields)
	{
		global $obj_db;
		$arr_return=array("err"=>"","id"=>0);
		(!isset($arr_fields["fun_id"]))? $arr_fields["fun_id"]=0:$arr_fields["fun_id"]=intval($arr_fields["fun_id"]);
		//保存操作
		$lng_id=$arr_fields["fun_id"];
		unset($arr_fields["fun_id"]);//释放id
		if(isset($arr_fields["fun_perms"]) && is_array($arr_fields["fun_perms"])){
			$arr_fields["fun_perms"]=serialize($arr_fields["fun_perms"]);
		}
		if(isset($arr_fields["fun_name"])){
			if(empty($arr_fields["fun_name"])){
				$arr_return["err"]="请输入名称 !";
				return $arr_return;
			}
			$obj_result=$obj_db->query("select fun_id from ".DB_PRE."temp_fun where fun_folder_id='".$arr_fields["fun_folder_id"]."' and fun_name='".$arr_fields["fun_name"]."' and fun_id!=".$lng_id);
			if($obj_rs=$obj_db->fetch_array($obj_result)){
				$arr_return["err"]="该名称已经存在!";
				return $arr_return;
			}
		}
		if($lng_id<1){
			$arr_return=self::_insert($arr_fields);
		}else{
			global $obj_db;
			$str_where="fun_id='".$lng_id."'";
			$arr_return=self::_update($arr_fields,$str_where);
			$arr_return["id"]=$lng_id;
		}
		if($arr_return["err"]==""){
			$arr_msg=self::me_refresh_fun($arr_return["id"]);
			$arr_return["path"]=$arr_msg["path"];
		}
		return $arr_return;
	}
	function me_refresh_fun($msgid){
		global $obj_db;
		$arr_return=array("path"=>"");
		$obj_result=$obj_db->query("select fun_id,fun_code,fun_folder_id,fun_name from ".DB_PRE."temp_fun where fun_id='".$msgid."'");
		if($obj_rs=$obj_db->fetch_array($obj_result)){
			$str_path=self::get_folders($obj_rs["fun_folder_id"]);
			if(!empty($str_path)) $str_path="/";
			$str_path.=$obj_rs["fun_name"];
			$arr_return["path"]=$str_path;
			$str_path=get_real_path('',TEMPLATES_DIR."/temp/sys/fun/".$str_path.".html");
			file_create($str_path,$obj_rs["fun_code"],1);
			$obj_db->execute("update ".DB_PRE."temp_fun set fun_path='".$arr_return["path"]."' where fun_id=".$msgid);
		}
		return $arr_return;
	}
	function get_folders($msgid){
		global $obj_db;
		$arr_folder=array();
		$arr_return=array();
		$obj_result=$obj_db->query("select folder_name,folder_pids from ".DB_PRE."temp_fun_folder where folder_id='".$msgid."'");
		if($obj_rs=$obj_db->fetch_array($obj_result)){
			if(!empty($obj_rs["folder_pids"]) && $obj_rs["folder_pids"]!=$msgid){
				$arr_x=explode(",",$obj_rs["folder_pids"]);
				$obj_result=$obj_db->query("select folder_name,folder_id from ".DB_PRE."temp_fun_folder where folder_id in(".$obj_rs["folder_pids"].")");
				while($obj_rs=$obj_db->fetch_array($obj_result)){
					$arr_folder["id_".$obj_rs["folder_id"]]=$obj_rs["folder_name"];
				}
				foreach($arr_x as $item){
					if(isset($arr_folder["id_".$item])) $arr_return[]=$arr_folder["id_".$item];
				}
			}else{
				$arr_return[]=$obj_rs["folder_name"];
			}
		}
		$str_path="";
		if(count($arr_return)>0){
			$str_path=implode("/",$arr_return);
		}
		return $str_path;
	}
	//添加
	private function _insert($arr_fields)
	{
		$arr_return=array("err"=>"");
		global $obj_db;
		if(!isset($arr_fields["fun_folder_id"])) $arr_fields["fun_folder_id"]=0;
		$arr_return=array("err"=>"","id"=>0);
		$arr_fields["fun_addtime"]=TIME;
		if(!isset($arr_fields["fun_name"]) || empty($arr_fields["fun_name"])){
			$arr_return["err"]="请输入名称 !";
			return $arr_return;
		}
		$arr_return=parent::on_insert(DB_PRE."temp_fun",$arr_fields);
		return $arr_return;
	}
	//修改
	private function _update($arr_fields,$msg_where)
	{
		$arr_return=array("err"=>"","id"=>0);
		if(isset($arr_fields["fun_name"]) && empty($arr_fields["fun_name"])){
			$arr_return["err"]="请输入目录名称 !";
			return $arr_return;
		}
		$arr_return=parent::on_update(DB_PRE."temp_fun",$arr_fields,$msg_where);
		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=parent::on_del(DB_PRE."temp_fun","fun_id in(".$str_id.")");
		return $arr_return;
	}

}
?>
