8382f823ea9e4bdb93a9b8008cc9509a505bb5fa
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Tree / Pagetree / PagetreeNode.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\Core\Versioning\VersionState;
18
19 /**
20 * Node designated for the page tree
21 */
22 class PagetreeNode extends \TYPO3\CMS\Backend\Tree\ExtDirectNode
23 {
24 /**
25 * Cached access rights to save some performance
26 *
27 * @var array
28 */
29 protected $cachedAccessRights = [];
30
31 /**
32 * Workspace Overlay Id
33 *
34 * @var int
35 */
36 protected $workspaceId = 0;
37
38 /**
39 * Mount Point Id
40 *
41 * @var int
42 */
43 protected $mountPoint = 0;
44
45 /**
46 * Readable Rootline
47 *
48 * @var string
49 */
50 protected $readableRootline = '';
51
52 /**
53 * Indicator if the node is a mount point
54 *
55 * @var bool
56 */
57 protected $isMountPoint = false;
58
59 /**
60 * Indicator if the page tree should stop here
61 *
62 * @var bool
63 */
64 protected $stopPageTree = false;
65
66 /**
67 * Background color for the node
68 *
69 * @var string
70 */
71 protected $backgroundColor = '';
72
73 /**
74 * Sets the background color
75 *
76 * @param string $backgroundColor
77 * @return void
78 */
79 public function setBackgroundColor($backgroundColor)
80 {
81 $this->backgroundColor = $backgroundColor;
82 }
83
84 /**
85 * Returns the background color
86 *
87 * @return string
88 */
89 public function getBackgroundColor()
90 {
91 return $this->backgroundColor;
92 }
93
94 /**
95 * Set's the original id of the element
96 *
97 * @param int $workspaceId
98 * @return void
99 */
100 public function setWorkspaceId($workspaceId)
101 {
102 $this->workspaceId = (int)$workspaceId;
103 }
104
105 /**
106 * Returns the original id of the element
107 *
108 * @return int
109 */
110 public function getWorkspaceId()
111 {
112 return $this->workspaceId;
113 }
114
115 /**
116 * Sets if the pagetree should stop here
117 *
118 * @param bool $stopPageTree
119 */
120 public function setStopPageTree($stopPageTree)
121 {
122 $this->stopPageTree = (bool)$stopPageTree;
123 }
124
125 /**
126 * Returns if the pagetree should stop here
127 *
128 * @return int
129 */
130 public function getStopPageTree()
131 {
132 return $this->stopPageTree;
133 }
134
135 /**
136 * Sets the mount point id
137 *
138 * @param int $mountPoint
139 * @return void
140 */
141 public function setMountPoint($mountPoint)
142 {
143 $this->mountPoint = (int)$mountPoint;
144 }
145
146 /**
147 * Returns the mount point id
148 *
149 * @return int
150 */
151 public function getMountPoint()
152 {
153 return $this->mountPoint;
154 }
155
156 /**
157 * Sets the indicator if the node is a mount point
158 *
159 * @param bool $isMountPoint
160 * @return void
161 */
162 public function setIsMountPoint($isMountPoint)
163 {
164 $this->isMountPoint = $isMountPoint == true;
165 }
166
167 /**
168 * Returns TRUE if the node is a mount point
169 *
170 * @return bool
171 */
172 public function isMountPoint()
173 {
174 return $this->isMountPoint;
175 }
176
177 /**
178 * Sets the readable rootline
179 *
180 * @param string $rootline
181 * @return void
182 */
183 public function setReadableRootline($rootline)
184 {
185 $this->readableRootline = $rootline;
186 }
187
188 /**
189 * Returns the readable rootline
190 *
191 * @return string
192 */
193 public function getReadableRootline()
194 {
195 return $this->readableRootline;
196 }
197
198 /**
199 * Checks if the user may create pages below the given page
200 *
201 * @return bool
202 */
203 protected function canCreate()
204 {
205 if (!isset($this->cachedAccessRights['create'])) {
206 $this->cachedAccessRights['create'] = $GLOBALS['BE_USER']->doesUserHaveAccess($this->record, 8);
207 }
208 return $this->cachedAccessRights['create'];
209 }
210
211 /**
212 * Checks if the user has editing rights
213 *
214 * @return bool
215 */
216 protected function canEdit()
217 {
218 if (!isset($this->cachedAccessRights['edit'])) {
219 $this->cachedAccessRights['edit'] =
220 $GLOBALS['BE_USER']->isAdmin()
221 || (
222 (int)$this->record['editlock'] === 0
223 && $GLOBALS['BE_USER']->doesUserHaveAccess($this->record, 2)
224 );
225 }
226 return $this->cachedAccessRights['edit'];
227 }
228
229 /**
230 * Checks if the user has the right to delete the page
231 *
232 * @return bool
233 */
234 protected function canRemove()
235 {
236 if (!isset($this->cachedAccessRights['remove'])) {
237 $this->cachedAccessRights['remove'] =
238 $GLOBALS['BE_USER']->isAdmin()
239 || (
240 (int)$this->record['editlock'] === 0
241 && $GLOBALS['BE_USER']->doesUserHaveAccess($this->record, 4)
242 );
243 if (!$this->isLeafNode() && !$GLOBALS['BE_USER']->uc['recursiveDelete']) {
244 $this->cachedAccessRights['remove'] = false;
245 }
246 }
247 return $this->cachedAccessRights['remove'];
248 }
249
250 /**
251 * Checks if the page can be disabled
252 *
253 * @return bool
254 */
255 public function canBeDisabledAndEnabled()
256 {
257 return $this->canEdit($this->record) && $GLOBALS['BE_USER']->checkLanguageAccess(0);
258 }
259
260 /**
261 * Checks if the page is allowed to can be cut
262 *
263 * @return bool
264 */
265 public function canBeCut()
266 {
267 return (
268 $this->canEdit($this->record)
269 && !VersionState::cast($this->record['t3ver_state'])->equals(VersionState::DELETE_PLACEHOLDER)
270 && $GLOBALS['BE_USER']->checkLanguageAccess(0)
271 );
272 }
273
274 /**
275 * Checks if the page is allowed to be edited
276 *
277 * @return bool
278 */
279 public function canBeEdited()
280 {
281 return $this->canEdit($this->record) && $GLOBALS['BE_USER']->checkLanguageAccess(0);
282 }
283
284 /**
285 * Checks if the page is allowed to be copied
286 *
287 * @return bool
288 */
289 public function canBeCopied()
290 {
291 return (
292 $GLOBALS['BE_USER']->doesUserHaveAccess($this->record, 1)
293 && !VersionState::cast($this->record['t3ver_state'])->equals(VersionState::DELETE_PLACEHOLDER)
294 && $GLOBALS['BE_USER']->checkLanguageAccess(0)
295 );
296 }
297
298 /**
299 * Checks if there can be new pages created
300 *
301 * @return bool
302 */
303 public function canCreateNewPages()
304 {
305 return $this->canCreate($this->record) && $GLOBALS['BE_USER']->checkLanguageAccess(0);
306 }
307
308 /**
309 * Checks if the page is allowed to be removed
310 *
311 * @return bool
312 */
313 public function canBeRemoved()
314 {
315 return (
316 $this->canRemove($this->record)
317 && !VersionState::cast($this->record['t3ver_state'])->equals(VersionState::DELETE_PLACEHOLDER)
318 && $GLOBALS['BE_USER']->checkLanguageAccess(0)
319 );
320 }
321
322 /**
323 * Checks if something can be pasted into the node
324 *
325 * @return bool
326 */
327 public function canBePastedInto()
328 {
329 return (
330 $this->canCreate($this->record)
331 && !VersionState::cast($this->record['t3ver_state'])->equals(VersionState::DELETE_PLACEHOLDER)
332 && $GLOBALS['BE_USER']->checkLanguageAccess(0)
333 );
334 }
335
336 /**
337 * Checks if something can be pasted after the node
338 *
339 * @return bool
340 */
341 public function canBePastedAfter()
342 {
343 return (
344 $this->canCreate($this->record)
345 && !VersionState::cast($this->record['t3ver_state'])->equals(VersionState::DELETE_PLACEHOLDER)
346 && $GLOBALS['BE_USER']->checkLanguageAccess(0)
347 );
348 }
349
350 /**
351 * Checks if the page is allowed to show history
352 *
353 * @return bool
354 */
355 public function canShowHistory()
356 {
357 return $GLOBALS['BE_USER']->checkLanguageAccess(0);
358 }
359
360 /**
361 * Checks if the page is allowed to be viewed
362 *
363 * @return bool
364 */
365 public function canBeViewed()
366 {
367 return !$this->isDeleted();
368 }
369
370 /**
371 * Checks if the page is allowed to show info
372 *
373 * @return bool
374 */
375 public function canShowInfo()
376 {
377 return true;
378 }
379
380 /**
381 * Checks if the page is allowed to be a temporary mount point
382 *
383 * @return bool
384 */
385 public function canBeTemporaryMountPoint()
386 {
387 return true;
388 }
389
390 /**
391 * Determines whether this node is deleted.
392 *
393 * @return bool
394 */
395 public function isDeleted()
396 {
397 return (
398 !empty($this->record['deleted'])
399 || VersionState::cast($this->record['t3ver_state'])->equals(VersionState::DELETE_PLACEHOLDER)
400 );
401 }
402
403 /**
404 * Returns the calculated id representation of this node
405 *
406 * @param string $prefix Defaults to 'p'
407 * @return string
408 */
409 public function calculateNodeId($prefix = 'p')
410 {
411 return $prefix . dechex($this->getId()) . ($this->getMountPoint() ? '-' . dechex($this->getMountPoint()) : '');
412 }
413
414 /**
415 * Returns the node in an array representation that can be used for serialization
416 *
417 * @param bool $addChildNodes
418 * @return array
419 */
420 public function toArray($addChildNodes = true)
421 {
422 $arrayRepresentation = parent::toArray();
423 $arrayRepresentation['id'] = $this->calculateNodeId();
424 $arrayRepresentation['realId'] = $this->getId();
425 $arrayRepresentation['nodeData']['id'] = $this->getId();
426 $arrayRepresentation['readableRootline'] = $this->getReadableRootline();
427 $arrayRepresentation['nodeData']['readableRootline'] = $this->getReadableRootline();
428 $arrayRepresentation['nodeData']['mountPoint'] = $this->getMountPoint();
429 $arrayRepresentation['nodeData']['workspaceId'] = $this->getWorkspaceId();
430 $arrayRepresentation['nodeData']['isMountPoint'] = $this->isMountPoint();
431 $arrayRepresentation['nodeData']['backgroundColor'] = htmlspecialchars($this->getBackgroundColor());
432 $arrayRepresentation['nodeData']['stopPageTree'] = $this->getStopPageTree();
433 $arrayRepresentation['nodeData']['serializeClassName'] = get_class($this);
434 return $arrayRepresentation;
435 }
436
437 /**
438 * Sets data of the node by a given data array
439 *
440 * @param array $data
441 * @return void
442 */
443 public function dataFromArray($data)
444 {
445 parent::dataFromArray($data);
446 $this->setWorkspaceId($data['workspaceId']);
447 $this->setMountPoint($data['mountPoint']);
448 $this->setReadableRootline($data['readableRootline']);
449 $this->setIsMountPoint($data['isMountPoint']);
450 $this->setBackgroundColor($data['backgroundColor']);
451 }
452 }