<?php
class extentClassAction extends Action
{
	public $rows	= array();
	
	public function initAction()
	{
		$this->table	= $this->T('menu');
	}
	
	public function defaultAction()
	{
		$this->data['title']	= '权限管理';
		$type					= $this->rock->get('type');
		switch($type){
			case 'um';
				$this->getmenu(0,1);
				$urows	= $this->getuser();
			break;	
			case 'gm';
				$this->getmenu(0,1);
				$urows	= $this->getgroup();
			break;
			case 'mu';
				$this->getmenu(0,0);
				$urows	= $this->getuser(1);
			break;
			case 'mg';
				$this->getmenu(0,0);
				$urows	= $this->getgroup(1);
			break;
			case 'view';
				$this->getmenu(0,1);
				$urows	= $this->getuser();
			break;
		}
		$this->smartydata['rows']		= $this->showrows($this->rows,"name,栏目名称,left@num,编号@url,链接地址@ispira,权限验证,center");;
		$this->smartydata['urows']		= $urows;
		$this->smartydata['type']		= $type;
	}
	
	public function getuser($lx=0)
	{
		$rs	= $this->db->getall("select * from `".PREFIX."admin` where `status`=1");
		foreach($rs as $k=>$v){
			if($lx == 1){
				$rs[$k]['name']	= '<input name="usercheckbox" value="'.$v['id'].'" class="checkbox" type="checkbox">'.$v['name'];
			}
		}
		return $this->showrows($rs,"name,姓名,left@user,用户名");
	}
	
	public function getgroup($lx=0)
	{
		$rs	= $this->db->getall("select * from `".PREFIX."group`");
		foreach($rs as $k=>$v){
			if($lx == 1){
				$rs[$k]['name']	= '<input name="groupcheckbox" value="'.$v['id'].'" class="checkbox" type="checkbox">'.$v['name'];
			}
			$rs[$k]['ispir']	= $this->rock->checkshow($v['ispir']);
		}
		return $this->showrows($rs,"name,组名,left@ispir,权限检测,center");
	}
	
	public function getmenu($pid,$lx=0)
	{
		$menu	= $this->db->getall("select `name`,`id`,`icons`,`url`,`num`,sort,`level`,`status`,`ispir`,(select count(1) from `$this->table` where `pid`=a.`id` and `status`=1)as `stotal` from `$this->table` a where `pid`='$pid' and `status` =1 order by `sort`");
		foreach($menu as $k=>$rs){
			$level	= $rs['level'];
			$name	= $rs['name'];
			$leav	= '';
			for($i=1;$i<$level;$i++)$leav.='<img src="images/white.gif" width="16" height="16" align="absmiddle">';
			if($level==1){
				if($lx == 1){
					$name	= '<input name="menucheckbox" value="'.$rs['id'].'" class="checkbox" type="checkbox">'.$name;
				}else{
					$name	= '<img src="images/white.gif" class="folderIcon" width="16" height="16" align="absmiddle">'.$name;
				}
			}else{
				if($lx == 1){
					$name	= ''.$leav.'<input name="menucheckbox" value="'.$rs['id'].'" class="checkbox" type="checkbox">'.$name;
				}else{
					$name	= ''.$leav.'<img src="images/white.gif" class="leafIcon" width="16" height="16" align="absmiddle">'.$name;
				}
			}
			$icons			= $rs['icons'];
			$rs['name']		= $name.'';
			$rs['statusa']	= $this->rock->checkshow($rs['status']);
			$rs['ispira']	= $this->rock->checkshow($rs['ispir']);
			$rs['iconsa']	= '<img src="images/white.gif" width="16" height="16" style="background-color:#222222" class="'.$icons.'">';
			if($this->rock->isempt($icons))$rs['iconsa']='';
			$this->rows[]	= $rs;
			$stotal			= $rs['stotal'];
			if($stotal>0){
				$this->getmenu($rs['id'],$lx);
			}
		}
	}
	
	/**
		保存
	*/
	public function saveAjax()
	{
		$type		= $this->rock->post('type');
		$mid		= $this->rock->post('mid');
		$checkaid	= $this->rock->post('checkaid');
		if($type == 'um'){
			$this->extentclear($mid);
		}else{
			$this->db->delete($this->T('sjoin'), "`type`='$type' and `mid`='$mid'");
		}
		$ntable		= '';
		$msg		= '';
		switch($type){
			case 'um';
				$ntable = ''.PREFIX.'menu';
			break;	
			case 'gm';
				$ntable = ''.PREFIX.'menu';
			break;
			case 'mu';
				$ntable = ''.PREFIX.'admin';
			break;
			case 'mg';
				$ntable = ''.PREFIX.'group';
			break;
		}
		if($ntable != ''){
			$this->db->insert($this->T('sjoin'),'`type`,`mid`,`sid`,`indate`',"select '$type','$mid',`id`,'$this->now' from `$ntable` where `id` in($checkaid)",true);
		}
		if($msg=='')$msg='success';
		echo $msg;
	}
	
	private function extentclear($uid)
	{
		$this->db->delete($this->T('sjoin'), "( (`type` in ('um','uu','ut') and `mid`='$uid') or (`type`='mu' and `sid`='$uid') )");
	}
	
	/**
		获取权限信息
	*/
	function getextentAjax()
	{
		$type		= $this->rock->post('type');
		$mid		= $this->rock->post('mid');
		$ntable		= '';
		$s			= '[0]';
		
		//权限查看的
		if($type == 'view' || $type == 'um'){
			$s.=$this->getuserext($mid);
		}else{
			$rsa	= $this->db->getall("select `sid` from `".PREFIX."sjoin` where `type`='$type' and `mid`='$mid'");
			foreach($rsa as $rs)$s.=',['.$rs['sid'].']';
		}
		echo $s;
	}

	/**
		查看人员菜单权限
	*/	
	function getuserext($uid)
	{
		$gasql	= " ( id in( select `sid` from `".PREFIX."sjoin` where `type`='ug' and `mid`='$uid') or id in( select `mid` from `".PREFIX."sjoin` where `type`='gu' and `sid`='$uid') )";//用户所在组id
		$gsql	= "select `id` from `".PREFIX."group` where $gasql ";
		$owhe	= " and (`id` in(select `sid` from `".PREFIX."sjoin` where ((`type`='um' and `mid`='$uid') or (`type`='gm' and `mid` in($gsql) ) ) ) or `id` in(select `mid` from `".PREFIX."sjoin` where ((`type`='mu' and `sid`='$uid') or (`type`='mg' and `sid` in($gsql) )) ))";
		if($this->db->rows(''.PREFIX.'group',"`ispir`=0 and $gasql")>0)$owhe=''; 	//不用权限验证的用户
		$guid	= '';
		$arss	= $this->db->getall("select `id` from `$this->table` where (`status` =1 $owhe) or (`status` =1 and `ispir`=0) order by `sort`");
		foreach($arss as $ars){
			$guid.=',['.$ars['id'].']';
		}
		return $guid;
	}
}