[TASK] Update php-cs-fixer to 2.5.0
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Tree / View / AbstractTreeView.php
1 <?php
2 namespace TYPO3\CMS\Backend\Tree\View;
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\Routing\Router;
18 use TYPO3\CMS\Backend\Routing\UriBuilder;
19 use TYPO3\CMS\Backend\Tree\Pagetree\Commands;
20 use TYPO3\CMS\Backend\Utility\BackendUtility;
21 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
22 use TYPO3\CMS\Core\Database\ConnectionPool;
23 use TYPO3\CMS\Core\Database\Query\QueryHelper;
24 use TYPO3\CMS\Core\Database\Query\Restriction\BackendWorkspaceRestriction;
25 use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
26 use TYPO3\CMS\Core\Imaging\Icon;
27 use TYPO3\CMS\Core\Imaging\IconFactory;
28 use TYPO3\CMS\Core\Localization\LanguageService;
29 use TYPO3\CMS\Core\Utility\GeneralUtility;
30
31 /**
32 * Base class for creating a browsable array/page/folder tree in HTML
33 */
34 abstract class AbstractTreeView
35 {
36 // EXTERNAL, static:
37 // If set, the first element in the tree is always expanded.
38 /**
39 * @var int
40 */
41 public $expandFirst = 0;
42
43 // If set, then ALL items will be expanded, regardless of stored settings.
44 /**
45 * @var int
46 */
47 public $expandAll = 0;
48
49 // Holds the current script to reload to.
50 /**
51 * @var string
52 */
53 public $thisScript = '';
54
55 // Which HTML attribute to use: alt/title. See init().
56 /**
57 * @var string
58 */
59 public $titleAttrib = 'title';
60
61 // If TRUE, no context menu is rendered on icons. If set to "titlelink" the
62 // icon is linked as the title is.
63 /**
64 * @var bool
65 */
66 public $ext_IconMode = false;
67
68 /**
69 * @var bool
70 */
71 public $ext_showPathAboveMounts = false;
72
73 // If set, the id of the mounts will be added to the internal ids array
74 /**
75 * @var int
76 */
77 public $addSelfId = 0;
78
79 // Used if the tree is made of records (not folders for ex.)
80 /**
81 * @var string
82 */
83 public $title = 'no title';
84
85 // If TRUE, a default title attribute showing the UID of the record is shown.
86 // This cannot be enabled by default because it will destroy many applications
87 // where another title attribute is in fact applied later.
88 /**
89 * @var bool
90 */
91 public $showDefaultTitleAttribute = false;
92
93 /**
94 * Needs to be initialized with $GLOBALS['BE_USER']
95 * Done by default in init()
96 *
97 * @var \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
98 */
99 public $BE_USER = '';
100
101 /**
102 * Needs to be initialized with e.g. $GLOBALS['BE_USER']->returnWebmounts()
103 * Default setting in init() is 0 => 0
104 * The keys are mount-ids (can be anything basically) and the
105 * values are the ID of the root element (COULD be zero or anything else.
106 * For pages that would be the uid of the page, zero for the pagetree root.)
107 *
108 * @var array|NULL
109 */
110 public $MOUNTS = null;
111
112 /**
113 * Database table to get the tree data from.
114 * Leave blank if data comes from an array.
115 *
116 * @var string
117 */
118 public $table = '';
119
120 /**
121 * Defines the field of $table which is the parent id field (like pid for table pages).
122 *
123 * @var string
124 */
125 public $parentField = 'pid';
126
127 /**
128 * WHERE clause used for selecting records for the tree. Is set by function init.
129 * Only makes sense when $this->table is set.
130 *
131 * @see init()
132 * @var string
133 */
134 public $clause = '';
135
136 /**
137 * Field for ORDER BY. Is set by function init.
138 * Only makes sense when $this->table is set.
139 *
140 * @see init()
141 * @var string
142 */
143 public $orderByFields = '';
144
145 /**
146 * Default set of fields selected from the tree table.
147 * Make SURE that these fields names listed herein are actually possible to select from $this->table (if that variable is set to a TCA table name)
148 *
149 * @see addField()
150 * @var array
151 */
152 public $fieldArray = ['uid', 'pid', 'title', 'is_siteroot'];
153
154 /**
155 * List of other fields which are ALLOWED to set (here, based on the "pages" table!)
156 *
157 * @see addField()
158 * @var array
159 */
160 public $defaultList = 'uid,pid,tstamp,sorting,deleted,perms_userid,perms_groupid,perms_user,perms_group,perms_everybody,crdate,cruser_id';
161
162 /**
163 * Unique name for the tree.
164 * Used as key for storing the tree into the BE users settings.
165 * Used as key to pass parameters in links.
166 * MUST NOT contain underscore chars.
167 * etc.
168 *
169 * @var string
170 */
171 public $treeName = '';
172
173 /**
174 * A prefix for table cell id's which will be wrapped around an item.
175 * Can be used for highlighting by JavaScript.
176 * Needs to be unique if multiple trees are on one HTML page.
177 *
178 * @see printTree()
179 * @var string
180 */
181 public $domIdPrefix = 'row';
182
183 /**
184 * If 1, HTML code is also accumulated in ->tree array during rendering of the tree
185 *
186 * @var int
187 */
188 public $makeHTML = 1;
189
190 /**
191 * If TRUE, records as selected will be stored internally in the ->recs array
192 *
193 * @var int
194 */
195 public $setRecs = 0;
196
197 /**
198 * Sets the associative array key which identifies a new sublevel if arrays are used for trees.
199 * This value has formerly been "subLevel" and "--sublevel--"
200 *
201 * @var string
202 */
203 public $subLevelID = '_SUB_LEVEL';
204
205 // *********
206 // Internal
207 // *********
208 // For record trees:
209 // one-dim array of the uid's selected.
210 /**
211 * @var array
212 */
213 public $ids = [];
214
215 // The hierarchy of element uids
216 /**
217 * @var array
218 */
219 public $ids_hierarchy = [];
220
221 // The hierarchy of versioned element uids
222 /**
223 * @var array
224 */
225 public $orig_ids_hierarchy = [];
226
227 // Temporary, internal array
228 /**
229 * @var array
230 */
231 public $buffer_idH = [];
232
233 // For FOLDER trees:
234 // Special UIDs for folders (integer-hashes of paths)
235 /**
236 * @var array
237 */
238 public $specUIDmap = [];
239
240 // For arrays:
241 // Holds the input data array
242 /**
243 * @var bool
244 */
245 public $data = false;
246
247 // Holds an index with references to the data array.
248 /**
249 * @var bool
250 */
251 public $dataLookup = false;
252
253 // For both types
254 // Tree is accumulated in this variable
255 /**
256 * @var array
257 */
258 public $tree = [];
259
260 // Holds (session stored) information about which items in the tree are unfolded and which are not.
261 /**
262 * @var array
263 */
264 public $stored = [];
265
266 // Points to the current mountpoint key
267 /**
268 * @var int
269 */
270 public $bank = 0;
271
272 // Accumulates the displayed records.
273 /**
274 * @var array
275 */
276 public $recs = [];
277
278 /**
279 * Constructor
280 */
281 public function __construct()
282 {
283 $this->determineScriptUrl();
284 }
285
286 /**
287 * Sets the script url depending on being a module or script request
288 */
289 protected function determineScriptUrl()
290 {
291 if ($routePath = GeneralUtility::_GP('route')) {
292 $router = GeneralUtility::makeInstance(Router::class);
293 $route = $router->match($routePath);
294 $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
295 $this->thisScript = (string)$uriBuilder->buildUriFromRoute($route->getOption('_identifier'));
296 } elseif ($moduleName = GeneralUtility::_GP('M')) {
297 $this->thisScript = BackendUtility::getModuleUrl($moduleName);
298 } else {
299 $this->thisScript = GeneralUtility::getIndpEnv('SCRIPT_NAME');
300 }
301 }
302
303 /**
304 * @return string
305 */
306 protected function getThisScript()
307 {
308 return strpos($this->thisScript, '?') === false ? $this->thisScript . '?' : $this->thisScript . '&';
309 }
310
311 /**
312 * Initialize the tree class. Needs to be overwritten
313 *
314 * @param string $clause Record WHERE clause
315 * @param string $orderByFields Record ORDER BY field
316 */
317 public function init($clause = '', $orderByFields = '')
318 {
319 // Setting BE_USER by default
320 $this->BE_USER = $GLOBALS['BE_USER'];
321 // Setting clause
322 if ($clause) {
323 $this->clause = $clause;
324 }
325 if ($orderByFields) {
326 $this->orderByFields = $orderByFields;
327 }
328 if (!is_array($this->MOUNTS)) {
329 // Dummy
330 $this->MOUNTS = [0 => 0];
331 }
332 // Sets the tree name which is used to identify the tree, used for JavaScript and other things
333 $this->treeName = str_replace('_', '', $this->treeName ?: $this->table);
334 // Setting this to FALSE disables the use of array-trees by default
335 $this->data = false;
336 $this->dataLookup = false;
337 }
338
339 /**
340 * Adds a fieldname to the internal array ->fieldArray
341 *
342 * @param string $field Field name to
343 * @param bool $noCheck If set, the fieldname will be set no matter what. Otherwise the field name must either be found as key in $GLOBALS['TCA'][$table]['columns'] or in the list ->defaultList
344 */
345 public function addField($field, $noCheck = false)
346 {
347 if ($noCheck || is_array($GLOBALS['TCA'][$this->table]['columns'][$field]) || GeneralUtility::inList($this->defaultList, $field)) {
348 $this->fieldArray[] = $field;
349 }
350 }
351
352 /**
353 * Resets the tree, recs, ids, ids_hierarchy and orig_ids_hierarchy internal variables. Use it if you need it.
354 */
355 public function reset()
356 {
357 $this->tree = [];
358 $this->recs = [];
359 $this->ids = [];
360 $this->ids_hierarchy = [];
361 $this->orig_ids_hierarchy = [];
362 }
363
364 /*******************************************
365 *
366 * output
367 *
368 *******************************************/
369 /**
370 * Will create and return the HTML code for a browsable tree
371 * Is based on the mounts found in the internal array ->MOUNTS (set in the constructor)
372 *
373 * @return string HTML code for the browsable tree
374 */
375 public function getBrowsableTree()
376 {
377 // Get stored tree structure AND updating it if needed according to incoming PM GET var.
378 $this->initializePositionSaving();
379 // Init done:
380 $lastMountPointPid = 0;
381 $treeArr = [];
382 // Traverse mounts:
383 foreach ($this->MOUNTS as $idx => $uid) {
384 // Set first:
385 $this->bank = $idx;
386 $isOpen = $this->stored[$idx][$uid] || $this->expandFirst;
387 // Save ids while resetting everything else.
388 $curIds = $this->ids;
389 $this->reset();
390 $this->ids = $curIds;
391 // Set PM icon for root of mount:
392 $cmd = $this->bank . '_' . ($isOpen ? '0_' : '1_') . $uid . '_' . $this->treeName;
393
394 $firstHtml = $this->PM_ATagWrap('', $cmd, '', $isOpen);
395 // Preparing rootRec for the mount
396 if ($uid) {
397 $rootRec = $this->getRecord($uid);
398 if (is_array($rootRec)) {
399 $firstHtml .= $this->getIcon($rootRec);
400 }
401
402 if ($this->ext_showPathAboveMounts) {
403 $mountPointPid = $rootRec['pid'];
404 if ($lastMountPointPid !== $mountPointPid) {
405 $title = Commands::getMountPointPath($mountPointPid);
406 $this->tree[] = ['isMountPointPath' => true, 'title' => $title];
407 }
408 $lastMountPointPid = $mountPointPid;
409 }
410 } else {
411 // Artificial record for the tree root, id=0
412 $rootRec = $this->getRootRecord();
413 $firstHtml .= $this->getRootIcon($rootRec);
414 }
415 if (is_array($rootRec)) {
416 // In case it was swapped inside getRecord due to workspaces.
417 $uid = $rootRec['uid'];
418 // Add the root of the mount to ->tree
419 $this->tree[] = ['HTML' => $firstHtml, 'row' => $rootRec, 'hasSub' => $isOpen, 'bank' => $this->bank];
420 // If the mount is expanded, go down:
421 if ($isOpen) {
422 $depthData = '<span class="treeline-icon treeline-icon-clear"></span>';
423 if ($this->addSelfId) {
424 $this->ids[] = $uid;
425 }
426 $this->getTree($uid, 999, $depthData);
427 }
428 // Add tree:
429 $treeArr = array_merge($treeArr, $this->tree);
430 }
431 }
432 return $this->printTree($treeArr);
433 }
434
435 /**
436 * Compiles the HTML code for displaying the structure found inside the ->tree array
437 *
438 * @param array|string $treeArr "tree-array" - if blank string, the internal ->tree array is used.
439 * @return string The HTML code for the tree
440 */
441 public function printTree($treeArr = '')
442 {
443 $titleLen = (int)$this->BE_USER->uc['titleLen'];
444 if (!is_array($treeArr)) {
445 $treeArr = $this->tree;
446 }
447 $out = '';
448 $closeDepth = [];
449 foreach ($treeArr as $treeItem) {
450 $classAttr = '';
451 if ($treeItem['isFirst']) {
452 $out .= '<ul class="list-tree">';
453 }
454
455 // Add CSS classes to the list item
456 if ($treeItem['hasSub']) {
457 $classAttr .= ' list-tree-control-open';
458 }
459
460 $idAttr = htmlspecialchars($this->domIdPrefix . $this->getId($treeItem['row']) . '_' . $treeItem['bank']);
461 $out .= '
462 <li id="' . $idAttr . '"' . ($classAttr ? ' class="' . trim($classAttr) . '"' : '') . '>
463 <span class="list-tree-group">
464 <span class="list-tree-icon">' . $treeItem['HTML'] . '</span>
465 <span class="list-tree-title">' . $this->wrapTitle($this->getTitleStr($treeItem['row'], $titleLen), $treeItem['row'], $treeItem['bank']) . '</span>
466 </span>';
467
468 if (!$treeItem['hasSub']) {
469 $out .= '</li>';
470 }
471
472 // We have to remember if this is the last one
473 // on level X so the last child on level X+1 closes the <ul>-tag
474 if ($treeItem['isLast']) {
475 $closeDepth[$treeItem['invertedDepth']] = 1;
476 }
477 // If this is the last one and does not have subitems, we need to close
478 // the tree as long as the upper levels have last items too
479 if ($treeItem['isLast'] && !$treeItem['hasSub']) {
480 for ($i = $treeItem['invertedDepth']; $closeDepth[$i] == 1; $i++) {
481 $closeDepth[$i] = 0;
482 $out .= '</ul></li>';
483 }
484 }
485 }
486 $out = '<ul class="list-tree list-tree-root list-tree-root-clean">' . $out . '</ul>';
487 return $out;
488 }
489
490 /*******************************************
491 *
492 * rendering parts
493 *
494 *******************************************/
495 /**
496 * Generate the plus/minus icon for the browsable tree.
497 *
498 * @param array $row Record for the entry
499 * @param int $a The current entry number
500 * @param int $c The total number of entries. If equal to $a, a "bottom" element is returned.
501 * @param int $nextCount The number of sub-elements to the current element.
502 * @param bool $isOpen The element was expanded to render subelements if this flag is set.
503 * @return string Image tag with the plus/minus icon.
504 * @access private
505 * @see \TYPO3\CMS\Backend\Tree\View\PageTreeView::PMicon()
506 */
507 public function PMicon($row, $a, $c, $nextCount, $isOpen)
508 {
509 if ($nextCount) {
510 $cmd = $this->bank . '_' . ($isOpen ? '0_' : '1_') . $row['uid'] . '_' . $this->treeName;
511 $bMark = $this->bank . '_' . $row['uid'];
512 return $this->PM_ATagWrap('', $cmd, $bMark, $isOpen);
513 }
514 return '';
515 }
516
517 /**
518 * Wrap the plus/minus icon in a link
519 *
520 * @param string $icon HTML string to wrap, probably an image tag.
521 * @param string $cmd Command for 'PM' get var
522 * @param string $bMark If set, the link will have an anchor point (=$bMark) and a name attribute (=$bMark)
523 * @param bool $isOpen
524 * @return string Link-wrapped input string
525 * @access private
526 */
527 public function PM_ATagWrap($icon, $cmd, $bMark = '', $isOpen = false)
528 {
529 if ($this->thisScript) {
530 $anchor = $bMark ? '#' . $bMark : '';
531 $name = $bMark ? ' name="' . $bMark . '"' : '';
532 $aUrl = $this->getThisScript() . 'PM=' . $cmd . $anchor;
533 return '<a class="list-tree-control ' . ($isOpen ? 'list-tree-control-open' : 'list-tree-control-closed') . '" href="' . htmlspecialchars($aUrl) . '"' . $name . '><i class="fa"></i></a>';
534 }
535 return $icon;
536 }
537
538 /**
539 * Wrapping $title in a-tags.
540 *
541 * @param string $title Title string
542 * @param array $row Item record
543 * @param int $bank Bank pointer (which mount point number)
544 * @return string
545 * @access private
546 */
547 public function wrapTitle($title, $row, $bank = 0)
548 {
549 $aOnClick = 'return jumpTo(' . GeneralUtility::quoteJSvalue($this->getJumpToParam($row)) . ',this,' . GeneralUtility::quoteJSvalue($this->domIdPrefix . $this->getId($row)) . ',' . $bank . ');';
550 return '<a href="#" onclick="' . htmlspecialchars($aOnClick) . '">' . $title . '</a>';
551 }
552
553 /**
554 * Wrapping the image tag, $icon, for the row, $row (except for mount points)
555 *
556 * @param string $icon The image tag for the icon
557 * @param array $row The row for the current element
558 * @return string The processed icon input value.
559 * @access private
560 */
561 public function wrapIcon($icon, $row)
562 {
563 return $icon;
564 }
565
566 /**
567 * Adds attributes to image tag.
568 *
569 * @param string $icon Icon image tag
570 * @param string $attr Attributes to add, eg. ' border="0"'
571 * @return string Image tag, modified with $attr attributes added.
572 */
573 public function addTagAttributes($icon, $attr)
574 {
575 return preg_replace('/ ?\\/?>$/', '', $icon) . ' ' . $attr . ' />';
576 }
577
578 /**
579 * Adds a red "+" to the input string, $str, if the field "php_tree_stop" in the $row (pages) is set
580 *
581 * @param string $str Input string, like a page title for the tree
582 * @param array $row record row with "php_tree_stop" field
583 * @return string Modified string
584 * @access private
585 */
586 public function wrapStop($str, $row)
587 {
588 if ($row['php_tree_stop']) {
589 $str .= '<a href="' . htmlspecialchars(GeneralUtility::linkThisScript(['setTempDBmount' => $row['uid']])) . '" class="text-danger">+</a> ';
590 }
591 return $str;
592 }
593
594 /*******************************************
595 *
596 * tree handling
597 *
598 *******************************************/
599 /**
600 * Returns TRUE/FALSE if the next level for $id should be expanded - based on
601 * data in $this->stored[][] and ->expandAll flag.
602 * Extending parent function
603 *
604 * @param int $id Record id/key
605 * @return bool
606 * @access private
607 * @see \TYPO3\CMS\Backend\Tree\View\PageTreeView::expandNext()
608 */
609 public function expandNext($id)
610 {
611 return $this->stored[$this->bank][$id] || $this->expandAll ? 1 : 0;
612 }
613
614 /**
615 * Get stored tree structure AND updating it if needed according to incoming PM GET var.
616 *
617 * @access private
618 */
619 public function initializePositionSaving()
620 {
621 // Get stored tree structure:
622 $this->stored = unserialize($this->BE_USER->uc['browseTrees'][$this->treeName]);
623 // PM action
624 // (If an plus/minus icon has been clicked, the PM GET var is sent and we
625 // must update the stored positions in the tree):
626 // 0: mount key, 1: set/clear boolean, 2: item ID (cannot contain "_"), 3: treeName
627 $PM = explode('_', GeneralUtility::_GP('PM'));
628 if (count($PM) === 4 && $PM[3] == $this->treeName) {
629 if (isset($this->MOUNTS[$PM[0]])) {
630 // set
631 if ($PM[1]) {
632 $this->stored[$PM[0]][$PM[2]] = 1;
633 $this->savePosition();
634 } else {
635 unset($this->stored[$PM[0]][$PM[2]]);
636 $this->savePosition();
637 }
638 }
639 }
640 }
641
642 /**
643 * Saves the content of ->stored (keeps track of expanded positions in the tree)
644 * $this->treeName will be used as key for BE_USER->uc[] to store it in
645 *
646 * @access private
647 */
648 public function savePosition()
649 {
650 $this->BE_USER->uc['browseTrees'][$this->treeName] = serialize($this->stored);
651 $this->BE_USER->writeUC();
652 }
653
654 /******************************
655 *
656 * Functions that might be overwritten by extended classes
657 *
658 ********************************/
659 /**
660 * Returns the root icon for a tree/mountpoint (defaults to the globe)
661 *
662 * @param array $rec Record for root.
663 * @return string Icon image tag.
664 */
665 public function getRootIcon($rec)
666 {
667 $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
668 return $this->wrapIcon($iconFactory->getIcon('apps-pagetree-root', Icon::SIZE_SMALL)->render(), $rec);
669 }
670
671 /**
672 * Get icon for the row.
673 *
674 * @param array|int $row Item row or uid
675 * @return string Image tag.
676 */
677 public function getIcon($row)
678 {
679 if (is_int($row)) {
680 $row = BackendUtility::getRecord($this->table, $row);
681 }
682 $title = $this->showDefaultTitleAttribute ? htmlspecialchars('UID: ' . $row['uid']) : $this->getTitleAttrib($row);
683 $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
684 $icon = $row['is_siteroot'] ? $iconFactory->getIcon('apps-pagetree-folder-root', Icon::SIZE_SMALL) : $iconFactory->getIconForRecord($this->table, $row, Icon::SIZE_SMALL);
685 $icon = '<span title="' . $title . '">' . $icon->render() . '</span>';
686 return $this->wrapIcon($icon, $row);
687 }
688
689 /**
690 * Returns the title for the input record. If blank, a "no title" label (localized) will be returned.
691 * Do NOT htmlspecialchar the string from this function - has already been done.
692 *
693 * @param array $row The input row array (where the key "title" is used for the title)
694 * @param int $titleLen Title length (30)
695 * @return string The title.
696 */
697 public function getTitleStr($row, $titleLen = 30)
698 {
699 $title = htmlspecialchars(GeneralUtility::fixed_lgd_cs($row['title'], $titleLen));
700 $title = trim($row['title']) === '' ? '<em>[' . htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.no_title')) . ']</em>' : $title;
701 return $title;
702 }
703
704 /**
705 * Returns the value for the image "title" attribute
706 *
707 * @param array $row The input row array (where the key "title" is used for the title)
708 * @return string The attribute value (is htmlspecialchared() already)
709 * @see wrapIcon()
710 */
711 public function getTitleAttrib($row)
712 {
713 return htmlspecialchars($row['title']);
714 }
715
716 /**
717 * Returns the id from the record (typ. uid)
718 *
719 * @param array $row Record array
720 * @return int The "uid" field value.
721 */
722 public function getId($row)
723 {
724 return $row['uid'];
725 }
726
727 /**
728 * Returns jump-url parameter value.
729 *
730 * @param array $row The record array.
731 * @return string The jump-url parameter.
732 */
733 public function getJumpToParam($row)
734 {
735 return $this->getId($row);
736 }
737
738 /********************************
739 *
740 * tree data buidling
741 *
742 ********************************/
743 /**
744 * Fetches the data for the tree
745 *
746 * @param int $uid item id for which to select subitems (parent id)
747 * @param int $depth Max depth (recursivity limit)
748 * @param string $depthData HTML-code prefix for recursive calls.
749
750 * @return int The count of items on the level
751 */
752 public function getTree($uid, $depth = 999, $depthData = '')
753 {
754 // Buffer for id hierarchy is reset:
755 $this->buffer_idH = [];
756 // Init vars
757 $depth = (int)$depth;
758 $HTML = '';
759 $a = 0;
760 $res = $this->getDataInit($uid);
761 $c = $this->getDataCount($res);
762 $crazyRecursionLimiter = 999;
763 $idH = [];
764 // Traverse the records:
765 while ($crazyRecursionLimiter > 0 && ($row = $this->getDataNext($res))) {
766 $pageUid = ($this->table === 'pages') ? $row['uid'] : $row['pid'];
767 if (!$this->getBackendUser()->isInWebMount($pageUid)) {
768 // Current record is not within web mount => skip it
769 continue;
770 }
771
772 $a++;
773 $crazyRecursionLimiter--;
774 $newID = $row['uid'];
775 if ($newID == 0) {
776 throw new \RuntimeException('Endless recursion detected: TYPO3 has detected an error in the database. Please fix it manually (e.g. using phpMyAdmin) and change the UID of ' . $this->table . ':0 to a new value. See http://forge.typo3.org/issues/16150 to get more information about a possible cause.', 1294586383);
777 }
778 // Reserve space.
779 $this->tree[] = [];
780 end($this->tree);
781 // Get the key for this space
782 $treeKey = key($this->tree);
783 // If records should be accumulated, do so
784 if ($this->setRecs) {
785 $this->recs[$row['uid']] = $row;
786 }
787 // Accumulate the id of the element in the internal arrays
788 $this->ids[] = ($idH[$row['uid']]['uid'] = $row['uid']);
789 $this->ids_hierarchy[$depth][] = $row['uid'];
790 $this->orig_ids_hierarchy[$depth][] = $row['_ORIG_uid'] ?: $row['uid'];
791
792 // Make a recursive call to the next level
793 $nextLevelDepthData = $depthData . '<span class="treeline-icon treeline-icon-' . ($a === $c ? 'clear' : 'line') . '"></span>';
794 $hasSub = $this->expandNext($newID) && !$row['php_tree_stop'];
795 if ($depth > 1 && $hasSub) {
796 $nextCount = $this->getTree($newID, $depth - 1, $nextLevelDepthData);
797 if (!empty($this->buffer_idH)) {
798 $idH[$row['uid']]['subrow'] = $this->buffer_idH;
799 }
800 // Set "did expand" flag
801 $isOpen = 1;
802 } else {
803 $nextCount = $this->getCount($newID);
804 // Clear "did expand" flag
805 $isOpen = 0;
806 }
807 // Set HTML-icons, if any:
808 if ($this->makeHTML) {
809 $HTML = $this->PMicon($row, $a, $c, $nextCount, $isOpen) . $this->wrapStop($this->getIcon($row), $row);
810 }
811 // Finally, add the row/HTML content to the ->tree array in the reserved key.
812 $this->tree[$treeKey] = [
813 'row' => $row,
814 'HTML' => $HTML,
815 'invertedDepth' => $depth,
816 'depthData' => $depthData,
817 'bank' => $this->bank,
818 'hasSub' => $nextCount && $hasSub,
819 'isFirst' => $a === 1,
820 'isLast' => $a === $c,
821 ];
822 }
823
824 $this->getDataFree($res);
825 $this->buffer_idH = $idH;
826 return $c;
827 }
828
829 /********************************
830 *
831 * Data handling
832 * Works with records and arrays
833 *
834 ********************************/
835 /**
836 * Returns the number of records having the parent id, $uid
837 *
838 * @param int $uid Id to count subitems for
839 * @return int
840 * @access private
841 */
842 public function getCount($uid)
843 {
844 if (is_array($this->data)) {
845 $res = $this->getDataInit($uid);
846 return $this->getDataCount($res);
847 }
848 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->table);
849 $queryBuilder->getRestrictions()
850 ->removeAll()
851 ->add(GeneralUtility::makeInstance(DeletedRestriction::class))
852 ->add(GeneralUtility::makeInstance(BackendWorkspaceRestriction::class));
853 $count = $queryBuilder
854 ->count('uid')
855 ->from($this->table)
856 ->where(
857 $queryBuilder->expr()->eq(
858 $this->parentField,
859 $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT)
860 ),
861 QueryHelper::stripLogicalOperatorPrefix($this->clause)
862 )
863 ->execute()
864 ->fetchColumn();
865
866 return (int)$count;
867 }
868
869 /**
870 * Returns root record for uid (<=0)
871 *
872 * @return array Array with title/uid keys with values of $this->title/0 (zero)
873 */
874 public function getRootRecord()
875 {
876 return ['title' => $this->title, 'uid' => 0];
877 }
878
879 /**
880 * Returns the record for a uid.
881 * For tables: Looks up the record in the database.
882 * For arrays: Returns the fake record for uid id.
883 *
884 * @param int $uid UID to look up
885 * @return array The record
886 */
887 public function getRecord($uid)
888 {
889 if (is_array($this->data)) {
890 return $this->dataLookup[$uid];
891 }
892 return BackendUtility::getRecordWSOL($this->table, $uid);
893 }
894
895 /**
896 * Getting the tree data: Selecting/Initializing data pointer to items for a certain parent id.
897 * For tables: This will make a database query to select all children to "parent"
898 * For arrays: This will return key to the ->dataLookup array
899 *
900 * @param int $parentId parent item id
901 *
902 * @return mixed Data handle (Tables: An sql-resource, arrays: A parentId integer. -1 is returned if there were NO subLevel.)
903 * @access private
904 */
905 public function getDataInit($parentId)
906 {
907 if (is_array($this->data)) {
908 if (!is_array($this->dataLookup[$parentId][$this->subLevelID])) {
909 $parentId = -1;
910 } else {
911 reset($this->dataLookup[$parentId][$this->subLevelID]);
912 }
913 return $parentId;
914 }
915 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->table);
916 $queryBuilder->getRestrictions()
917 ->removeAll()
918 ->add(GeneralUtility::makeInstance(DeletedRestriction::class))
919 ->add(GeneralUtility::makeInstance(BackendWorkspaceRestriction::class));
920 $queryBuilder
921 ->select(...$this->fieldArray)
922 ->from($this->table)
923 ->where(
924 $queryBuilder->expr()->eq(
925 $this->parentField,
926 $queryBuilder->createNamedParameter($parentId, \PDO::PARAM_INT)
927 ),
928 QueryHelper::stripLogicalOperatorPrefix($this->clause)
929 );
930
931 foreach (QueryHelper::parseOrderBy($this->orderByFields) as $orderPair) {
932 list($fieldName, $order) = $orderPair;
933 $queryBuilder->addOrderBy($fieldName, $order);
934 }
935
936 return $queryBuilder->execute();
937 }
938
939 /**
940 * Getting the tree data: Counting elements in resource
941 *
942 * @param mixed $res Data handle
943 * @return int number of items
944 * @access private
945 * @see getDataInit()
946 */
947 public function getDataCount(&$res)
948 {
949 if (is_array($this->data)) {
950 return count($this->dataLookup[$res][$this->subLevelID]);
951 }
952 return $res->rowCount();
953 }
954
955 /**
956 * Getting the tree data: next entry
957 *
958 * @param mixed $res Data handle
959 *
960 * @return array item data array OR FALSE if end of elements.
961 * @access private
962 * @see getDataInit()
963 */
964 public function getDataNext(&$res)
965 {
966 if (is_array($this->data)) {
967 if ($res < 0) {
968 $row = false;
969 } else {
970 list(, $row) = each($this->dataLookup[$res][$this->subLevelID]);
971 }
972 return $row;
973 }
974 while ($row = $res->fetch()) {
975 BackendUtility::workspaceOL($this->table, $row, $this->BE_USER->workspace, true);
976 if (is_array($row)) {
977 break;
978 }
979 }
980 return $row;
981 }
982
983 /**
984 * Getting the tree data: frees data handle
985 *
986 * @param mixed $res Data handle
987 * @access private
988 */
989 public function getDataFree(&$res)
990 {
991 if (!is_array($this->data)) {
992 $res->closeCursor();
993 }
994 }
995
996 /**
997 * Used to initialize class with an array to browse.
998 * The array inputted will be traversed and an internal index for lookup is created.
999 * The keys of the input array are perceived as "uid"s of records which means that keys GLOBALLY must be unique like uids are.
1000 * "uid" and "pid" "fakefields" are also set in each record.
1001 * All other fields are optional.
1002 *
1003 * @param array $dataArr The input array, see examples below in this script.
1004 * @param bool $traverse Internal, for recursion.
1005 * @param int $pid Internal, for recursion.
1006 */
1007 public function setDataFromArray(&$dataArr, $traverse = false, $pid = 0)
1008 {
1009 if (!$traverse) {
1010 $this->data = &$dataArr;
1011 $this->dataLookup = [];
1012 // Add root
1013 $this->dataLookup[0][$this->subLevelID] = &$dataArr;
1014 }
1015 foreach ($dataArr as $uid => $val) {
1016 $dataArr[$uid]['uid'] = $uid;
1017 $dataArr[$uid]['pid'] = $pid;
1018 // Gives quick access to id's
1019 $this->dataLookup[$uid] = &$dataArr[$uid];
1020 if (is_array($val[$this->subLevelID])) {
1021 $this->setDataFromArray($dataArr[$uid][$this->subLevelID], true, $uid);
1022 }
1023 }
1024 }
1025
1026 /**
1027 * Sets the internal data arrays
1028 *
1029 * @param array $treeArr Content for $this->data
1030 * @param array $treeLookupArr Content for $this->dataLookup
1031 */
1032 public function setDataFromTreeArray(&$treeArr, &$treeLookupArr)
1033 {
1034 $this->data = &$treeArr;
1035 $this->dataLookup = &$treeLookupArr;
1036 }
1037
1038 /**
1039 * @return LanguageService
1040 */
1041 protected function getLanguageService()
1042 {
1043 return $GLOBALS['LANG'];
1044 }
1045
1046 /**
1047 * @return BackendUserAuthentication
1048 */
1049 protected function getBackendUser()
1050 {
1051 return $GLOBALS['BE_USER'];
1052 }
1053 }