<?php
if (!eregi("admin.php", $PHP_SELF)) { 
	die ("Access Denied"); 
}
if ( $xoopsUser->isAdmin($xoopsModule->mid()) ) {

/*********************************************************/
/* Admin/Authors Functions                               */
/*********************************************************/

function displayGroups() {
        global $xoopsDB, $xoopsConfig, $xoopsModule;
        xoops_cp_header();
        OpenTable();
        echo "<h4 style='text-align:left'>"._AM_EDITADG."</h4>";
        $result = $xoopsDB->query("SELECT groupid, name, type FROM ".$xoopsDB->prefix("groups")."");
        echo "<table border='0' cellpadding='0' cellspacing='0' valign='top' width='40%'>
<tr><td class='bg2'>
<table width='100%' border='0' cellpadding='4' cellspacing='1'>
";
	$myts =& MyTextSanitizer::getInstance();

        while(list($a_groupid, $name, $type) = $xoopsDB->fetchRow($result)) {
		$name = $myts->makeTboxData4Save($name);
                echo "<tr><td class='bg3'>$name</td>";
                echo "<td class='bg1'><a href='admin.php?fct=groups&amp;op=modify&amp;g_id=$a_groupid'>"._AM_MODIFY."</a>";
                if ( $a_groupid == 1 || $type == "Anonymous" || $type == "User" ) {
			echo "</td></tr>";
                } else {
                    	echo "&nbsp;<a href='admin.php?fct=groups&amp;op=del&amp;g_id=$a_groupid&amp;name=$name'>"._AM_DELETE."</a></td></tr>";
                }
        }
	echo "</table></td></tr></table>";
	$name_value = "";
	$desc_value = "";
	$a_mod_value = array();
	$r_mod_value = array();
	$r_block_value = array();
	$op_value = "add";
	$submit_value = _AM_CREATENEWADG;
	$g_id_value = "";
	$type_value = "";
	$form_title = _AM_CREATENEWADG;
	include(XOOPS_ROOT_PATH."/modules/system/admin/groups/groupform.php");
	CloseTable();
        xoops_cp_footer();
}


function modifyGroup($g_id) {
        global $xoopsUser, $xoopsDB, $xoopsConfig, $xoopsModule;
	$myts =& MyTextSanitizer::getInstance();
        xoops_cp_header();
        OpenTable();
	$thisgroup = new XoopsGroup($g_id);
	$name_value = $thisgroup->getVar("name", "E");
	$desc_value = $thisgroup->getVar("description", "E");
	$a_mod_value = XoopsModule::getByRight($thisgroup->getVar("groupid"),"A");
	$r_mod_value = XoopsModule::getByRight($thisgroup->getVar("groupid"),"R");
	$r_block_value = XoopsBlock::getAllBlocksByGroup($thisgroup->getVar("groupid"), false);
	$op_value = "update";
	$submit_value = _AM_UPDATEADG;
	$g_id_value = $thisgroup->getVar("groupid");
	$type_value = $thisgroup->getVar("type", "E");
	$form_title = _AM_MODIFYADG;
	include(XOOPS_ROOT_PATH."/modules/system/admin/groups/groupform.php");
	echo "<br /><h4 style='text-align:left'>"._AM_EDITMEMBER."</h4>";
	$sql = "SELECT l.uid, u.uname FROM ".$xoopsDB->prefix("groups_users_link")." l, ".$xoopsDB->prefix("users")." u WHERE l.groupid = ".$g_id." AND l.uid=u.uid ORDER BY uname";
	$result = $xoopsDB->query($sql);
	$admins= array();
	while($myrow = $xoopsDB->fetchArray($result)){
		$admins['uid'][] = $myrow['uid'];
		$admins['uname'][] = $myrow['uname'];
		//array_push($admins, $myrow);
	}
	$sql = "SELECT uid, uname FROM ".$xoopsDB->prefix("users")." WHERE level>0 ORDER BY uname";
	$result = $xoopsDB->query($sql);
	$users = array();
	while($myrow = $xoopsDB->fetchArray($result)){
		if (!in_array($myrow['uid'], $admins['uid'])) {
			$users['uid'][] = $myrow['uid'];
			$users['uname'][] = $myrow['uname'];
		}
	}
	echo "<table border='0'>
	<tr><td align='center'>"._AM_NONMEMBERS."</td><td></td><td align='center'>"._AM_MEMBERS."</td></tr>
	<tr><td>
	<form action='admin.php' method='post'>
	<select name='uids[]' size='10' multiple='multiple'>\n";
	$usersize = sizeof($users['uid']);
	$adminsize = sizeof($admins['uid']);
	for ( $i = 0; $i < $usersize; $i++ ) {
		echo "<option value='".$users['uid'][$i]."'>".$myts->makeTboxData4Show($users['uname'][$i])."</option>\n";
	}
	echo "</select>
	</td>
	<td align='center'>
	<input type='hidden' name='op' value='addUser' />
	<input type='hidden' name='fct' value='groups' />
	<input type='hidden' name='groupid' value='".$thisgroup->getVar("groupid")."' />
	<input type='submit' name='submit' value='"._AM_ADDBUTTON."' />
	</form><br />
	<form action='admin.php' method='post' />
	<input type='hidden' name='op' value='delUser' />
	<input type='hidden' name='fct' value='groups' />
	<input type='hidden' name='groupid' value='".$thisgroup->getVar("groupid")."' />
	<input type='submit' name='submit' value='"._AM_DELBUTTON."' />
	</td>
	<td>
	<select name='uids[]' size='10' multiple='multiple'>";
	for ( $i = 0; $i < $adminsize; $i++ ) {
		echo "<option value='".$admins['uid'][$i]."'>".$myts->makeTboxData4Show($admins['uname'][$i])."</option>";
	}
	echo "</select>
	</td></tr>
	</form>
	</table>";	
	CloseTable();
	xoops_cp_footer();
}

function updateGroup($g_id, $name, $desc, $admin_mids, $read_mids, $read_bids) {
	global $xoopsDB;
	if (!$g_id || !$name) { 
		redirect_header("admin.php?fct=groups&op=adminMain",2,_AM_UNEED2ENTER); 
		exit();
	}
	$xoopsgroup = new XoopsGroup($g_id);
	$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);
	}
	exit();
}

} else {
    echo "Access Denied";
}

?>