‪TYPO3CMS  9.5
CategoryMenuUtility.php
Go to the documentation of this file.
1 <?php
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 
20 
28 {
32  protected static ‪$sortingField;
33 
42  public function ‪collectPages($selectedCategories, $configuration, $parentObject)
43  {
44  $selectedPages = [];
45  $categoriesPerPage = [];
46  // Determine the name of the relation field
47  $relationField = '';
48  if (isset($configuration['relation.'])) {
49  $relationField = $parentObject->parent_cObj->stdWrap(
50  $configuration['relation'],
51  $configuration['relation.']
52  );
53  } elseif (isset($configuration['relation'])) {
54  $relationField = $configuration['relation'];
55  }
56  // Get the pages for each selected category
57  $selectedCategories = GeneralUtility::intExplode(',', $selectedCategories, true);
58  foreach ($selectedCategories as $aCategory) {
59  $collection = ‪CategoryCollection::load(
60  $aCategory,
61  true,
62  'pages',
63  $relationField
64  );
65  $categoryUid = 0;
66  if ($collection instanceof ‪AbstractRecordCollection) {
67  $categoryUid = $collection->getUid();
68  }
69  // Loop on the results, overlay each page record found
70  foreach ($collection as $pageItem) {
71  $parentObject->getSysPage()->versionOL('pages', $pageItem, true);
72  if (is_array($pageItem)) {
73  $selectedPages[$pageItem['uid']] = $parentObject->getSysPage()->getPageOverlay($pageItem);
74  // Keep a list of the categories each page belongs to
75  if (!isset($categoriesPerPage[$pageItem['uid']])) {
76  $categoriesPerPage[$pageItem['uid']] = [];
77  }
78  $categoriesPerPage[$pageItem['uid']][] = $categoryUid;
79  }
80  }
81  }
82  // Loop on the selected pages to add the categories they belong to, as comma-separated list of category uid's)
83  // (this makes them available for rendering, if needed)
84  foreach ($selectedPages as $uid => $pageRecord) {
85  $selectedPages[$uid]['_categories'] = implode(',', $categoriesPerPage[$uid]);
86  }
87 
88  // Sort the pages according to the sorting property
89  self::$sortingField = isset($configuration['sorting.']) ? $parentObject->getParentContentObject()->stdWrap($configuration['sorting'], $configuration['sorting.']) : $configuration['sorting'];
90  $order = isset($configuration['order.']) ? $parentObject->getParentContentObject()->stdWrap($configuration['order'], $configuration['order.']) : $configuration['order'];
91  $selectedPages = $this->‪sortPages($selectedPages, $order);
92 
93  return $selectedPages;
94  }
95 
106  protected function ‪sortPages($pages, $order)
107  {
108  // Perform the sorting only if a criterion was actually defined
109  if (!empty(self::$sortingField)) {
110  // Check that the sorting field exists (checking the first record is enough)
111  $firstPage = current($pages);
112  if (isset($firstPage[self::$sortingField])) {
113  // Make sure the order property is either "asc" or "desc" (default is "asc")
114  if (!empty($order)) {
115  $order = strtolower($order);
116  if ($order !== 'desc') {
117  $order = 'asc';
118  }
119  }
120  uasort(
121  $pages,
122  [
123  self::class,
124  'sortPagesUtility'
125  ]
126  );
127  // If the sort order is descending, reverse the sorted array
128  if ($order === 'desc') {
129  $pages = array_reverse($pages, true);
130  }
131  }
132  }
133  return $pages;
134  }
135 
143  public static function ‪sortPagesUtility($pageA, $pageB)
144  {
145  return strnatcasecmp($pageA[self::$sortingField], $pageB[self::$sortingField]);
146  }
147 }
‪TYPO3\CMS\Frontend\ContentObject\Menu\CategoryMenuUtility\$sortingField
‪static string $sortingField
Definition: CategoryMenuUtility.php:31
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection
Definition: AbstractRecordCollection.php:33
‪TYPO3\CMS\Frontend\ContentObject\Menu\CategoryMenuUtility\sortPages
‪array sortPages($pages, $order)
Definition: CategoryMenuUtility.php:105
‪TYPO3\CMS\Frontend\ContentObject\Menu\CategoryMenuUtility\sortPagesUtility
‪static array sortPagesUtility($pageA, $pageB)
Definition: CategoryMenuUtility.php:142
‪TYPO3\CMS\Frontend\ContentObject\Menu
Definition: AbstractMenuContentObject.php:2
‪TYPO3\CMS\Frontend\Category\Collection\CategoryCollection\load
‪static TYPO3 CMS Core Collection CollectionInterface load($id, $fillItems=false, $tableName='', $fieldName='')
Definition: CategoryCollection.php:71
‪TYPO3\CMS\Frontend\ContentObject\Menu\CategoryMenuUtility\collectPages
‪array collectPages($selectedCategories, $configuration, $parentObject)
Definition: CategoryMenuUtility.php:41
‪TYPO3\CMS\Frontend\ContentObject\Menu\CategoryMenuUtility
Definition: CategoryMenuUtility.php:28
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Frontend\Category\Collection\CategoryCollection
Definition: CategoryCollection.php:31