Revert "[RELEASE] Release of TYPO3 4.6.0alpha1"
[Packages/TYPO3.CMS.git] / t3lib / contextmenu / class.t3lib_contextmenu_abstractdataprovider.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010-2011 TYPO3 Tree Team <http://forge.typo3.org/projects/typo3v4-extjstrees>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * Abstract Context Menu Data Provider
30 *
31 * @author Stefan Galinski <stefan.galinski@gmail.com>
32 * @package TYPO3
33 * @subpackage t3lib
34 */
35 abstract class t3lib_contextmenu_AbstractDataProvider {
36 /**
37 * List of actions that are generally disabled
38 *
39 * @var array
40 */
41 protected $disableItems = array();
42
43 /**
44 * Context Menu Type (e.g. table.pages, table.tt_content)
45 *
46 * @var string
47 */
48 protected $contextMenuType = '';
49
50 /**
51 * Returns the context menu type
52 *
53 * @return string
54 */
55 public function getContextMenuType() {
56 return $this->contextMenuType;
57 }
58
59 /**
60 * Sets the context menu type
61 *
62 * @param string $contextMenuType
63 * @return void
64 */
65 public function setContextMenuType($contextMenuType) {
66 $this->contextMenuType = $contextMenuType;
67 }
68
69 /**
70 * Returns the actions of the node
71 *
72 * @param t3lib_tree_Node $node
73 * @return t3lib_contextmenu_ActionCollection
74 */
75 abstract public function getActionsForNode(t3lib_tree_Node $node);
76
77 /**
78 * Returns the configuration of the specified context menu type
79 *
80 * @return array
81 */
82 protected function getConfiguration() {
83 $contextMenuActions = $GLOBALS['BE_USER']->getTSConfig(
84 'options.contextMenu.' . $this->contextMenuType . '.items'
85 );
86
87 return $contextMenuActions['properties'];
88 }
89
90 /**
91 * Evaluates a given display condition and returns TRUE if the condition matches
92 *
93 * Examples:
94 * getContextInfo|inCutMode:1 || isInCopyMode:1
95 * isLeafNode:1
96 * isLeafNode:1 && isInCutMode:1
97 *
98 * @param t3lib_tree_Node $node
99 * @param string $displayCondition
100 * @return boolean
101 */
102 protected function evaluateDisplayCondition(t3lib_tree_Node $node, $displayCondition) {
103 if ($displayCondition === '') {
104 return TRUE;
105 }
106
107 // parse condition string
108 $conditions = array();
109 preg_match_all('/(.+?)(>=|<=|!=|=|>|<)(.+?)(\|\||&&|$)/is', $displayCondition, $conditions);
110
111 $lastResult = FALSE;
112 $chainType = '';
113 $amountOfConditions = count($conditions[0]);
114 for ($i = 0; $i < $amountOfConditions; ++$i) {
115 // check method for existence
116 $method = trim($conditions[1][$i]);
117 list($method, $index) = explode('|', $method);
118 if (!method_exists($node, $method)) {
119 continue;
120 }
121
122 // fetch compare value
123 $returnValue = call_user_func(array($node, $method));
124 if (is_array($returnValue)) {
125 $returnValue = $returnValue[$index];
126 }
127
128 // compare fetched and expected values
129 $operator = trim($conditions[2][$i]);
130 $expected = trim($conditions[3][$i]);
131 if ($operator === '=') {
132 $returnValue = ($returnValue == $expected);
133 } elseif ($operator === '>') {
134 $returnValue = ($returnValue > $expected);
135 } elseif ($operator === '<') {
136 $returnValue = ($returnValue < $expected);
137 } elseif ($operator === '>=') {
138 $returnValue = ($returnValue >= $expected);
139 } elseif ($operator === '<=') {
140 $returnValue = ($returnValue <= $expected);
141 } elseif ($operator === '!=') {
142 $returnValue = ($returnValue != $expected);
143 } else {
144 $returnValue = FALSE;
145 $lastResult = FALSE;
146 }
147
148 // chain last result and the current if requested
149 if ($chainType === '||') {
150 $lastResult = ($lastResult || $returnValue);
151 } elseif ($chainType === '&&') {
152 $lastResult = ($lastResult && $returnValue);
153 } else {
154 $lastResult = $returnValue;
155 }
156
157 // save chain type for the next condition
158 $chainType = trim($conditions[4][$i]);
159 }
160
161 return $lastResult;
162 }
163
164 /**
165 * Returns the next context menu level
166 *
167 * @param array $actions
168 * @param t3lib_tree_Node $node
169 * @param int $level
170 * @return t3lib_contextmenu_ActionCollection
171 */
172 protected function getNextContextMenuLevel(array $actions, t3lib_tree_Node $node, $level = 0) {
173 /** @var $actionCollection t3lib_contextmenu_ActionCollection */
174 $actionCollection = t3lib_div::makeInstance('t3lib_contextmenu_ActionCollection');
175
176 if ($level > 5) {
177 return $actionCollection;
178 }
179
180 $type = '';
181 foreach ($actions as $index => $actionConfiguration) {
182 if (substr($index, -1) !== '.') {
183 $type = $actionConfiguration;
184 if ($type !== 'DIVIDER') {
185 continue;
186 }
187 }
188
189 if (!in_array($type, array('DIVIDER', 'SUBMENU', 'ITEM'))) {
190 continue;
191 }
192
193 /** @var $action t3lib_contextmenu_Action */
194 $action = t3lib_div::makeInstance('t3lib_contextmenu_Action');
195 $action->setId($index);
196
197 if ($type === 'DIVIDER') {
198 $action->setType('divider');
199 } else {
200 if (in_array($actionConfiguration['name'], $this->disableItems)
201 || (isset($actionConfiguration['displayCondition'])
202 && trim($actionConfiguration['displayCondition']) !== ''
203 && !$this->evaluateDisplayCondition($node, $actionConfiguration['displayCondition'])
204 )
205 ) {
206 unset($action);
207 continue;
208 }
209
210 $label = $GLOBALS['LANG']->sL($actionConfiguration['label'], TRUE);
211 if ($type === 'SUBMENU') {
212 $action->setType('submenu');
213 $action->setChildActions(
214 $this->getNextContextMenuLevel($actionConfiguration, $node, $level + 1)
215 );
216 } else {
217 $action->setType('action');
218 $action->setCallbackAction($actionConfiguration['callbackAction']);
219
220 if (is_array($actionConfiguration['customAttributes.'])) {
221 $action->setCustomAttributes($actionConfiguration['customAttributes.']);
222 }
223 }
224
225 $action->setLabel($label);
226 if (isset($actionConfiguration['icon']) && trim($actionConfiguration['icon']) !== '') {
227 $action->setIcon($actionConfiguration['icon']);
228 } elseif (isset($actionConfiguration['spriteIcon'])) {
229 $action->setClass(
230 t3lib_iconWorks::getSpriteIconClasses($actionConfiguration['spriteIcon'])
231 );
232 }
233 }
234
235 $actionCollection->offsetSet($level . intval($index), $action);
236 $actionCollection->ksort();
237 }
238
239 return $actionCollection;
240 }
241 }
242
243 ?>