-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathMenuItem.php
More file actions
332 lines (284 loc) · 6.83 KB
/
MenuItem.php
File metadata and controls
332 lines (284 loc) · 6.83 KB
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
<?php
/**
* NovaeZMenuManagerBundle.
*
* @package NovaeZMenuManagerBundle
*
* @author Novactive <f.alexandre@novactive.com>
* @copyright 2019 Novactive
* @license https://github.com/Novactive/NovaeZMenuManagerBundle/blob/master/LICENSE
*/
namespace Novactive\EzMenuManagerBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\Mapping as ORM;
use Novactive\EzMenuManager\Traits\IdentityTrait;
/**
* Class MenuItem.
*
* @ORM\Entity()
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="type", type="string")
* @ORM\Table(name="menu_manager_menu_item")
*
* @package Novactive\EzMenuManagerBundle\Entity
*/
class MenuItem
{
use IdentityTrait;
/**
* @ORM\Column(name="name", type="string", nullable=true)
*
* @var string
*/
protected $name;
/**
* @ORM\Column(name="url", type="text", nullable=true)
*
* @var string
*/
protected $url;
/**
* @ORM\Column(name="target", type="string", nullable=true)
*
* @var string
*/
protected $target;
/**
* @var Menu
*
* @ORM\ManyToOne(targetEntity="Novactive\EzMenuManagerBundle\Entity\Menu", inversedBy="items")
*/
protected $menu;
/**
* @var MenuItem[]|ArrayCollection
*
* @ORM\OneToMany(
* targetEntity="Novactive\EzMenuManagerBundle\Entity\MenuItem",
* mappedBy="parent",
* cascade={"persist","remove"},
* orphanRemoval=true
* )
* @ORM\OrderBy({"position" = "ASC"})
*/
protected $childrens;
/**
* @var MenuItem
*
* @ORM\ManyToOne(
* targetEntity="Novactive\EzMenuManagerBundle\Entity\MenuItem",
* inversedBy="childrens"
* )
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
*/
protected $parent;
/**
* @var int
*
* @ORM\Column(name="position", type="integer")
*/
protected $position = 0;
/**
* @ORM\Column(name="options", type="text")
*
* @var array
*/
protected $options;
/**
* @ORM\Column(name="remote_id", type="string", nullable=true)
*
* @var string
*/
protected $remoteId;
/**
* MenuItem constructor.
*/
public function __construct(?string $remoteId = null)
{
$this->childrens = new ArrayCollection();
$this->options = json_encode([]);
$this->remoteId = $remoteId ?? md5(uniqid(get_class($this), true));
}
/**
* @return string
*/
public function getName(): ?string
{
return $this->name;
}
/**
* @return string
*/
public function getRemoteId(): ?string
{
return $this->remoteId;
}
/**
* @param $language
*/
public function getTranslatedName($language): ?string
{
$name = json_decode($this->getName(), true);
return is_array($name) ? ($name[$language] ?? null) : $this->getName();
}
public function setName(string $name): void
{
$this->name = $name;
}
/**
* @return string
*/
public function getUrl(): ?string
{
return $this->url;
}
/**
* @param $language
*/
public function getTranslatedUrl($language): ?string
{
$url = json_decode($this->getUrl(), true);
return is_array($url) ? ($url[$language] ?? null) : $this->getUrl();
}
/**
* @param string $url
*/
public function setUrl($url): void
{
$this->url = $url;
}
/**
* @return string
*/
public function getTarget(): ?string
{
return $this->target;
}
public function setTarget(string $target): void
{
$this->target = $target;
}
public function getMenu(): Menu
{
return $this->menu;
}
public function setMenu(Menu $menu): void
{
$this->menu = $menu;
}
public function hasChildrens(): bool
{
return !$this->childrens->isEmpty();
}
/**
* @return MenuItem[]|ArrayCollection
*/
public function getChildrens()
{
$criteria = new Criteria();
$criteria->orderBy(['position' => Criteria::ASC]);
return $this->childrens->matching($criteria);
}
/**
* @param MenuItem[] $childrens
*/
public function setChildrens($childrens): void
{
$this->childrens = $childrens;
}
/**
* @return array
*/
public function addChildren(MenuItem $children): void
{
if (false === $this->childrens->indexOf($children)) {
$children->setParent($this);
$this->childrens->add($children);
}
}
public function removeChildren(MenuItem $children): void
{
$this->childrens->removeElement($children);
}
public function getParent(): ?MenuItem
{
return $this->parent;
}
/**
* @param MenuItem|null $parent
*/
public function setParent($parent): void
{
$this->parent = $parent;
}
public function removeParent(): void
{
if ($this->parent) {
$this->parent->removeChildren($this);
}
$this->parent = null;
}
public function getPosition(): int
{
return $this->position;
}
public function setPosition(int $position): void
{
$this->position = $position;
}
public function getOptions(): array
{
return json_decode($this->options, true);
}
public function setOptions(array $options): void
{
$this->options = json_encode($options);
}
/**
* @param $name
*/
public function getOption($name, $default = false)
{
$options = $this->getOptions();
return $options[$name] ?? $default;
}
/**
* @param $name
* @param $value
*/
public function setOption($name, $value): void
{
$options = $this->getOptions();
$options[$name] = $value;
$this->setOptions($options);
}
/**
* @return string
*/
public function __toString()
{
return (string) $this->id;
}
public function update(array $properties): void
{
foreach ($properties as $property => $value) {
if ($this->$property !== $value) {
$this->$property = $value;
}
}
}
public function assignPositions(): void
{
/** @var MenuItem[] $childrens */
$childrens = $this->getChildrens()->getValues();
usort($childrens, function (MenuItem $itemA, MenuItem $itemB) {
return $itemA->getPosition() <=> $itemB->getPosition();
});
$position = 0;
foreach ($childrens as $child) {
$child->setPosition($position);
$child->assignPositions();
++$position;
}
}
}