1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
4
|
|
|
* |
5
|
|
|
* Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics) |
6
|
|
|
* |
7
|
|
|
* This program is free software: you can redistribute it and/or modify |
8
|
|
|
* it under the terms of the GNU Affero General Public License as published |
9
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
10
|
|
|
* (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU Affero General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU Affero General Public License |
18
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
declare(strict_types=1); |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
25
|
|
|
* |
26
|
|
|
* Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics) |
27
|
|
|
* |
28
|
|
|
* This program is free software; you can redistribute it and/or |
29
|
|
|
* modify it under the terms of the GNU General Public License |
30
|
|
|
* as published by the Free Software Foundation; either version 2 |
31
|
|
|
* of the License, or (at your option) any later version. |
32
|
|
|
* |
33
|
|
|
* This program is distributed in the hope that it will be useful, |
34
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
35
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
36
|
|
|
* GNU General Public License for more details. |
37
|
|
|
* |
38
|
|
|
* You should have received a copy of the GNU General Public License |
39
|
|
|
* along with this program; if not, write to the Free Software |
40
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
41
|
|
|
*/ |
42
|
|
|
|
43
|
|
|
namespace App\Helpers\Trees; |
44
|
|
|
|
45
|
|
|
use JsonSerializable; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* This class represents a node for the bootstrap treeview node. |
49
|
|
|
* When you serialize an array of these objects to JSON, you can use the serialized data in data for the treeview. |
50
|
|
|
*/ |
51
|
|
|
final class TreeViewNode implements JsonSerializable |
52
|
|
|
{ |
53
|
|
|
private $text; |
54
|
|
|
private $href; |
55
|
|
|
private $nodes; |
56
|
|
|
|
57
|
|
|
private $state = null; |
58
|
|
|
|
59
|
|
|
private $tags; |
60
|
|
|
|
61
|
|
|
private $id; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Creates a new TreeView node with the given parameters. |
65
|
|
|
* |
66
|
|
|
* @param string $text The text that is shown in the node. (e.g. the name of the node) |
67
|
|
|
* @param string|null $href A link for the node. You have to activate "enableLinks" option in init of the treeview. |
68
|
|
|
* Set this to null, if no href should be used. |
69
|
|
|
* @param array|null $nodes An array containing other TreeViewNodes. They will be used as children nodes of the |
70
|
|
|
* newly created nodes. Set to null, if it should not have children. |
71
|
|
|
*/ |
72
|
|
|
public function __construct(string $text, ?string $href = null, ?array $nodes = null) |
73
|
|
|
{ |
74
|
|
|
$this->text = $text; |
75
|
|
|
$this->href = $href; |
76
|
|
|
$this->nodes = $nodes; |
77
|
|
|
|
78
|
|
|
//$this->state = new TreeViewNodeState(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Return the ID of the entity associated with this node. |
83
|
|
|
* Null if this node is not connected with an entity. |
84
|
|
|
*/ |
85
|
|
|
public function getId(): ?int |
86
|
|
|
{ |
87
|
|
|
return $this->id; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Sets the ID of the entity associated with this node. |
92
|
|
|
* Null if this node is not connected with an entity. |
93
|
|
|
* |
94
|
|
|
* @return $this |
95
|
|
|
*/ |
96
|
|
|
public function setId(?int $id): self |
97
|
|
|
{ |
98
|
|
|
$this->id = $id; |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Returns the node text. |
105
|
|
|
*/ |
106
|
|
|
public function getText(): string |
107
|
|
|
{ |
108
|
|
|
return $this->text; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Sets the node text. |
113
|
|
|
* |
114
|
|
|
* @param string $text the new node text |
115
|
|
|
* |
116
|
|
|
* @return TreeViewNode |
117
|
|
|
*/ |
118
|
|
|
public function setText(string $text): self |
119
|
|
|
{ |
120
|
|
|
$this->text = $text; |
121
|
|
|
|
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Returns the href link. |
127
|
|
|
*/ |
128
|
|
|
public function getHref(): ?string |
129
|
|
|
{ |
130
|
|
|
return $this->href; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Sets the href link. |
135
|
|
|
* |
136
|
|
|
* @param string|null $href the new href link |
137
|
|
|
* |
138
|
|
|
* @return TreeViewNode |
139
|
|
|
*/ |
140
|
|
|
public function setHref(?string $href): self |
141
|
|
|
{ |
142
|
|
|
$this->href = $href; |
143
|
|
|
|
144
|
|
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Returns the children nodes of this node. |
149
|
|
|
* |
150
|
|
|
* @return TreeViewNode[]|null |
151
|
|
|
*/ |
152
|
|
|
public function getNodes(): ?array |
153
|
|
|
{ |
154
|
|
|
return $this->nodes; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Sets the children nodes. |
159
|
|
|
* |
160
|
|
|
* @param array|null $nodes The new children nodes |
161
|
|
|
* |
162
|
|
|
* @return TreeViewNode |
163
|
|
|
*/ |
164
|
|
|
public function setNodes(?array $nodes): self |
165
|
|
|
{ |
166
|
|
|
$this->nodes = $nodes; |
167
|
|
|
|
168
|
|
|
return $this; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function getState(): ?TreeViewNodeState |
172
|
|
|
{ |
173
|
|
|
return $this->state; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function setState(TreeViewNodeState $state): self |
177
|
|
|
{ |
178
|
|
|
$this->state = $state; |
179
|
|
|
|
180
|
|
|
return $this; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function setDisabled(?bool $disabled): self |
184
|
|
|
{ |
185
|
|
|
//Lazy loading of state, so it does not need to get serialized and transfered, when it is empty. |
186
|
|
|
if (null === $this->state) { |
187
|
|
|
$this->state = new TreeViewNodeState(); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
$this->state->setDisabled($disabled); |
191
|
|
|
|
192
|
|
|
return $this; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
public function setSelected(?bool $selected): self |
196
|
|
|
{ |
197
|
|
|
//Lazy loading of state, so it does not need to get serialized and transfered, when it is empty. |
198
|
|
|
if (null === $this->state) { |
199
|
|
|
$this->state = new TreeViewNodeState(); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
$this->state->setSelected($selected); |
203
|
|
|
|
204
|
|
|
return $this; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function setExpanded(?bool $selected): self |
|
|
|
|
208
|
|
|
{ |
209
|
|
|
//Lazy loading of state, so it does not need to get serialized and transfered, when it is empty. |
210
|
|
|
if (null === $this->state) { |
211
|
|
|
$this->state = new TreeViewNodeState(); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
$this->state->setExpanded(true); |
215
|
|
|
|
216
|
|
|
return $this; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
public function getTags(): ?array |
220
|
|
|
{ |
221
|
|
|
return $this->tags; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
public function addTag(string $new_tag): self |
225
|
|
|
{ |
226
|
|
|
//Lazy loading tags |
227
|
|
|
if (null === $this->tags) { |
228
|
|
|
$this->tags = []; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
$this->tags[] = $new_tag; |
232
|
|
|
|
233
|
|
|
return $this; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
public function jsonSerialize() |
237
|
|
|
{ |
238
|
|
|
$ret = [ |
239
|
|
|
'text' => $this->text, |
240
|
|
|
]; |
241
|
|
|
|
242
|
|
|
if (null !== $this->href) { |
243
|
|
|
$ret['href'] = $this->href; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
if (null !== $this->tags) { |
247
|
|
|
$ret['tags'] = $this->tags; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
if (null !== $this->nodes) { |
251
|
|
|
$ret['nodes'] = $this->nodes; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
if (null !== $this->state) { |
255
|
|
|
$ret['state'] = $this->state; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
return $ret; |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.