<?php
/*
===========================注册模块控制器
*/
class cls_tab_sys_admin_group extends cls_base_table
{
	private $obj_db;
	//构造函数
	function __construct()
	{
		parent::__construct();
		global $obj_db;
		$this->obj_db=$obj_db;
	}
	//保存
	function on_save($arr_fields)
	{
		$arr_return=array("err"=>"");
		(!isset($arr_fields["group_id"]))? $arr_fields["group_id"]=0:$arr_fields["group_id"]=intval($arr_fields["group_id"]);
		$lng_id=$arr_fields["group_id"];
		unset($arr_fields["group_id"]);//释放id
		if(isset($arr_fields["group_pid"])){
			$arr_fields["group_pids"]=$arr_fields["group_pid"];
			$str_sql="select group_pids from ".DB_PRE."sys_admin_group where group_id='".$arr_fields["group_pid"]."'";
			$obj_result  = $this->obj_db->query($str_sql);
			if($obj_rs=$this->obj_db->fetch_array($obj_result))
			{
				$arr_fields["group_pids"]=$obj_rs["group_pids"].",".$arr_fields["group_pid"];
			}
		}
		$arr_fields["group_updatetime"]=TIME;
		if($lng_id<1){
			$arr_fields["group_addtime"]=TIME;
			$arr_return=self::_insert($arr_fields);
		}else{
			$str_where="group_id='".$lng_id."'";
			$arr_return=self::_update($arr_fields,$str_where);
			$arr_return["id"]=$lng_id;
		}
		if(isset($arr_fields["group_pageact"])){
			//继承
			self::on_inherit($arr_return["id"]);
		}
		return $arr_return;
	}
	//添加
	private function _insert($arr_fields)
	{
		$arr_return=array("err"=>"");
		if(!isset($arr_fields["group_name"])||$arr_fields["group_name"]==""){
			$arr_return["err"]="组名不能为空！";
			return $arr_return;
		}
		if(!isset($arr_fields["group_pid"])) $arr_fields["group_pid"]=0;
		$obj_result = $this->obj_db->query("SELECT * FROM ".DB_PRE."sys_admin_group where group_name='".$arr_fields["group_name"]."' and group_pid='".$arr_fields["group_pid"]."'");
		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_group",$arr_fields);
		return $arr_return;
	}
	//修改
	private function _update($arr_fields,$msg_where)
	{
		$arr_return=parent::on_update(DB_PRE."sys_admin_group",$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_id_next=array();
		$str_sql="select group_id from ".DB_PRE."sys_admin_group where group_pid in (".$str_id.")";
		$obj_result = $this->obj_db->query($str_sql);
		while($obj_rs = $this->obj_db->fetch_array($obj_result))
		{
			$arr_id_next[]=$obj_rs["group_id"];
		}
		if(count($arr_id_next)>0)
		{
			$arr_return=self::on_del($arr_id_next);
		}

		$arr_return=parent::on_del(DB_PRE."sys_admin_group","group_id in(".$str_id.")");
		return $arr_return;
	}
	//权限继承设置
	function on_inherit($msg_id){
		$arr_fields=array();
		$str_sql="select * from ".DB_PRE."sys_admin_group where group_id='".$msg_id."'";
		$obj_result = $this->obj_db->query($str_sql);
		if($obj_rs = $this->obj_db->fetch_array($obj_result))
		{
			//继承上级设置
			$arr_inherit=self::get_inherit_up($obj_rs["group_id"],$obj_rs["group_pid"]);
			$str_pageact="";
			if($arr_inherit["pageact"]!=""&&$obj_rs["group_pageact"]!=""){
				$str_pageact=$arr_inherit["pageact"]."||".$obj_rs["group_pageact"];
			}else if($arr_inherit["pageact"]!=""){
				$str_pageact=$arr_inherit["pageact"];
			}else if($obj_rs["group_pageact"]!=""){
				$str_pageact=$obj_rs["group_pageact"];
			}
			$str_page="";
			if($arr_inherit["page"]!=""&&$obj_rs["group_page"]!=""){
				$str_page=$arr_inherit["page"]."|".$obj_rs["group_page"];
			}else if($arr_inherit["page"]!=""){
				$str_page=$arr_inherit["page"];
			}else if($obj_rs["group_page"]!=""){
				$str_page=$obj_rs["group_page"];
			}
			$str_ids="";
			if($arr_inherit["ids"]!=""&&$obj_rs["group_model_ids"]!=""){
				$str_ids=$arr_inherit["ids"].",".$obj_rs["group_model_ids"];
			}else if($arr_inherit["ids"]!=""){
				$str_ids=$arr_inherit["ids"];
			}else if($obj_rs["group_model_ids"]!=""){
				$str_ids=$obj_rs["group_model_ids"];
			}
			//分解
			$arr_inherit_model_ids=array();
			$arr_inherit_model_page_act=array();
			$arr_inherit_model_page=array();
			$arr_model_page_act=array();
			$arr_x=explode(",",$str_ids);
			foreach($arr_x as $item){
				if(!in_array($item,$arr_inherit_model_ids)){
					$arr_inherit_model_ids[]=$item;
				}
			}
			$arr_x=explode("|",$str_page);
			foreach($arr_x as $item){
				if(!in_array($item,$arr_inherit_model_page)){
					$arr_inherit_model_page[]=$item;
				}
			}
			$arr_fields["group_inherit_model_ids"]=implode(",",$arr_inherit_model_ids);
			$arr_fields["group_inherit_page"]=implode(",",$arr_inherit_model_page);
			$arr_dim_model=explode("||",$str_pageact);
			foreach($arr_dim_model as $item){
				$arr_x=explode("=>",$item);
				$arr_model_page[$arr_x[0]]=array();
				if(count($arr_x)>1){
					$arr_y=explode("|",$arr_x[1]);
					foreach($arr_y as $item_next){
						$arr_z=explode("=",$item_next);
						if(!isset($arr_model_page[$arr_x[0]])) $arr_model_page[$arr_x[0]]=array();
						if(!in_array($arr_z[0],$arr_model_page[$arr_x[0]])){
							$arr_model_page[$arr_x[0]][]=$arr_z[0];
						}
						if(count($arr_z)>1){
							$arr_x_act=explode(",",$arr_z[1]);
							if(!isset($arr_model_page_act[$arr_x[0]][$arr_z[0]])) 
							{
								$arr_model_page_act[$arr_x[0]][$arr_z[0]]=array();
							}
							foreach($arr_x_act as $item){
								if($item=="") continue;
								if(!in_array($item,$arr_model_page_act[$arr_x[0]][$arr_z[0]])){
									$arr_model_page_act[$arr_x[0]][$arr_z[0]][]=$item;
								}
							}
						}
					}
				}
			}
			$arr_model=array();
			foreach($arr_model_page_act as $item=>$key){
				$arr_page=array();
				foreach($key as $item_next=>$key_next){
					$arr_page[]=$item_next."=".implode(",",$key_next);
				}
				if(count($arr_page)>0){
					$str_page=implode("|",$arr_page);
					$arr_model[]=$item."=>".$str_page;
				}
			}
			$arr_fields["group_inherit_pageact"]=implode("||",$arr_model);
			$str_sql="update ".DB_PRE."sys_admin_group set group_inherit_pageact='".$arr_fields["group_inherit_pageact"]."',group_inherit_page='".$arr_fields["group_inherit_page"]."',group_inherit_model_ids='".$arr_fields["group_inherit_model_ids"]."' where group_id='".$msg_id."'";
			$this->obj_db->execute($str_sql);
			//向下继承设置
			self::on_inherit_down($obj_rs["group_id"]);
		}
	}
	//继承上级设置
	function get_inherit_up($msg_id,$msg_pid){
		$arr_return=array("pageact"=>"","page"=>"","ids"=>"");
		$str_sql="select * from ".DB_PRE."sys_admin_group where group_id='".$msg_pid."' and group_inherit_down=1";
		$obj_result = $this->obj_db->query($str_sql);
		if($obj_rs = $this->obj_db->fetch_array($obj_result))
		{
			$arr_return["pageact"]=$obj_rs["group_inherit_pageact"];
			$arr_return["page"]=$obj_rs["group_inherit_page"];
			$arr_return["ids"]=$obj_rs["group_inherit_model_ids"];
		}
		return $arr_return;
	}
	//向下继承设置
	function on_inherit_down($msg_id){
	}
}
?>
