<?php
!defined("MONKEYMAO") && die("Accessed Defined!");
if(cpchecksubmit('updategroup')){
	$newgrouptitle=trim($newgrouptitle);
	$newid=0;
	if($newgrouptitle){
		$db -> query("INSERT INTO {$tablepre}usergroups (grouptitle, about) VALUES ('$newgrouptitle', '$newabout')");
		$newid = $db -> insert_id();
	}
	

	if(is_array($delete) && count($delete)){
		foreach($delete AS $key => $id){
			$id <=6 && cpmsg("error");
		}
		$ids=idarray($delete);
		$db -> query("DELETE FROM {$tablepre}usergroups WHERE groupid IN($ids)");
		$db -> query("UPDATE {$tablepre}members SET groupid=0 WHERE  groupid IN($ids)");
	}
	cache('usergroups');
	$newid ? cpmsg("succeeded", 'index.php?action=usergroups&edit='.$newid) : cpmsg("succeeded", 'index.php?action=usergroups');
}elseif($edit){
	$edit = ispage($edit) ? $edit : 0;
	$group = $db -> queryarray("SELECT * FROM {$tablepre}usergroups WHERE groupid='$edit'");
	if(!$group){
		cpmsg("error");
	}
	if(cpchecksubmit('upgroup')){
		$db -> query("UPDATE {$tablepre}usergroups SET grouptitle = '$newgrouptitle', allowedvisit = '$newallowedvisit', upbizinfo = '$newupbizinfo', allowdownload = '$newallowdownload', about = '$newabout' WHERE groupid='$edit'");
		cache('usergroups');
		cpmsg("succeeded", "index.php?action=usergroups&edit=$edit");
			
	}else{
		include theme('usergroups_edit', 'themes/admin','admin');
	}
}else{
	$sysgroups = $usergroups = array();
	$query = $db -> query("SELECT groupid, type, grouptitle, about FROM {$tablepre}usergroups");
	while($group = $db -> fetch_array($query)){
		if($group['type'] == 'system'){
			$sysgroups[$group['groupid']] = $group;
		}else{
			$usergroups[$group['groupid']] = $group;		
		}
		
	}

	include theme('usergroups', 'themes/admin','admin');
}
?>