Tree.php
3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\Menu;
defined('JPATH_PLATFORM') or die;
/**
* Menu Tree class to represent a menu tree hierarchy
*
* @since 3.8.0
*/
class Tree
{
/**
* The root menu node
*
* @var Node
*
* @since 3.8.0
*/
protected $root = null;
/**
* The current working menu node
*
* @var Node
*
* @since 3.8.0
*/
protected $current = null;
/**
* The CSS style array
*
* @var string[]
*
* @since 3.8.0
*/
protected $css = array();
/**
* Constructor
*
* @since 3.8.0
*/
public function __construct()
{
$this->root = new Node;
$this->current = $this->root;
}
/**
* Get the root node
*
* @return Node
*
* @since 3.8.0
*/
public function getRoot()
{
return $this->root;
}
/**
* Get the current node
*
* @return Node
*
* @since 3.8.0
*/
public function getCurrent()
{
return $this->current;
}
/**
* Get the current node
*
* @param Node $node The node to be set as current
*
* @return void
*
* @since 3.8.0
*/
public function setCurrent($node)
{
if ($node)
{
$this->current = $node;
}
}
/**
* Method to get the parent and set it as active optionally
*
* @param bool $setCurrent Set that parent as the current node for further working
*
* @return Node
*
* @since 3.8.0
*/
public function getParent($setCurrent = true)
{
$parent = $this->current->getParent();
if ($setCurrent)
{
$this->setCurrent($parent);
}
return $parent;
}
/**
* Method to reset the working pointer to the root node and optionally clear all menu nodes
*
* @param bool $clear Whether to clear the existing menu items or just reset the pointer to root element
*
* @return Node The root node
*
* @since 3.8.0
*/
public function reset($clear = false)
{
if ($clear)
{
$this->root = new Node;
$this->css = array();
}
$this->current = $this->root;
return $this->current;
}
/**
* Method to add a child
*
* @param Node $node The node to process
* @param bool $setCurrent Set this new child as the current node for further working
*
* @return Node The newly added node
*
* @since 3.8.0
*/
public function addChild(Node $node, $setCurrent = false)
{
$this->current->addChild($node);
if ($setCurrent)
{
$this->setCurrent($node);
}
return $node;
}
/**
* Method to get the CSS class name for an icon identifier or create one if
* a custom image path is passed as the identifier
*
* @return string CSS class name
*
* @since 3.8.0
*/
public function getIconClass()
{
static $classes = array();
$identifier = $this->current->get('class');
// Top level is special
if (trim($identifier) == '' || !$this->current->hasParent())
{
return null;
}
if (!isset($classes[$identifier]))
{
// We were passed a class name
if (substr($identifier, 0, 6) == 'class:')
{
$class = substr($identifier, 6);
}
// We were passed background icon url. Build the CSS class for the icon
else
{
$class = preg_replace('#\.[^.]*$#', '', basename($identifier));
$class = preg_replace('#\.\.[^A-Za-z0-9\.\_\- ]#', '', $class);
if ($class)
{
$this->css[] = ".menu-$class {background: url($identifier) no-repeat;}";
}
}
$classes[$identifier] = "menu-$class";
}
return $classes[$identifier];
}
/**
* Get the CSS declarations for this tree
*
* @return string[]
*
* @since 3.8.0
*/
public function getCss()
{
return $this->css;
}
}