Alphabetical sorting of user groups
authorMarcel Werk <burntime@woltlab.com>
Thu, 4 Jun 2020 17:24:12 +0000 (19:24 +0200)
committerMarcel Werk <burntime@woltlab.com>
Thu, 4 Jun 2020 17:24:12 +0000 (19:24 +0200)
Closes #3353

wcfsetup/install/files/lib/acp/form/PaidSubscriptionAddForm.class.php
wcfsetup/install/files/lib/acp/form/UserAssignToGroupForm.class.php
wcfsetup/install/files/lib/acp/form/UserGroupAssignmentAddForm.class.php
wcfsetup/install/files/lib/acp/form/UserGroupOptionForm.class.php
wcfsetup/install/files/lib/acp/form/UserGroupPromoteOwnerForm.class.php
wcfsetup/install/files/lib/acp/form/UserMailForm.class.php
wcfsetup/install/files/lib/acp/form/UserRankAddForm.class.php
wcfsetup/install/files/lib/data/user/group/UserGroup.class.php
wcfsetup/install/files/lib/system/bulk/processing/user/AbstractUserGroupsUserBulkProcessingAction.class.php
wcfsetup/install/files/lib/system/condition/UserGroupCondition.class.php
wcfsetup/install/files/lib/system/option/user/group/UserGroupsUserGroupOptionType.class.php

index dfa94ac193e76c0bd8874176a7f1897fb71fddc3..188a136278ad57471660d49cd5eacb09bffed4bc 100644 (file)
@@ -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'));
index 79e2306b9fa78e5e35e78904d49c5b9e45c8b6d2..f592a868c35c720230348dd26f199e39a2f4dfe6 100755 (executable)
@@ -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]);
        }
 }
index 5a33b512720a32ddb43920ef83e7fc594dfcc68c..35436f059bd689dabcc33fb13afcf182594dd3e6 100644 (file)
@@ -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();
index 59a2f1e475b62f7802868d29c4d0b177e87c78d1..09586ea774d45cda26577b6dbcfa4e2801e48391 100644 (file)
@@ -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])) {
index 15f6527348b490d9baaec1c525424165f138931e..010e02b5e368ea190b200d2044cd83027163c06f 100644 (file)
@@ -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();
-               });
        }
        
        /**
index e51e0635674b8e288d61da1c79dcbee7d55bd66c..d258d8bc87d3fff4c65815f83ed437d28abd4f4b 100755 (executable)
@@ -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]);
        }
        
        /**
index 338846c41070ea1987abe94d499203406b8d9f7f..197b5ea2e706e3329d7efb7e863b0c2dec37fbe0 100644 (file)
@@ -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,
index d9b9313785fdc0a7f2ef1a72094a9166565f6536..451b4b3470971a1d1313ef6af3c7f60dfa1c9689 100644 (file)
@@ -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());
+               });
+       }
 }
index 45fe0c346c545ea4919b53f7cac15b8b71e9c4db..c5240cce4c83cea5c9694ecb8ca665585af1d36e 100644 (file)
@@ -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]);
        }
        
        /**
index f3a9383abafe4385b4b7f5bc4f6d1f61bf19db73..a3c9978555b1083862b8d3e5658aff3b250e4cb7 100644 (file)
@@ -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;
index df9410e4056ae33a4ce6535e472bce0ff35b26e3..c92710befaca63a6b18d1e5c15f6bcaae8bf4f3d 100644 (file)
@@ -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 = '';