[TASK] Use name-resolution instead of strings where possible: 17
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / ContentObject / Menu / CategoryMenuUtility.php
1 <?php
2 namespace TYPO3\CMS\Frontend\ContentObject\Menu;
3
4 /**
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18
19 /**
20 * Utility class for menus based on category collections of pages.
21 *
22 * Returns all the relevant pages for rendering with a menu content object.
23 *
24 * @author Francois Suter <francois.suter@typo3.org>
25 */
26 class CategoryMenuUtility {
27
28 /**
29 * @var string Name of the field used for sorting the pages
30 */
31 static protected $sortingField;
32
33 /**
34 * Collects all pages for the selected categories, sorted according to configuration.
35 *
36 * @param string $selectedCategories Comma-separated list of system categories primary keys
37 * @param array $configuration TypoScript configuration for the "special." keyword
38 * @param \TYPO3\CMS\Frontend\ContentObject\Menu\AbstractMenuContentObject $parentObject Back-reference to the calling object
39 * @return string List of selected pages
40 */
41 public function collectPages($selectedCategories, $configuration, $parentObject) {
42 $selectedPages = array();
43 $categoriesPerPage = array();
44 // Determine the name of the relation field
45 $relationField = '';
46 if (isset($configuration['relation.'])) {
47 $relationField = $parentObject->parent_cObj->stdWrap(
48 $configuration['relation'],
49 $configuration['relation.']
50 );
51 } elseif (isset($configuration['relation'])) {
52 $relationField = $configuration['relation'];
53 }
54 // Get the pages for each selected category
55 $selectedCategories = GeneralUtility::intExplode(',', $selectedCategories, TRUE);
56 foreach ($selectedCategories as $aCategory) {
57 $collection = \TYPO3\CMS\Frontend\Category\Collection\CategoryCollection::load(
58 $aCategory,
59 TRUE,
60 'pages',
61 $relationField
62 );
63 $categoryUid = $collection->getUid();
64 // Loop on the results, overlay each page record found
65 foreach ($collection as $pageItem) {
66 $parentObject->getSysPage()->versionOL('pages', $pageItem, TRUE);
67 if (is_array($pageItem)) {
68 $selectedPages[$pageItem['uid']] = $parentObject->getSysPage()->getPageOverlay($pageItem);
69 // Keep a list of the categories each page belongs to
70 if (!isset($categoriesPerPage[$pageItem['uid']])) {
71 $categoriesPerPage[$pageItem['uid']] = array();
72 }
73 $categoriesPerPage[$pageItem['uid']][] = $categoryUid;
74 }
75 }
76 }
77 // Loop on the selected pages to add the categories they belong to, as comma-separated list of category uid's)
78 // (this makes them available for rendering, if needed)
79 foreach ($selectedPages as $uid => $pageRecord) {
80 $selectedPages[$uid]['_categories'] = implode(',', $categoriesPerPage[$uid]);
81 }
82
83 // Sort the pages according to the sorting property
84 self::$sortingField = isset($configuration['sorting.']) ? $parentObject->getParentContentObject()->stdWrap($configuration['sorting'], $configuration['sorting.']) : $configuration['sorting'];
85 $order = isset($configuration['order.']) ? $parentObject->getParentContentObject()->stdWrap($configuration['order'], $configuration['order.']) : $configuration['order'];
86 $selectedPages = $this->sortPages($selectedPages, $order);
87
88 return $selectedPages;
89 }
90
91 /**
92 * Sorts the selected pages
93 *
94 * If the sorting field is not defined or does not corresponding to an existing field
95 * of the "pages" tables, the list of pages will remain unchanged.
96 *
97 * @param array $pages List of selected pages
98 * @param string $order Order for sorting (should "asc" or "desc")
99 * @return array Sorted list of pages
100 */
101 protected function sortPages($pages, $order) {
102 // Perform the sorting only if a criterion was actually defined
103 if (!empty(self::$sortingField)) {
104 // Check that the sorting field exists (checking the first record is enough)
105 $firstPage = current($pages);
106 if (isset($firstPage[self::$sortingField])) {
107 // Make sure the order property is either "asc" or "desc" (default is "asc")
108 if (!empty($order)) {
109 $order = strtolower($order);
110 if ($order != 'desc') {
111 $order = 'asc';
112 }
113 }
114 uasort(
115 $pages,
116 array(
117 \TYPO3\CMS\Frontend\ContentObject\Menu\CategoryMenuUtility::class,
118 'sortPagesUtility'
119 )
120 );
121 // If the sort order is descending, reverse the sorted array
122 if ($order == 'desc') {
123 $pages = array_reverse($pages, TRUE);
124 }
125 }
126 }
127 return $pages;
128 }
129
130 /**
131 * Static utility for sorting pages according to the selected criterion
132 *
133 * @param array $pageA Record for first page to be compared
134 * @param array $pageB Record for second page to be compared
135 * @return array -1 if first argument is smaller than second argument, 1 if first is greater than second and 0 if both are equal
136 */
137 static public function sortPagesUtility($pageA, $pageB) {
138 return strnatcasecmp($pageA[self::$sortingField], $pageB[self::$sortingField]);
139 }
140 }