Completed
Push — master ( 028ddc...8aadd3 )
by Alex
13:57
created

Page::getCreatedAt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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
     * @param  \DateTime $createdAt
319
     * @return $this
320
     */
321
    public function setCreatedAt(\DateTime $createdAt)
322
    {
323
        $this->createdAt = $createdAt;
324
325
        return $this;
326
    }
327
328
    /**
329
     * @return \DateTime
330
     */
331
    public function getCreatedAt()
332
    {
333
        return $this->createdAt;
334
    }
335
336
    /**
337
     * @return bool
338
     */
339
    public function isEnabled()
340
    {
341
        return $this->enabled;
342
    }
343
344
    /**
345
     * @param bool $enabled
346
     *
347
     * @return Page
348
     */
349
    public function setEnabled($enabled)
350
    {
351
        $this->enabled = $enabled;
352
353
        return $this;
354
    }
355
356
    /**
357
     * @return Page
358
     */
359
    public function getParent()
360
    {
361
        return $this->parent;
362
    }
363
364
    /**
365
     * @param Page|null $parent
366
     *
367
     * @return Page
368
     */
369
    public function setParent(Page $parent = null)
370
    {
371
        if ($parent === $this) {
372
            // Refuse the page to have itself as parent
373
            $this->parent = null;
374
375
            return $this;
376
        }
377
        $this->parent = $parent;
378
379
        return $this;
380
    }
381
382
    /**
383
     * @return Page[]|ArrayCollection
384
     */
385
    public function getChildren()
386
    {
387
        return $this->children;
388
    }
389
390
    /**
391
     * @param Page $page
392
     *
393
     * @return Page
394
     */
395
    public function addChild(Page $page)
396
    {
397
        $this->children->add($page);
398
399
        return $this;
400
    }
401
402
    /**
403
     * @param Page $page
404
     *
405
     * @return Page
406
     */
407
    public function removeChild(Page $page)
408
    {
409
        $this->children->removeElement($page);
410
411
        return $this;
412
    }
413
414
    /**
415
     * @return bool
416
     */
417
    public function isHomepage()
418
    {
419
        return $this->homepage;
420
    }
421
422
    /**
423
     * @param bool $homepage
424
     *
425
     * @return Page
426
     */
427
    public function setHomepage($homepage)
428
    {
429
        $this->homepage = $homepage;
430
431
        return $this;
432
    }
433
434
    /**
435
     * @return string
436
     */
437
    public function getHost()
438
    {
439
        return $this->host;
440
    }
441
442
    /**
443
     * @param string $host
444
     *
445
     * @return Page
446
     */
447
    public function setHost($host)
448
    {
449
        $this->host = $host;
450
451
        return $this;
452
    }
453
454
    /**
455
     * @return string
456
     */
457
    public function getLocale()
458
    {
459
        return $this->locale;
460
    }
461
462
    /**
463
     * @param string $locale
464
     *
465
     * @return Page
466
     */
467
    public function setLocale($locale)
468
    {
469
        $this->locale = $locale;
470
471
        return $this;
472
    }
473
474
    /**
475
     * @param string $separator
476
     *
477
     * @return string
478
     */
479 View Code Duplication
    public function getTree($separator = '/')
480
    {
481
        $tree = '';
482
483
        $current = $this;
484
        do {
485
            $tree = $current->getSlug().$separator.$tree;
486
            $current = $current->getParent();
487
        } while ($current);
488
489
        return trim($tree, $separator);
490
    }
491
492
    /**
493
     * @ORM\PrePersist()
494
     * @ORM\PreUpdate()
495
     */
496
    public function updateSlug()
497
    {
498
        if (!$this->slug) {
499
            $this->slug = Transliterator::transliterate($this->title);
500
        }
501
    }
502
503
    /**
504
     * @ORM\PreRemove()
505
     *
506
     * @param LifecycleEventArgs $event
507
     */
508 View Code Duplication
    public function onRemove(LifecycleEventArgs $event)
509
    {
510
        $em = $event->getEntityManager();
511
        if ($this->children) {
512
            foreach ($this->children as $child) {
513
                $child->setParent(null);
514
                $em->persist($child);
515
            }
516
        }
517
        $this->enabled = false;
518
        $this->parent = null;
519
        $this->title .= '-'.$this->getId().'-deleted';
520
        $this->slug .= '-'.$this->getId().'-deleted';
521
    }
522
}
523