<?php
require 'inc.php';
$obj_tab_group=new cls_tab_sys_admin_group();
$obj_tab_page=new cls_tab_sys_admin_page();
$arr_model_page=array();
$arr_model_page_act=array();
(isset($get_me_pid))?$get_me_pid=intval($get_me_pid):$get_me_pid=0;

$page_path="<a href=\"javascript:meSetpid('0');\">顶级</a>";
$page_path.=me_path($get_me_pid);

$page_footer_info=<<<HTML
<script>function meSetpid(msgpid){frmSet("me_pid",msgpid);frmExecute('');}</script>
<input type='hidden' name='me_pid' value='$get_me_pid'>
HTML;


if($get_act=="del"){
	$arr_return=$obj_tab_group->on_del($get_id);
	if($arr_return["err"]!="") $obj_msg->alert($arr_return["err"]);
}
if($get_act=="del_sel"){
	$arr_return=$obj_tab_group->on_del($get_groupid);
	if($arr_return["err"]!="") $obj_msg->alert($arr_return["err"]);
}
if($get_act=="sort_up"){
	$arr_return=$obj_tab_group->on_sort_act($get_id,"up");
	if($arr_return["err"]!="") $obj_msg->alert($arr_return["err"]);
}
if($get_act=="sort_down"){
	$arr_return=$obj_tab_group->on_sort_act($get_id,"down");
	if($arr_return["err"]!="") $obj_msg->alert($arr_return["err"]);
}
switch($get_act)
{	
	case 'save':
	{
		$arr_fields=array();
		$arr_article=array("channel"=>array(),"folder"=>array());
		if(isset($get_channel_id)) $arr_article["channel"]=$get_channel_id;
		if(isset($get_folder_id)) $arr_article["folder"]=$get_folder_id;
		$arr_fields["group_article"]=serialize($arr_article);
		$arr_fields["group_id"]=$get_id;
		$arr_fields["group_name"]=$get_group_name;
		$arr_fields["group_pid"]=$get_me_pid;
		$str_inherit_down=0;
		if(isset($get_group_inherit_down)) $str_inherit_down=$get_group_inherit_down;
		$arr_fields["group_inherit_down"]=$str_inherit_down;
		$arr_fields["group_page"]="";
		$arr_fields["group_model_ids"]="";
		if(isset($get_model_id)&&is_array($get_model_id)){
			$arr_fields["group_model_ids"]=implode(",",$get_model_id);
			$int_count=count($get_model_id);
			$arr_group_model=array();
			for($i=0;$i<$int_count;$i++)
			{
				$str_dim_page="get_page_".$get_model_id[$i];
				if(isset($$str_dim_page)){
					$arr_page=$$str_dim_page;
					$arr_fields["group_page"]=implode("|",$arr_page);
					$arr_group_page=array();
					$str_page_act="";
					foreach($arr_page as $item){
						$str_dim_page_act="get_page_act_".$item;
						$str_page_act="";
						if(isset($$str_dim_page_act)){
							$arr_page_act=$$str_dim_page_act;
							$str_page_act="";
							if(isset($arr_page_act)){
								$str_page_act=implode(",",$arr_page_act);
							}
						}
						$str_dim_page_url="get_page_url_".$item;
						$arr_group_page[]=$$str_dim_page_url."=".$str_page_act;
					}
					$str_x=implode("|",$arr_group_page);
					$str_dim_model_regkey="get_model_regkey_".$get_model_id[$i];
					$arr_group_model[]=$$str_dim_model_regkey."=>".$str_x;
				}
			}
			$arr_fields["group_pageact"]=implode("||",$arr_group_model);
		}
		$arr_msg=$obj_tab_group->on_save($arr_fields);
		if(isset($arr_msg["id"])) $get_id=$arr_msg["id"];
		include 'tpl/_msg.tpl.php';
	    break;
	}
	case 'edit':
	{
		$arr_group=array();
		$arr_parent_model_ids=array();
		$arr_parent_model_page=array();
		$arr_parent_model_page_act=array();
		$arr_model=me_get_model_page();
		$obj_result = $obj_db->query("SELECT * FROM ".DB_PRE."sys_admin_group where group_id='".$get_id."'");
		if($obj_rs = $obj_db->fetch_array($obj_result))
		{
			$obj_rs["group_model_ids"]=explode(",",$obj_rs["group_model_ids"]);
			$obj_rs["group_article"]=unserialize($obj_rs["group_article"]);

			$arr_dim_model=explode("||",$obj_rs["group_pageact"]);
			foreach($arr_dim_model as $item){
				$arr_x=explode("=>",$item);
				$arr_model_page[$arr_x[0]]=array();
				$arr_parent_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);
						$arr_model_page[$arr_x[0]][]=$arr_z[0];
						$arr_parent_model_page[$arr_x[0]]=array();
						if(count($arr_z)>1){
							$arr_model_page_act[$arr_x[0]][$arr_z[0]]=explode(",",$arr_z[1]);
							$arr_parent_model_page_act[$arr_x[0]][$arr_z[0]]=array();
						}
					}
				}
			}
			$arr_group = $obj_rs;

			$arr_channel=array();
			$obj_result = $obj_db->query("SELECT channel_id,channel_name FROM ".DB_PRE."article_channel order by channel_sort,channel_id");
			while($obj_rs = $obj_db->fetch_array($obj_result))
			{
				$obj_rs["folder"]=me_folder_next($obj_rs["channel_id"],0,0,$arr_group["group_article"]["folder"]);
				$arr_channel[]=$obj_rs;
			}


			if($arr_group["group_pid"]>0){
				//继承上级权限
				$obj_result = $obj_db->query("SELECT group_inherit_pageact,group_inherit_model_ids FROM ".DB_PRE."sys_admin_group where group_id ='".$arr_group["group_pid"]."' and group_inherit_down=1");
				if($obj_rs = $obj_db->fetch_array($obj_result))
				{
					$arr_x=explode(",",$obj_rs["group_inherit_model_ids"]);
					foreach($arr_x as $item){
						if(!in_array($item,$arr_parent_model_ids)){
							$arr_parent_model_ids[]=$item;
						}
					}
					$arr_dim_model=explode("||",$obj_rs["group_inherit_pageact"]);
					foreach($arr_dim_model as $item){
						$arr_x=explode("=>",$item);
						$arr_parent_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_parent_model_page[$arr_x[0]])) $arr_parent_model_page[$arr_x[0]]=array();
								if(!in_array($arr_z[0],$arr_parent_model_page[$arr_x[0]])){
									$arr_parent_model_page[$arr_x[0]][]=$arr_z[0];
								}
								if(count($arr_z)>1){
									$arr_x_act=explode(",",$arr_z[1]);
									if(!isset($arr_parent_model_page_act[$arr_x[0]][$arr_z[0]])) 
									{
										$arr_parent_model_page_act[$arr_x[0]][$arr_z[0]]=array();
									}
									foreach($arr_x_act as $item){
										if(!in_array($item,$arr_parent_model_page_act[$arr_x[0]][$arr_z[0]])){
											$arr_parent_model_page_act[$arr_x[0]][$arr_z[0]][]=$item;
										}
									}
								}
							}
						}
					}
				}
			}
		}
		include 'tpl/admin_group.edit.tpl.php';
		break;
	}
	case 'add':
	{
		$arr_model=me_get_model_page();
		$get_id=0;
		include "tpl/admin_group.add.tpl.php";
		break;
	}
	default:
	{
		//分页
		$arr_group=array();
		$obj_result = $obj_db->query("SELECT * FROM ".DB_PRE."sys_admin_group where group_pid='".$get_me_pid."' order by group_sort,group_id");
		while($obj_rs = $obj_db->fetch_array($obj_result))
		{
			$arr_group[] = $obj_rs;
		}
		include "tpl/admin_group.tpl.php";

	}
}

function me_get_model_page(){
	global $obj_db,$obj_tab_page;
	global $arr_model_page,$arr_model_page_act;
	$arr_model=array();
	$obj_result = $obj_db->query("SELECT * FROM ".DB_PRE."sys_model order by model_sort,model_id");
	while($obj_rs = $obj_db->fetch_array($obj_result))
	{
		$obj_rs["page"]=array();
		$obj_result2 = $obj_db->query("SELECT * FROM ".DB_PRE."sys_admin_page where page_model_id='".$obj_rs["model_id"]."' order by page_sort,page_id");
		$arr_model_page[$obj_rs["model_regkey"]]=array();
		while($obj_rs2 = $obj_db->fetch_array($obj_result2))
		{
			$obj_rs2["page_act"]=$obj_tab_page->get_arr_act($obj_rs2["page_act"]);
			$obj_rs["page"][]=$obj_rs2;
			$arr_model_page_act[$obj_rs["model_regkey"]][$obj_rs2["page_url"]]=array();
		}
		$arr_model[]=$obj_rs;
	}
	return $arr_model;
}
function me_path($msg_pid)
{
	global $obj_db,$obj_tab_group;
	$str_sql="select group_name,group_pid,group_id from ".DB_PRE."sys_admin_group where group_id='".$msg_pid."'";
	$obj_result = $obj_db->query($str_sql);
	$str_val="";
	if($obj_rs = $obj_db->fetch_array($obj_result))
	{
		$str_val=" -> <a href=\"javascript:meSetpid('".$obj_rs['group_id']."');\">".$obj_rs['group_name']."</a>";
		$str_val=me_path($obj_rs['group_pid']).$str_val;
	}
	return $str_val;

}
function me_folder_next($msg_channelid,$msg_pid=0,$msg_len=0,$arr_sel=array())
{
	global $obj_tab_folder,$obj_db;
	$arr_folder=array();
	$str_sql="select folder_id,folder_name,folder_pid,folder_pids from ".DB_PRE."article_folder where folder_pid=".$msg_pid." and folder_channel_id=".$msg_channelid;
	$obj_result = $obj_db->query($str_sql);
	$str_space="";
	$msg_len++;
	for($i=0;$i<$msg_len;$i++){
		$str_space.="&nbsp;&nbsp;&nbsp;&nbsp;";
	}
	$str_cont="";
	while($obj_rs = $obj_db->fetch_array($obj_result))
	{
		$str_pids=str_replace(",","_",$obj_rs["folder_pids"]);
		$str_checked="";
		if(in_array($obj_rs["folder_id"],$arr_sel)) $str_checked=" checked";
		$str_cont.="<li>".$str_space."<input type='checkbox' name='folder_id[]' value='".$obj_rs["folder_id"]."'".$str_checked." onclick='me_folder_check(this.checked,".$obj_rs["folder_id"].");' id='id_c".$msg_channelid."_".$str_pids."'>".$obj_rs["folder_name"]."</li>";
		$str_cont=$str_cont.me_folder_next($msg_channelid,$obj_rs['folder_id'],$msg_len,$arr_sel);
	}
	return $str_cont;
}
?>
