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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
<?php
/**
* Class TOCItem
* A representation of a single entry in a table of contents or breadcrumb.
* One entry contains all children.
*
* @author Thomas Hackl <hackl@data-quest.de>
* @license GPL2 or any later version
* @since Stud.IP 5.0
*/
class TOCItem
{
// This item's display title.
public $title = '';
// Optional icon.
public $icon = null;
// A URL for link handling.
public $url = '';
// Additional attributes for URL
public $link_attributes = [];
// Parent item.
public $parent = null;
// Array of child items.
public $children = [];
// Marks the currently active item.
public $active = false;
public function __construct($title)
{
$this->title = $title;
}
/**
* Get the display title.
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set a new display title.
* @param string $newTitle
* @return $this
*/
public function setTitle(string $newTitle)
{
$this->title = $newTitle;
return $this;
}
/**
* Get this item's icon.
* @return Icon
*/
public function getIcon()
{
return $this->icon;
}
/**
* Set a new icon.
* @param Icon $icon
* @return $this
*/
public function setIcon(Icon $icon)
{
$this->icon = $icon;
return $this;
}
/**
* Get this item's URL, with additional parameters if set.
* @return string
*/
public function getUrl()
{
return count($this->link_attributes) > 0 ?
URLHelper::getLink($this->url, $this->link_attributes) :
$this->url;
}
/**
* Set a new URL, optionally with additional link parameters.
* @param string $newUrl
* @param mixed|null $parameters
* @return $this
*/
public function setUrl(string $newUrl, mixed $parameters = null)
{
$this->url = $newUrl;
if ($parameters !== null) {
$this->link_attributes = $parameters;
}
return $this;
}
/**
* Gets the additional link attributes.
* @return array
*/
public function getLinkAttributes()
{
return $this->link_attributes;
}
/**
* Sets new additional link attributes.
* @param array $attributes
* @return $this
*/
public function setLinkAttributes($attributes)
{
$this->link_attributes = $attributes;
return $this;
}
/**
* Gets this item's parent element.
* @return null|TOCItem
*/
public function getParent()
{
return $this->parent;
}
/**
* Sets a new parent element for this item.
* @param TOCItem $newParent
* @return $this
*/
public function setParent(TOCItem $newParent)
{
$this->parent = $newParent;
return $this;
}
/**
* Gets this item's children as array.
* @return array
*/
public function getChildren()
{
return $this->children;
}
/**
* Sets a new array of child elements.
* @param array $newChildren
* @return $this
*/
public function setChildren(array $newChildren)
{
$this->children = $newChildren;
return $this;
}
/**
* Add a new child to this item, optionally at a given position.
*
* @param TOCItem $newChild
* @param null|int $position
* @return $this
*/
public function addChild(TOCItem $newChild, $position = null)
{
$newChild->setParent($this);
if ($position === null) {
$this->children[] = $newChild;
} else {
$this->children = array_splice($this->children, $position, 0, $newChild);
}
return $this;
}
/**
* Removes the given child.
*
* @param TOCItem $child
* @return $this
*/
public function removeChild(TOCItem $child)
{
array_filter($this->children, function ($c) use ($child) {
return $c != $child;
});
return $this;
}
/**
* Does the current item have children?
*
* @return bool
*/
public function hasChildren()
{
return count($this->children) > 0;
}
/**
* Count the elements in the hierarchy, starting at (and counting in) current item.
*
* @return int
*/
public function countAllChildren()
{
// Current item + all direct children.
$count = 1;
foreach ($this->children as $child) {
$count += $child->countAllChildren();
}
return $count;
}
/**
* Check if current item is active.
*
* @return bool
*/
public function isActive()
{
return $this->active;
}
/**
* Set active status of current item.
*
* @param bool $state
* @return $this
*/
public function setActive(bool $state)
{
$this->active = $state;
return $this;
}
public function getActiveItem()
{
if ($this->isActive()) {
return $this;
} else {
$active = array_filter($this->getRoot()->flatten(), function ($i) {
return $i->isActive();
});
return array_shift($active);
}
}
/**
* Is the current element the root of its hierarchy?
*
* @return bool
*/
public function isRoot()
{
return $this->parent == null;
}
/**
* Get the root element of the current hierarchy (= the element with no parent)
*
* @return bool
*/
public function getRoot()
{
return $this->isRoot() ? $this : $this->parent->getRoot();
}
/**
* Get a string representation of the current hierarchy as breadcrumb-like path.
*
* @param string $separator which separator tu use between elements?
* @return string
*/
public function getPath($separator = '/')
{
$path = $this->title;
if (!$this->isRoot()) {
$path = $this->parent->getPath($separator) . $separator . ' ' . $path . ' ';
}
return $path;
}
/**
* Generates a flat representation of the current item and all its children and children's children.
* @return array|TOCItem[]
*/
public function flatten()
{
$flat = [$this];
foreach ($this->getChildren() as $child) {
$flat = array_merge($flat, $child->flatten());
}
return $flat;
}
/**
* String representation of this item.
* @return string
*/
public function __toString()
{
return $this->title;
}
}
|