TYPO3 CMS  TYPO3_8-7
ExtJsJsonTreeRenderer.php
Go to the documentation of this file.
1 <?php
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  */
19 
24 {
30  protected $recursionLevel = 0;
31 
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 
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 
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 
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 }
renderNode(\TYPO3\CMS\Backend\Tree\TreeRepresentationNode $node, $recursive=true)
getNodeArray(\TYPO3\CMS\Backend\Tree\TreeRepresentationNode $node)
renderNodeCollection(TreeNodeCollection $collection, $recursive=true)
renderTree(\TYPO3\CMS\Backend\Tree\AbstractTree $tree, $recursive=true)