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; |
23
|
|
|
|
24
|
|
|
use App\Entity\Base\DBElement; |
25
|
|
|
use App\Entity\Base\NamedDBElement; |
26
|
|
|
use App\Entity\Base\StructuralDBElement; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* This class represents a node for the bootstrap treeview node. |
30
|
|
|
* When you serialize an array of these objects to JSON, you can use the serialized data in data for the treeview. |
31
|
|
|
*/ |
32
|
|
|
class TreeViewNode |
33
|
|
|
{ |
34
|
|
|
protected $text; |
35
|
|
|
protected $href; |
36
|
|
|
protected $nodes; |
37
|
|
|
|
38
|
|
|
protected $state = null; |
39
|
|
|
|
40
|
|
|
protected $tags; |
41
|
|
|
|
42
|
|
|
protected $id; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Creates a new TreeView node with the given parameters. |
46
|
|
|
* |
47
|
|
|
* @param string $text The text that is shown in the node. (e.g. the name of the node) |
48
|
|
|
* @param string|null $href A link for the node. You have to activate "enableLinks" option in init of the treeview. |
49
|
|
|
* Set this to null, if no href should be used. |
50
|
|
|
* @param array|null $nodes An array containing other TreeViewNodes. They will be used as children nodes of the |
51
|
|
|
* newly created nodes. Set to null, if it should not have children. |
52
|
|
|
*/ |
53
|
|
|
public function __construct(string $text, ?string $href = null, ?array $nodes = null) |
54
|
|
|
{ |
55
|
|
|
$this->text = $text; |
56
|
|
|
$this->href = $href; |
57
|
|
|
$this->nodes = $nodes; |
58
|
|
|
|
59
|
|
|
//$this->state = new TreeViewNodeState(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Return the ID of the entity associated with this node. |
64
|
|
|
* Null if this node is not connected with an entity. |
65
|
|
|
* @return int|null |
66
|
|
|
*/ |
67
|
|
|
public function getId() : ?int |
68
|
|
|
{ |
69
|
|
|
return $this->id; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Sets the ID of the entity associated with this node. |
74
|
|
|
* Null if this node is not connected with an entity. |
75
|
|
|
* @param int|null $id |
76
|
|
|
* @return $this |
77
|
|
|
*/ |
78
|
|
|
public function setId(?int $id): self |
79
|
|
|
{ |
80
|
|
|
$this->id = $id; |
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Returns the node text. |
86
|
|
|
* |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
public function getText(): string |
90
|
|
|
{ |
91
|
|
|
return $this->text; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Sets the node text. |
96
|
|
|
* |
97
|
|
|
* @param string $text The new node text. |
98
|
|
|
* |
99
|
|
|
* @return TreeViewNode |
100
|
|
|
*/ |
101
|
|
|
public function setText(string $text): self |
102
|
|
|
{ |
103
|
|
|
$this->text = $text; |
104
|
|
|
|
105
|
|
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Returns the href link. |
110
|
|
|
* |
111
|
|
|
* @return string|null |
112
|
|
|
*/ |
113
|
|
|
public function getHref(): ?string |
114
|
|
|
{ |
115
|
|
|
return $this->href; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Sets the href link. |
120
|
|
|
* |
121
|
|
|
* @param string|null $href The new href link. |
122
|
|
|
* |
123
|
|
|
* @return TreeViewNode |
124
|
|
|
*/ |
125
|
|
|
public function setHref(?string $href): self |
126
|
|
|
{ |
127
|
|
|
$this->href = $href; |
128
|
|
|
|
129
|
|
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Returns the children nodes of this node. |
134
|
|
|
* |
135
|
|
|
* @return TreeViewNode[]|null |
136
|
|
|
*/ |
137
|
|
|
public function getNodes(): ?array |
138
|
|
|
{ |
139
|
|
|
return $this->nodes; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Sets the children nodes. |
144
|
|
|
* |
145
|
|
|
* @param array|null $nodes The new children nodes |
146
|
|
|
* |
147
|
|
|
* @return TreeViewNode |
148
|
|
|
*/ |
149
|
|
|
public function setNodes(?array $nodes): self |
150
|
|
|
{ |
151
|
|
|
$this->nodes = $nodes; |
152
|
|
|
|
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function getState(): ?TreeViewNodeState |
157
|
|
|
{ |
158
|
|
|
return $this->state; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function setState(TreeViewNodeState $state): self |
162
|
|
|
{ |
163
|
|
|
$this->state = $state; |
164
|
|
|
|
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function setDisabled(?bool $disabled): self |
169
|
|
|
{ |
170
|
|
|
//Lazy loading of state, so it does not need to get serialized and transfered, when it is empty. |
171
|
|
|
if (null == $this->state) { |
172
|
|
|
$this->state = new TreeViewNodeState(); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
$this->state->setDisabled($disabled); |
176
|
|
|
|
177
|
|
|
return $this; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function setSelected(?bool $selected): self |
181
|
|
|
{ |
182
|
|
|
//Lazy loading of state, so it does not need to get serialized and transfered, when it is empty. |
183
|
|
|
if (null == $this->state) { |
184
|
|
|
$this->state = new TreeViewNodeState(); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
$this->state->setSelected($selected); |
188
|
|
|
|
189
|
|
|
return $this; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
public function getTags(): ?array |
193
|
|
|
{ |
194
|
|
|
return $this->tags; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
public function addTag(string $new_tag): self |
198
|
|
|
{ |
199
|
|
|
//Lazy loading tags |
200
|
|
|
if (null == $this->tags) { |
201
|
|
|
$this->tags = []; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
$this->tags[] = $new_tag; |
205
|
|
|
|
206
|
|
|
return $this; |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|