708272596e16eb4fb1a901c1a2c4fd656debedee
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid_styled_content / Classes / ViewHelpers / Menu / UpdatedViewHelper.php
1 <?php
2 namespace TYPO3\CMS\FluidStyledContent\ViewHelpers\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\Fluid\Core\ViewHelper\AbstractViewHelper;
18
19 /**
20 * A view helper which returns recently updated subpages (multiple levels) of the given pages
21 *
22 * = Example =
23 *
24 * <code title="Pages with the similar keyword(s) of page uid = 1 and uid = 2">
25 * <ce:menu.updated pageUids="{0: 1, 1: 2}" as="pages">
26 * <f:for each="{pages}" as="page">
27 * {page.title}
28 * </f:for>
29 * </ce:menu.updated>
30 * </code>
31 *
32 * <output>
33 * Recently updated subpage 1
34 * Recently updated subpage 2
35 * Recently updated subpage 3
36 * </output>
37 */
38 class UpdatedViewHelper extends AbstractViewHelper
39 {
40 use MenuViewHelperTrait;
41
42 /**
43 * Output escaping is disabled as child content contains HTML by default
44 *
45 * @var bool
46 */
47 protected $escapeOutput = false;
48
49 /**
50 * Initialize ViewHelper arguments
51 *
52 * @return void
53 */
54 public function initializeArguments()
55 {
56 $this->registerArgument('as', 'string', 'Name of the template variable that will contain selected pages', true);
57 $this->registerArgument('pageUids', 'array', 'Page UIDs of parent pages', false, array());
58 $this->registerArgument('sortField', 'string', 'Field to sort pages; possible values: starttime, lastUpdated, tstamp, crdate', false, 'SYS_LASTCHANGED');
59 $this->registerArgument('maximumAge', 'string', 'Maximum age of pages to be included; supports mathematical expressions', false, '604800');
60 $this->registerArgument('includeNotInMenu', 'boolean', 'Include pages that are marked "hide in menu"?', false, false);
61 $this->registerArgument('includeMenuSeparator', 'boolean', 'Include pages of the type "Menu separator"?', false, false);
62 $this->registerArgument('excludeNoSearchPages', 'boolean', 'Exclude pages that are NOT marked "include in search"?', false, true);
63 }
64
65 /**
66 * Render the view helper
67 *
68 * @return string
69 */
70 public function render()
71 {
72 $typoScriptFrontendController = $this->getTypoScriptFrontendController();
73 $as = (string)$this->arguments['as'];
74 $pageUids = (array)$this->arguments['pageUids'];
75 $sortField = $this->arguments['sortField'];
76 $maximumAge = $this->arguments['maximumAge'];
77 $includeNotInMenu = (bool)$this->arguments['includeNotInMenu'];
78 $includeMenuSeparator = (bool)$this->arguments['includeMenuSeparator'];
79 $excludeNoSearchPages = (bool)$this->arguments['excludeNoSearchPages'];
80
81 // If no pages have been defined, use the current page
82 if (empty($pageUids)) {
83 $pageUids = array($typoScriptFrontendController->page['uid']);
84 }
85
86 $unfilteredPageTreeUids = array();
87 foreach ($pageUids as $pageUid) {
88 $unfilteredPageTreeUids = array_merge(
89 $unfilteredPageTreeUids,
90 explode(
91 ',',
92 $typoScriptFrontendController->cObj->getTreeList($pageUid, 20)
93 )
94 );
95 }
96 $pageTreeUids = array_unique($unfilteredPageTreeUids);
97
98 $constraints = $this->getPageConstraints($includeNotInMenu, $includeMenuSeparator);
99
100 if ($excludeNoSearchPages) {
101 $constraints .= ' AND no_search = 0';
102 }
103
104 if (!in_array($sortField, ['starttime', 'lastUpdated', 'tstamp', 'crdate'])) {
105 $sortField = 'SYS_LASTCHANGED';
106 }
107
108 $minimumTimeStamp = time() - (int)$typoScriptFrontendController->cObj->calc($maximumAge);
109 $constraints .= ' AND ' . $sortField . ' >=' . $minimumTimeStamp;
110
111 $pages = $typoScriptFrontendController->sys_page->getMenuForPages(
112 $pageTreeUids,
113 '*',
114 $sortField . ' DESC',
115 $constraints
116 );
117 return $this->renderChildrenWithVariables(array(
118 $as => $pages
119 ));
120 }
121 }