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\PageRepository") |
24
|
|
|
*/ |
25
|
|
|
abstract class Page |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @return int |
29
|
|
|
*/ |
30
|
|
|
abstract public function getId(); |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
* @ORM\Column(name="title", type="string", length=255) |
35
|
|
|
*/ |
36
|
|
|
protected $title; |
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="page_content", type="text", nullable=true) |
47
|
|
|
*/ |
48
|
|
|
protected $content; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var string |
52
|
|
|
* @ORM\Column(name="meta_description", type="string", length=255, nullable=true) |
53
|
|
|
*/ |
54
|
|
|
protected $metaDescription; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var string |
58
|
|
|
* @ORM\Column(name="meta_title", type="string", length=255, nullable=true) |
59
|
|
|
*/ |
60
|
|
|
protected $metaTitle; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var string |
64
|
|
|
* @ORM\Column(name="meta_keywords", type="string", length=255, nullable=true) |
65
|
|
|
*/ |
66
|
|
|
protected $metaKeywords; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var Category |
70
|
|
|
*/ |
71
|
|
|
protected $category; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @var string |
75
|
|
|
* @ORM\Column(name="css", type="text", nullable=true) |
76
|
|
|
*/ |
77
|
|
|
protected $css; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @var string |
81
|
|
|
* @ORM\Column(name="js", type="text", nullable=true) |
82
|
|
|
*/ |
83
|
|
|
protected $js; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @var \DateTime |
87
|
|
|
* |
88
|
|
|
* @ORM\Column(name="created_at", type="datetime") |
89
|
|
|
*/ |
90
|
|
|
protected $createdAt; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @var bool |
94
|
|
|
* @ORM\Column(name="enabled", type="boolean") |
95
|
|
|
*/ |
96
|
|
|
protected $enabled = false; |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @var bool |
100
|
|
|
* @ORM\Column(name="homepage", type="boolean") |
101
|
|
|
*/ |
102
|
|
|
protected $homepage = false; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @var string |
106
|
|
|
* @ORM\Column(name="host", type="string", length=255, nullable=true) |
107
|
|
|
*/ |
108
|
|
|
protected $host; |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @var string |
112
|
|
|
* @ORM\Column(name="locale", type="string", length=6, nullable=true) |
113
|
|
|
*/ |
114
|
|
|
protected $locale; |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @var Page |
118
|
|
|
*/ |
119
|
|
|
protected $parent; |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @var Page[]|ArrayCollection |
123
|
|
|
*/ |
124
|
|
|
protected $children; |
125
|
|
|
|
126
|
|
|
public function __toString() |
127
|
|
|
{ |
128
|
|
|
return $this->title; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function __construct() |
132
|
|
|
{ |
133
|
|
|
$this->createdAt = new \DateTime(); |
134
|
|
|
$this->children = new ArrayCollection(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
|
|
public function getTitle() |
141
|
|
|
{ |
142
|
|
|
return $this->title; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param string $title |
147
|
|
|
* |
148
|
|
|
* @return Page |
149
|
|
|
*/ |
150
|
|
|
public function setTitle($title) |
151
|
|
|
{ |
152
|
|
|
$this->title = $title; |
153
|
|
|
|
154
|
|
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return string |
159
|
|
|
*/ |
160
|
|
|
public function getSlug() |
161
|
|
|
{ |
162
|
|
|
return $this->slug; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param string $slug |
167
|
|
|
* |
168
|
|
|
* @return Page |
169
|
|
|
*/ |
170
|
|
|
public function setSlug($slug) |
171
|
|
|
{ |
172
|
|
|
$this->slug = $slug; |
173
|
|
|
|
174
|
|
|
return $this; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @return string |
179
|
|
|
*/ |
180
|
|
|
public function getContent() |
181
|
|
|
{ |
182
|
|
|
return $this->content; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param string $content |
187
|
|
|
* |
188
|
|
|
* @return Page |
189
|
|
|
*/ |
190
|
|
|
public function setContent($content) |
191
|
|
|
{ |
192
|
|
|
$this->content = $content; |
193
|
|
|
|
194
|
|
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @return string |
199
|
|
|
*/ |
200
|
|
|
public function getMetaDescription() |
201
|
|
|
{ |
202
|
|
|
return $this->metaDescription; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @param string $metaDescription |
207
|
|
|
* |
208
|
|
|
* @return Page |
209
|
|
|
*/ |
210
|
|
|
public function setMetaDescription($metaDescription) |
211
|
|
|
{ |
212
|
|
|
$this->metaDescription = $metaDescription; |
213
|
|
|
|
214
|
|
|
return $this; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @return string |
219
|
|
|
*/ |
220
|
|
|
public function getMetaTitle() |
221
|
|
|
{ |
222
|
|
|
return $this->metaTitle; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @param string $metaTitle |
227
|
|
|
* |
228
|
|
|
* @return Page |
229
|
|
|
*/ |
230
|
|
|
public function setMetaTitle($metaTitle) |
231
|
|
|
{ |
232
|
|
|
$this->metaTitle = $metaTitle; |
233
|
|
|
|
234
|
|
|
return $this; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @return string |
239
|
|
|
*/ |
240
|
|
|
public function getMetaKeywords() |
241
|
|
|
{ |
242
|
|
|
return $this->metaKeywords; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @param string $metaKeywords |
247
|
|
|
* |
248
|
|
|
* @return Page |
249
|
|
|
*/ |
250
|
|
|
public function setMetaKeywords($metaKeywords) |
251
|
|
|
{ |
252
|
|
|
$this->metaKeywords = $metaKeywords; |
253
|
|
|
|
254
|
|
|
return $this; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @return Category |
259
|
|
|
*/ |
260
|
|
|
public function getCategory() |
261
|
|
|
{ |
262
|
|
|
return $this->category; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* @param Category $category |
267
|
|
|
* |
268
|
|
|
* @return Page |
269
|
|
|
*/ |
270
|
|
|
public function setCategory($category) |
271
|
|
|
{ |
272
|
|
|
$this->category = $category; |
273
|
|
|
|
274
|
|
|
return $this; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @return string |
279
|
|
|
*/ |
280
|
|
|
public function getCss() |
281
|
|
|
{ |
282
|
|
|
return $this->css; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* @param string $css |
287
|
|
|
* |
288
|
|
|
* @return Page |
289
|
|
|
*/ |
290
|
|
|
public function setCss($css) |
291
|
|
|
{ |
292
|
|
|
$this->css = $css; |
293
|
|
|
|
294
|
|
|
return $this; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* @return string |
299
|
|
|
*/ |
300
|
|
|
public function getJs() |
301
|
|
|
{ |
302
|
|
|
return $this->js; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* @param string $js |
307
|
|
|
* |
308
|
|
|
* @return Page |
309
|
|
|
*/ |
310
|
|
|
public function setJs($js) |
311
|
|
|
{ |
312
|
|
|
$this->js = $js; |
313
|
|
|
|
314
|
|
|
return $this; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* @return \DateTime |
319
|
|
|
*/ |
320
|
|
|
public function getCreatedAt() |
321
|
|
|
{ |
322
|
|
|
return $this->createdAt; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* @return bool |
327
|
|
|
*/ |
328
|
|
|
public function isEnabled() |
329
|
|
|
{ |
330
|
|
|
return $this->enabled; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* @param bool $enabled |
335
|
|
|
* |
336
|
|
|
* @return Page |
337
|
|
|
*/ |
338
|
|
|
public function setEnabled($enabled) |
339
|
|
|
{ |
340
|
|
|
$this->enabled = $enabled; |
341
|
|
|
|
342
|
|
|
return $this; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* @return Page |
347
|
|
|
*/ |
348
|
|
|
public function getParent() |
349
|
|
|
{ |
350
|
|
|
return $this->parent; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* @param Page|null $parent |
355
|
|
|
* |
356
|
|
|
* @return Page |
357
|
|
|
*/ |
358
|
|
|
public function setParent(Page $parent = null) |
359
|
|
|
{ |
360
|
|
|
if ($parent === $this) { |
361
|
|
|
// Refuse the page to have itself as parent |
362
|
|
|
$this->parent = null; |
363
|
|
|
|
364
|
|
|
return $this; |
365
|
|
|
} |
366
|
|
|
$this->parent = $parent; |
367
|
|
|
|
368
|
|
|
return $this; |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* @return Page[]|ArrayCollection |
373
|
|
|
*/ |
374
|
|
|
public function getChildren() |
375
|
|
|
{ |
376
|
|
|
return $this->children; |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* @param Page $page |
381
|
|
|
* |
382
|
|
|
* @return Page |
383
|
|
|
*/ |
384
|
|
|
public function addChild(Page $page) |
385
|
|
|
{ |
386
|
|
|
$this->children->add($page); |
387
|
|
|
|
388
|
|
|
return $this; |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
/** |
392
|
|
|
* @param Page $page |
393
|
|
|
* |
394
|
|
|
* @return Page |
395
|
|
|
*/ |
396
|
|
|
public function removeChild(Page $page) |
397
|
|
|
{ |
398
|
|
|
$this->children->removeElement($page); |
399
|
|
|
|
400
|
|
|
return $this; |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
/** |
404
|
|
|
* @return bool |
405
|
|
|
*/ |
406
|
|
|
public function isHomepage() |
407
|
|
|
{ |
408
|
|
|
return $this->homepage; |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
/** |
412
|
|
|
* @param bool $homepage |
413
|
|
|
* |
414
|
|
|
* @return Page |
415
|
|
|
*/ |
416
|
|
|
public function setHomepage($homepage) |
417
|
|
|
{ |
418
|
|
|
$this->homepage = $homepage; |
419
|
|
|
|
420
|
|
|
return $this; |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
/** |
424
|
|
|
* @return string |
425
|
|
|
*/ |
426
|
|
|
public function getHost() |
427
|
|
|
{ |
428
|
|
|
return $this->host; |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
/** |
432
|
|
|
* @param string $host |
433
|
|
|
* |
434
|
|
|
* @return Page |
435
|
|
|
*/ |
436
|
|
|
public function setHost($host) |
437
|
|
|
{ |
438
|
|
|
$this->host = $host; |
439
|
|
|
|
440
|
|
|
return $this; |
441
|
|
|
} |
442
|
|
|
|
443
|
|
|
/** |
444
|
|
|
* @return string |
445
|
|
|
*/ |
446
|
|
|
public function getLocale() |
447
|
|
|
{ |
448
|
|
|
return $this->locale; |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
/** |
452
|
|
|
* @param string $locale |
453
|
|
|
* |
454
|
|
|
* @return Page |
455
|
|
|
*/ |
456
|
|
|
public function setLocale($locale) |
457
|
|
|
{ |
458
|
|
|
$this->locale = $locale; |
459
|
|
|
|
460
|
|
|
return $this; |
461
|
|
|
} |
462
|
|
|
|
463
|
|
|
/** |
464
|
|
|
* @param string $separator |
465
|
|
|
* |
466
|
|
|
* @return string |
467
|
|
|
*/ |
468
|
|
View Code Duplication |
public function getTree($separator = '/') |
469
|
|
|
{ |
470
|
|
|
$tree = ''; |
471
|
|
|
|
472
|
|
|
$current = $this; |
473
|
|
|
do { |
474
|
|
|
$tree = $current->getSlug().$separator.$tree; |
475
|
|
|
$current = $current->getParent(); |
476
|
|
|
} while ($current); |
477
|
|
|
|
478
|
|
|
return trim($tree, $separator); |
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
/** |
482
|
|
|
* @ORM\PrePersist() |
483
|
|
|
* @ORM\PreUpdate() |
484
|
|
|
*/ |
485
|
|
|
public function updateSlug() |
486
|
|
|
{ |
487
|
|
|
if (!$this->slug) { |
488
|
|
|
$this->slug = Transliterator::transliterate($this->title); |
489
|
|
|
} |
490
|
|
|
} |
491
|
|
|
|
492
|
|
|
/** |
493
|
|
|
* @ORM\PreRemove() |
494
|
|
|
* |
495
|
|
|
* @param LifecycleEventArgs $event |
496
|
|
|
*/ |
497
|
|
View Code Duplication |
public function onRemove(LifecycleEventArgs $event) |
498
|
|
|
{ |
499
|
|
|
$em = $event->getEntityManager(); |
500
|
|
|
if ($this->children) { |
501
|
|
|
foreach ($this->children as $child) { |
502
|
|
|
$child->setParent(null); |
503
|
|
|
$em->persist($child); |
504
|
|
|
} |
505
|
|
|
} |
506
|
|
|
$this->enabled = false; |
507
|
|
|
$this->parent = null; |
508
|
|
|
$this->title .= '-'.$this->getId().'-deleted'; |
509
|
|
|
$this->slug .= '-'.$this->getId().'-deleted'; |
510
|
|
|
} |
511
|
|
|
} |
512
|
|
|
|