<?php
/*
===========================注册模块控制器
*/
class cls_tab_sys_model extends cls_base_table
{
	private $obj_db;
	private $perms;
	//构造函数
	function __construct()
	{
		parent::__construct();
		global $obj_db;
		$this->obj_db=$obj_db;
		$this->perms=array();
		$this->perms["sysmodel"]=array("sys","company","personal","other","article","temp","pay");
	}
	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)
	{
		$arr_return=array("err"=>"","id"=>0);
		(!isset($arr_fields["model_id"]))? $arr_fields["model_id"]=0:$arr_fields["model_id"]=intval($arr_fields["model_id"]);
		//保存操作
		$lng_id=$arr_fields["model_id"];
		unset($arr_fields["model_id"]);//释放id

		$arr_fields["model_updatetime"]=TIME;
		if($lng_id<1){
			$arr_fields["model_addtime"]=TIME;
			$arr_return=self::_insert($arr_fields);
		}else{
			$str_where="model_id='".$lng_id."'";
			$arr_return=self::_update($arr_fields,$str_where);
			$arr_return["id"]=$lng_id;
		}
		return $arr_return;
	}
	//添加
	private function _insert($arr_fields)
	{
		$arr_return=array("err"=>"","id"=>0);
		if(!isset($arr_fields["model_regkey"])||$arr_fields["model_regkey"]==""){
			$arr_return["err"]="请输入注册关键词！";
			return $arr_return;
		}
		//查看模块是否已经被注册
		$obj_result = $this->obj_db->query("SELECT * FROM ".DB_PRE."sys_model where model_regkey='".$arr_fields["model_regkey"]."'");
		if($obj_rs = $this->obj_db->fetch_array($obj_result))
		{
			$arr_return["err"]="模块已经被注册！";
			return $arr_return;
		}
		$arr_return=parent::on_insert(DB_PRE."sys_model",$arr_fields);
		return $arr_return;
	}
	//修改
	private function _update($arr_fields,$msg_where)
	{
		$arr_return=array("err"=>"","id"=>0);
		$arr_return=parent::on_update(DB_PRE."sys_model",$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;
		}
		$str_sysmodel=implode($this->perms["sysmodel"],",");
		$arr_return=parent::on_del(DB_PRE."sys_model","model_id in(".$str_id.") and ',".$str_sysmodel.",' not like concat('%,',model_regkey,',%')");
		return $arr_return;
	}
	//排序
	function on_sort_act($msgid,$msgact){
		$arr_return=array("err"=>"");
		$obj_result = $this->obj_db->query("SELECT model_sort FROM ".DB_PRE."sys_model where model_id='".$msgid."'");
		if($obj_rs = $this->obj_db->fetch_array($obj_result))
		{
			$lng_sort=$obj_rs["model_sort"];
		}else{
			$arr_return["err"]="排序记录不存在！[id=".$msgid."]";
			return $arr_return;
		}
		self::on_format_sort($msgid);
		if($msgact=="up"){
			$obj_result = $this->obj_db->query("SELECT model_id,model_sort FROM ".DB_PRE."sys_model where model_sort<'".$lng_sort."' order by model_sort desc,model_id desc limit 0,1");
		}else{
			$obj_result = $this->obj_db->query("SELECT model_id,model_sort FROM ".DB_PRE."sys_model where model_sort>'".$lng_sort."' order by model_sort,model_id limit 0,1");
		}
		if($obj_rs = $this->obj_db->fetch_array($obj_result))
		{
			$lng_sort_new=$obj_rs["model_sort"];
			$this->obj_db->execute("update ".DB_PRE."sys_model set model_sort=".$lng_sort." where model_id='".$obj_rs["model_id"]."'");
			$this->obj_db->execute("update ".DB_PRE."sys_model set model_sort=".$lng_sort_new." where model_id='".$msgid."'");
		}
	}
	//格式化排序
	function on_format_sort($msg_model_id){
		$lng_model_id=intval($msg_model_id);
		//取所有排序相同的记录
		$obj_result = $this->obj_db->query("SELECT model_sort,count(*) as num FROM ".DB_PRE."sys_model group by model_sort having num>1 order by model_sort desc");
		while($obj_rs = $this->obj_db->fetch_array($obj_result))
		{
			$lng_num=$obj_rs["num"]-1;
			//把大于这个排序的记录后移lng_num位
			$this->obj_db->execute("update ".DB_PRE."sys_model set model_sort=model_sort+".$lng_num." where model_sort>'".$obj_rs["model_sort"]."'");
			$obj_result2 = $this->obj_db->query("SELECT model_id,model_sort FROM ".DB_PRE."sys_model where model_sort='".$obj_rs["model_sort"]."' order by model_sort");
			$lng_sort=0;
			while($obj_rs2 = $this->obj_db->fetch_array($obj_result2))
			{
				if($lng_sort>0){
					//第一个排序不变
					$this->obj_db->execute("update ".DB_PRE."sys_model set model_sort=model_sort+".$lng_sort." where model_id='".$obj_rs2["model_id"]."'");
				}
				$lng_sort++;
			}
		}
	}
	function get_model_path($msg_key){

		$arr_return=array("err"=>"","path"=>"");
		$str_msg=chk_dir_name($msg_key);
		if($str_msg!=""){
			$arr_return["err"]=$str_msg;
		}else{
			$arr_return["path"]=SYS_ROOT."/model/".$msg_key;
		}
		return $arr_return;
	}
}
?>
