1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Alpixel\Bundle\MenuBundle\Entity; |
4
|
|
|
|
5
|
|
|
use Alpixel\Bundle\MenuBundle\Model\ItemInterface; |
6
|
|
|
use Alpixel\Bundle\MenuBundle\Model\MenuInterface; |
7
|
|
|
use Alpixel\Bundle\MenuBundle\Validator\Constraints\RouteExists; |
8
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
9
|
|
|
use Doctrine\ORM\Mapping as ORM; |
10
|
|
|
use Gedmo\Mapping\Annotation as Gedmo; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @ORM\Table(name="alpixel_menu_item") |
14
|
|
|
* @ORM\Entity(repositoryClass="Alpixel\Bundle\MenuBundle\Entity\Repository\ItemRepository") |
15
|
|
|
*/ |
16
|
|
|
class Item implements ItemInterface |
17
|
|
|
{ |
18
|
|
|
const URI_TYPE_ANCHOR = 'anchor'; |
19
|
|
|
const URI_TYPE_EXTERNAL = 'external'; |
20
|
|
|
const URI_TYPE_INTERNAL = 'internal'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var int |
24
|
|
|
* |
25
|
|
|
* @ORM\Column(name="item_id", type="integer", nullable=false) |
26
|
|
|
* @ORM\Id |
27
|
|
|
* @ORM\GeneratedValue(strategy="IDENTITY") |
28
|
|
|
*/ |
29
|
|
|
protected $id; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @Gedmo\SortableGroup |
33
|
|
|
* |
34
|
|
|
* @ORM\ManyToOne(targetEntity="Alpixel\Bundle\MenuBundle\Entity\Item", inversedBy="children") |
35
|
|
|
* @ORM\JoinColumn(name="parent_id", referencedColumnName="item_id", onDelete="SET NULL") |
36
|
|
|
*/ |
37
|
|
|
protected $parent; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @ORM\OneToMany(targetEntity="Alpixel\Bundle\MenuBundle\Entity\Item", mappedBy="parent") |
41
|
|
|
* @ORM\OrderBy({"position": "ASC"}) |
42
|
|
|
*/ |
43
|
|
|
protected $children; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @Gedmo\SortableGroup |
47
|
|
|
* |
48
|
|
|
* @ORM\ManyToOne(targetEntity="Alpixel\Bundle\MenuBundle\Entity\Menu", inversedBy="items") |
49
|
|
|
* @ORM\JoinColumn(name="menu_id", referencedColumnName="menu_id") |
50
|
|
|
*/ |
51
|
|
|
protected $menu; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var string |
55
|
|
|
* |
56
|
|
|
* @ORM\Column(name="name", type="string", length=255, nullable=false) |
57
|
|
|
*/ |
58
|
|
|
protected $name; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @RouteExists |
62
|
|
|
* @ORM\Column(name="uri", type="text", nullable=false) |
63
|
|
|
*/ |
64
|
|
|
protected $uri; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @Gedmo\SortablePosition |
68
|
|
|
* |
69
|
|
|
* @ORM\Column(name="position", type="integer", nullable=false) |
70
|
|
|
*/ |
71
|
|
|
protected $position; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @Gedmo\Slug(fields={"name"}, updatable=true, separator="_") |
75
|
|
|
* @ORM\Column(length=128) |
76
|
|
|
**/ |
77
|
|
|
protected $slug; |
78
|
|
|
|
79
|
|
|
public function __construct() |
80
|
|
|
{ |
81
|
|
|
$this->children = new ArrayCollection(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get string defined. |
86
|
|
|
* |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
public function __toString() |
90
|
|
|
{ |
91
|
|
|
return (string) $this->name; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get Id. |
96
|
|
|
* |
97
|
|
|
* @return int |
98
|
|
|
*/ |
99
|
|
|
public function getId() |
100
|
|
|
{ |
101
|
|
|
return $this->id; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get menu. |
106
|
|
|
* |
107
|
|
|
* @return Menu |
108
|
|
|
*/ |
109
|
|
|
public function getMenu() |
110
|
|
|
{ |
111
|
|
|
return $this->menu; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Set menu. |
116
|
|
|
* |
117
|
|
|
* @param Menu $menu |
118
|
|
|
* |
119
|
|
|
* @return self |
120
|
|
|
*/ |
121
|
|
|
public function setMenu(MenuInterface $menu) |
122
|
|
|
{ |
123
|
|
|
$this->menu = $menu; |
124
|
|
|
|
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Get parent Item. |
130
|
|
|
* |
131
|
|
|
* @return null\Item |
132
|
|
|
*/ |
133
|
|
|
public function getParent() |
134
|
|
|
{ |
135
|
|
|
return $this->parent; |
|
|
|
|
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Set parent Item. |
140
|
|
|
* |
141
|
|
|
* @param ItemInterface $menu |
|
|
|
|
142
|
|
|
* |
143
|
|
|
* @return self |
144
|
|
|
*/ |
145
|
|
|
public function setParent(ItemInterface $item = null) |
146
|
|
|
{ |
147
|
|
|
$this->parent = $item; |
148
|
|
|
|
149
|
|
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Get children of Item. |
154
|
|
|
* |
155
|
|
|
* @return ArrayCollection (Item) |
156
|
|
|
*/ |
157
|
|
|
public function getChildren() |
158
|
|
|
{ |
159
|
|
|
$children = $this->children; |
160
|
|
|
|
161
|
|
|
return $children; |
|
|
|
|
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Set Item from ArrayCollection. |
166
|
|
|
* |
167
|
|
|
* @param null\ArrayCollection |
168
|
|
|
* |
169
|
|
|
* @return self |
170
|
|
|
*/ |
171
|
|
|
public function addChildren(ArrayCollection $collection) |
172
|
|
|
{ |
173
|
|
|
foreach ($collection as $item) { |
174
|
|
|
$this->setChildren($item); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
return $this; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Set children of Item. |
182
|
|
|
* |
183
|
|
|
* @param Item $item |
184
|
|
|
* |
185
|
|
|
* @return self |
186
|
|
|
*/ |
187
|
|
|
public function setChildren(ItemInterface $item) |
188
|
|
|
{ |
189
|
|
|
if ($this->children->contains($item) === false) { |
190
|
|
|
$this->children->add($item); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
return $this; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Remove children. |
198
|
|
|
* |
199
|
|
|
* @param ItemInterface $item |
200
|
|
|
* |
201
|
|
|
* @return $this |
202
|
|
|
*/ |
203
|
|
|
public function removeChildren(ItemInterface $item) |
204
|
|
|
{ |
205
|
|
|
if ($this->children->contains($item) === true) { |
206
|
|
|
$this->children->removeElement($item); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
return $this; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Get name displayed in Item. |
214
|
|
|
* |
215
|
|
|
* @return string |
216
|
|
|
*/ |
217
|
|
|
public function getName() |
218
|
|
|
{ |
219
|
|
|
return $this->name; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Set name displayed in Item. |
224
|
|
|
* |
225
|
|
|
* @param string |
226
|
|
|
* |
227
|
|
|
* @return self |
228
|
|
|
*/ |
229
|
|
|
public function setName($name) |
230
|
|
|
{ |
231
|
|
|
$this->name = $name; |
232
|
|
|
|
233
|
|
|
return $this; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Get URL. |
238
|
|
|
* |
239
|
|
|
* @return string |
240
|
|
|
*/ |
241
|
|
|
public function getUri() |
242
|
|
|
{ |
243
|
|
|
return $this->uri; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Set URL. |
248
|
|
|
* |
249
|
|
|
* @param string $url |
|
|
|
|
250
|
|
|
* |
251
|
|
|
* @return self |
252
|
|
|
*/ |
253
|
|
|
public function setUri($uri) |
254
|
|
|
{ |
255
|
|
|
$this->uri = $uri; |
256
|
|
|
|
257
|
|
|
return $this; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Get position of Item. |
262
|
|
|
* |
263
|
|
|
* @return int |
264
|
|
|
*/ |
265
|
|
|
public function getPosition() |
266
|
|
|
{ |
267
|
|
|
return $this->position; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Set position. |
272
|
|
|
* |
273
|
|
|
* @param int $position |
274
|
|
|
* |
275
|
|
|
* @return self |
276
|
|
|
*/ |
277
|
|
|
public function setPosition($position) |
278
|
|
|
{ |
279
|
|
|
$this->position = $position; |
280
|
|
|
|
281
|
|
|
return $this; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Gets the value of slug. |
286
|
|
|
* |
287
|
|
|
* @return mixed |
288
|
|
|
*/ |
289
|
|
|
public function getSlug() |
290
|
|
|
{ |
291
|
|
|
return $this->slug; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Sets the value of slug. |
296
|
|
|
* |
297
|
|
|
* @param mixed $slug the slug |
298
|
|
|
* |
299
|
|
|
* @return self |
300
|
|
|
*/ |
301
|
|
|
public function setSlug($slug) |
302
|
|
|
{ |
303
|
|
|
$this->slug = $slug; |
304
|
|
|
|
305
|
|
|
return $this; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
public static function getUriType() |
309
|
|
|
{ |
310
|
|
|
return [ |
311
|
|
|
self::URI_TYPE_ANCHOR, |
312
|
|
|
self::URI_TYPE_EXTERNAL, |
313
|
|
|
self::URI_TYPE_INTERNAL, |
314
|
|
|
]; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
public static function uriTypeExists($type) |
318
|
|
|
{ |
319
|
|
|
return in_array($type, self::getUriType()); |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
|