<?php
/*
===========================管理页控制器
*/
class cls_tab_sys_admin_page extends cls_base_table
{
	private $obj_db;
	//构造函数
	function __construct($msg_db="")
	{
		parent::__construct();
		global $obj_db;
		$this->obj_db=$obj_db;
	}
	//保存
	function on_save($arr_fields)
	{
		$arr_return=array("err"=>"","id"=>0);
		(!isset($arr_fields["page_id"]))? $arr_fields["page_id"]=0:$arr_fields["page_id"]=intval($arr_fields["page_id"]);
		//保存操作
		$lng_id=$arr_fields["page_id"];
		unset($arr_fields["page_id"]);//释放id
		if($lng_id<1){
			$arr_return=self::_insert($arr_fields);
		}else{
			$str_where="page_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["page_model_id"])||intval($arr_fields["page_model_id"])<1){
			$arr_return["err"]="请选择页面所属注册模块！";
			return $arr_return;
		}
		$arr_fields["page_model_id"]=intval($arr_fields["page_model_id"]);
		//查看注册模块是否存在
		$obj_result = $this->obj_db->query("SELECT * FROM ".DB_PRE."sys_model where model_id='".$arr_fields["page_model_id"]."'");
		if(!$obj_rs = $this->obj_db->fetch_array($obj_result))
		{
			$arr_return["err"]="选择注册模块不存在！";
			return $arr_return;
		}
		//查看页面是否已经被注册
		$obj_result = $this->obj_db->query("SELECT * FROM ".DB_PRE."sys_admin_page where page_model_id='".$arr_fields["page_model_id"]."' and page_url='".$arr_fields["page_url"]."'");
		if($obj_rs = $this->obj_db->fetch_array($obj_result))
		{
			$arr_return["err"]="管理页已经被注册！";
			return $arr_return;
		}
		$arr_return=parent::on_insert(DB_PRE."sys_admin_page",$arr_fields);
		return $arr_return;
	}
	//修改
	private function _update($arr_fields,$msg_where)
	{
		$arr_return=array("err"=>"","id"=>0);
		if(isset($arr_fields["page_model_id"])&&in_array("page_model_id",$arr_fields)){
			//查看是否修改注册模块
			$arr_fields["page_model_id"]=intval($arr_fields["page_model_id"]);
			$obj_result = $this->obj_db->query("SELECT * FROM ".DB_PRE."sys_admin_page where page_model_id='".$arr_fields["page_model_id"]."' and ".$msg_where);
			if(!$obj_rs = $this->obj_db->fetch_array($obj_result))
			{
				//查看注册模块是否存在
				$obj_result = $this->obj_db->query("SELECT * FROM ".DB_PRE."sys_model where model_id='".$arr_fields["page_model_id"]."'");
				if(!$obj_rs = $this->obj_db->fetch_array($obj_result))
				{
					$arr_return["err"]="选择注册模块不存在！";
					return $arr_return;
				}
			}
		}
		$arr_return=parent::on_update(DB_PRE."sys_admin_page",$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."sys_admin_page","page_id in(".$str_id.")");
		return $arr_return;
	}
	//排序
	function on_sort_act($msgid,$msgact){
		$arr_return=array("err"=>"");
		$obj_result = $this->obj_db->query("SELECT page_model_id,page_sort FROM ".DB_PRE."sys_admin_page where page_id='".$msgid."'");
		if($obj_rs = $this->obj_db->fetch_array($obj_result))
		{
			$lng_sort=$obj_rs["page_sort"];
			$lng_model_id=$obj_rs["page_model_id"];
		}else{
			$arr_return["err"]="排序记录不存在！[id=".$msgid."]";
			return $arr_return;
		}
		self::on_format_sort($lng_model_id);
		if($msgact=="up"){
			$obj_result = $this->obj_db->query("SELECT page_id,page_sort FROM ".DB_PRE."sys_admin_page where page_sort<'".$lng_sort."' and page_model_id='".$lng_model_id."' order by page_sort desc,page_id desc limit 0,1");
		}else{
			$obj_result = $this->obj_db->query("SELECT page_id,page_sort FROM ".DB_PRE."sys_admin_page where page_sort>'".$lng_sort."' and page_model_id='".$lng_model_id."' order by page_sort,page_id limit 0,1");
		}
		if($obj_rs = $this->obj_db->fetch_array($obj_result))
		{
			$lng_sort_new=$obj_rs["page_sort"];
			$this->obj_db->execute("update ".DB_PRE."sys_admin_page set page_sort=".$lng_sort." where page_id='".$obj_rs["page_id"]."'");
			$this->obj_db->execute("update ".DB_PRE."sys_admin_page set page_sort=".$lng_sort_new." where page_id='".$msgid."'");
		}
	}
	//格式化排序
	function on_format_sort($msg_model_id){
		$lng_model_id=intval($msg_model_id);
		//取所有排序相同的记录
		$obj_result = $this->obj_db->query("SELECT page_sort,count(*) as num FROM ".DB_PRE."sys_admin_page where page_model_id='".$lng_model_id."' group by page_sort,page_model_id having num>1 order by page_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_admin_page set page_sort=page_sort+".$lng_num." where page_sort>'".$obj_rs["page_sort"]."' and page_model_id='".$lng_model_id."'");
			$obj_result2 = $this->obj_db->query("SELECT page_id,page_sort FROM ".DB_PRE."sys_admin_page where page_sort='".$obj_rs["page_sort"]."' and page_model_id='".$lng_model_id."' order by page_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_admin_page set page_sort=page_sort+".$lng_sort." where page_id='".$obj_rs2["page_id"]."'");
				}
				$lng_sort++;
			}
		}
	}
	function get_arr_act($msg_act)
	{
		$arr_act=array();
		$arr_x=explode(chr(10),$msg_act);
		foreach($arr_x as $item)
		{
			if($item!=""){
				$arr_y=explode(":",$item);
				$str_x=trim($arr_y[0]);
				$str_y=$arr_y[0];
				if(count($arr_y)>1) $str_y=$arr_y[1];
				$arr_act[$str_x]=$str_y;
			}
		}
		return $arr_act;
	}
}
?>
