<?php 
/*
　* @$Id
　* @
　* @Ver: 2.0
　* @Create: 2009-11-18 8:10
　* @Modify: 
　* @Author: CPWEB http://www.cpweb.net.cn
　* @Copyright (C) 2008 www.cpweb.net.cn. All Rights Reserved.
　* @License CPWEB is free software and use is subject to license terms
　*/
class GroupsAction extends GlobalAction 
{
	/*function _initialize()
	{
		parent::_initialize();
		$this->_checkSecurity('usergroup');
	}*/
	public $permissions = array();
	public $permissions_set = array();
	public function index()
	{
		$MeGroups=D("MeGroupsView");
		$list=$MeGroups->where($map)->findAll();
		if($list!==false){
			$this->assign('list',$list);
		}
		$this->display();
	}
	public function adds()
	{
		$MeGroups=M("MeGroups");
		$Type=M('Type');
		$map['type_values']='zidingyi_group';
		$typegroups=$Type->where($map)->find();
		$data=$_POST;
		//添加用户组
		$groupsadd['name']=$data['name'];
		$groupsadd['description']=$data['description'];
		$groupsadd['type_id']=$typegroups['id'];
		$megroups=$MeGroups->add($groupsadd);
		$Permissions=M('Permissions');
		if($megroups){
			//设置用户组权限
			$mep['type_values']='usergroup';
			$type=$Type->where($mep)->find();
			$mdp['type_values']='xt';
			$typed=$Type->where($mdp)->find();
			foreach($data AS $key => $value){
				if($key!=='think_html_token' && $key!=='Submit2' && $key!=='description' && $key!=='name'){
					//添加设置
					$dataadd['type_id_i']=$typed['id'];
					$dataadd['value_i']=$key;
					$dataadd['type_id_ii']=$type['id'];
					$dataadd['value_ii']=$megroups;
					$dataadd["valueing"]=$value;
					$Permissions->add($dataadd);
				}
        	}
			$this->assign('jumpUrl',__URL__);
			$this->success(L('CPWEB_SUCCESS_ADD'));
		}else{
			$this->error(L('CPWEB_ERROR_ADD'));
		}
	}
	public function edit()
	{
		$id=intval($_GET["id"]);
		if (!$id) $this->error(L('CPWEB_SELECT_NOT_EXIST'));
		//if (in_array($id,array(1,2),true))$this->error('内置用户组不可编辑');
		$MeGroupsView=D("MeGroupsView");
		$megrmap['MeGroups.id']=$id;
		$list=$MeGroupsView->where($megrmap)->find();
		if (!$list) $this->error(L('CPWEB_SELECT_NOT_EXIST'));
		//获取用户组权限
		$Permissions = M('Permissions');
		$map['value_ii'] = $id;
		$permissions = $Permissions->where($map)->findAll();
		foreach ($permissions AS $key ){
			$this->assign($key['value_i'],$key['valueing']);
		}
		$this->assign('list',$list);
		$this->display();
	}	
	public function edits()
	{
		$id=intval($_POST['id']);
		$MeGroups=M("MeGroups");
		$mep['id']=$id;
		$megroupsview=$MeGroups->where($mep)->find();
		$data=$_POST;
		//编辑用户组
		$groupssave=$megroupsview;
		$groupssave['name']=$data['name'];
		$groupssave['description']=$data['description'];
		$megroups=$MeGroups->save($groupssave);
		$Permissions=M('Permissions');
		$Type=M('Type');
		//设置用户组权限
		$type_mep['type_values']='usergroup';
		$type=$Type->where($type_mep)->find();
		$type_map['type_values']='xt';
		$typed=$Type->where($type_map)->find();
		$perm_map['value_ii']=$id;
		$permissions=$Permissions->where($perm_map)->findall();
		$perm=array();
		foreach ($permissions AS $key ){
			$perm[$key['value_i']]=$key;
		}
		foreach($data AS $key => $value) {
			if($key!=="__hash__" && $key!=='Submit2' && $key!=='description' && $key!=='name' && $key!=='id'){
				if(array_key_exists($key,$perm)){
					//修改权限
					$datasave['id']=$perm[$key]['id'];
					$datasave['type_id_i']=$perm[$key]['type_id_i'];
					$datasave["value_i"]=$perm[$key]['value_i'];
					$datasave['type_id_ii']=$perm[$key]['type_id_ii'];
					$datasave['value_ii']=$perm[$key]['value_ii'];
					$datasave["valueing"]=$value;
					$Permissions->save($datasave);
				}else{
					//添加权限
					$dataadd['type_id_i']=$typed['id'];
					$dataadd['value_i']=$key;
					$dataadd['type_id_ii']=$type['id'];
					$dataadd['value_ii']=$id;
					$dataadd["valueing"]=$value;
					$Permissions->add($dataadd);
				}
			}
       	}
	$this->assign('jumpUrl',__URL__);
	$this->success(L('CPWEB_SUCCESS_EDIT'));
	}
	public function submit(){
		$id=$_REQUEST['id'];
		//删除组之前将属于此组用户更新到"禁止访问"组，防止用户用户属于非法用户组
		//用户组删除后，所属这个组的用户将会转移到“禁止访问”用户组
		$ma['me_groups_id']=$id;
		$U=M('MeStatus')->count($ma);
		if($U) $this->error(L('CPWEB_ERROR_DELETE_MEMBER'));
		$Permissions = M('Permissions');
		$map['value_ii'] = $id;
		$permissions=$Permissions->where($map)->findAll();
		if($permissions){
			foreach($permissions AS $key => $value){
				if($Permi){
					$Permi=$Permi.','.$value['id'];
				}else{
					$Permi=$value['id'];
				}
        	}
			$Permissions->delete($Permi);
		}
		//更新成功后删除指定数据
		$this->_subAction('MeGroups');
	}
}
?>