cc8711f31d5d6c89fe58094179adc6e2c7f6528e
[Packages/TYPO3.CMS.git] / t3lib / tree / renderer / class.t3lib_tree_renderer_extjsjson.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010-2011 Steffen Kamper <steffen@typo3.org>
6 * (c) 2010-2011 Steffen Ritter <info@steffen-ritter.net>
7 * All rights reserved
8 *
9 * This script is part of the TYPO3 project. The TYPO3 project is
10 * free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * The GNU General Public License can be found at
16 * http://www.gnu.org/copyleft/gpl.html.
17 * A copy is found in the textfile GPL.txt and important notices to the license
18 * from the author is found in LICENSE.txt distributed with these scripts.
19 *
20 *
21 * This script is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * This copyright notice MUST APPEAR in all copies of the script!
27 ***************************************************************/
28
29
30 /**
31 * Renderer for unordered lists
32 *
33 * @author Steffen Kamper <steffen@typo3.org>
34 * @author Steffen Ritter <info@steffen-ritter.net>
35 * @package TYPO3
36 * @subpackage t3lib
37 */
38 class t3lib_tree_Renderer_ExtJsJson extends t3lib_tree_Renderer_Abstract {
39 /**
40 * recursion level
41 *
42 * @var int
43 */
44 protected $recursionLevel = 0;
45
46 /**
47 * Renders a node recursive or just a single instance
48 *
49 * @param t3lib_tree_RepresentationNode $node
50 * @param bool $recursive
51 * @return mixed
52 */
53 public function renderNode(t3lib_tree_RepresentationNode $node, $recursive = TRUE) {
54 $nodeArray = $this->getNodeArray($node);
55
56 if ($recursive && $node->hasChildNodes()) {
57 $this->recursionLevel++;
58 $children = $this->renderNodeCollection($node->getChildNodes());
59 $nodeArray['children'] = $children;
60 $this->recursionLevel--;
61 }
62
63 return $nodeArray;
64 }
65
66 /**
67 *
68 */
69 protected function getNodeArray(t3lib_tree_RepresentationNode $node) {
70 $nodeArray = array(
71 'iconCls' => $node->getIcon(),
72 'text' => $node->getLabel(),
73 'leaf' => !$node->hasChildNodes(),
74 'id' => $node->getId(),
75 'uid' => $node->getId()
76 );
77
78 return $nodeArray;
79 }
80
81 /**
82 * Renders a node collection recursive or just a single instance
83 *
84 * @param t3lib_tree_NodeCollection $node
85 * @param bool $recursive
86 * @return mixed
87 */
88 public function renderTree(t3lib_tree_AbstractTree $tree, $recursive = TRUE) {
89 $this->recursionLevel = 0;
90 $children = $this->renderNode($tree->getRoot(), $recursive);
91
92 return json_encode($children);
93 }
94
95 /**
96 * Renders an tree recursive or just a single instance
97 *
98 * @param t3lib_tree_AbstractTree $node
99 * @param bool $recursive
100 * @return mixed
101 */
102 public function renderNodeCollection(t3lib_tree_NodeCollection $collection, $recursive = TRUE) {
103 foreach ($collection as $node) {
104 $treeItems[] = $this->renderNode($node, $recursive);
105 }
106 return $treeItems;
107 }
108 }
109
110 ?>