TYPO3  7.6
KeywordsViewHelper.php
Go to the documentation of this file.
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  */
18 
42 {
43  use MenuViewHelperTrait;
44 
50  public function initializeArguments()
51  {
52  $this->registerArgument('as', 'string', 'Name of template variable which will contain selected pages', true);
53  $this->registerArgument('entryLevel', 'integer', 'The entry level', false, 0);
54  $this->registerArgument('pageUids', 'array', 'Page UIDs of pages to fetch the keywords from', false, array());
55  $this->registerArgument('keywords', 'array', 'Keywords for which to search', false, array());
56  $this->registerArgument('includeNotInMenu', 'boolean', 'Include pages that are marked "hide in menu"?', false, false);
57  $this->registerArgument('includeMenuSeparator', 'boolean', 'Include pages of the type "Menu separator"?', false, false);
58  $this->registerArgument('excludeNoSearchPages', 'boolean', 'Exclude pages that are NOT marked "include in search"?', false, true);
59  }
60 
66  public function render()
67  {
68  $typoScriptFrontendController = $this->getTypoScriptFrontendController();
69  $as = (string)$this->arguments['as'];
70  $entryLevel = (int)$this->arguments['entryLevel'];
71  $pageUids = (array)$this->arguments['pageUids'];
72  $keywords = (array)$this->arguments['keywords'];
73  $includeNotInMenu = (bool)$this->arguments['includeNotInMenu'];
74  $includeMenuSeparator = (bool)$this->arguments['includeMenuSeparator'];
75  $excludeNoSearchPages = (bool)$this->arguments['excludeNoSearchPages'];
76 
77  // If no pages have been defined, use the current page
78  if (empty($pageUids)) {
79  $pageUids = array($typoScriptFrontendController->page['uid']);
80  }
81 
82  // Transform the keywords list into an array
83  if (!is_array($keywords)) {
84  $unfilteredKeywords = $this->keywordsToArray($keywords);
85  } else {
86  $unfilteredKeywords = $keywords;
87  }
88 
89  // Use the keywords of the page when none has been given
90  if (empty($keywords)) {
91  foreach ($pageUids as $pageUid) {
92  $page = $typoScriptFrontendController->sys_page->getPage($pageUid);
93  $unfilteredKeywords = array_merge(
94  $unfilteredKeywords,
95  $this->keywordsToArray($page['keywords'])
96  );
97  }
98  }
99  $filteredKeywords = array_unique($unfilteredKeywords);
100 
101  $constraints = $this->getPageConstraints($includeNotInMenu, $includeMenuSeparator);
102  if ($excludeNoSearchPages) {
103  $constraints .= ' AND no_search = 0';
104  }
105 
106  $keywordConstraints = array();
107  if ($filteredKeywords) {
108  $db = $this->getDatabaseConnection();
109  foreach ($filteredKeywords as $keyword) {
110  $keyword = $db->fullQuoteStr('%' . $db->escapeStrForLike($keyword, 'pages') . '%', 'pages');
111  $keywordConstraints[] = 'keywords LIKE ' . $keyword;
112  }
113  $constraints .= ' AND (' . implode(' OR ', $keywordConstraints) . ')';
114  }
115 
116  // Start point
117  if ($entryLevel < 0) {
118  $entryLevel = count($typoScriptFrontendController->tmpl->rootLine) - 1 + $entryLevel;
119  }
120  $startUid = $typoScriptFrontendController->tmpl->rootLine[$entryLevel]['uid'];
121  $treePageUids = explode(
122  ',',
123  $typoScriptFrontendController->cObj->getTreeList($startUid, 20)
124  );
125 
126  $pages = $typoScriptFrontendController->sys_page->getMenuForPages(
127  array_merge([$startUid], $treePageUids),
128  '*',
129  '',
130  $constraints
131  );
132  return $this->renderChildrenWithVariables(array(
133  $as => $pages
134  ));
135  }
136 
145  protected function keywordsToArray($keywords)
146  {
147  $keywordList = preg_split('/[,;' . LF . ']/', $keywords);
148 
149  return array_filter(array_map('trim', $keywordList));
150  }
151 
155  protected function getDatabaseConnection()
156  {
157  return $GLOBALS['TYPO3_DB'];
158  }
159 }