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