[TASK] Remove ext:dbal from installation steps
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Tree / Pagetree / ExtdirectTreeCommands.php
1 <?php
2 namespace TYPO3\CMS\Backend\Tree\Pagetree;
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
17 use TYPO3\CMS\Backend\Controller\UserSettingsController;
18 use TYPO3\CMS\Backend\Utility\BackendUtility;
19 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21
22 /**
23 * Commands for the Page tree
24 */
25 class ExtdirectTreeCommands
26 {
27 /**
28 * Visibly the page
29 *
30 * @param \stdClass $nodeData
31 * @return array
32 */
33 public function visiblyNode($nodeData)
34 {
35 /** @var $node PagetreeNode */
36 $node = GeneralUtility::makeInstance(PagetreeNode::class, (array)$nodeData);
37 try {
38 Commands::visiblyNode($node);
39 $newNode = Commands::getNode($node->getId());
40 $newNode->setLeaf($node->isLeafNode());
41 $returnValue = $newNode->toArray();
42 } catch (\Exception $exception) {
43 $returnValue = [
44 'success' => false,
45 'error' => $exception->getMessage()
46 ];
47 }
48 return $returnValue;
49 }
50
51 /**
52 * Hide the page
53 *
54 * @param \stdClass $nodeData
55 * @return array
56 */
57 public function disableNode($nodeData)
58 {
59 /** @var $node PagetreeNode */
60 $node = GeneralUtility::makeInstance(PagetreeNode::class, (array)$nodeData);
61 try {
62 Commands::disableNode($node);
63 $newNode = Commands::getNode($node->getId());
64 $newNode->setLeaf($node->isLeafNode());
65 $returnValue = $newNode->toArray();
66 } catch (\Exception $exception) {
67 $returnValue = [
68 'success' => false,
69 'message' => $exception->getMessage()
70 ];
71 }
72 return $returnValue;
73 }
74
75 /**
76 * Delete the page
77 *
78 * @param \stdClass $nodeData
79 * @return array
80 */
81 public function deleteNode($nodeData)
82 {
83 /** @var $node PagetreeNode */
84 $node = GeneralUtility::makeInstance(PagetreeNode::class, (array)$nodeData);
85 try {
86 Commands::deleteNode($node);
87 $returnValue = [];
88 if (static::getBackendUser()->workspace) {
89 $record = Commands::getNodeRecord($node->getId());
90 if ($record['_ORIG_uid']) {
91 $newNode = Commands::getNewNode($record);
92 $returnValue = $newNode->toArray();
93 }
94 }
95 } catch (\Exception $exception) {
96 $returnValue = [
97 'success' => false,
98 'message' => $exception->getMessage()
99 ];
100 }
101 return $returnValue;
102 }
103
104 /**
105 * Restore the page
106 *
107 * @param \stdClass $nodeData
108 * @param int $destination
109 * @return array
110 */
111 public function restoreNode($nodeData, $destination)
112 {
113 /** @var $node PagetreeNode */
114 $node = GeneralUtility::makeInstance(PagetreeNode::class, (array)$nodeData);
115 try {
116 Commands::restoreNode($node, $destination);
117 $newNode = Commands::getNode($node->getId());
118 $returnValue = $newNode->toArray();
119 } catch (\Exception $exception) {
120 $returnValue = [
121 'success' => false,
122 'message' => $exception->getMessage()
123 ];
124 }
125 return $returnValue;
126 }
127
128 /**
129 * Updates the given field with a new text value, may be used to inline update
130 * the title field in the new page tree
131 *
132 * @param \stdClass $nodeData
133 * @param string $updatedLabel
134 * @return array
135 */
136 public function updateLabel($nodeData, $updatedLabel)
137 {
138 if ($updatedLabel === '') {
139 return [];
140 }
141 /** @var $node PagetreeNode */
142 $node = GeneralUtility::makeInstance(PagetreeNode::class, (array)$nodeData);
143 try {
144 Commands::updateNodeLabel($node, $updatedLabel);
145 $shortendedText = GeneralUtility::fixed_lgd_cs($updatedLabel, (int)static::getBackendUser()->uc['titleLen']);
146 $returnValue = [
147 'editableText' => $updatedLabel,
148 'updatedText' => htmlspecialchars($shortendedText)
149 ];
150 } catch (\Exception $exception) {
151 $returnValue = [
152 'success' => false,
153 'message' => $exception->getMessage()
154 ];
155 }
156 return $returnValue;
157 }
158
159 /**
160 * Clear cache of the page
161 *
162 * @param \stdClass $nodeData
163 * @return void
164 */
165 public static function clearCacheOfPage($nodeData)
166 {
167 $node = GeneralUtility::makeInstance(PagetreeNode::class, (array)$nodeData);
168
169 $permissionClause = static::getBackendUser()->getPagePermsClause(1);
170 $access = BackendUtility::readPageAccess($node->getId(), $permissionClause);
171 if ($access) {
172 $dataHandler = GeneralUtility::makeInstance(\TYPO3\CMS\Core\DataHandling\DataHandler::class);
173 $dataHandler->start([], []);
174 $dataHandler->clear_cacheCmd($node->getId());
175 }
176 }
177
178 /**
179 * Sets a temporary mount point
180 *
181 * @param \stdClass $nodeData
182 * @return array
183 */
184 public static function setTemporaryMountPoint($nodeData)
185 {
186 /** @var $node PagetreeNode */
187 $node = GeneralUtility::makeInstance(PagetreeNode::class, (array)$nodeData);
188 static::getBackendUser()->uc['pageTree_temporaryMountPoint'] = $node->getId();
189 static::getBackendUser()->writeUC(static::getBackendUser()->uc);
190 return Commands::getMountPointPath();
191 }
192
193 /**
194 * Moves the source node directly as the first child of the destination node
195 *
196 * @param \stdClass $nodeData
197 * @param int $destination
198 * @return array
199 */
200 public function moveNodeToFirstChildOfDestination($nodeData, $destination)
201 {
202 /** @var $node PagetreeNode */
203 $node = GeneralUtility::makeInstance(PagetreeNode::class, (array)$nodeData);
204 try {
205 Commands::moveNode($node, $destination);
206 $newNode = Commands::getNode($node->getId(), false);
207 $newNode->setLeaf($node->isLeafNode());
208 $returnValue = $newNode->toArray();
209 } catch (\Exception $exception) {
210 $returnValue = [
211 'success' => false,
212 'message' => $exception->getMessage()
213 ];
214 }
215 return $returnValue;
216 }
217
218 /**
219 * Moves the source node directly after the destination node
220 *
221 * @param \stdClass $nodeData
222 * @param int $destination
223 * @return array
224 */
225 public function moveNodeAfterDestination($nodeData, $destination)
226 {
227 /** @var $node PagetreeNode */
228 $node = GeneralUtility::makeInstance(PagetreeNode::class, (array)$nodeData);
229 try {
230 Commands::moveNode($node, -$destination);
231 $newNode = Commands::getNode($node->getId(), false);
232 $newNode->setLeaf($node->isLeafNode());
233 $returnValue = $newNode->toArray();
234 } catch (\Exception $exception) {
235 $returnValue = [
236 'success' => false,
237 'message' => $exception->getMessage()
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 {
253 /** @var $node PagetreeNode */
254 $node = GeneralUtility::makeInstance(PagetreeNode::class, (array)$nodeData);
255 try {
256 $newPageId = Commands::copyNode($node, $destination);
257 $newNode = Commands::getNode($newPageId);
258 $newNode->setLeaf($node->isLeafNode());
259 $returnValue = $newNode->toArray();
260 } catch (\Exception $exception) {
261 $returnValue = [
262 'success' => false,
263 'message' => $exception->getMessage()
264 ];
265 }
266 return $returnValue;
267 }
268
269 /**
270 * Copies the source node directly after the destination node and returns the
271 * created node.
272 *
273 * @param \stdClass $nodeData
274 * @param int $destination
275 * @return array
276 */
277 public function copyNodeAfterDestination($nodeData, $destination)
278 {
279 /** @var $node PagetreeNode */
280 $node = GeneralUtility::makeInstance(PagetreeNode::class, (array)$nodeData);
281 try {
282 $newPageId = Commands::copyNode($node, -$destination);
283 $newNode = Commands::getNode($newPageId);
284 $newNode->setLeaf($node->isLeafNode());
285 $returnValue = $newNode->toArray();
286 } catch (\Exception $exception) {
287 $returnValue = [
288 'success' => false,
289 'message' => $exception->getMessage()
290 ];
291 }
292 return $returnValue;
293 }
294
295 /**
296 * Inserts a new node as the first child node of the destination node and returns the created node.
297 *
298 * @param \stdClass $parentNodeData
299 * @param int $pageType
300 * @return array
301 */
302 public function insertNodeToFirstChildOfDestination($parentNodeData, $pageType)
303 {
304 /** @var $parentNode PagetreeNode */
305 $parentNode = GeneralUtility::makeInstance(PagetreeNode::class, (array)$parentNodeData);
306 try {
307 $newPageId = Commands::createNode($parentNode, $parentNode->getId(), $pageType);
308 $returnValue = Commands::getNode($newPageId)->toArray();
309 } catch (\Exception $exception) {
310 $returnValue = [
311 'success' => false,
312 'message' => $exception->getMessage()
313 ];
314 }
315 return $returnValue;
316 }
317
318 /**
319 * Inserts a new node directly after the destination node and returns the created node.
320 *
321 * @param \stdClass $parentNodeData
322 * @param int $destination
323 * @param int $pageType
324 * @return array
325 */
326 public function insertNodeAfterDestination($parentNodeData, $destination, $pageType)
327 {
328 /** @var $parentNode PagetreeNode */
329 $parentNode = GeneralUtility::makeInstance(PagetreeNode::class, (array)$parentNodeData);
330 try {
331 $newPageId = Commands::createNode($parentNode, -$destination, $pageType);
332 $returnValue = Commands::getNode($newPageId)->toArray();
333 } catch (\Exception $exception) {
334 $returnValue = [
335 'success' => false,
336 'message' => $exception->getMessage()
337 ];
338 }
339 return $returnValue;
340 }
341
342 /**
343 * Returns the view link of a given node
344 *
345 * @param \stdClass $nodeData
346 * @return string
347 */
348 public static function getViewLink($nodeData)
349 {
350 /** @var $node PagetreeNode */
351 $node = GeneralUtility::makeInstance(PagetreeNode::class, (array)$nodeData);
352 $javascriptLink = BackendUtility::viewOnClick($node->getId());
353 $extractedLink = '';
354 if (preg_match('/window\\.open\\(\'([^\']+)\'/i', $javascriptLink, $match)) {
355 // Clean JSON-serialized ampersands ('&')
356 // @see GeneralUtility::quoteJSvalue()
357 $extractedLink = json_decode('"' . trim($match[1], '"') . '"', JSON_HEX_AMP);
358 };
359 return $extractedLink;
360 }
361
362 /**
363 * Adds the rootline of a given node to the tree expansion state and adds the node
364 * itself as the current selected page. This leads to the expansion and selection of
365 * the node in the tree after a refresh.
366 *
367 * @static
368 * @param string $stateId
369 * @param int $nodeId
370 * @return array
371 */
372 public static function addRootlineOfNodeToStateHash($stateId, $nodeId)
373 {
374 $mountPoints = array_map('intval', static::getBackendUser()->returnWebmounts());
375 if (empty($mountPoints)) {
376 $mountPoints = [0];
377 }
378 if (!empty(static::getBackendUser()->uc['pageTree_temporaryMountPoint'])) {
379 $mountPoints[] = (int)static::getBackendUser()->uc['pageTree_temporaryMountPoint'];
380 }
381 $mountPoints = array_unique($mountPoints);
382 /** @var $userSettingsController UserSettingsController */
383 $userSettingsController = GeneralUtility::makeInstance(UserSettingsController::class);
384 $state = $userSettingsController->process('get', 'BackendComponents.States.' . $stateId);
385 if (empty($state)) {
386 $state = new \stdClass();
387 $state->stateHash = new \stdClass();
388 }
389 $state->stateHash = (object)$state->stateHash;
390 $rootline = BackendUtility::BEgetRootLine($nodeId, '', (int)static::getBackendUser()->workspace !== 0);
391 $rootlineIds = [];
392 foreach ($rootline as $pageData) {
393 $rootlineIds[] = (int)$pageData['uid'];
394 }
395 foreach ($mountPoints as $mountPoint) {
396 if (!in_array($mountPoint, $rootlineIds, true)) {
397 continue;
398 }
399 $isFirstNode = true;
400 foreach ($rootline as $pageData) {
401 $node = Commands::getNewNode($pageData, $mountPoint);
402 if ($isFirstNode) {
403 $isFirstNode = false;
404 $state->stateHash->lastSelectedNode = $node->calculateNodeId();
405 } else {
406 $state->stateHash->{$node->calculateNodeId('')} = 1;
407 }
408 }
409 }
410 $userSettingsController->process('set', 'BackendComponents.States.' . $stateId, $state);
411 return (array)$state->stateHash;
412 }
413
414 /**
415 * Gets the path steps for a given page.
416 * This methods considers multiple mount points,
417 * thus the returned array is multidimensional, e.g.
418 *
419 * array(
420 * array('p0', 'p1', 'p13', 'p44'),
421 * array('p0', 'p13-1', 'p44-1'),
422 * )
423 *
424 * @param int $pageId
425 * @return array
426 */
427 public static function getNodePaths($pageId)
428 {
429 $pagePaths = [];
430 $mountPoints = array_map('intval', static::getBackendUser()->returnWebmounts());
431 if (empty($mountPoints)) {
432 $mountPoints = [0];
433 }
434 $mountPoints[] = (int)static::getBackendUser()->uc['pageTree_temporaryMountPoint'];
435 $mountPoints = array_unique($mountPoints);
436 $rootLine = BackendUtility::BEgetRootLine($pageId, '', (int)static::getBackendUser()->workspace !== 0);
437 $rootLineIds = [];
438 foreach ($rootLine as $rootLineLevel) {
439 $rootLineIds[] = (int)$rootLineLevel['uid'];
440 }
441 foreach ($mountPoints as $mountPoint) {
442 $pagePath = [];
443 if (!in_array($mountPoint, $rootLineIds, true)) {
444 continue;
445 }
446 foreach ($rootLine as $rootLineLevel) {
447 $node = Commands::getNewNode($rootLineLevel, $mountPoint);
448 array_unshift($pagePath, $node->calculateNodeId());
449 // Break if mount-point has been reached
450 if ($mountPoint === (int)$rootLineLevel['uid']) {
451 break;
452 }
453 }
454 // Attach valid partial root-lines
455 if (!empty($pagePath)) {
456 if ($mountPoint !== 0) {
457 array_unshift($pagePath, Commands::getNewNode(['uid' => 0])->calculateNodeId());
458 }
459 $pagePaths[] = $pagePath;
460 }
461 }
462 return $pagePaths;
463 }
464
465 /**
466 * @return BackendUserAuthentication
467 */
468 protected static function getBackendUser()
469 {
470 return $GLOBALS['BE_USER'];
471 }
472 }