unify and consolidate group fetching method for initial template fill and ajax request

This commit is contained in:
Arthur Schiwon 2014-04-17 18:13:35 +02:00
parent d87347e64e
commit 3e411c82a5
3 changed files with 56 additions and 81 deletions

View File

@ -30,46 +30,57 @@ if (isset($_GET['pattern']) && !empty($_GET['pattern'])) {
$groups = array(); $groups = array();
$adminGroups = array(); $adminGroups = array();
$groupManager = \OC_Group::getManager(); $groupManager = \OC_Group::getManager();
$isAdmin = OC_User::isAdminUser(OC_User::getUser());
//we pass isAdmin as true, because OC_SubAdmin has no search feature,
//groups will be filtered out later
$groupsInfo = new \OC\Group\MetaData(OC_User::getUser(), true, $groupManager);
$groupsInfo->setSorting($groupsInfo::SORT_USERCOUNT);
list($adminGroup, $groups) = $groupsInfo->get($pattern);
$accessiblegroups = $groupManager->search($pattern); $accessiblegroups = $groupManager->search($pattern);
if (!OC_User::isAdminUser(OC_User::getUser())) { if(!$isAdmin) {
$subadminGroups = OC_SubAdmin::getSubAdminsGroups(OC_User::getUser()); $subadminGroups = OC_SubAdmin::getSubAdminsGroups(OC_User::getUser());
$accessiblegroups = array_intersect($accessiblegroups, $subadminGroups); $accessiblegroups = array_intersect($groups, $subadminGroups);
} }
$sortGroupsIndex = 0; // $sortGroupsIndex = 0;
$sortGroupsKeys = array(); // $sortGroupsKeys = array();
$sortAdminGroupsIndex = 0; // $sortAdminGroupsIndex = 0;
$sortAdminGroupsKeys = array(); // $sortAdminGroupsKeys = array();
//
foreach($accessiblegroups as $group) { // foreach($accessiblegroups as $group) {
$gid = $group->getGID(); // $gid = $group->getGID();
$usersInGroup = OC_Group::usersInGroup($gid, ''); // $group = $groupManager->get($gid);
if (!OC_User::isAdminUser($gid)) { // if(!$group) {
$groups[] = array( // continue;
'id' => str_replace(' ','', $gid ), // }
'name' => $gid, // $usersInGroup = $group->count();
'usercount' => count($usersInGroup), // if (!OC_User::isAdminUser($gid)) {
); // $groups[] = array(
$sortGroupsKeys[$sortGroupsIndex] = count($usersInGroup); // 'id' => str_replace(' ','', $gid ),
$sortGroupsIndex++; // 'name' => $gid,
} else { // 'usercount' => $usersInGroup,
$adminGroup[] = array( // );
'id' => str_replace(' ','', $gid ), // $sortGroupsKeys[$sortGroupsIndex] = $usersInGroup;
'name' => $gid, // $sortGroupsIndex++;
'usercount' => count($usersInGroup) // } else {
); // $adminGroup[] = array(
$sortAdminGroupsKeys[$sortAdminGroupsIndex] = count($usersInGroup); // 'id' => str_replace(' ','', $gid ),
$sortAdminGroupsIndex++; // 'name' => $gid,
} // 'usercount' => $usersInGroup
} // );
// $sortAdminGroupsKeys[$sortAdminGroupsIndex] = $usersInGroup;
if(!empty($groups)) { // $sortAdminGroupsIndex++;
array_multisort($sortGroupsKeys, SORT_DESC, $groups); // }
} // }
if(!empty($adminGroup)) { //
array_multisort($sortAdminGroupsKeys, SORT_DESC, $adminGroup); // if(!empty($groups)) {
} // array_multisort($sortGroupsKeys, SORT_DESC, $groups);
// }
// if(!empty($adminGroup)) {
// array_multisort($sortAdminGroupsKeys, SORT_DESC, $adminGroup);
// }
OC_JSON::success( OC_JSON::success(
array('data' => array('adminGroups' => $adminGroups, 'groups' => $groups))); array('data' => array('adminGroups' => $adminGroups, 'groups' => $groups)));

View File

@ -28,21 +28,21 @@
<li data-gid="admin"> <li data-gid="admin">
<a href="#"><?php p($l->t('Admins')); ?></a> <a href="#"><?php p($l->t('Admins')); ?></a>
<span class="utils"> <span class="utils">
<span class="usercount"><?php if($adminGroup['useringroup'] > 0) { p($adminGroup['useringroup']); } ?></span> <span class="usercount"><?php if($adminGroup['usercount'] > 0) { p($adminGroup['usercount']); } ?></span>
</span> </span>
</li> </li>
<?php endforeach; ?> <?php endforeach; ?>
<!--List of Groups--> <!--List of Groups-->
<?php foreach($_["groups"] as $group): ?> <?php foreach($_["groups"] as $group): ?>
<li data-gid="<?php p($group['name']) ?>" data-usercount="<?php p($group['useringroup']) ?>"> <li data-gid="<?php p($group['name']) ?>" data-usercount="<?php p($group['usercount']) ?>">
<a href="#"> <a href="#">
<span><?php p($group['name']); ?></span> <span><?php p($group['name']); ?></span>
<img class="svg action rename" src="<?php p(image_path('core', 'actions/rename.svg'))?>" <img class="svg action rename" src="<?php p(image_path('core', 'actions/rename.svg'))?>"
original-title="<?php p($l->t('Edit'))?>" alt="<?php p($l->t("change group name"))?>" title="<?php p($l->t("change group name"))?>" /> original-title="<?php p($l->t('Edit'))?>" alt="<?php p($l->t("change group name"))?>" title="<?php p($l->t("change group name"))?>" />
</a> </a>
<span class="utils"> <span class="utils">
<span class="usercount"><?php if($group['useringroup'] > 0) { p($group['useringroup']); } ?></span> <span class="usercount"><?php if($group['usercount'] > 0) { p($group['usercount']); } ?></span>
<a href="#" class="action delete" original-title="<?php p($l->t('Delete'))?>"> <a href="#" class="action delete" original-title="<?php p($l->t('Delete'))?>">
<img src="<?php print_unescaped(image_path('core', 'actions/delete.svg')) ?>" class="svg" /> <img src="<?php print_unescaped(image_path('core', 'actions/delete.svg')) ?>" class="svg" />
</a> </a>

View File

@ -19,8 +19,6 @@ OC_Util::addStyle( 'settings', 'settings' );
OC_App::setActiveNavigationEntry( 'core_users' ); OC_App::setActiveNavigationEntry( 'core_users' );
$users = array(); $users = array();
$groups = array();
$adminGroup = array();
$userManager = \OC_User::getManager(); $userManager = \OC_User::getManager();
$groupManager = \OC_Group::getManager(); $groupManager = \OC_Group::getManager();
@ -36,16 +34,19 @@ if (isset($_GET['limit'])) {
} }
$isadmin = OC_User::isAdminUser(OC_User::getUser()); $isadmin = OC_User::isAdminUser(OC_User::getUser());
$groupsInfo = new \OC\Group\MetaData(OC_User::getUser(), $isadmin, $groupManager);
$groupsInfo->setSorting($groupsInfo::SORT_USERCOUNT);
list($adminGroup, $groups) = $groupsInfo->get();
$recoveryAdminEnabled = OC_App::isEnabled('files_encryption') && $recoveryAdminEnabled = OC_App::isEnabled('files_encryption') &&
OC_Appconfig::getValue( 'files_encryption', 'recoveryAdminEnabled' ); OC_Appconfig::getValue( 'files_encryption', 'recoveryAdminEnabled' );
if($isadmin) { if($isadmin) {
$accessiblegroups = OC_Group::getGroups();
$accessibleusers = OC_User::getDisplayNames('', 30); $accessibleusers = OC_User::getDisplayNames('', 30);
$subadmins = OC_SubAdmin::getAllSubAdmins(); $subadmins = OC_SubAdmin::getAllSubAdmins();
}else{ }else{
$accessiblegroups = OC_SubAdmin::getSubAdminsGroups(OC_User::getUser()); $accessibleusers = OC_Group::displayNamesInGroups($groups, '', 30);
$accessibleusers = OC_Group::displayNamesInGroups($accessiblegroups, '', 30);
$subadmins = false; $subadmins = false;
} }
@ -85,50 +86,13 @@ foreach($accessibleusers as $uid => $displayName) {
); );
} }
$sortGroupsIndex = 0;
$sortGroupsKeys = array();
$sortAdminGroupsIndex = 0;
$sortAdminGroupsKeys = array();
foreach( $accessiblegroups as $gid ) {
$group = $groupManager->get($gid);
if(!$group) {
continue;
}
$usersInGroup = $group->count();
if (!OC_User::isAdminUser($gid)) {
$groups[] = array(
'id' => str_replace(' ','', $gid ),
'name' => $gid,
'useringroup' => $usersInGroup,
);
$sortGroupsKeys[$sortGroupsIndex] = $usersInGroup;
$sortGroupsIndex++;
} else {
$adminGroup[] = array(
'id' => str_replace(' ','', $gid ),
'name' => $gid,
'useringroup' => $usersInGroup
);
$sortAdminGroupsKeys[$sortAdminGroupsIndex] = $usersInGroup;
$sortAdminGroupsIndex++;
}
}
//sorts groups by number of users (descending)
if(!empty($groups)) {
array_multisort($sortGroupsKeys, SORT_DESC, $groups);
}
if(!empty($adminGroup)) {
array_multisort($sortAdminGroupsKeys, SORT_DESC, $adminGroup);
}
$tmpl = new OC_Template( "settings", "users/main", "user" ); $tmpl = new OC_Template( "settings", "users/main", "user" );
$tmpl->assign( 'users', $users ); $tmpl->assign( 'users', $users );
$tmpl->assign( 'groups', $groups ); $tmpl->assign( 'groups', $groups );
$tmpl->assign( 'adminGroup', $adminGroup ); $tmpl->assign( 'adminGroup', $adminGroup );
$tmpl->assign( 'isadmin', (int) $isadmin); $tmpl->assign( 'isadmin', (int) $isadmin);
$tmpl->assign( 'subadmins', $subadmins); $tmpl->assign( 'subadmins', $subadmins);
$tmpl->assign( 'numofgroups', count($accessiblegroups)); $tmpl->assign( 'numofgroups', count($groups) + count($adminGroup));
$tmpl->assign( 'quota_preset', $quotaPreset); $tmpl->assign( 'quota_preset', $quotaPreset);
$tmpl->assign( 'default_quota', $defaultQuota); $tmpl->assign( 'default_quota', $defaultQuota);
$tmpl->assign( 'defaultQuotaIsUserDefined', $defaultQuotaIsUserDefined); $tmpl->assign( 'defaultQuotaIsUserDefined', $defaultQuotaIsUserDefined);