<?php
// $Id: groups.php,v 1.1 2004/07/11 10:28:51 Farsus Exp $
// -------------------------------------------------------------------------//
//			       RUNCMS                                                   //
//                                                                          //
//	 reliable - Unique - Nocost &  Simplicity & ease off use                //
//                       < http://www.runcms.org >                          //
// -------------------------------------------------------------------------//
// Original Author: Kazumi Ono
// Author Website : http://www.mywebaddons.com/ , http://www.myweb.ne.jp
// License Type   : GPL: See /manual/LICENSES/GPL.txt
// ------------------------------------------------------------------------- //

if ( $xoopsUser->isAdmin($xoopsModule->mid()) ) {

//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function displayGroups() {
global $db, $myts, $xoopsConfig, $xoopsModule;

xoops_cp_header();
OpenTable();

echo "<h4 style='text-align:left'>"._AM_EDITADG."</h4>";
$result = $db->query("SELECT groupid, name, type FROM ".$db->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'>";

while(list($a_groupid, $name, $type) = $db->fetch_row($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'>"._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'>"._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_once(XOOPS_ROOT_PATH."/modules/system/admin/groups/groupform.php");

CloseTable();
xoops_cp_footer();
}

//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function modifyGroup($g_id) {
global $HTTP_GET_VARS, $HTTP_POST_VARS, $xoopsUser, $db, $myts, $xoopsConfig, $xoopsModule;

$userstart = $adminstart = 0;

if ( !empty($HTTP_POST_VARS['userstart']) ) {
	$userstart = intval($HTTP_POST_VARS['userstart']);
	} elseif (!empty($HTTP_GET_VARS['userstart'])) {
		$userstart = intval($HTTP_GET_VARS['userstart']);
	}

if ( !empty($HTTP_POST_VARS['adminstart']) ) {
	$adminstart = intval($HTTP_POST_VARS['adminstart']);
	} elseif (!empty($HTTP_GET_VARS['adminstart'])) {
		$adminstart = intval($HTTP_GET_VARS['adminstart']);
	}

xoops_cp_header();
OpenTable();

echo '<a href="admin.php?fct=groups">'. _AM_GROUPSMAIN .'</a>&nbsp;<span style="font-weight:bold;">&raquo;&raquo;</span>&nbsp;'. _AM_MODIFYADG.'<br /><br />';

$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  = _UPDATE;
$g_id_value    = $thisgroup->getVar("groupid");
$type_value    = $thisgroup->getVar("type", "E");
$form_title    = _AM_MODIFYADG;

include_once(XOOPS_ROOT_PATH."/modules/system/admin/groups/groupform.php");
echo "<br /><h4 style='text-align:left'>"._AM_EDITMEMBER."</h4>";

$usercount = XoopsUser::countAllUsers('level>0');
$sql       = "SELECT COUNT(*) FROM ".$db->prefix("groups_users_link")." l, ".$db->prefix("users")." u WHERE l.groupid = ".$g_id." AND l.uid=u.uid";
list($admincount) = $db->fetch_row($db->query($sql));

if ( $usercount < 1000 && $admincount < 1000 ) {
	$sql    = "SELECT l.uid, u.uname FROM ".$db->prefix("groups_users_link")." l, ".$db->prefix("users")." u WHERE l.groupid = ".$g_id." AND l.uid=u.uid ORDER BY uname";
	$result = $db->query($sql);
	$admins = array();
	while ( $myrow = $db->fetch_array($result) ) {
		$admins[$myrow['uid']] = $myts->makeTboxData4Show($myrow['uname']);
	}
	$userslist =& XoopsUser::getAllUsersList('level>0', 'uname ASC');
	$users     =& array_diff($userslist, $admins);

?>
<table align=center border="0"><tr>
<td align="center"><?php echo _AM_NONMEMBERS;?></td>
<td></td>
<td align="center"><?php echo _AM_MEMBERS;?></td>
</tr><tr>
<td><form action="admin.php" method="post">
<select class="select" name="uids[]" size="10" multiple="multiple">
<?php

$size = count($userids);

foreach ($users as $u_id => $u_name) {
	echo '<option value="'.$u_id.'">'.$u_name.'</option>'."";
}

?>
</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="<?php echo $thisgroup->getVar("groupid");?>" />
<input type="submit" class="button" name="submit" value="<?php echo _ADD;?> --&gt;" />
</form>
<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="<?php echo $thisgroup->getVar("groupid");?>" />
<input type="submit" class="button" name="submit" value="&lt;-- <?php echo _REMOVE;?>" />
</td>
<td><select class="select" name="uids[]" size="10" multiple="multiple">
<?php

foreach ( $admins as $a_id => $a_name ) {
	echo '<option value="'.$a_id.'">'.$a_name.'</option>';
}

?>
</select></td></tr></form></table>
<?php

} else {

$sql    = "SELECT l.uid, u.uname FROM ".$db->prefix("groups_users_link")." l, ".$db->prefix("users")." u WHERE l.groupid = ".$g_id." AND l.uid=u.uid ORDER BY uname";
$result = $db->query($sql, 1000, $adminstart);
$admins = array();

while ( $myrow = $db->fetch_array($result) ) {
	$admins[$myrow['uid']] = $myts->makeTboxData4Show($myrow['uname']);
}

?>
<a href="<?php echo XOOPS_URL;?>/modules/system/admin.php?fct=findusers&amp;group=<?php echo $g_id;?>"><?php echo _AM_FINDU4GROUP;?></a><br />
<form action="admin.php" method="post">
<table border="0"><tr>
<td align="center"><?php echo _AM_MEMBERS;?><br />
<?php

$nav = new XoopsPageNav($admincount, 1000, $adminstart, "adminstart", "fct=groups&op=modify&g_id=".$g_id);
echo $nav->renderNav(4);

?>
</td>
</tr><tr>
<td align="center">
<input type="hidden" name="op" value="delUser" />
<input type="hidden" name="fct" value="groups" />
<input type="hidden" name="groupid" value="<?php echo $thisgroup->getVar("groupid");?>" />
<input type="hidden" name="adminstart" value="<?php echo $adminstart;?>" />
<select class="select" name="uids[]" size="10" multiple="multiple">
<?php

foreach ( $admins as $a_id => $a_name ) {
	echo '<option value="'.$a_id.'">'.$a_name.'</option>';
}

?>
</select><br />
<input type="submit" class="button" name="submit" value="<?php echo _DELETE;?>" />
</td></tr></table></form>
<?php

}

CloseTable();
xoops_cp_footer();
}

//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function updateGroup($g_id, $name, $desc, $admin_mids, $read_mids, $read_bids) {
global $db;

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, _UPDATED);
	}
exit();
}

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