<?php
class admin_admingroupControl extends skymvc{
	
	public function __construct(){
		parent::__construct();
		$this->loadConfig('permission');
		
		$this->loadModel(array("admin_group"));
	}
	
	public function onDefault(){
		$zulist=$this->admin_group->select(array("order"=>" id DESC"));
		$this->smarty->assign("zulist",$zulist);
		$this->smarty->display("admin/admin_group.html");
	}
	
	public function onAdd(){
		$id=intval(get('id','i'));
		if($id){
			$zu=$this->admin_group->selectRow(array("where"=>array('id'=>$id)));
			$zups=unserialize($zu['content']);
			$this->smarty->assign("zu",$zu);
		}
		$permission=$this->config_item();
		if($permission){
			foreach($permission as $key=>$val)
			{
				$tmparr=array();
				$chk="";
				$str.="<tr>";
				$str.="<td align='right'>".$key."：</td>";
				$str.="<td>";
				if($zups[$key])
				{
					
					foreach($zups[$key] as $t)
					{
						$tmparr=array_merge($tmparr,array($t)) ;
					}
					
				}
				foreach($val as $k=>$v){
					$chk="";
					if($tmparr){	
						if(in_array($v['access'],$tmparr))
						{
							$chk=" checked='checked' ";
						}
					}
					$str.= " <input type='checkbox' name='ps[".$key."][]' class='percheck' value='".$v['access']."' ".$chk." > ".$v['title']; 
				}
				$str.="</td>";
				$str.= "</tr>";
			}
		}
		$this->smarty->assign("str",$str);
		$this->smarty->display("admin/admin_group_add.html");
	}
	
	public function onSave(){
		$id=get_post('id','i');			
		$p=array();
		$title=post('title','h');
		if(empty($title))
		{
			$this->gomsg($this->lang['admin_group_unempty']);
		}
		$ps=post('ps','h');
		if($ps)
		{
			foreach($ps as $key=>$arr)
			{				
				foreach($arr as $k=>$v)
				{
					$p[$key][]=$v;
				}
				
			}
		}
		$data['title']=$title;
		$data['content']=serialize($p);
		
		if($id){
			$this->admin_group->update($data,array("id"=>$id));
		}else{
			$data['siteid']=intval($_SESSION['ssadmin']['siteid']);
			$this->admin_group->insert($data);
		}
		$this->gomsg($this->lang['edit_success']);
	}
	
	public function onDelete(){
		$id=get('id','i');
		$this->admin_group->delete(array("id"=>$id));
		echo json_encode(array("error"=>0,"message"=>$this->lang['delete_success']));
	}
	
		
		 
}
?>