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