145a1ff3f45aefcb0335f4e17712ecf4d17144e3
[Packages/TYPO3.CMS.git] / typo3 / sysext / pagetree / classes / contextmenu / class.tx_pagetree_contextmenu_dataprovider.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010 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 * Context Menu Data Provider for the Page Tree
30 *
31 * @author Stefan Galinski <stefan.galinski@gmail.com>
32 * @package TYPO3
33 * @subpackage t3lib
34 */
35 class tx_pagetree_ContextMenu_DataProvider extends t3lib_contextmenu_AbstractDataProvider {
36 /**
37 * Old Context Menu Options (access mapping)
38 *
39 * @var array
40 */
41 protected $contextMenuMapping = array(
42 // @todo should be used (compatibility)
43 'view' => 'canBeViewed',
44 'edit' => 'canBeEdited',
45 'new' => 'canCreateNewPages',
46 'info' => 'canShowInfo',
47 'copy' => 'canBeCopied',
48 'cut' => 'canBeCut',
49 'paste' => 'canBePasted',
50 'move_wizard' => 'canBeMoved',
51 'new_wizard' => 'dsfdsfdsf',
52 'mount_as_treeroot' => 'canBeTemporaryMountPoint',
53 'hide' => 'canBeDisabled',
54 'delete' => 'canBeRemoved',
55 'history' => 'canShowHistory',
56 );
57
58 /**
59 * Returns the actions for the node
60 *
61 * @param tx_pagetree_Node $node
62 * @return t3lib_contextmenu_ActionCollection
63 */
64 public function getActionsForNode(t3lib_tree_Node $node) {
65 $contextMenuActions = $this->getNextContextMenuLevel($this->getConfiguration(), $node);
66
67 return $contextMenuActions;
68 }
69
70 /**
71 * Evaluates a given display condition and returns true if the condition matches
72 *
73 * Examples:
74 * getContextInfo|inCutMode:1 || isInCopyMode:1
75 * isLeafNode:1
76 * isLeafNode:1 && isInCutMode:1
77 *
78 * @param tx_pagetree_Node $node
79 * @param string $displayCondition
80 * @return boolean
81 */
82 protected function evaluateDisplayCondition(tx_pagetree_Node $node, $displayCondition) {
83 if ($displayCondition === '') {
84 return TRUE;
85 }
86
87 // parse condition string
88 $conditions = array();
89 preg_match_all('/(.+?)(>=|<=|!=|=|>|<)(.+?)(\|\||&&|$)/is', $displayCondition, $conditions);
90
91 $lastResult = FALSE;
92 $chainType = '';
93 $amountOfConditions = count($conditions[0]);
94 for ($i = 0; $i < $amountOfConditions; ++$i) {
95 // check method for existence
96 $method = trim($conditions[1][$i]);
97 list($method, $index) = explode('|', $method);
98 if (!method_exists($node, $method)) {
99 continue;
100 }
101
102 // fetch compare value
103 $returnValue = call_user_func(array($node, $method));
104 if (is_array($returnValue)) {
105 $returnValue = $returnValue[$index];
106 }
107
108 // compare fetched and expected values
109 $operator = trim($conditions[2][$i]);
110 $expected = trim($conditions[3][$i]);
111 if ($operator === '=') {
112 $returnValue = ($returnValue == $expected);
113 } elseif ($operator === '>') {
114 $returnValue = ($returnValue > $expected);
115 } elseif ($operator === '<') {
116 $returnValue = ($returnValue < $expected);
117 } elseif ($operator === '>=') {
118 $returnValue = ($returnValue >= $expected);
119 } elseif ($operator === '<=') {
120 $returnValue = ($returnValue <= $expected);
121 } elseif ($operator === '!=') {
122 $returnValue = ($returnValue != $expected);
123 } else {
124 $returnValue = FALSE;
125 $lastResult = FALSE;
126 }
127
128 // chain last result and the current if requested
129 if ($chainType === '||') {
130 $lastResult = ($lastResult || $returnValue);
131 } elseif ($chainType === '&&') {
132 $lastResult = ($lastResult && $returnValue);
133 } else {
134 $lastResult = $returnValue;
135 }
136
137 // save chain type for the next condition
138 $chainType = trim($conditions[4][$i]);
139 }
140
141 return $lastResult;
142 }
143
144 /**
145 * Returns the next context menu level
146 *
147 * @param array $actions
148 * @param tx_pagetree_Node $node
149 * @param int $level
150 * @return t3lib_contextmenu_ActionCollection
151 */
152 protected function getNextContextMenuLevel(array $actions, tx_pagetree_Node $node, $level = 0) {
153 /** @var $actionCollection t3lib_contextmenu_ActionCollection */
154 $actionCollection = t3lib_div::makeInstance('t3lib_contextmenu_ActionCollection');
155
156 if ($level > 5) {
157 return $actionCollection;
158 }
159
160 $type = '';
161 foreach ($actions as $index => $actionConfiguration) {
162 if (substr($index, -1) !== '.') {
163 $type = $actionConfiguration;
164 if ($type !== 'DIVIDER') {
165 continue;
166 }
167 }
168
169 if (!in_array($type, array('DIVIDER', 'SUBMENU', 'ITEM'))) {
170 continue;
171 }
172
173 /** @var $action tx_pagetree_ContextMenu_Action */
174 $action = t3lib_div::makeInstance('tx_pagetree_ContextMenu_Action');
175 $action->setId($index);
176
177 if ($type === 'DIVIDER') {
178 $action->setType('divider');
179 } else {
180 if (isset($actionConfiguration['displayCondition'])
181 && trim($actionConfiguration['displayCondition']) !== ''
182 && !$this->evaluateDisplayCondition($node, $actionConfiguration['displayCondition'])
183 ) {
184 unset($action);
185 continue;
186 }
187
188 if ($type === 'SUBMENU') {
189 $label = $GLOBALS['LANG']->sL($actionConfiguration['label'], TRUE);
190 $action->setType('submenu');
191 $action->setChildActions(
192 $this->getNextContextMenuLevel($actionConfiguration, $node, ++$level)
193 );
194 } else {
195 $label = $GLOBALS['LANG']->sL($actionConfiguration['label'], TRUE);
196 $action->setType('action');
197 $action->setCallbackAction($actionConfiguration['callbackAction']);
198 }
199
200 $action->setLabel($label);
201 if (isset($actionConfiguration['icon']) && trim($actionConfiguration['icon']) !== '') {
202 $action->setIcon($actionConfiguration['icon']);
203 } elseif (isset($actionConfiguration['spriteIcon'])) {
204 $action->setClass(
205 t3lib_iconWorks::getSpriteIconClasses($actionConfiguration['spriteIcon'])
206 );
207 }
208 }
209
210 $actionCollection->append($action);
211 }
212
213 return $actionCollection;
214 }
215 }
216
217 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['typo3/sysext/pagetree/classes/class.tx_pagetree_contextmenu_dataprovider.php']) {
218 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['typo3/sysext/pagetree/classes/class.tx_pagetree_contextmenu_dataprovider.php']);
219 }
220
221 ?>