<?php
if ( !eregi("admin.php", $PHP_SELF) ) {
	 die ("Access Denied"); 
}
include_once(XOOPS_ROOT_PATH."/modules/system/admin/groups/groups.php");
include_once(XOOPS_ROOT_PATH."/class/xoopsgroup.php");
if( $xoopsUser->isAdmin($xoopsModule->mid()) ) {
	$op = "display";
	if ( isset($HTTP_POST_VARS) ) {
		foreach ( $HTTP_POST_VARS as $k => $v ) {
			$$k = $v;
  		}
	}
	if ( isset($HTTP_GET_VARS['op']) ) {
		if ( $HTTP_GET_VARS['op'] == "modify" || $HTTP_GET_VARS['op'] == "del" || $HTTP_GET_VARS['op'] == "delConf" ) {
			$op = $HTTP_GET_VARS['op'];
			$g_id = $HTTP_GET_VARS['g_id'];
		}
	}
	switch($op) {
        	case "modify":
                	modifyGroup($g_id);
               	 	break;
        	case "update":
			$admin_mids = empty($admin_mids) ? array() : $admin_mids;
			$read_mids = empty($read_mids) ? array() : $read_mids;
			$read_bids = empty($read_bids) ? array() : $read_bids;
                	updateGroup($g_id, $name, $desc, $admin_mids, $read_mids, $read_bids);
                	break;
        	case "add":
                	if (!$name) {
				xoops_cp_header();
                        	echo _AM_UNEED2ENTER;
				xoops_cp_footer();
                        	exit();
                	}
			$admin_mids = empty($admin_mids) ? array() : $admin_mids;
			$read_mids = empty($read_mids) ? array() : $read_mids;
			$read_bids = empty($read_bids) ? array() : $read_bids;
			$xoopsgroup = new XoopsGroup();
			$xoopsgroup->setVar("name", $name);
			$xoopsgroup->setVar("description", $desc);
			$xoopsgroup->setAdminRightModules($admin_mids);
			array_push($read_mids, 1);
			$xoopsgroup->setReadRightModules($read_mids);
			$xoopsgroup->setReadRightBlocks($read_bids);
			if (!$xoopsgroup->store()) {
				xoops_cp_header();
				echo $xoopsgroup->getErrors();
				xoops_cp_footer();
			} else {
				redirect_header("admin.php?fct=groups&op=adminMain",1,_AM_DBUPDATED);
			}
                	break;
        	case "del":
                	xoops_cp_header();
                	echo "<b> "._AM_DELETEADG."</b><br><br>";
                	printf(_AM_AREUSUREDEL,$name);
                	echo "<table><tr><td>\n";
			echo myTextForm("admin.php?fct=groups&op=delConf&g_id=$g_id", _AM_YES);
			echo "</td><td>\n";
			echo myTextForm("admin.php?fct=groups&op=adminMain" , _AM_NO);
			echo "</td></tr></table>\n";
                	xoops_cp_footer();
                	break;

        	case "delConf":
			$xoopsgroup = new XoopsGroup($g_id);
			$xoopsgroup->delete();
                	redirect_header("admin.php?fct=groups&op=adminMain",1,_AM_DBUPDATED);
                	break;
		case "addUser":
			$size = sizeof($uids);
			for ( $i = 0; $i < $size; $i++ ) {
				$sql = "INSERT INTO ".$xoopsDB->prefix("groups_users_link")." (groupid, uid) VALUES (".$groupid.",".$uids[$i].")";
				$xoopsDB->query($sql);	
			}
			redirect_header("admin.php?fct=groups&op=modify&g_id=".$groupid."",0,_AM_DBUPDATED);
			break;
		case "delUser":
			$size = sizeof($uids);
			for ( $i = 0; $i < $size; $i++ ) {
				$sql = "DELETE FROM ".$xoopsDB->prefix("groups_users_link")." WHERE uid=".$uids[$i]." AND groupid=".$groupid."";
				$xoopsDB->query($sql);	
			}
			redirect_header("admin.php?fct=groups&op=modify&g_id=".$groupid."",0,_AM_DBUPDATED);
			break;
		case "display":
    		default:
			displayGroups();
			break;
	}

} else {
    	echo "Access Denied";
}
?>