[BUGFIX] Show correct record title for be_groups and be_users 31/9631/17
authorMarkus Klein <klein.t3@mfc-linz.at>
Wed, 12 Dec 2012 10:48:42 +0000 (11:48 +0100)
committerWouter Wolters <typo3@wouterwolters.nl>
Fri, 16 Aug 2013 19:01:11 +0000 (21:01 +0200)
Currently it lists groups only using field title of
be_groups table regardless of any label definitions in TCA.
Use BackendUtility::getRecordTitle() instead.

Change-Id: I051c29687cb51d1883846b3cb780409c1e1cadc4
Resolves: #34631
Releases: 6.2, 6.1, 6.0
Reviewed-on: https://review.typo3.org/9631
Reviewed-by: Wouter Wolters
Tested-by: Wouter Wolters
typo3/sysext/backend/Classes/Utility/BackendUtility.php
typo3/sysext/core/Classes/Utility/ArrayUtility.php
typo3/sysext/core/Tests/Unit/Utility/ArrayUtilityTest.php

index 019b2ac..2d116e9 100644 (file)
@@ -1266,13 +1266,12 @@ class BackendUtility {
         * @return array
         */
        static public function getUserNames($fields = 'username,usergroup,usergroup_cached_list,uid', $where = '') {
-               $be_user_Array = array();
-               $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($fields, 'be_users', 'pid=0 ' . $where . self::deleteClause('be_users'), '', 'username');
-               while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
-                       $be_user_Array[$row['uid']] = $row;
-               }
-               $GLOBALS['TYPO3_DB']->sql_free_result($res);
-               return $be_user_Array;
+               return self::getRecordsSortedByTitle(
+                       GeneralUtility::trimExplode(',', $fields, TRUE),
+                       'be_users',
+                       'username',
+                       'AND pid=0 ' . $where
+               );
        }
 
        /**
@@ -1280,16 +1279,48 @@ class BackendUtility {
         *
         * @param string $fields Field list
         * @param string $where WHERE clause
-        * @return      array
+        * @return array
         */
        static public function getGroupNames($fields = 'title,uid', $where = '') {
-               $be_group_Array = array();
-               $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($fields, 'be_groups', 'pid=0 ' . $where . self::deleteClause('be_groups'), '', 'title');
-               while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
-                       $be_group_Array[$row['uid']] = $row;
+               return self::getRecordsSortedByTitle(
+                       GeneralUtility::trimExplode(',', $fields, TRUE),
+                       'be_groups',
+                       'title',
+                       'AND pid=0 ' . $where
+               );
+       }
+
+       /**
+        * Returns an array of all non-deleted records of a table sorted by a given title field.
+        * The value of the title field will be replaced by the return value
+        * of self::getRecordTitle() before the sorting is performed.
+        *
+        * @param array $fields Fields to select
+        * @param string $table Table name
+        * @param string $titleField Field that will contain the record title
+        * @param string $where Additional where clause
+        * @return array Array of sorted records
+        */
+       static protected function getRecordsSortedByTitle(array $fields, $table, $titleField, $where = '') {
+               $fieldsIndex = array_flip($fields);
+               // Make sure the titleField is amongst the fields when getting sorted
+               $fieldsIndex[$titleField] = 1;
+
+               $result = array();
+               $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', $table, '1=1 ' . $where . self::deleteClause($table));
+               while ($record = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
+                       // store the uid, because it might be unset if it's not among the requested $fields
+                       $recordId = $record['uid'];
+                       $record[$titleField] = self::getRecordTitle($table, $record);
+
+                       // include only the requested fields in the result
+                       $result[$recordId] = array_intersect_key($record, $fieldsIndex);
                }
                $GLOBALS['TYPO3_DB']->sql_free_result($res);
-               return $be_group_Array;
+
+               // sort records by $sortField. This is not done in the query because the title might have been overwritten by
+               // self::getRecordTitle();
+               return \TYPO3\CMS\Core\Utility\ArrayUtility::sortArraysByKey($result, $titleField);
        }
 
        /**
index 543fd67..372b448 100644 (file)
@@ -271,6 +271,42 @@ class ArrayUtility {
                return $array;
        }
 
+       /**
+        * Sort an array of arrays by a given key using uasort
+        *
+        * @param array $arrays Array of arrays to sort
+        * @param string $key Key to sort after
+        * @param integer $ascending Set to TRUE for ascending order, FALSE for descending order
+        * @return array Array of sorted arrays
+        * @throws \RuntimeException
+        */
+       static public function sortArraysByKey(array $arrays, $key, $ascending = TRUE) {
+               if (!count($arrays))
+                       return $arrays;
+
+               $sortResult = uasort($arrays, function($a, $b) use ($key, $ascending) {
+                       if (!isset($a[$key]) || !isset($b[$key])) {
+                               throw new \RuntimeException('The specified sorting key "' . $key . '" is not available in the given array.', 1373727309);
+                       }
+                       $valueA = strtolower($a[$key]);
+                       $valueB = strtolower($b[$key]);
+                       if ($valueA === $valueB) {
+                               return 0;
+                       } else {
+                               if ($ascending) {
+                                       return $valueA > $valueB ? 1 : -1;
+                               } else {
+                                       return $valueA < $valueB ? 1 : -1;
+                               }
+                       }
+               });
+               if (!$sortResult) {
+                       throw new \RuntimeException('The function uasort() failed for unknown reasons.', 1373727329);
+               }
+
+               return $arrays;
+       }
+
        /**
         * Exports an array as string.
         * Similar to var_export(), but representation follows the TYPO3 core CGL.
index e44c4e5..e1f55c1 100644 (file)
@@ -789,6 +789,148 @@ class ArrayUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                $this->assertSame($expectedResult, ArrayUtility::sortByKeyRecursive($unsortedArray));
        }
 
+       ///////////////////////
+       // Tests concerning sortArraysByKey
+       ///////////////////////
+       /**
+        * Data provider for sortArraysByKeyCheckIfSortingIsCorrect
+        */
+       public function sortArraysByKeyCheckIfSortingIsCorrectDataProvider() {
+               return array(
+                       'assoc array index' => array(
+                               array(
+                                       '22' => array(
+                                               'uid' => '22',
+                                               'title' => 'b',
+                                               'dummy' => 2
+                                       ),
+                                       '24' => array(
+                                               'uid' => '24',
+                                               'title' => 'a',
+                                               'dummy' => 3
+                                       ),
+                                       '23' => array(
+                                               'uid' => '23',
+                                               'title' => 'a',
+                                               'dummy' => 4
+                                       ),
+                               ),
+                               'title',
+                               TRUE,
+                               array(
+                                       '23' => array(
+                                               'uid' => '23',
+                                               'title' => 'a',
+                                               'dummy' => 4
+                                       ),
+                                       '24' => array(
+                                               'uid' => '24',
+                                               'title' => 'a',
+                                               'dummy' => 3
+                                       ),
+                                       '22' => array(
+                                               'uid' => '22',
+                                               'title' => 'b',
+                                               'dummy' => 2
+                                       ),
+                               ),
+                       ),
+                       'numeric array index' => array(
+                               array(
+                                       22 => array(
+                                               'uid' => '22',
+                                               'title' => 'b',
+                                               'dummy' => 2
+                                       ),
+                                       24 => array(
+                                               'uid' => '24',
+                                               'title' => 'a',
+                                               'dummy' => 3
+                                       ),
+                                       23 => array(
+                                               'uid' => '23',
+                                               'title' => 'a',
+                                               'dummy' => 4
+                                       ),
+                               ),
+                               'title',
+                               TRUE,
+                               array(
+                                       23 => array(
+                                               'uid' => '23',
+                                               'title' => 'a',
+                                               'dummy' => 4
+                                       ),
+                                       24 => array(
+                                               'uid' => '24',
+                                               'title' => 'a',
+                                               'dummy' => 3
+                                       ),
+                                       22 => array(
+                                               'uid' => '22',
+                                               'title' => 'b',
+                                               'dummy' => 2
+                                       ),
+                               ),
+                       ),
+                       'numeric array index DESC' => array(
+                               array(
+                                       23 => array(
+                                               'uid' => '23',
+                                               'title' => 'a',
+                                               'dummy' => 4
+                                       ),
+                                       22 => array(
+                                               'uid' => '22',
+                                               'title' => 'b',
+                                               'dummy' => 2
+                                       ),
+                                       24 => array(
+                                               'uid' => '24',
+                                               'title' => 'a',
+                                               'dummy' => 3
+                                       ),
+                               ),
+                               'title',
+                               FALSE,
+                               array(
+                                       22 => array(
+                                               'uid' => '22',
+                                               'title' => 'b',
+                                               'dummy' => 2
+                                       ),
+                                       24 => array(
+                                               'uid' => '24',
+                                               'title' => 'a',
+                                               'dummy' => 3
+                                       ),
+                                       23 => array(
+                                               'uid' => '23',
+                                               'title' => 'a',
+                                               'dummy' => 4
+                                       ),
+                               ),
+                       ),
+               );
+       }
+
+       /**
+        * @test
+        * @dataProvider sortArraysByKeyCheckIfSortingIsCorrectDataProvider
+        */
+       public function sortArraysByKeyCheckIfSortingIsCorrect(array $array, $key, $ascending, $expectedResult) {
+               $sortedArray = ArrayUtility::sortArraysByKey($array, $key, $ascending);
+               $this->assertSame($sortedArray, $expectedResult);
+       }
+
+       /**
+        * @test
+        * @expectedException \RuntimeException
+        */
+       public function sortArraysByKeyThrowsExceptionForNonExistingKey() {
+               ArrayUtility::sortArraysByKey(array(array('a'), array('a')), 'dummy');
+       }
+
        ///////////////////////
        // Tests concerning arrayExport
        ///////////////////////