Completed
Push — master ( 5d51b4...87fdd9 )
by Paul
10s
created

Bundle/CoreBundle/Entity/View.php (2 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Victoire\Bundle\CoreBundle\Entity;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\Common\Collections\Collection;
7
use Doctrine\ORM\Mapping as ORM;
8
use Gedmo\Mapping\Annotation as Gedmo;
9
use Knp\DoctrineBehaviors\Model\Translatable\Translatable;
10
use Symfony\Component\PropertyAccess\PropertyAccess;
11
use Victoire\Bundle\BusinessPageBundle\Entity\BusinessTemplate;
12
use Victoire\Bundle\TemplateBundle\Entity\Template;
13
use Victoire\Bundle\ViewReferenceBundle\ViewReference\ViewReference;
14
use Victoire\Bundle\WidgetBundle\Entity\Widget;
15
use Victoire\Bundle\WidgetMapBundle\Entity\WidgetMap;
16
17
/**
18
 * Victoire View
19
 * A victoire view is a visual representation with a widget map.
20
 *
21
 * @Gedmo\Tree(type="nested")
22
 * @Gedmo\TranslationEntity(class="Victoire\Bundle\I18nBundle\Entity\ViewTranslation")
23
 * @ORM\InheritanceType("SINGLE_TABLE")
24
 * @ORM\DiscriminatorColumn(name="type", type="string")
25
 * @ORM\Entity(repositoryClass="Victoire\Bundle\CoreBundle\Repository\ViewRepository")
26
 * @ORM\Table("vic_view")
27
 * @ORM\HasLifecycleCallbacks
28
 */
29
abstract class View
30
{
31
    use \Gedmo\Timestampable\Traits\TimestampableEntity;
32
    use Translatable;
33
34
    /**
35
     * @var int
36
     *
37
     * @ORM\Column(name="id", type="integer")
38
     * @ORM\Id
39
     * @ORM\GeneratedValue(strategy="AUTO")
40
     */
41
    protected $id;
42
43
    /**
44
     * @var string
45
     *
46
     * @ORM\Column(name="bodyId", type="string", length=255, nullable=true)
47
     */
48
    protected $bodyId;
49
50
    /**
51
     * @var string
52
     *
53
     * @ORM\Column(name="bodyClass", type="string", length=255, nullable=true)
54
     */
55
    protected $bodyClass;
56
57
    /**
58
     * @var [WidgetMap]
59
     *
60
     * @ORM\OneToMany(targetEntity="\Victoire\Bundle\WidgetMapBundle\Entity\WidgetMap", mappedBy="view", orphanRemoval=true, cascade={"persist", "remove"})
61
     */
62
    protected $widgetMaps = [];
63
64
    /**
65
     * @Gedmo\TreeParent
66
     * @ORM\ManyToOne(targetEntity="View", inversedBy="children", cascade={"persist"})
67
     * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="SET NULL")
68
     */
69
    protected $parent;
70
71
    /**
72
     * @var int
73
     *
74
     * @ORM\Column(name="position", type="integer", nullable=false)
75
     */
76
    protected $position = 0;
77
78
    /**
79
     * @Gedmo\TreeLeft
80
     * @ORM\Column(name="lft", type="integer")
81
     */
82
    protected $lft;
83
84
    /**
85
     * @Gedmo\TreeLevel
86
     * @ORM\Column(name="lvl", type="integer")
87
     */
88
    protected $lvl;
89
90
    /**
91
     * @Gedmo\TreeRight
92
     * @ORM\Column(name="rgt", type="integer")
93
     */
94
    protected $rgt;
95
96
    /**
97
     * @Gedmo\TreeRoot
98
     * @ORM\Column(name="root", type="integer", nullable=true)
99
     */
100
    protected $root;
101
102
    /**
103
     * @ORM\OneToMany(targetEntity="View", mappedBy="parent", cascade={"remove"})
104
     * @ORM\OrderBy({"lft" = "ASC"})
105
     */
106
    protected $children = [];
107
108
    /**
109
     * This relation is dynamicly added by PageSubscriber.
110
     */
111
    protected $author;
112
113
    /**
114
     * @var string
115
     *
116
     * @ORM\Column(name="undeletable", type="boolean")
117
     */
118
    protected $undeletable = false;
119
120
    /**
121
     * @var ViewReference[]
122
     *                      The reference is related to viewsReferences.xml file which list all app views.
123
     *                      This is used to speed up the routing system and identify virtual pages (BusinessPage).
124
     */
125
    protected $references;
126
127
    /**
128
     * @var string
129
     *
130
     * @ORM\ManyToOne(targetEntity="\Victoire\Bundle\TemplateBundle\Entity\Template", inversedBy="inheritors", cascade={"persist"})
131
     * @ORM\JoinColumn(name="template_id", referencedColumnName="id", onDelete="CASCADE")
132
     */
133
    protected $template;
134
135
    /**
136
     * @var string
137
     *
138
     * @ORM\Column(name="cssHash", type="string", length=40 ,nullable=true)
139
     */
140
    protected $cssHash;
141
142
    /**
143
     * @deprecated
144
     * @ORM\Column(name="widget_map", type="array")
145
     */
146
    protected $widgetMap = [];
147
148
    /**
149
     * @var string
150
     *
151
     * @ORM\OneToMany(targetEntity="\Victoire\Bundle\WidgetBundle\Entity\Widget", mappedBy="view", cascade={"persist", "remove"})
152
     * @ORM\OrderBy({"id" = "ASC"})
153
     */
154
    protected $widgets;
155
    /**
156
     * @var bool
157
     *
158
     * @ORM\Column(name="cssUpToDate", type="boolean")
159
     */
160
    protected $cssUpToDate = false;
161
162
    /**
163
     * Construct.
164
     **/
165
    public function __construct()
166
    {
167
        $this->children = new ArrayCollection();
168
        $this->widgetMaps = new ArrayCollection();
169
        $this->translations = new ArrayCollection();
170
        $this->references = [];
171
    }
172
173
    /**
174
     * to string.
175
     *
176
     * @return string
177
     **/
178
    public function __toString()
179
    {
180
        return $this->getName();
181
    }
182
183
    /**
184
     * Get id.
185
     *
186
     * @return int
187
     */
188
    public function getId()
189
    {
190
        return $this->id;
191
    }
192
193
    /**
194
     * Set id.
195
     *
196
     * @param id $id
197
     */
198
    public function setId($id)
199
    {
200
        $this->id = $id;
201
    }
202
203
    /**
204
     * Set template.
205
     *
206
     * @param View $template
207
     *
208
     * @return View
209
     */
210
    public function setTemplate($template)
211
    {
212
        $this->template = $template;
213
214
        return $this;
215
    }
216
217
    /**
218
     * Get template.
219
     *
220
     * @return string
221
     */
222
    public function getTemplate()
223
    {
224
        return $this->template;
225
    }
226
227
    /**
228
     * Set parent.
229
     *
230
     * @param View $parent
231
     */
232
    public function setParent(View $parent = null)
233
    {
234
        $this->parent = $parent;
235
    }
236
237
    /**
238
     * Get parent.
239
     *
240
     * @return View parent
241
     */
242
    public function getParent()
243
    {
244
        return $this->parent;
245
    }
246
247
    /**
248
     * Set children.
249
     *
250
     * @param View[] $children
251
     *
252
     * @return View
253
     */
254
    public function setChildren($children)
255
    {
256
        $this->children = $children;
257
        if ($children !== null) {
258
            foreach ($children as $child) {
259
                $child->setParent($this);
260
            }
261
        }
262
263
        return $this;
264
    }
265
266
    /**
267
     * Get children.
268
     *
269
     * @return View[]
270
     */
271
    public function getChildren()
272
    {
273
        return $this->children;
274
    }
275
276
    /**
277
     * Has children.
278
     *
279
     * @return int
280
     */
281
    public function hasChildren()
282
    {
283
        return count($this->children);
284
    }
285
286
    /**
287
     * Get WebView children.
288
     *
289
     * @return string
290
     */
291
    public function getWebViewChildren()
292
    {
293
        $webViewChildren = [];
294
        foreach ($this->children as $child) {
295
            if (!$child instanceof BusinessTemplate) {
296
                $webViewChildren[] = $child;
297
            }
298
        }
299
300
        return $webViewChildren;
301
    }
302
303
    /**
304
     * Add child.
305
     *
306
     * @param View $child
307
     */
308
    public function addChild(View $child)
309
    {
310
        $this->children[] = $child;
311
    }
312
313
    /**
314
     * Remove child.
315
     *
316
     * @param View $child
317
     */
318
    public function removeChild(View $child)
319
    {
320
        $this->children->removeElement($child);
321
    }
322
323
    /**
324
     * Get the left value.
325
     *
326
     * @return int
327
     */
328
    public function getLft()
329
    {
330
        return $this->lft;
331
    }
332
333
    /**
334
     * Set the left value.
335
     *
336
     * @param int $lft
337
     */
338
    public function setLft($lft)
339
    {
340
        $this->lft = $lft;
341
    }
342
343
    /**
344
     * Get the right value.
345
     *
346
     * @return int
347
     */
348
    public function getRgt()
349
    {
350
        return $this->rgt;
351
    }
352
353
    /**
354
     * Set the right value.
355
     *
356
     * @param int $rgt
357
     */
358
    public function setRgt($rgt)
359
    {
360
        $this->rgt = $rgt;
361
    }
362
363
    /**
364
     * Get the level value.
365
     *
366
     * @return int
367
     */
368
    public function getLvl()
369
    {
370
        return $this->lvl;
371
    }
372
373
    /**
374
     * Set the level value.
375
     *
376
     * @param int $lvl
377
     */
378
    public function setLvl($lvl)
379
    {
380
        $this->lvl = $lvl;
381
    }
382
383
    /**
384
     * Get the root value.
385
     *
386
     * @return int
387
     */
388
    public function getRoot()
389
    {
390
        return $this->root;
391
    }
392
393
    /**
394
     * Set the root value.
395
     *
396
     * @param int $root
397
     */
398
    public function setRoot($root)
399
    {
400
        $this->root = $root;
401
    }
402
403
    /**
404
     * Set undeletable.
405
     *
406
     * @param bool $undeletable
407
     *
408
     * @return View The current instance
409
     */
410
    public function setUndeletable($undeletable)
411
    {
412
        $this->undeletable = $undeletable;
413
414
        return $this;
415
    }
416
417
    /**
418
     * Is the widget is undeletable.
419
     *
420
     * @return string
421
     */
422
    public function isUndeletable()
423
    {
424
        return $this->undeletable;
425
    }
426
427
    /**
428
     * Get author.
429
     *
430
     * @return string
431
     */
432
    public function getAuthor()
433
    {
434
        return $this->author;
435
    }
436
437
    /**
438
     * Set author.
439
     *
440
     * @param string $author
441
     *
442
     * @return $this
443
     */
444
    public function setAuthor($author)
445
    {
446
        $this->author = $author;
447
448
        return $this;
449
    }
450
451
    /**
452
     * Get bodyId.
453
     *
454
     * @return string
455
     */
456
    public function getBodyId()
457
    {
458
        return $this->bodyId;
459
    }
460
461
    /**
462
     * Set bodyId.
463
     *
464
     * @param string $bodyId
465
     *
466
     * @return $this
467
     */
468
    public function setBodyId($bodyId)
469
    {
470
        $this->bodyId = $bodyId;
471
472
        return $this;
473
    }
474
475
    /**
476
     * Get bodyClass.
477
     *
478
     * @return string
479
     */
480
    public function getBodyClass()
481
    {
482
        return $this->bodyClass;
483
    }
484
485
    /**
486
     * Set bodyClass.
487
     *
488
     * @param string $bodyClass
489
     *
490
     * @return $this
491
     */
492
    public function setBodyClass($bodyClass)
493
    {
494
        $this->bodyClass = $bodyClass;
495
496
        return $this;
497
    }
498
499
    /**
500
     * Set widgets.
501
     *
502
     * @param [WidgetMap] $widgetMaps
503
     *
504
     * @return View
505
     */
506
    public function setWidgetMaps($widgetMaps)
507
    {
508
        $this->widgetMaps = $widgetMaps;
509
510
        return $this;
511
    }
512
513
    /**
514
     * Get widgets.
515
     *
516
     * @return Collection[WidgetMap]
517
     */
518
    public function getWidgetMaps()
519
    {
520
        return $this->widgetMaps;
521
    }
522
523
    /**
524
     * Add widget.
525
     *
526
     * @param Widget $widgetMap
527
     */
528
    public function addWidgetMap(WidgetMap $widgetMap)
529
    {
530
        if (!$widgetMap->getView()) {
531
            $widgetMap->setView($this);
532
        }
533
        $this->widgetMaps[] = $widgetMap;
534
    }
535
536
    /**
537
     * Remove a widgetMap.
538
     *
539
     * @param WidgetMap $widgetMap
540
     */
541
    public function removeWidgetMap(WidgetMap $widgetMap)
542
    {
543
        $this->widgetMaps->removeElement($widgetMap);
544
    }
545
546
    /**
547
     * Get widgets ids as array.
548
     *
549
     * @return array
550
     */
551
    public function getWidgetsIds()
552
    {
553
        $widgetIds = [];
554
        foreach ($this->getBuiltWidgetMap() as $slot => $_widgetMaps) {
555
            foreach ($_widgetMaps as $widgetMap) {
556
                foreach ($widgetMap->getWidgets() as $widget) {
557
                    $widgetIds[] = $widget->getId();
558
                }
559
            }
560
        }
561
562
        return $widgetIds;
563
    }
564
565
    /**
566
     * Get builtWidgetMap.
567
     *
568
     * @return array
569
     */
570
    public function getBuiltWidgetMap()
571
    {
572
        return $this->builtWidgetMap;
573
    }
574
575
    /**
576
     * Set builtWidgetMap.
577
     *
578
     * @param string $builtWidgetMap
579
     *
580
     * @return $this
581
     */
582
    public function setBuiltWidgetMap($builtWidgetMap)
583
    {
584
        $this->builtWidgetMap = $builtWidgetMap;
585
586
        return $this;
587
    }
588
589
    /**
590
     * Get discriminator type.
591
     *
592
     * @return int
593
     */
594
    public function getType()
595
    {
596
        $class = get_called_class();
597
598
        return $class::TYPE;
599
    }
600
601
    /**
602
     * Set position.
603
     *
604
     * @param int $position
605
     */
606
    public function setPosition($position)
607
    {
608
        $this->position = $position;
609
    }
610
611
    /**
612
     * Get position.
613
     *
614
     * @return int
615
     */
616
    public function getPosition()
617
    {
618
        return $this->position;
619
    }
620
621
    /**
622
     * Get reference according to the current locale.
623
     *
624
     * @param string $locale
625
     *
626
     * @return null|ViewReference
627
     */
628
    public function getReference($locale = null)
629
    {
630
        $locale = $locale ?: $this->getCurrentLocale();
631
        if (is_array($this->references) && isset($this->references[$locale])) {
632
            return $this->references[$locale];
633
        }
634
    }
635
636
    /**
637
     * Get references.
638
     *
639
     * @return ViewReference[]
640
     */
641
    public function getReferences()
642
    {
643
        return $this->references;
644
    }
645
646
    /**
647
     * Set references.
648
     *
649
     * @param ViewReference[] $references
650
     *
651
     * @return $this
652
     */
653
    public function setReferences($references)
654
    {
655
        $this->references = $references;
656
657
        return $this;
658
    }
659
660
    /**
661
     * Set reference.
662
     *
663
     * @param ViewReference $reference
664
     * @param string        $locale
665
     *
666
     * @return $this
667
     */
668
    public function setReference(ViewReference $reference, $locale = null)
669
    {
670
        $locale = $locale ?: $this->getCurrentLocale();
671
        $this->references[$locale] = $reference;
672
673
        return $this;
674
    }
675
676
    /**
677
     * Get CSS hash.
678
     *
679
     * @return string
680
     */
681
    public function getCssHash()
682
    {
683
        return $this->cssHash;
684
    }
685
686
    /**
687
     * Set CSS hash.
688
     *
689
     * @param string $cssHash
690
     *
691
     * @return $this
692
     */
693
    public function setCssHash($cssHash)
694
    {
695
        $this->cssHash = $cssHash;
696
697
        return $this;
698
    }
699
700
    /**
701
     * Change cssHash.
702
     */
703
    public function changeCssHash()
704
    {
705
        $this->cssHash = sha1(uniqid());
706
    }
707
708
    /**
709
     * @deprecated
710
     * Get widgetMap.
711
     *
712
     * @return widgetMap
713
     */
714
    public function getWidgetMap()
715
    {
716
        return $this->widgetMap;
717
    }
718
719
    /**
720
     * @deprecated
721
     * Get widgets.
722
     *
723
     * @return string
724
     */
725
    public function getWidgets()
726
    {
727
        return $this->widgets;
728
    }
729
730
    public function isTemplateOf(View $view)
731
    {
732
        while ($_view = $view->getTemplate()) {
733
            if ($this == $_view) {
734
                return true;
735
            }
736
            $view = $_view;
737
        }
738
739
        return false;
740
    }
741
742
    /**
743
     * Get cssUpToDate.
744
     *
745
     * @return bool
746
     */
747
    public function isCssUpToDate()
748
    {
749
        return $this->cssUpToDate;
750
    }
751
752
    /**
753
     * Set CssUpToDate.
754
     *
755
     * @param bool $cssUpToDate
756
     *
757
     * @return $this
758
     */
759
    public function setCssUpToDate($cssUpToDate)
760
    {
761
        $this->cssUpToDate = $cssUpToDate;
762
763
        return $this;
764
    }
765
766
    /**
767
     * {@inheritdoc}
768
     */
769
    public static function getTranslationEntityClass()
770
    {
771
        return '\\Victoire\\Bundle\\I18nBundle\\Entity\\ViewTranslation';
772
    }
773
774
    public function getName()
775
    {
776
        return PropertyAccess::createPropertyAccessor()->getValue($this->translate(null, false), 'getName');
777
    }
778
779
    public function setName($name, $locale = null)
780
    {
781
        $this->translate($locale, false)->setName($name);
0 ignored issues
show
It seems like setName() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
782
        $this->mergeNewTranslations();
783
    }
784
785
    public function getSlug()
786
    {
787
        return PropertyAccess::createPropertyAccessor()->getValue($this->translate(null, false), 'getSlug');
788
    }
789
790
    public function setSlug($slug, $locale = null)
791
    {
792
        $this->translate($locale, false)->setSlug($slug);
0 ignored issues
show
It seems like setSlug() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
793
        $this->mergeNewTranslations();
794
    }
795
}
796