AbstractContextMenuDataProvider.php 7.27 KB
Newer Older
1
2
3
<?php
namespace TYPO3\CMS\Backend\ContextMenu;

4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
8
9
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
10
 *
11
12
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
15
 * The TYPO3 project - inspiring people to share!
 */
16

17
use TYPO3\CMS\Backend\Utility\BackendUtility;
18
19
20
21
22
23
24
use TYPO3\CMS\Core\Imaging\Icon;
use TYPO3\CMS\Core\Imaging\IconFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Backend\ContextMenu\ContextMenuAction;
use TYPO3\CMS\Backend\ContextMenu\ContextMenuActionCollection;
use TYPO3\CMS\Backend\Tree\TreeNode;

Nicole Cordes's avatar
Nicole Cordes committed
25

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
 * Abstract Context Menu Data Provider
 */
abstract class AbstractContextMenuDataProvider {

	/**
	 * List of actions that are generally disabled
	 *
	 * @var array
	 */
	protected $disableItems = array();

	/**
	 * Context Menu Type (e.g. table.pages, table.tt_content)
	 *
	 * @var string
	 */
	protected $contextMenuType = '';

	/**
	 * Returns the context menu type
	 *
	 * @return string
	 */
	public function getContextMenuType() {
		return $this->contextMenuType;
	}

	/**
	 * Sets the context menu type
	 *
	 * @param string $contextMenuType
	 * @return void
	 */
	public function setContextMenuType($contextMenuType) {
		$this->contextMenuType = $contextMenuType;
	}

	/**
	 * Returns the actions of the node
	 *
67
68
	 * @param TreeNode $node
	 * @return ContextMenuActionCollection
69
	 */
70
	abstract public function getActionsForNode(TreeNode $node);
71
72
73
74
75
76
77

	/**
	 * Returns the configuration of the specified context menu type
	 *
	 * @return array
	 */
	protected function getConfiguration() {
78
		$contextMenuActions = $this->getBackendUser()->getTSConfig('options.contextMenu.' . $this->contextMenuType . '.items');
79
80
81
82
83
84
85
86
87
88
89
		return $contextMenuActions['properties'];
	}

	/**
	 * Evaluates a given display condition and returns TRUE if the condition matches
	 *
	 * Examples:
	 * getContextInfo|inCutMode:1 || isInCopyMode:1
	 * isLeafNode:1
	 * isLeafNode:1 && isInCutMode:1
	 *
90
	 * @param TreeNode $node
91
	 * @param string $displayCondition
92
	 * @return bool
93
	 */
94
	protected function evaluateDisplayCondition(TreeNode $node, $displayCondition) {
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
		if ($displayCondition === '') {
			return TRUE;
		}
		// Parse condition string
		$conditions = array();
		preg_match_all('/(.+?)(>=|<=|!=|=|>|<)(.+?)(\\|\\||&&|$)/is', $displayCondition, $conditions);
		$lastResult = FALSE;
		$chainType = '';
		$amountOfConditions = count($conditions[0]);
		for ($i = 0; $i < $amountOfConditions; ++$i) {
			// Check method for existence
			$method = trim($conditions[1][$i]);
			list($method, $index) = explode('|', $method);
			if (!method_exists($node, $method)) {
				continue;
			}
			// Fetch compare value
			$returnValue = call_user_func(array($node, $method));
			if (is_array($returnValue)) {
				$returnValue = $returnValue[$index];
			}
			// Compare fetched and expected values
			$operator = trim($conditions[2][$i]);
			$expected = trim($conditions[3][$i]);
			if ($operator === '=') {
				$returnValue = $returnValue == $expected;
			} elseif ($operator === '>') {
				$returnValue = $returnValue > $expected;
			} elseif ($operator === '<') {
				$returnValue = $returnValue < $expected;
			} elseif ($operator === '>=') {
				$returnValue = $returnValue >= $expected;
			} elseif ($operator === '<=') {
				$returnValue = $returnValue <= $expected;
			} elseif ($operator === '!=') {
				$returnValue = $returnValue != $expected;
			} else {
				$returnValue = FALSE;
				$lastResult = FALSE;
			}
			// Chain last result and the current if requested
			if ($chainType === '||') {
				$lastResult = $lastResult || $returnValue;
			} elseif ($chainType === '&&') {
				$lastResult = $lastResult && $returnValue;
			} else {
				$lastResult = $returnValue;
			}
			// Save chain type for the next condition
			$chainType = trim($conditions[4][$i]);
		}
		return $lastResult;
	}

	/**
	 * Returns the next context menu level
	 *
	 * @param array $actions
153
	 * @param TreeNode $node
154
	 * @param int $level
155
	 * @return ContextMenuActionCollection
156
	 */
157
158
159
160
161
	protected function getNextContextMenuLevel(array $actions, TreeNode $node, $level = 0) {
		/** @var $actionCollection ContextMenuActionCollection */
		$actionCollection = GeneralUtility::makeInstance(ContextMenuActionCollection::class);
		/** @var $iconFactory IconFactory */
		$iconFactory = GeneralUtility::makeInstance(IconFactory::class);
162
163
164
165
166
167
168
169
170
171
172
173
174
175
		if ($level > 5) {
			return $actionCollection;
		}
		$type = '';
		foreach ($actions as $index => $actionConfiguration) {
			if (substr($index, -1) !== '.') {
				$type = $actionConfiguration;
				if ($type !== 'DIVIDER') {
					continue;
				}
			}
			if (!in_array($type, array('DIVIDER', 'SUBMENU', 'ITEM'))) {
				continue;
			}
176
177
			/** @var $action ContextMenuAction */
			$action = GeneralUtility::makeInstance(ContextMenuAction::class);
178
179
180
181
			$action->setId($index);
			if ($type === 'DIVIDER') {
				$action->setType('divider');
			} else {
182
				if (in_array($actionConfiguration['name'], $this->disableItems) || isset($actionConfiguration['displayCondition']) && trim($actionConfiguration['displayCondition']) !== '' && !$this->evaluateDisplayCondition($node, $actionConfiguration['displayCondition'])) {
183
184
185
					unset($action);
					continue;
				}
186
				$label = $this->getLanguageService()->sL($actionConfiguration['label'], TRUE);
187
188
189
190
191
192
193
				if ($type === 'SUBMENU') {
					$action->setType('submenu');
					$action->setChildActions($this->getNextContextMenuLevel($actionConfiguration, $node, $level + 1));
				} else {
					$action->setType('action');
					$action->setCallbackAction($actionConfiguration['callbackAction']);
					if (is_array($actionConfiguration['customAttributes.'])) {
194
195
196
						if (!empty($actionConfiguration['customAttributes.']['contentUrl'])) {
							$actionConfiguration['customAttributes.']['contentUrl'] = $this->replaceModuleTokenInContentUrl($actionConfiguration['customAttributes.']['contentUrl']);
						}
197
198
199
200
						$action->setCustomAttributes($actionConfiguration['customAttributes.']);
					}
				}
				$action->setLabel($label);
201
202
				if (!isset($actionConfiguration['iconName'])) {
					$actionConfiguration['iconName'] = 'miscellaneous-placeholder';
203
				}
204
				$action->setIcon($iconFactory->getIcon($actionConfiguration['iconName'], Icon::SIZE_SMALL)->render());
205
			}
206
			$actionCollection->offsetSet($level . (int)$index, $action);
207
208
209
210
211
			$actionCollection->ksort();
		}
		return $actionCollection;
	}

212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
	/**
	 * Add the CSRF token to the module URL if a "M" parameter is found
	 *
	 * @param string $contentUrl
	 * @return string
	 */
	protected function replaceModuleTokenInContentUrl($contentUrl) {
		$parsedUrl = parse_url($contentUrl);
		parse_str($parsedUrl['query'], $urlParameters);
		if (isset($urlParameters['M'])) {
			$moduleName = $urlParameters['M'];
			unset($urlParameters['M']);
			$contentUrl = BackendUtility::getModuleUrl($moduleName, $urlParameters);
		}
		return $contentUrl;
	}
228

229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
	/**
	 * Returns LanguageService
	 *
	 * @return \TYPO3\CMS\Lang\LanguageService
	 */
	protected function getLanguageService() {
		return $GLOBALS['LANG'];
	}

	/**
	 * Returns the current BE user.
	 *
	 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
	 */
	protected function getBackendUser() {
		return $GLOBALS['BE_USER'];
	}

247
}