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