1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Zikula package. |
7
|
|
|
* |
8
|
|
|
* Copyright Zikula - https://ziku.la/ |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Zikula\MenuModule\Entity; |
15
|
|
|
|
16
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
17
|
|
|
use Doctrine\ORM\Mapping as ORM; |
18
|
|
|
use Gedmo\Mapping\Annotation as Gedmo; |
19
|
|
|
use Knp\Menu\NodeInterface; |
20
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
21
|
|
|
use Zikula\Bundle\CoreBundle\Doctrine\EntityAccess; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Represents one menu item in a nested set. |
25
|
|
|
* Extends NodeInterface in order to integrate with KnpMenu. |
26
|
|
|
* |
27
|
|
|
* @ORM\Entity(repositoryClass="Zikula\MenuModule\Entity\Repository\MenuItemRepository") |
28
|
|
|
* @ORM\Table(name="menu_items") |
29
|
|
|
* @Gedmo\Tree(type="nested") |
30
|
|
|
*/ |
31
|
|
|
class MenuItemEntity extends EntityAccess implements NodeInterface |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @ORM\Column(type="integer") |
35
|
|
|
* @ORM\Id |
36
|
|
|
* @ORM\GeneratedValue |
37
|
|
|
* @var int |
38
|
|
|
*/ |
39
|
|
|
private $id; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @ORM\Column(length=64) |
43
|
|
|
* @Assert\Length(min="1", max="64") |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
private $title; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @Gedmo\TreeLeft |
50
|
|
|
* @ORM\Column(type="integer") |
51
|
|
|
* @var int |
52
|
|
|
*/ |
53
|
|
|
private $lft; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @Gedmo\TreeLevel |
57
|
|
|
* @ORM\Column(type="integer") |
58
|
|
|
* @var int |
59
|
|
|
*/ |
60
|
|
|
private $lvl; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @Gedmo\TreeRight |
64
|
|
|
* @ORM\Column(type="integer") |
65
|
|
|
* @var int |
66
|
|
|
*/ |
67
|
|
|
private $rgt; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @Gedmo\TreeRoot |
71
|
|
|
* @ORM\ManyToOne(targetEntity="MenuItemEntity") |
72
|
|
|
* @ORM\JoinColumn(referencedColumnName="id", onDelete="CASCADE") |
73
|
|
|
* @var self |
74
|
|
|
*/ |
75
|
|
|
private $root; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @Gedmo\TreeParent |
79
|
|
|
* @ORM\ManyToOne(targetEntity="MenuItemEntity", inversedBy="children") |
80
|
|
|
* @ORM\JoinColumn(referencedColumnName="id", onDelete="CASCADE") |
81
|
|
|
* @var self |
82
|
|
|
*/ |
83
|
|
|
private $parent; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @ORM\OneToMany(targetEntity="MenuItemEntity", mappedBy="parent") |
87
|
|
|
* @ORM\OrderBy({"lft" = "ASC"}) |
88
|
|
|
*/ |
89
|
|
|
private $children; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @ORM\Column(type="array") |
93
|
|
|
* possible keys |
94
|
|
|
* [ |
95
|
|
|
* 'route' => null, |
96
|
|
|
* 'routeParameters' => [], |
97
|
|
|
* 'icon' => null, |
98
|
|
|
* 'uri' => null, |
99
|
|
|
* 'label' => null, |
100
|
|
|
* 'attributes' => [], |
101
|
|
|
* 'linkAttributes' => [], |
102
|
|
|
* 'childrenAttributes' => [], |
103
|
|
|
* 'labelAttributes' => [], |
104
|
|
|
* 'extras' => [], |
105
|
|
|
* 'current' => null, |
106
|
|
|
* 'display' => true, |
107
|
|
|
* 'displayChildren' => true, |
108
|
|
|
* ] |
109
|
|
|
*/ |
110
|
|
|
private $options; |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* MenuItemEntity constructor. |
114
|
|
|
*/ |
115
|
|
|
public function __construct() |
116
|
|
|
{ |
117
|
|
|
$this->title = ''; |
118
|
|
|
$this->options = [ |
119
|
|
|
/*'routeParameters' => [], |
120
|
|
|
'attributes' => [], |
121
|
|
|
'linkAttributes' => [], |
122
|
|
|
'childrenAttributes' => [], |
123
|
|
|
'labelAttributes' => [], |
124
|
|
|
'extras' => [], |
125
|
|
|
'display' => true, |
126
|
|
|
'displayChildren' => true*/ |
127
|
|
|
]; |
128
|
|
|
$this->children = new ArrayCollection(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function getId(): ?int |
132
|
|
|
{ |
133
|
|
|
return $this->id; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function setId(int $id): self |
137
|
|
|
{ |
138
|
|
|
$this->id = $id; |
139
|
|
|
|
140
|
|
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function getTitle(): string |
144
|
|
|
{ |
145
|
|
|
return $this->title; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function setTitle(string $title): self |
149
|
|
|
{ |
150
|
|
|
$this->title = $title; |
151
|
|
|
|
152
|
|
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function getLft(): int |
156
|
|
|
{ |
157
|
|
|
return $this->lft; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function getLvl(): int |
161
|
|
|
{ |
162
|
|
|
return $this->lvl; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function getRgt(): int |
166
|
|
|
{ |
167
|
|
|
return $this->rgt; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function getRoot(): ?self |
171
|
|
|
{ |
172
|
|
|
return $this->root; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function setRoot(self $root): self |
176
|
|
|
{ |
177
|
|
|
$this->root = $root; |
178
|
|
|
|
179
|
|
|
return $this; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function getParent(): ?self |
183
|
|
|
{ |
184
|
|
|
return $this->parent; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function setParent(self $parent = null): self |
188
|
|
|
{ |
189
|
|
|
$this->parent = $parent; |
190
|
|
|
|
191
|
|
|
return $this; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
public function getName(): string |
195
|
|
|
{ |
196
|
|
|
return $this->title; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public function getOptions(): array |
200
|
|
|
{ |
201
|
|
|
return $this->options; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function getOption(string $name, $default = null) |
205
|
|
|
{ |
206
|
|
|
return $this->options[$name] ?? $default; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
public function setOptions(array $options = []): self |
210
|
|
|
{ |
211
|
|
|
$this->options = $options; |
212
|
|
|
|
213
|
|
|
return $this; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
public function setOption(string $name, $value): self |
217
|
|
|
{ |
218
|
|
|
$this->options[$name] = $value; |
219
|
|
|
|
220
|
|
|
return $this; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
public function removeOption(string $name): self |
224
|
|
|
{ |
225
|
|
|
if ($this->hasOption($name)) { |
226
|
|
|
unset($this->options[$name]); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
return $this; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
public function hasOption(string $name): bool |
233
|
|
|
{ |
234
|
|
|
return isset($this->options[$name]); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
public function hasOptions(): bool |
238
|
|
|
{ |
239
|
|
|
return !empty($this->options); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
public function getChildren(): \Traversable |
243
|
|
|
{ |
244
|
|
|
return $this->children; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
public function getAttributes(): array |
248
|
|
|
{ |
249
|
|
|
return $this->hasAttributes() ? $this->options['attributes'] : []; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @return mixed |
254
|
|
|
*/ |
255
|
|
|
public function getAttribute(string $name, $default = null) |
256
|
|
|
{ |
257
|
|
|
return $this->options['attributes'][$name] ?? $default; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
public function setAttributes(array $attributes = []): self |
261
|
|
|
{ |
262
|
|
|
$this->options['attributes'] = $attributes; |
263
|
|
|
|
264
|
|
|
return $this; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
public function setAttribute(string $name, $value): self |
268
|
|
|
{ |
269
|
|
|
$this->options['attributes'][$name] = $value; |
270
|
|
|
|
271
|
|
|
return $this; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
public function removeAttribute(string $name): self |
275
|
|
|
{ |
276
|
|
|
if (isset($this->options['attributes'][$name])) { |
277
|
|
|
unset($this->options['attributes'][$name]); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
return $this; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
public function hasAttributes(): bool |
284
|
|
|
{ |
285
|
|
|
return !empty($this->options['attributes']); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
public function getLinkAttributes(): array |
289
|
|
|
{ |
290
|
|
|
return $this->hasLinkAttributes() ? $this->options['linkAttributes'] : []; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* @return mixed |
295
|
|
|
*/ |
296
|
|
|
public function getLinkAttribute(string $name, $default = null) |
297
|
|
|
{ |
298
|
|
|
return $this->options['linkAttributes'][$name] ?? $default; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
public function setLinkAttributes(array $attributes = []): self |
302
|
|
|
{ |
303
|
|
|
$this->options['linkAttributes'] = $attributes; |
304
|
|
|
|
305
|
|
|
return $this; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
public function setLinkAttribute(string $name, $value): self |
309
|
|
|
{ |
310
|
|
|
$this->options['linkAttributes'][$name] = $value; |
311
|
|
|
|
312
|
|
|
return $this; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
public function removeLinkAttribute(string $name): self |
316
|
|
|
{ |
317
|
|
|
if (isset($this->options['linkAttributes'][$name])) { |
318
|
|
|
unset($this->options['linkAttributes'][$name]); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
return $this; |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
public function hasLinkAttributes(): bool |
325
|
|
|
{ |
326
|
|
|
return !empty($this->options['linkAttributes']); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
public function getChildrenAttributes(): array |
330
|
|
|
{ |
331
|
|
|
return $this->hasChildrenAttributes() ? $this->options['childrenAttributes'] : []; |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* @return mixed |
336
|
|
|
*/ |
337
|
|
|
public function getChildrenAttribute(string $name, $default = null) |
338
|
|
|
{ |
339
|
|
|
return $this->options['childrenAttributes'][$name] ?? $default; |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
public function setChildrenAttributes(array $attributes = []): self |
343
|
|
|
{ |
344
|
|
|
$this->options['childrenAttributes'] = $attributes; |
345
|
|
|
|
346
|
|
|
return $this; |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
public function setChildrenAttribute(string $name, $value): self |
350
|
|
|
{ |
351
|
|
|
$this->options['childrenAttributes'][$name] = $value; |
352
|
|
|
|
353
|
|
|
return $this; |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
public function removeChildrenAttribute(string $name): self |
357
|
|
|
{ |
358
|
|
|
if (isset($this->options['childrenAttributes'][$name])) { |
359
|
|
|
unset($this->options['childrenAttributes'][$name]); |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
return $this; |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
public function hasChildrenAttributes(): bool |
366
|
|
|
{ |
367
|
|
|
return !empty($this->options['childrenAttributes']); |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
public function getLabelAttributes(): array |
371
|
|
|
{ |
372
|
|
|
return $this->hasLabelAttributes() ? $this->options['labelAttributes'] : []; |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* @return mixed |
377
|
|
|
*/ |
378
|
|
|
public function getLabelAttribute(string $name, $default = null) |
379
|
|
|
{ |
380
|
|
|
return $this->options['labelAttributes'][$name] ?? $default; |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
public function setLabelAttributes(array $attributes = []): self |
384
|
|
|
{ |
385
|
|
|
$this->options['labelAttributes'] = $attributes; |
386
|
|
|
|
387
|
|
|
return $this; |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
public function setLabelAttribute(string $name, $value): self |
391
|
|
|
{ |
392
|
|
|
$this->options['labelAttributes'][$name] = $value; |
393
|
|
|
|
394
|
|
|
return $this; |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
public function removeLabelAttribute(string $name): self |
398
|
|
|
{ |
399
|
|
|
if (isset($this->options['labelAttributes'][$name])) { |
400
|
|
|
unset($this->options['labelAttributes'][$name]); |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
return $this; |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
public function hasLabelAttributes(): bool |
407
|
|
|
{ |
408
|
|
|
return !empty($this->options['labelAttributes']); |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
public function getExtras(): array |
412
|
|
|
{ |
413
|
|
|
return $this->hasExtras() ? $this->options['extras'] : []; |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
/** |
417
|
|
|
* @return mixed |
418
|
|
|
*/ |
419
|
|
|
public function getExtra(string $name, $default = null) |
420
|
|
|
{ |
421
|
|
|
return $this->options['extras'][$name] ?? $default; |
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
public function setExtras(array $attributes = []): self |
425
|
|
|
{ |
426
|
|
|
$this->options['extras'] = $attributes; |
427
|
|
|
|
428
|
|
|
return $this; |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
public function setExtra(string $name, $value): self |
432
|
|
|
{ |
433
|
|
|
$this->options['extras'][$name] = $value; |
434
|
|
|
|
435
|
|
|
return $this; |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
public function removeExtra(string $name): self |
439
|
|
|
{ |
440
|
|
|
if (isset($this->options['extras'][$name])) { |
441
|
|
|
unset($this->options['extras'][$name]); |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
return $this; |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
public function hasExtras(): bool |
448
|
|
|
{ |
449
|
|
|
return !empty($this->options['extras']); |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
public function toJson($prefix = ''): string |
453
|
|
|
{ |
454
|
|
|
return json_encode([ |
455
|
|
|
'id' => $prefix . $this->id, |
456
|
|
|
'title' => $this->title, |
457
|
|
|
'text' => $this->title, |
458
|
|
|
'options' => $this->options, |
459
|
|
|
'parent' => null !== $this->getParent() ? $this->getParent()->getId() : null, |
460
|
|
|
'root' => null !== $this->getRoot() ? $this->getRoot()->getId() : null |
461
|
|
|
]); |
462
|
|
|
} |
463
|
|
|
} |
464
|
|
|
|