<?php
	/****************************************
	DirCMS会员组权限分配词典（位运算）：
	1：read    浏览权限 二进制：001
	2：publish 发布权限 二进制：010
	4：comment 评论权限 二进制：100
	*****************************************/
	!defined('DIRCMS_MANAGE') && exit('Access Denied!');
	$r=cache_read('module.cache.php',DIRCMS_ROOT.'data/cache_module/');
	$r=$r['member']['roleid']; 
	if(!in_array($_roleid,$r) && $_userid!=ADMIN_FOUNDERS)showmsg_nourl('您没有该操作的权限!');

	$action=empty($action)?'manage':trim($action);
	$c=new cache();
	switch($action)
	{
		case 'manage':
			if(isset($do_submit)) 
			{
				$i=0;
				foreach($id as $val)
				{
					$db->update(DB_PRE.'membergroup',array('groupname'=>$groupname[$val],'orderby'=>$orderby[$val]),'id='.intval($val));
					$i++;
				}
				$c->cache_membergroup();
				showmsg($LANG['MANAGE_SUCEESS'],ADMIN_FILE.'?file=membergroup&action=manage');
			}
			$result=$db->fetch_all("SELECT * FROM ".DB_PRE."membergroup ORDER BY ".DB_PRE."membergroup.orderby ASC,id DESC");
			include admin_tlp('membergroup_manage');			
			break;
		case 'add':
			if(isset($do_submit)) 
			{
				if(!$newmembergroup['groupname'])showmsg('会员组名称不能为空!',ADMIN_FILE.'?file=membergroup&action=add');
				$db->insert(DB_PRE.'membergroup',$newmembergroup,true);
				$c->cache_membergroup();
				showmsg($LANG['MANAGE_SUCEESS'],ADMIN_FILE.'?file=membergroup&action=manage');
			}
			include admin_tlp('membergroup_add');
			break;
		case 'disable':
			$isdisabled=intval($isdisabled);
			if($db->query("UPDATE `".DB_PRE."membergroup` SET `".DB_PRE."membergroup`.`isdisabled`={$isdisabled} WHERE `".DB_PRE."membergroup`.`id`=".intval($id)." AND `".DB_PRE."membergroup`.`issystem`=0",true))
			{
				$c->cache_membergroup();
				showmsg($LANG['MANAGE_SUCEESS'],ADMIN_FILE.'?file=membergroup&action=manage');
			}
			else 
			{
				showmsg($LANG['MANAGE_ERROR'],ADMIN_FILE.'?file=membergroup&action=manage');
			}
			break;
		case 'delete':
			$r=$db->fetch_one("SELECT issystem FROM `".DB_PRE."membergroup` WHERE `".DB_PRE."membergroup`.`id`=".intval($id));
			if($r['issystem']==1)showmsg('禁止删除系统自带会员组!',ADMIN_FILE.'?file=membergroup&action=manage',2000);
			$db->query("DELETE FROM `".DB_PRE."membergroup` WHERE `".DB_PRE."membergroup`.`id`=".intval($id),true);
			$db->query("DELETE FROM `".DB_PRE."member` WHERE `".DB_PRE."member`.`id`!=".ADMIN_FOUNDERS." AND `".DB_PRE."member`.`groupid`=".intval($id),true);
			cache_delete('membergroup'.$id.'.cache.php',DIRCMS_ROOT.'data/cache/');
			$c->cache_membergroup();
			showmsg($LANG['MANAGE_SUCEESS'],ADMIN_FILE.'?file=membergroup&action=manage');
			break;
	}
?>