From 64dc7a6c77ad74eb7f9066f978a21d463e1fc079 Mon Sep 17 00:00:00 2001 From: Marcel Werk Date: Thu, 4 Jun 2020 19:24:12 +0200 Subject: [PATCH] Alphabetical sorting of user groups Closes #3353 --- .../form/PaidSubscriptionAddForm.class.php | 2 +- .../acp/form/UserAssignToGroupForm.class.php | 2 +- .../form/UserGroupAssignmentAddForm.class.php | 6 +--- .../acp/form/UserGroupOptionForm.class.php | 2 +- .../form/UserGroupPromoteOwnerForm.class.php | 8 ++--- .../files/lib/acp/form/UserMailForm.class.php | 2 +- .../lib/acp/form/UserRankAddForm.class.php | 2 +- .../lib/data/user/group/UserGroup.class.php | 32 +++++++++++++++++-- ...erGroupsUserBulkProcessingAction.class.php | 6 +--- .../condition/UserGroupCondition.class.php | 6 +--- .../UserGroupsUserGroupOptionType.class.php | 6 +--- 11 files changed, 40 insertions(+), 34 deletions(-) diff --git a/wcfsetup/install/files/lib/acp/form/PaidSubscriptionAddForm.class.php b/wcfsetup/install/files/lib/acp/form/PaidSubscriptionAddForm.class.php index dfa94ac193..188a136278 100644 --- a/wcfsetup/install/files/lib/acp/form/PaidSubscriptionAddForm.class.php +++ b/wcfsetup/install/files/lib/acp/form/PaidSubscriptionAddForm.class.php @@ -142,7 +142,7 @@ class PaidSubscriptionAddForm extends AbstractForm { I18nHandler::getInstance()->register('title'); // get available user groups - $this->availableUserGroups = UserGroup::getAccessibleGroups([], [UserGroup::GUESTS, UserGroup::EVERYONE, UserGroup::USERS]); + $this->availableUserGroups = UserGroup::getSortedAccessibleGroups([], [UserGroup::GUESTS, UserGroup::EVERYONE, UserGroup::USERS]); if (!count(PaymentMethodHandler::getInstance()->getPaymentMethods())) { throw new NamedUserException(WCF::getLanguage()->get('wcf.acp.paidSubscription.error.noPaymentMethods')); diff --git a/wcfsetup/install/files/lib/acp/form/UserAssignToGroupForm.class.php b/wcfsetup/install/files/lib/acp/form/UserAssignToGroupForm.class.php index 79e2306b9f..f592a868c3 100755 --- a/wcfsetup/install/files/lib/acp/form/UserAssignToGroupForm.class.php +++ b/wcfsetup/install/files/lib/acp/form/UserAssignToGroupForm.class.php @@ -184,6 +184,6 @@ class UserAssignToGroupForm extends AbstractForm { * Get a list of available groups. */ protected function readGroups() { - $this->groups = UserGroup::getAccessibleGroups([], [UserGroup::GUESTS, UserGroup::EVERYONE, UserGroup::USERS]); + $this->groups = UserGroup::getSortedAccessibleGroups([], [UserGroup::GUESTS, UserGroup::EVERYONE, UserGroup::USERS]); } } diff --git a/wcfsetup/install/files/lib/acp/form/UserGroupAssignmentAddForm.class.php b/wcfsetup/install/files/lib/acp/form/UserGroupAssignmentAddForm.class.php index 5a33b51272..35436f059b 100644 --- a/wcfsetup/install/files/lib/acp/form/UserGroupAssignmentAddForm.class.php +++ b/wcfsetup/install/files/lib/acp/form/UserGroupAssignmentAddForm.class.php @@ -79,7 +79,7 @@ class UserGroupAssignmentAddForm extends AbstractForm { * @inheritDoc */ public function readData() { - $this->userGroups = UserGroup::getGroupsByType([], [ + $this->userGroups = UserGroup::getSortedGroupsByType([], [ UserGroup::EVERYONE, UserGroup::GUESTS, UserGroup::OWNER, @@ -91,10 +91,6 @@ class UserGroupAssignmentAddForm extends AbstractForm { } } - uasort($this->userGroups, function(UserGroup $groupA, UserGroup $groupB) { - return strcmp($groupA->getName(), $groupB->getName()); - }); - $this->conditions = UserGroupAssignmentHandler::getInstance()->getGroupedObjectTypes(); parent::readData(); diff --git a/wcfsetup/install/files/lib/acp/form/UserGroupOptionForm.class.php b/wcfsetup/install/files/lib/acp/form/UserGroupOptionForm.class.php index 59a2f1e475..09586ea774 100644 --- a/wcfsetup/install/files/lib/acp/form/UserGroupOptionForm.class.php +++ b/wcfsetup/install/files/lib/acp/form/UserGroupOptionForm.class.php @@ -117,7 +117,7 @@ class UserGroupOptionForm extends AbstractForm { } // read accessible groups - $this->groups = UserGroup::getAccessibleGroups(); + $this->groups = UserGroup::getSortedAccessibleGroups(); if ($this->userGroupOption->usersOnly) { $guestGroup = UserGroup::getGroupByType(UserGroup::GUESTS); if (isset($this->groups[$guestGroup->groupID])) { diff --git a/wcfsetup/install/files/lib/acp/form/UserGroupPromoteOwnerForm.class.php b/wcfsetup/install/files/lib/acp/form/UserGroupPromoteOwnerForm.class.php index 15f6527348..010e02b5e3 100644 --- a/wcfsetup/install/files/lib/acp/form/UserGroupPromoteOwnerForm.class.php +++ b/wcfsetup/install/files/lib/acp/form/UserGroupPromoteOwnerForm.class.php @@ -48,19 +48,15 @@ class UserGroupPromoteOwnerForm extends AbstractFormBuilderForm { throw new IllegalLinkException(); } - $this->groups = UserGroup::getGroupsByType([UserGroup::OTHER]); + $this->groups = UserGroup::getSortedGroupsByType([UserGroup::OTHER]); $this->groups = array_filter($this->groups, function (UserGroup $group) { return $group->isAdminGroup(); }); if (empty($this->groups)) { // fallback for broken installations without an admin group - $this->groups = UserGroup::getGroupsByType([UserGroup::OTHER]); + $this->groups = UserGroup::getSortedGroupsByType([UserGroup::OTHER]); } - - uasort($this->groups, function(UserGroup $a, UserGroup $b) { - return $a->getName() <=> $b->getName(); - }); } /** diff --git a/wcfsetup/install/files/lib/acp/form/UserMailForm.class.php b/wcfsetup/install/files/lib/acp/form/UserMailForm.class.php index e51e063567..d258d8bc87 100755 --- a/wcfsetup/install/files/lib/acp/form/UserMailForm.class.php +++ b/wcfsetup/install/files/lib/acp/form/UserMailForm.class.php @@ -209,7 +209,7 @@ class UserMailForm extends AbstractForm { $this->userList->readObjects(); } - $this->groups = UserGroup::getAccessibleGroups([], [UserGroup::GUESTS, UserGroup::EVERYONE]); + $this->groups = UserGroup::getSortedAccessibleGroups([], [UserGroup::GUESTS, UserGroup::EVERYONE]); } /** diff --git a/wcfsetup/install/files/lib/acp/form/UserRankAddForm.class.php b/wcfsetup/install/files/lib/acp/form/UserRankAddForm.class.php index 338846c410..197b5ea2e7 100644 --- a/wcfsetup/install/files/lib/acp/form/UserRankAddForm.class.php +++ b/wcfsetup/install/files/lib/acp/form/UserRankAddForm.class.php @@ -241,7 +241,7 @@ class UserRankAddForm extends AbstractForm { 'customCssClassName' => $this->customCssClassName, 'groupID' => $this->groupID, 'rankTitle' => $this->rankTitle, - 'availableGroups' => UserGroup::getGroupsByType([], [UserGroup::GUESTS, UserGroup::EVERYONE]), + 'availableGroups' => UserGroup::getSortedGroupsByType([], [UserGroup::GUESTS, UserGroup::EVERYONE]), 'requiredPoints' => $this->requiredPoints, 'rankImage' => $this->rankImage, 'repeatImage' => $this->repeatImage, diff --git a/wcfsetup/install/files/lib/data/user/group/UserGroup.class.php b/wcfsetup/install/files/lib/data/user/group/UserGroup.class.php index d9b9313785..451b4b3470 100644 --- a/wcfsetup/install/files/lib/data/user/group/UserGroup.class.php +++ b/wcfsetup/install/files/lib/data/user/group/UserGroup.class.php @@ -125,6 +125,22 @@ class UserGroup extends DatabaseObject implements ITitledObject { return $groups; } + /** + * Returns a sorted list of groups filtered by given type. + * + * @param integer[] $types + * @param integer[] $invalidGroupTypes + * @return UserGroup[] + * @since 5.3 + */ + public static function getSortedGroupsByType(array $types = [], array $invalidGroupTypes = []) { + $userGroups = self::getGroupsByType($types, $invalidGroupTypes); + + self::sortGroups($userGroups); + + return $userGroups; + } + /** * Returns unique group by given type. Only works for the default user groups. * @@ -270,9 +286,7 @@ class UserGroup extends DatabaseObject implements ITitledObject { public static function getSortedAccessibleGroups(array $groupTypes = [], array $invalidGroupTypes = []) { $userGroups = self::getAccessibleGroups($groupTypes, $invalidGroupTypes); - uasort($userGroups, function(UserGroup $groupA, UserGroup $groupB) { - return strcasecmp($groupA->getName(), $groupB->getName()); - }); + self::sortGroups($userGroups); return $userGroups; } @@ -545,4 +559,16 @@ class UserGroup extends DatabaseObject implements ITitledObject { return self::$ownerGroup ? self::$ownerGroup->groupID : null; } + + /** + * Sorts the given user groups alphabetically. + * + * @param UserGroup[] $userGroups + * @since 5.3 + */ + public static function sortGroups(array &$userGroups) { + uasort($userGroups, function(UserGroup $groupA, UserGroup $groupB) { + return strcasecmp($groupA->getName(), $groupB->getName()); + }); + } } diff --git a/wcfsetup/install/files/lib/system/bulk/processing/user/AbstractUserGroupsUserBulkProcessingAction.class.php b/wcfsetup/install/files/lib/system/bulk/processing/user/AbstractUserGroupsUserBulkProcessingAction.class.php index 45fe0c346c..c5240cce4c 100644 --- a/wcfsetup/install/files/lib/system/bulk/processing/user/AbstractUserGroupsUserBulkProcessingAction.class.php +++ b/wcfsetup/install/files/lib/system/bulk/processing/user/AbstractUserGroupsUserBulkProcessingAction.class.php @@ -45,11 +45,7 @@ abstract class AbstractUserGroupsUserBulkProcessingAction extends AbstractUserBu public function __construct(DatabaseObject $object) { parent::__construct($object); - $this->availableUserGroups = UserGroup::getAccessibleGroups([], [UserGroup::GUESTS, UserGroup::EVERYONE, UserGroup::OWNER, UserGroup::USERS]); - - uasort($this->availableUserGroups, function(UserGroup $groupA, UserGroup $groupB) { - return strcmp($groupA->getName(), $groupB->getName()); - }); + $this->availableUserGroups = UserGroup::getSortedAccessibleGroups([], [UserGroup::GUESTS, UserGroup::EVERYONE, UserGroup::OWNER, UserGroup::USERS]); } /** diff --git a/wcfsetup/install/files/lib/system/condition/UserGroupCondition.class.php b/wcfsetup/install/files/lib/system/condition/UserGroupCondition.class.php index f3a9383aba..a3c9978555 100644 --- a/wcfsetup/install/files/lib/system/condition/UserGroupCondition.class.php +++ b/wcfsetup/install/files/lib/system/condition/UserGroupCondition.class.php @@ -174,11 +174,7 @@ HTML; $invalidGroupTypes[] = UserGroup::GUESTS; } - $this->userGroups = UserGroup::getAccessibleGroups([], $invalidGroupTypes); - - uasort($this->userGroups, function(UserGroup $groupA, UserGroup $groupB) { - return strcmp($groupA->getName(), $groupB->getName()); - }); + $this->userGroups = UserGroup::getSortedAccessibleGroups([], $invalidGroupTypes); } return $this->userGroups; diff --git a/wcfsetup/install/files/lib/system/option/user/group/UserGroupsUserGroupOptionType.class.php b/wcfsetup/install/files/lib/system/option/user/group/UserGroupsUserGroupOptionType.class.php index df9410e405..c92710befa 100644 --- a/wcfsetup/install/files/lib/system/option/user/group/UserGroupsUserGroupOptionType.class.php +++ b/wcfsetup/install/files/lib/system/option/user/group/UserGroupsUserGroupOptionType.class.php @@ -24,11 +24,7 @@ class UserGroupsUserGroupOptionType extends AbstractOptionType implements IUserG $selectedGroups = explode(',', $value); // get all groups - $groups = UserGroup::getGroupsByType(); - - usort($groups, function(UserGroup $groupA, UserGroup $groupB) { - return strcasecmp($groupA->getName(), $groupB->getName()); - }); + $groups = UserGroup::getSortedGroupsByType(); // generate html $html = ''; -- 2.20.1