TYPO3  7.6
ExtJsJsonTreeRenderer.php
Go to the documentation of this file.
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 
21 {
27  protected $recursionLevel = 0;
28 
36  public function renderNode(\TYPO3\CMS\Backend\Tree\TreeRepresentationNode $node, $recursive = true)
37  {
38  $nodeArray = $this->getNodeArray($node);
39  if ($recursive && $node->hasChildNodes()) {
40  $this->recursionLevel++;
41  $children = $this->renderNodeCollection($node->getChildNodes());
42  $nodeArray['children'] = $children;
43  $this->recursionLevel--;
44  }
45  return $nodeArray;
46  }
47 
54  protected function getNodeArray(\TYPO3\CMS\Backend\Tree\TreeRepresentationNode $node)
55  {
56  $nodeArray = array(
57  'iconCls' => $node->getIcon(),
58  'text' => $node->getLabel(),
59  'leaf' => !$node->hasChildNodes(),
60  'id' => $node->getId(),
61  'uid' => $node->getId()
62  );
63 
64  foreach ($nodeArray as &$nodeItem) {
65  if (is_string($nodeItem)) {
66  $nodeItem = htmlspecialchars($nodeItem);
67  }
68  }
69 
70  return $nodeArray;
71  }
72 
80  public function renderTree(\TYPO3\CMS\Backend\Tree\AbstractTree $tree, $recursive = true)
81  {
82  $this->recursionLevel = 0;
83  $children = $this->renderNode($tree->getRoot(), $recursive);
84  return json_encode($children);
85  }
86 
94  public function renderNodeCollection(\TYPO3\CMS\Backend\Tree\TreeNodeCollection $collection, $recursive = true)
95  {
96  foreach ($collection as $node) {
97  $treeItems[] = $this->renderNode($node, $recursive);
98  }
99  return $treeItems;
100  }
101 }