TYPO3 CMS  TYPO3_7-6
AbstractContextMenuDataProvider.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 
22 
27 {
33  protected $disableItems = [];
34 
40  protected $contextMenuType = '';
41 
47  public function getContextMenuType()
48  {
50  }
51 
59  {
60  $this->contextMenuType = $contextMenuType;
61  }
62 
69  abstract public function getActionsForNode(TreeNode $node);
70 
76  protected function getConfiguration()
77  {
78  $contextMenuActions = $this->getBackendUser()->getTSConfig('options.contextMenu.' . $this->contextMenuType . '.items');
79  return $contextMenuActions['properties'];
80  }
81 
94  protected function evaluateDisplayCondition(TreeNode $node, $displayCondition)
95  {
96  if ($displayCondition === '') {
97  return true;
98  }
99  // Parse condition string
100  $conditions = [];
101  preg_match_all('/(.+?)(>=|<=|!=|=|>|<)(.+?)(\\|\\||&&|$)/is', $displayCondition, $conditions);
102  $lastResult = false;
103  $chainType = '';
104  $amountOfConditions = count($conditions[0]);
105  for ($i = 0; $i < $amountOfConditions; ++$i) {
106  // Check method for existence
107  $method = trim($conditions[1][$i]);
108  list($method, $index) = explode('|', $method);
109  if (!method_exists($node, $method)) {
110  continue;
111  }
112  // Fetch compare value
113  $returnValue = call_user_func([$node, $method]);
114  if (is_array($returnValue)) {
115  $returnValue = $returnValue[$index];
116  }
117  // Compare fetched and expected values
118  $operator = trim($conditions[2][$i]);
119  $expected = trim($conditions[3][$i]);
120  if ($operator === '=') {
121  $returnValue = $returnValue == $expected;
122  } elseif ($operator === '>') {
123  $returnValue = $returnValue > $expected;
124  } elseif ($operator === '<') {
125  $returnValue = $returnValue < $expected;
126  } elseif ($operator === '>=') {
127  $returnValue = $returnValue >= $expected;
128  } elseif ($operator === '<=') {
129  $returnValue = $returnValue <= $expected;
130  } elseif ($operator === '!=') {
131  $returnValue = $returnValue != $expected;
132  } else {
133  $returnValue = false;
134  $lastResult = false;
135  }
136  // Chain last result and the current if requested
137  if ($chainType === '||') {
138  $lastResult = $lastResult || $returnValue;
139  } elseif ($chainType === '&&') {
140  $lastResult = $lastResult && $returnValue;
141  } else {
142  $lastResult = $returnValue;
143  }
144  // Save chain type for the next condition
145  $chainType = trim($conditions[4][$i]);
146  }
147  return $lastResult;
148  }
149 
158  protected function getNextContextMenuLevel(array $actions, TreeNode $node, $level = 0)
159  {
161  $actionCollection = GeneralUtility::makeInstance(ContextMenuActionCollection::class);
163  $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
164  if ($level > 5) {
165  return $actionCollection;
166  }
167  $type = '';
168  foreach ($actions as $index => $actionConfiguration) {
169  if (substr($index, -1) !== '.') {
170  $type = $actionConfiguration;
171  if ($type !== 'DIVIDER') {
172  continue;
173  }
174  }
175  if (!in_array($type, ['DIVIDER', 'SUBMENU', 'ITEM'])) {
176  continue;
177  }
179  $action = GeneralUtility::makeInstance(ContextMenuAction::class);
180  $action->setId($index);
181  if ($type === 'DIVIDER') {
182  $action->setType('divider');
183  } else {
184  if (in_array($actionConfiguration['name'], $this->disableItems) || isset($actionConfiguration['displayCondition']) && trim($actionConfiguration['displayCondition']) !== '' && !$this->evaluateDisplayCondition($node, $actionConfiguration['displayCondition'])) {
185  unset($action);
186  continue;
187  }
188  $label = $this->getLanguageService()->sL($actionConfiguration['label'], true);
189  if ($type === 'SUBMENU') {
190  $action->setType('submenu');
191  $action->setChildActions($this->getNextContextMenuLevel($actionConfiguration, $node, $level + 1));
192  } else {
193  $action->setType('action');
194  $action->setCallbackAction($actionConfiguration['callbackAction']);
195  if (is_array($actionConfiguration['customAttributes.'])) {
196  if (!empty($actionConfiguration['customAttributes.']['contentUrl'])) {
197  $actionConfiguration['customAttributes.']['contentUrl'] = $this->replaceModuleTokenInContentUrl($actionConfiguration['customAttributes.']['contentUrl']);
198  }
199  $action->setCustomAttributes($actionConfiguration['customAttributes.']);
200  }
201  }
202  $action->setLabel($label);
203  if (!isset($actionConfiguration['iconName'])) {
204  $actionConfiguration['iconName'] = 'miscellaneous-placeholder';
205  }
206  $action->setIcon($iconFactory->getIcon($actionConfiguration['iconName'], Icon::SIZE_SMALL)->render());
207  }
208  $actionCollection->offsetSet($level . (int)$index, $action);
209  $actionCollection->ksort();
210  }
211  return $actionCollection;
212  }
213 
220  protected function replaceModuleTokenInContentUrl($contentUrl)
221  {
222  $parsedUrl = parse_url($contentUrl);
223  parse_str($parsedUrl['query'], $urlParameters);
224  if (isset($urlParameters['M'])) {
225  $moduleName = $urlParameters['M'];
226  unset($urlParameters['M']);
227  $contentUrl = BackendUtility::getModuleUrl($moduleName, $urlParameters);
228  }
229  return $contentUrl;
230  }
231 
237  protected function getLanguageService()
238  {
239  return $GLOBALS['LANG'];
240  }
241 
247  protected function getBackendUser()
248  {
249  return $GLOBALS['BE_USER'];
250  }
251 }
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']