[BUGFIX] Fix several typos in php comments
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Tree / TableConfiguration / ArrayTreeRenderer.php
1 <?php
2 namespace TYPO3\CMS\Core\Tree\TableConfiguration;
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\Tree\TreeNodeCollection;
18
19 /**
20 * Renders a tca tree array for the SelectElementTree
21 */
22 class ArrayTreeRenderer extends \TYPO3\CMS\Backend\Tree\Renderer\AbstractTreeRenderer
23 {
24 /**
25 * recursion level
26 *
27 * @var int
28 */
29 protected $recursionLevel = 0;
30
31 /**
32 * Renders a node recursive or just a single instance
33 *
34 * @param \TYPO3\CMS\Backend\Tree\TreeRepresentationNode $node
35 * @param bool $recursive
36 * @return array
37 */
38 public function renderNode(\TYPO3\CMS\Backend\Tree\TreeRepresentationNode $node, $recursive = true)
39 {
40 $nodeArray = [];
41 $nodeArray[] = $this->getNodeArray($node);
42 if ($recursive && $node->hasChildNodes()) {
43 $this->recursionLevel++;
44 $children = $this->renderNodeCollection($node->getChildNodes());
45 foreach ($children as $child) {
46 $nodeArray[] = $child;
47 }
48 $this->recursionLevel--;
49 }
50 return $nodeArray;
51 }
52
53 /**
54 * Get node array
55 *
56 * @param \TYPO3\CMS\Backend\Tree\TreeRepresentationNode|DatabaseTreeNode $node
57 * @return array
58 */
59 protected function getNodeArray(\TYPO3\CMS\Backend\Tree\TreeRepresentationNode $node)
60 {
61 $overlayIconName = '';
62 if (is_object($node->getIcon())) {
63 $iconName = $node->getIcon()->getIdentifier();
64 if (is_object($node->getIcon()->getOverlayIcon())) {
65 $overlayIconName = $node->getIcon()->getOverlayIcon()->getIdentifier();
66 }
67 } else {
68 $iconName = $node->getIcon();
69 }
70 $nodeArray = [
71 'identifier' => htmlspecialchars($node->getId()),
72 // No need for htmlspecialchars() here as d3 is using 'textContent' property of the HTML DOM node
73 'name' => $node->getLabel(),
74 'icon' => $iconName,
75 'overlayIcon' => $overlayIconName,
76 'depth' => $this->recursionLevel,
77 'hasChildren' => (bool)$node->hasChildNodes(),
78 'selectable' => true,
79 ];
80 if ($node instanceof DatabaseTreeNode) {
81 $nodeArray['checked'] = (bool)$node->getSelected();
82 if (!$node->getSelectable()) {
83 $nodeArray['checked'] = false;
84 $nodeArray['selectable'] = false;
85 }
86 }
87 return $nodeArray;
88 }
89
90 /**
91 * Renders a node collection recursive or just a single instance
92 *
93 * @param \TYPO3\CMS\Backend\Tree\AbstractTree $tree
94 * @param bool $recursive
95 * @return array
96 */
97 public function renderTree(\TYPO3\CMS\Backend\Tree\AbstractTree $tree, $recursive = true)
98 {
99 $this->recursionLevel = 0;
100 return $this->renderNode($tree->getRoot(), $recursive);
101 }
102
103 /**
104 * Renders a tree recursively or just a single instance
105 *
106 * @param TreeNodeCollection $collection
107 * @param bool $recursive
108 * @return array
109 */
110 public function renderNodeCollection(TreeNodeCollection $collection, $recursive = true)
111 {
112 $treeItems = [];
113 foreach ($collection as $node) {
114 $allNodes = $this->renderNode($node, $recursive);
115 if ($allNodes[0]) {
116 $treeItems[] = $allNodes[0];
117 }
118 $nodeCount = count($allNodes);
119 if ($nodeCount > 1) {
120 for ($i = 1; $i < $nodeCount; $i++) {
121 $treeItems[] = $allNodes[$i];
122 }
123 }
124 }
125 return $treeItems;
126 }
127 }