7d20b32f05c81d3eb1b88d300adfff85a8af54ba
[Packages/TYPO3.CMS.git] / t3lib / tree / pagetree / extdirect / class.t3lib_tree_pagetree_extdirect_commands.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 * Commands for the Page tree
30 *
31 * @author Stefan Galinski <stefan.galinski@gmail.com>
32 * @package TYPO3
33 * @subpackage t3lib
34 */
35 class t3lib_tree_pagetree_extdirect_Commands {
36 /**
37 * Visibly the page
38 *
39 * @param stdClass $nodeData
40 * @return array
41 */
42 public function visiblyNode($nodeData) {
43 /** @var $node t3lib_tree_pagetree_Node */
44 $node = t3lib_div::makeInstance('t3lib_tree_pagetree_Node', (array) $nodeData);
45
46 try {
47 t3lib_tree_pagetree_Commands::visiblyNode($node);
48 $newNode = t3lib_tree_pagetree_Commands::getNode($node->getId());
49 $newNode->setLeaf($node->isLeafNode());
50 $returnValue = $newNode->toArray();
51 } catch (Exception $exception) {
52 $returnValue = array(
53 'success' => FALSE,
54 'error' => $exception->getMessage(),
55 );
56 }
57
58 return $returnValue;
59 }
60
61 /**
62 * Hide the page
63 *
64 * @param stdClass $nodeData
65 * @return array
66 */
67 public function disableNode($nodeData) {
68 /** @var $node t3lib_tree_pagetree_Node */
69 $node = t3lib_div::makeInstance('t3lib_tree_pagetree_Node', (array) $nodeData);
70
71 try {
72 t3lib_tree_pagetree_Commands::disableNode($node);
73 $newNode = t3lib_tree_pagetree_Commands::getNode($node->getId());
74 $newNode->setLeaf($node->isLeafNode());
75 $returnValue = $newNode->toArray();
76 } catch (Exception $exception) {
77 $returnValue = array(
78 'success' => FALSE,
79 'message' => $exception->getMessage(),
80 );
81 }
82
83 return $returnValue;
84 }
85
86 /**
87 * Delete the page
88 *
89 * @param stdClass $nodeData
90 * @return array
91 */
92 public function deleteNode($nodeData) {
93 /** @var $node t3lib_tree_pagetree_Node */
94 $node = t3lib_div::makeInstance('t3lib_tree_pagetree_Node', (array) $nodeData);
95
96 try {
97 t3lib_tree_pagetree_Commands::deleteNode($node);
98
99 $returnValue = array();
100 if ($GLOBALS['BE_USER']->workspace) {
101 $record = t3lib_tree_pagetree_Commands::getNodeRecord($node->getId());
102 if ($record['_ORIG_uid']) {
103 $newNode = t3lib_tree_pagetree_Commands::getNewNode($record);
104 $returnValue = $newNode->toArray();
105 }
106 }
107 } catch (Exception $exception) {
108 $returnValue = array(
109 'success' => FALSE,
110 'message' => $exception->getMessage(),
111 );
112 }
113
114 return $returnValue;
115 }
116
117 /**
118 * Restore the page
119 *
120 * @param stdClass $nodeData
121 * @param int $destination
122 * @return array
123 */
124 public function restoreNode($nodeData, $destination) {
125 /** @var $node t3lib_tree_pagetree_Node */
126 $node = t3lib_div::makeInstance('t3lib_tree_pagetree_Node', (array) $nodeData);
127
128 try {
129 t3lib_tree_pagetree_Commands::restoreNode($node, $destination);
130 $newNode = t3lib_tree_pagetree_Commands::getNode($node->getId());
131 $returnValue = $newNode->toArray();
132 } catch (Exception $exception) {
133 $returnValue = array(
134 'success' => FALSE,
135 'message' => $exception->getMessage(),
136 );
137 }
138
139 return $returnValue;
140 }
141
142 /**
143 * Updates the given field with a new text value, may be used to inline update
144 * the title field in the new page tree
145 *
146 * @param stdClass $nodeData
147 * @param string $updatedLabel
148 * @return array
149 */
150 public function updateLabel($nodeData, $updatedLabel) {
151 if ($updatedLabel === '') {
152 return array();
153 }
154
155 /** @var $node t3lib_tree_pagetree_Node */
156 $node = t3lib_div::makeInstance('t3lib_tree_pagetree_Node', (array) $nodeData);
157
158 try {
159 t3lib_tree_pagetree_Commands::updateNodeLabel($node, $updatedLabel);
160
161 $shortendedText = t3lib_div::fixed_lgd_cs($updatedLabel, intval($GLOBALS['BE_USER']->uc['titleLen']));
162 $returnValue = array(
163 'editableText' => $updatedLabel,
164 'updatedText' => htmlspecialchars($shortendedText),
165 );
166 } catch (Exception $exception) {
167 $returnValue = array(
168 'success' => FALSE,
169 'message' => $exception->getMessage(),
170 );
171 }
172
173 return $returnValue;
174 }
175
176 /**
177 * Sets a temporary mount point
178 *
179 * @param stdClass $nodeData
180 * @return array
181 */
182 public static function setTemporaryMountPoint($nodeData) {
183 /** @var $node t3lib_tree_pagetree_Node */
184 $node = t3lib_div::makeInstance('t3lib_tree_pagetree_Node', (array) $nodeData);
185 $GLOBALS['BE_USER']->uc['pageTree_temporaryMountPoint'] = $node->getId();
186 $GLOBALS['BE_USER']->writeUC($GLOBALS['BE_USER']->uc);
187
188 return t3lib_tree_pagetree_Commands::getMountPointPath();
189 }
190
191 /**
192 * Moves the source node directly as the first child of the destination node
193 *
194 * @param stdClass $nodeData
195 * @param int $destination
196 * @return array
197 */
198 public function moveNodeToFirstChildOfDestination($nodeData, $destination) {
199 /** @var $node t3lib_tree_pagetree_Node */
200 $node = t3lib_div::makeInstance('t3lib_tree_pagetree_Node', (array) $nodeData);
201
202 try {
203 t3lib_tree_pagetree_Commands::moveNode($node, $destination);
204 $newNode = t3lib_tree_pagetree_Commands::getNode($node->getId(), FALSE);
205 $newNode->setLeaf($node->isLeafNode());
206 $returnValue = $newNode->toArray();
207 } catch (Exception $exception) {
208 $returnValue = array(
209 'success' => FALSE,
210 'message' => $exception->getMessage(),
211 );
212 }
213
214 return $returnValue;
215 }
216
217 /**
218 * Moves the source node directly after the destination node
219 *
220 * @param stdClass $nodeData
221 * @param int $destination
222 * @return void
223 */
224 public function moveNodeAfterDestination($nodeData, $destination) {
225 /** @var $node t3lib_tree_pagetree_Node */
226 $node = t3lib_div::makeInstance('t3lib_tree_pagetree_Node', (array) $nodeData);
227
228 try {
229 t3lib_tree_pagetree_Commands::moveNode($node, -$destination);
230 $newNode = t3lib_tree_pagetree_Commands::getNode($node->getId(), FALSE);
231 $newNode->setLeaf($node->isLeafNode());
232 $returnValue = $newNode->toArray();
233 } catch (Exception $exception) {
234 $returnValue = array(
235 'success' => FALSE,
236 'message' => $exception->getMessage(),
237 );
238 }
239
240 return $returnValue;
241 }
242
243 /**
244 * Copies the source node directly as the first child of the destination node and
245 * returns the created node.
246 *
247 * @param stdClass $nodeData
248 * @param int $destination
249 * @return array
250 */
251 public function copyNodeToFirstChildOfDestination($nodeData, $destination) {
252 /** @var $node t3lib_tree_pagetree_Node */
253 $node = t3lib_div::makeInstance('t3lib_tree_pagetree_Node', (array) $nodeData);
254
255 /** @var $dataProvider t3lib_tree_pagetree_DataProvider */
256 $dataProvider = t3lib_div::makeInstance('t3lib_tree_pagetree_DataProvider');
257
258 try {
259 $newPageId = t3lib_tree_pagetree_Commands::copyNode($node, $destination);
260 $newNode = t3lib_tree_pagetree_Commands::getNode($newPageId);
261 $newNode->setLeaf($node->isLeafNode());
262 $returnValue = $newNode->toArray();
263 } catch (Exception $exception) {
264 $returnValue = array(
265 'success' => FALSE,
266 'message' => $exception->getMessage(),
267 );
268 }
269
270 return $returnValue;
271 }
272
273 /**
274 * Copies the source node directly after the destination node and returns the
275 * created node.
276 *
277 * @param stdClass $nodeData
278 * @param int $destination
279 * @return array
280 */
281 public function copyNodeAfterDestination($nodeData, $destination) {
282 /** @var $node t3lib_tree_pagetree_Node */
283 $node = t3lib_div::makeInstance('t3lib_tree_pagetree_Node', (array) $nodeData);
284
285 /** @var $dataProvider t3lib_tree_pagetree_DataProvider */
286 $dataProvider = t3lib_div::makeInstance('t3lib_tree_pagetree_DataProvider');
287
288 try {
289 $newPageId = t3lib_tree_pagetree_Commands::copyNode($node, -$destination);
290 $newNode = t3lib_tree_pagetree_Commands::getNode($newPageId);
291 $newNode->setLeaf($node->isLeafNode());
292 $returnValue = $newNode->toArray();
293 } catch (Exception $exception) {
294 $returnValue = array(
295 'success' => FALSE,
296 'message' => $exception->getMessage(),
297 );
298 }
299
300 return $returnValue;
301 }
302
303 /**
304 * Inserts a new node as the first child node of the destination node and returns the created node.
305 *
306 * @param stdClass $parentNodeData
307 * @param int $pageType
308 * @return array
309 */
310 public function insertNodeToFirstChildOfDestination($parentNodeData, $pageType) {
311 /** @var $parentNode t3lib_tree_pagetree_Node */
312 $parentNode = t3lib_div::makeInstance('t3lib_tree_pagetree_Node', (array) $parentNodeData);
313
314 try {
315 $newPageId = t3lib_tree_pagetree_Commands::createNode($parentNode, $parentNode->getId(), $pageType);
316 $returnValue = t3lib_tree_pagetree_Commands::getNode($newPageId)->toArray();
317 } catch (Exception $exception) {
318 $returnValue = array(
319 'success' => FALSE,
320 'message' => $exception->getMessage(),
321 );
322 }
323
324 return $returnValue;
325 }
326
327 /**
328 * Inserts a new node directly after the destination node and returns the created node.
329 *
330 * @param stdClass $parentNodeData
331 * @param int $destination
332 * @param int $pageType
333 * @return array
334 */
335 public function insertNodeAfterDestination($parentNodeData, $destination, $pageType) {
336 /** @var $parentNode t3lib_tree_pagetree_Node */
337 $parentNode = t3lib_div::makeInstance('t3lib_tree_pagetree_Node', (array) $parentNodeData);
338
339 try {
340 $newPageId = t3lib_tree_pagetree_Commands::createNode($parentNode, -$destination, $pageType);
341 $returnValue = t3lib_tree_pagetree_Commands::getNode($newPageId)->toArray();
342 } catch (Exception $exception) {
343 $returnValue = array(
344 'success' => FALSE,
345 'message' => $exception->getMessage(),
346 );
347 }
348
349 return $returnValue;
350 }
351
352 /**
353 * Returns the view link of a given node
354 *
355 * @param stdClass $nodeData
356 * @return string
357 */
358 public static function getViewLink($nodeData) {
359 /** @var $node t3lib_tree_pagetree_Node */
360 $node = t3lib_div::makeInstance('t3lib_tree_pagetree_Node', (array) $nodeData);
361
362 $javascriptLink = t3lib_BEfunc::viewOnClick($node->getId());
363 preg_match('/window\.open\(\'([^\']+)\'/i', $javascriptLink, $match);
364
365 return $match[1];
366 }
367
368 /**
369 * Adds the rootline of a given node to the tree expansion state and adds the node
370 * itself as the current selected page. This leads to the expansion and selection of
371 * the node in the tree after a refresh.
372 *
373 * @static
374 * @param string $stateId
375 * @param int $nodeId
376 * @return array
377 */
378 public static function addRootlineOfNodeToStateHash($stateId, $nodeId) {
379 $mountPoints = array_map('intval', $GLOBALS['BE_USER']->returnWebmounts());
380 if (count($mountPoints) == 0) {
381 $mountPoints = array(0);
382 }
383 $mountPoints[] = intval($GLOBALS['BE_USER']->uc['pageTree_temporaryMountPoint']);
384 $mountPoints = array_unique($mountPoints);
385
386 /** @var $userSettings extDirect_DataProvider_BackenduserSettings */
387 $userSettings = t3lib_div::makeInstance('extDirect_DataProvider_BackenduserSettings');
388 $state = $userSettings->get('BackendComponents.States.' . $stateId);
389 $state->stateHash = (object) $state->stateHash;
390
391 $rootline = t3lib_BEfunc::BEgetRootLine($nodeId, '', ($GLOBALS['BE_USER']->workspace != 0));
392 $rootlineIds = array();
393 foreach ($rootline as $pageData) {
394 $rootlineIds[] = intval($pageData['uid']);
395 }
396
397 foreach ($mountPoints as $mountPoint) {
398 if (!in_array($mountPoint, $rootlineIds, TRUE)) {
399 continue;
400 }
401
402 $isFirstNode = TRUE;
403 foreach ($rootline as $pageData) {
404 $node = t3lib_tree_pagetree_Commands::getNewNode($pageData, $mountPoint);
405
406 if ($isFirstNode) {
407 $isFirstNode = FALSE;
408 $state->stateHash->lastSelectedNode = $node->calculateNodeId();
409 } else {
410 $state->stateHash->{$node->calculateNodeId('')} = 1;
411 }
412 }
413 }
414
415 $userSettings->set('BackendComponents.States.' . $stateId, $state);
416
417 return (array)$state->stateHash;
418 }
419 }
420
421 ?>