1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the OrbitaleCmsBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Alexandre Rock Ancelet <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Orbitale\Bundle\CmsBundle\Entity; |
13
|
|
|
|
14
|
|
|
use Behat\Transliterator\Transliterator; |
15
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
16
|
|
|
use Doctrine\ORM\Mapping as ORM; |
17
|
|
|
use Doctrine\ORM\Event\LifecycleEventArgs; |
18
|
|
|
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @UniqueEntity("slug") |
22
|
|
|
* @ORM\HasLifecycleCallbacks() |
23
|
|
|
* @ORM\MappedSuperclass(repositoryClass="Orbitale\Bundle\CmsBundle\Repository\CategoryRepository") |
24
|
|
|
*/ |
25
|
|
|
abstract class Category |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @return int |
29
|
|
|
*/ |
30
|
|
|
abstract public function getId(); |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
* @ORM\Column(name="name", type="string", length=255) |
35
|
|
|
*/ |
36
|
|
|
protected $name; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
* @ORM\Column(name="slug", type="string", length=255, unique=true) |
41
|
|
|
*/ |
42
|
|
|
protected $slug; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string |
46
|
|
|
* @ORM\Column(name="description", type="text", nullable=true) |
47
|
|
|
*/ |
48
|
|
|
protected $description; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var bool |
52
|
|
|
* @ORM\Column(name="enabled", type="boolean") |
53
|
|
|
*/ |
54
|
|
|
protected $enabled = false; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var \DateTime |
58
|
|
|
* |
59
|
|
|
* @ORM\Column(name="created_at", type="datetime") |
60
|
|
|
*/ |
61
|
|
|
protected $createdAt; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var Category |
65
|
|
|
*/ |
66
|
|
|
protected $parent; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var Category[]|ArrayCollection |
70
|
|
|
*/ |
71
|
|
|
protected $children; |
72
|
|
|
|
73
|
|
|
public function __toString() |
74
|
|
|
{ |
75
|
|
|
return $this->name; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function __construct() |
79
|
|
|
{ |
80
|
|
|
$this->createdAt = new \DateTime(); |
81
|
|
|
$this->children = new ArrayCollection(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
public function getName() |
88
|
|
|
{ |
89
|
|
|
return $this->name; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $name |
94
|
|
|
* |
95
|
|
|
* @return Category |
96
|
|
|
*/ |
97
|
|
|
public function setName($name) |
98
|
|
|
{ |
99
|
|
|
$this->name = $name; |
100
|
|
|
|
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
public function getDescription() |
108
|
|
|
{ |
109
|
|
|
return $this->description; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param string $description |
114
|
|
|
* |
115
|
|
|
* @return Category |
116
|
|
|
*/ |
117
|
|
|
public function setDescription($description) |
118
|
|
|
{ |
119
|
|
|
$this->description = $description; |
120
|
|
|
|
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return mixed |
126
|
|
|
*/ |
127
|
|
|
public function getSlug() |
128
|
|
|
{ |
129
|
|
|
return $this->slug; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param mixed $slug |
134
|
|
|
* |
135
|
|
|
* @return Category |
136
|
|
|
*/ |
137
|
|
|
public function setSlug($slug) |
138
|
|
|
{ |
139
|
|
|
$this->slug = $slug; |
140
|
|
|
|
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return bool |
146
|
|
|
*/ |
147
|
|
|
public function isEnabled() |
148
|
|
|
{ |
149
|
|
|
return $this->enabled; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param bool $enabled |
154
|
|
|
* |
155
|
|
|
* @return Category |
156
|
|
|
*/ |
157
|
|
|
public function setEnabled($enabled) |
158
|
|
|
{ |
159
|
|
|
$this->enabled = $enabled; |
160
|
|
|
|
161
|
|
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @return Category |
166
|
|
|
*/ |
167
|
|
|
public function getParent() |
168
|
|
|
{ |
169
|
|
|
return $this->parent; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param mixed $parent |
174
|
|
|
* |
175
|
|
|
* @return Category |
176
|
|
|
*/ |
177
|
|
|
public function setParent(Category $parent = null) |
178
|
|
|
{ |
179
|
|
|
if ($parent === $this) { |
180
|
|
|
// Refuse the category to have itself as parent |
181
|
|
|
$this->parent = null; |
182
|
|
|
|
183
|
|
|
return $this; |
184
|
|
|
} |
185
|
|
|
$this->parent = $parent; |
186
|
|
|
|
187
|
|
|
return $this; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @return \DateTime |
192
|
|
|
*/ |
193
|
|
|
public function getCreatedAt() |
194
|
|
|
{ |
195
|
|
|
return $this->createdAt; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @return Category[]|ArrayCollection |
200
|
|
|
*/ |
201
|
|
|
public function getChildren() |
202
|
|
|
{ |
203
|
|
|
return $this->children; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @param Category $child |
208
|
|
|
* |
209
|
|
|
* @return Category[] |
210
|
|
|
*/ |
211
|
|
|
public function addChild(Category $child) |
212
|
|
|
{ |
213
|
|
|
$this->children->add($child); |
214
|
|
|
|
215
|
|
|
return $this; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @param Category $child |
220
|
|
|
* |
221
|
|
|
* @return Category[] |
222
|
|
|
*/ |
223
|
|
|
public function removeChild(Category $child) |
224
|
|
|
{ |
225
|
|
|
$this->children->removeElement($child); |
226
|
|
|
|
227
|
|
|
return $this; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @param string $separator |
232
|
|
|
* |
233
|
|
|
* @return string |
234
|
|
|
*/ |
235
|
|
View Code Duplication |
public function getTree($separator = '/') |
236
|
|
|
{ |
237
|
|
|
$tree = ''; |
238
|
|
|
|
239
|
|
|
$current = $this; |
240
|
|
|
do { |
241
|
|
|
$tree = $current->getSlug().$separator.$tree; |
242
|
|
|
$current = $current->getParent(); |
243
|
|
|
} while ($current); |
244
|
|
|
|
245
|
|
|
return trim($tree, $separator); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @ORM\PrePersist() |
250
|
|
|
* @ORM\PreUpdate() |
251
|
|
|
*/ |
252
|
|
|
public function updateSlug() |
253
|
|
|
{ |
254
|
|
|
if (!$this->slug) { |
255
|
|
|
$this->slug = Transliterator::transliterate($this->name); |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* @ORM\PreRemove() |
261
|
|
|
* |
262
|
|
|
* @param LifecycleEventArgs $event |
263
|
|
|
*/ |
264
|
|
View Code Duplication |
public function onRemove(LifecycleEventArgs $event) |
265
|
|
|
{ |
266
|
|
|
$em = $event->getEntityManager(); |
267
|
|
|
if ($this->children) { |
268
|
|
|
foreach ($this->children as $child) { |
269
|
|
|
$child->setParent(null); |
270
|
|
|
$em->persist($child); |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
$this->enabled = false; |
274
|
|
|
$this->parent = null; |
275
|
|
|
$this->name .= '-'.$this->getId().'-deleted'; |
276
|
|
|
$this->slug .= '-'.$this->getId().'-deleted'; |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
|