Issues (3627)

app/bundles/PageBundle/Entity/Page.php (2 issues)

1
<?php
2
3
/*
4
 * @copyright   2014 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @link        http://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\PageBundle\Entity;
13
14
use Doctrine\ORM\Mapping as ORM;
15
use Mautic\ApiBundle\Serializer\Driver\ApiMetadataDriver;
16
use Mautic\CategoryBundle\Entity\Category;
17
use Mautic\CoreBundle\Doctrine\Mapping\ClassMetadataBuilder;
18
use Mautic\CoreBundle\Entity\FormEntity;
19
use Mautic\CoreBundle\Entity\TranslationEntityInterface;
20
use Mautic\CoreBundle\Entity\TranslationEntityTrait;
21
use Mautic\CoreBundle\Entity\VariantEntityInterface;
22
use Mautic\CoreBundle\Entity\VariantEntityTrait;
23
use Symfony\Component\Validator\Constraints as Assert;
24
use Symfony\Component\Validator\Constraints\Callback;
25
use Symfony\Component\Validator\Constraints\NotBlank;
26
use Symfony\Component\Validator\Context\ExecutionContextInterface;
27
use Symfony\Component\Validator\Mapping\ClassMetadata;
28
29
class Page extends FormEntity implements TranslationEntityInterface, VariantEntityInterface
30
{
31
    use TranslationEntityTrait;
32
    use VariantEntityTrait;
33
34
    /**
35
     * @var int
36
     */
37
    private $id;
38
39
    /**
40
     * @var string
41
     */
42
    private $title;
43
44
    /**
45
     * @var string
46
     */
47
    private $alias;
48
49
    /**
50
     * @var string
51
     */
52
    private $template;
53
54
    /**
55
     * @var string
56
     */
57
    private $customHtml;
58
59
    /**
60
     * @var array
61
     */
62
    private $content = [];
63
64
    /**
65
     * @var \DateTime
66
     */
67
    private $publishUp;
68
69
    /**
70
     * @var \DateTime
71
     */
72
    private $publishDown;
73
74
    /**
75
     * @var int
76
     */
77
    private $hits = 0;
78
79
    /**
80
     * @var int
81
     */
82
    private $uniqueHits = 0;
83
84
    /**
85
     * @var int
86
     */
87
    private $variantHits = 0;
88
89
    /**
90
     * @var int
91
     */
92
    private $revision = 1;
93
94
    /**
95
     * @var string
96
     */
97
    private $metaDescription;
98
99
    /**
100
     * @var string
101
     */
102
    private $redirectType;
103
104
    /**
105
     * @var string
106
     */
107
    private $redirectUrl;
108
109
    /**
110
     * @var \Mautic\CategoryBundle\Entity\Category
111
     **/
112
    private $category;
113
114
    /**
115
     * @var bool
116
     */
117
    private $isPreferenceCenter;
118
119
    /**
120
     * @var bool
121
     */
122
    private $noIndex;
123
124
    /**
125
     * Used to identify the page for the builder.
126
     */
127
    private $sessionId;
128
129
    public function __clone()
130
    {
131
        $this->id = null;
132
        $this->clearTranslations();
133
        $this->clearVariants();
134
135
        parent::__clone();
136
    }
137
138
    /**
139
     * Constructor.
140
     */
141
    public function __construct()
142
    {
143
        $this->translationChildren = new \Doctrine\Common\Collections\ArrayCollection();
144
        $this->variantChildren     = new \Doctrine\Common\Collections\ArrayCollection();
145
    }
146
147
    public static function loadMetadata(ORM\ClassMetadata $metadata)
148
    {
149
        $builder = new ClassMetadataBuilder($metadata);
150
151
        $builder->setTable('pages')
152
            ->setCustomRepositoryClass('Mautic\PageBundle\Entity\PageRepository')
153
            ->addIndex(['alias'], 'page_alias_search');
154
155
        $builder->addId();
156
157
        $builder->addField('title', 'string');
158
159
        $builder->addField('alias', 'string');
160
161
        $builder->addNullableField('template', 'string');
162
163
        $builder->createField('customHtml', 'text')
164
            ->columnName('custom_html')
165
            ->nullable()
166
            ->build();
167
168
        $builder->createField('content', 'array')
169
            ->nullable()
170
            ->build();
171
172
        $builder->addPublishDates();
173
174
        $builder->addField('hits', 'integer');
175
176
        $builder->createField('uniqueHits', 'integer')
177
            ->columnName('unique_hits')
178
            ->build();
179
180
        $builder->createField('variantHits', 'integer')
181
            ->columnName('variant_hits')
182
            ->build();
183
184
        $builder->addField('revision', 'integer');
185
186
        $builder->createField('metaDescription', 'string')
187
            ->columnName('meta_description')
188
            ->nullable()
189
            ->build();
190
191
        $builder->createField('redirectType', 'string')
192
            ->columnName('redirect_type')
193
            ->nullable()
194
            ->length(100)
195
            ->build();
196
197
        $builder->createField('redirectUrl', 'string')
198
            ->columnName('redirect_url')
199
            ->nullable()
200
            ->length(2048)
201
            ->build();
202
203
        $builder->addCategory();
204
205
        $builder->createField('isPreferenceCenter', 'boolean')
206
            ->columnName('is_preference_center')
207
            ->nullable()
208
            ->build();
209
210
        $builder->createField('noIndex', 'boolean')
211
            ->columnName('no_index')
212
            ->nullable()
213
            ->build();
214
215
        self::addTranslationMetadata($builder, self::class);
216
        self::addVariantMetadata($builder, self::class);
217
    }
218
219
    public static function loadValidatorMetadata(ClassMetadata $metadata)
220
    {
221
        $metadata->addPropertyConstraint('title', new NotBlank([
222
            'message' => 'mautic.core.title.required',
223
        ]));
224
225
        $metadata->addConstraint(new Callback([
226
            'callback' => function (Page $page, ExecutionContextInterface $context) {
227
                $type = $page->getRedirectType();
228
                if (!is_null($type)) {
229
                    $validator = $context->getValidator();
230
                    $violations = $validator->validate($page->getRedirectUrl(), [
231
                        new Assert\Url(
232
                            [
233
                                'message' => 'mautic.core.value.required',
234
                            ]
235
                        ),
236
                    ]);
237
238
                    if (count($violations) > 0) {
239
                        $string = (string) $violations;
240
                        $context->buildViolation($string)
241
                            ->atPath('redirectUrl')
242
                            ->addViolation();
243
                    }
244
                }
245
246
                if ($page->isVariant()) {
247
                    // Get a summation of weights
248
                    $parent = $page->getVariantParent();
249
                    $children = $parent ? $parent->getVariantChildren() : $page->getVariantChildren();
250
251
                    $total = 0;
252
                    foreach ($children as $child) {
253
                        $settings = $child->getVariantSettings();
254
                        $total += (int) $settings['weight'];
255
                    }
256
257
                    if ($total > 100) {
258
                        $context->buildViolation('mautic.core.variant_weights_invalid')
259
                            ->atPath('variantSettings[weight]')
260
                            ->addViolation();
261
                    }
262
                }
263
            },
264
        ]));
265
    }
266
267
    /**
268
     * Prepares the metadata for API usage.
269
     *
270
     * @param $metadata
271
     */
272
    public static function loadApiMetadata(ApiMetadataDriver $metadata)
273
    {
274
        $metadata->setGroupPrefix('page')
275
            ->addListProperties(
276
                [
277
                    'id',
278
                    'title',
279
                    'alias',
280
                    'category',
281
                ]
282
            )
283
            ->addProperties(
284
                [
285
                    'language',
286
                    'publishUp',
287
                    'publishDown',
288
                    'hits',
289
                    'uniqueHits',
290
                    'variantHits',
291
                    'revision',
292
                    'metaDescription',
293
                    'redirectType',
294
                    'redirectUrl',
295
                    'isPreferenceCenter',
296
                    'noIndex',
297
                    'variantSettings',
298
                    'variantStartDate',
299
                    'variantParent',
300
                    'variantChildren',
301
                    'translationParent',
302
                    'translationChildren',
303
                    'template',
304
                    'customHtml',
305
                ]
306
            )
307
            ->setMaxDepth(1, 'variantParent')
308
            ->setMaxDepth(1, 'variantChildren')
309
            ->setMaxDepth(1, 'translationParent')
310
            ->setMaxDepth(1, 'translationChildren')
311
            ->build();
312
    }
313
314
    /**
315
     * Get id.
316
     *
317
     * @return int
318
     */
319
    public function getId()
320
    {
321
        return $this->id;
322
    }
323
324
    /**
325
     * Set title.
326
     *
327
     * @param string $title
328
     *
329
     * @return Page
330
     */
331
    public function setTitle($title)
332
    {
333
        $this->isChanged('title', $title);
334
        $this->title = $title;
335
336
        return $this;
337
    }
338
339
    /**
340
     * Get title.
341
     *
342
     * @return string
343
     */
344
    public function getTitle()
345
    {
346
        return $this->title;
347
    }
348
349
    /**
350
     * Set alias.
351
     *
352
     * @param string $alias
353
     *
354
     * @return Page
355
     */
356
    public function setAlias($alias)
357
    {
358
        $this->isChanged('alias', $alias);
359
        $this->alias = $alias;
360
361
        return $this;
362
    }
363
364
    /**
365
     * Get alias.
366
     *
367
     * @return string
368
     */
369
    public function getAlias()
370
    {
371
        return $this->alias;
372
    }
373
374
    /**
375
     * Set content.
376
     *
377
     * @param string $content
378
     *
379
     * @return Page
380
     */
381
    public function setContent($content)
382
    {
383
        $this->isChanged('content', $content);
384
        $this->content = $content;
0 ignored issues
show
Documentation Bug introduced by
It seems like $content of type string is incompatible with the declared type array of property $content.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
385
386
        return $this;
387
    }
388
389
    /**
390
     * Get content.
391
     *
392
     * @return string
393
     */
394
    public function getContent()
395
    {
396
        return $this->content;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->content returns the type array which is incompatible with the documented return type string.
Loading history...
397
    }
398
399
    /**
400
     * Set publishUp.
401
     *
402
     * @param \DateTime $publishUp
403
     *
404
     * @return Page
405
     */
406
    public function setPublishUp($publishUp)
407
    {
408
        $this->isChanged('publishUp', $publishUp);
409
        $this->publishUp = $publishUp;
410
411
        return $this;
412
    }
413
414
    /**
415
     * Get publishUp.
416
     *
417
     * @return \DateTime
418
     */
419
    public function getPublishUp()
420
    {
421
        return $this->publishUp;
422
    }
423
424
    /**
425
     * Set publishDown.
426
     *
427
     * @param \DateTime $publishDown
428
     *
429
     * @return Page
430
     */
431
    public function setPublishDown($publishDown)
432
    {
433
        $this->isChanged('publishDown', $publishDown);
434
        $this->publishDown = $publishDown;
435
436
        return $this;
437
    }
438
439
    /**
440
     * Get publishDown.
441
     *
442
     * @return \DateTime
443
     */
444
    public function getPublishDown()
445
    {
446
        return $this->publishDown;
447
    }
448
449
    /**
450
     * Set hits.
451
     *
452
     * @param int $hits
453
     *
454
     * @return Page
455
     */
456
    public function setHits($hits)
457
    {
458
        $this->hits = $hits;
459
460
        return $this;
461
    }
462
463
    /**
464
     * Get hits.
465
     *
466
     * @param bool $includeVariants
467
     *
468
     * @return int|mixed
469
     */
470
    public function getHits($includeVariants = false)
471
    {
472
        return ($includeVariants) ? $this->getAccumulativeVariantCount('getHits') : $this->hits;
473
    }
474
475
    /**
476
     * Set revision.
477
     *
478
     * @param int $revision
479
     *
480
     * @return Page
481
     */
482
    public function setRevision($revision)
483
    {
484
        $this->revision = $revision;
485
486
        return $this;
487
    }
488
489
    /**
490
     * Get revision.
491
     *
492
     * @return int
493
     */
494
    public function getRevision()
495
    {
496
        return $this->revision;
497
    }
498
499
    /**
500
     * Set metaDescription.
501
     *
502
     * @param string $metaDescription
503
     *
504
     * @return Page
505
     */
506
    public function setMetaDescription($metaDescription)
507
    {
508
        $this->isChanged('metaDescription', $metaDescription);
509
        $this->metaDescription = $metaDescription;
510
511
        return $this;
512
    }
513
514
    /**
515
     * Get metaDescription.
516
     *
517
     * @return string
518
     */
519
    public function getMetaDescription()
520
    {
521
        return $this->metaDescription;
522
    }
523
524
    /**
525
     * Set redirectType.
526
     *
527
     * @param string $redirectType
528
     *
529
     * @return Page
530
     */
531
    public function setRedirectType($redirectType)
532
    {
533
        $this->isChanged('redirectType', $redirectType);
534
        $this->redirectType = $redirectType;
535
536
        return $this;
537
    }
538
539
    /**
540
     * Get redirectType.
541
     *
542
     * @return string
543
     */
544
    public function getRedirectType()
545
    {
546
        return $this->redirectType;
547
    }
548
549
    /**
550
     * Set redirectUrl.
551
     *
552
     * @param string $redirectUrl
553
     *
554
     * @return Page
555
     */
556
    public function setRedirectUrl($redirectUrl)
557
    {
558
        $this->isChanged('redirectUrl', $redirectUrl);
559
        $this->redirectUrl = $redirectUrl;
560
561
        return $this;
562
    }
563
564
    /**
565
     * Get redirectUrl.
566
     *
567
     * @return string
568
     */
569
    public function getRedirectUrl()
570
    {
571
        return $this->redirectUrl;
572
    }
573
574
    /**
575
     * Set category.
576
     *
577
     * @param \Mautic\CategoryBundle\Entity\Category $category
578
     *
579
     * @return Page
580
     */
581
    public function setCategory(Category $category = null)
582
    {
583
        $this->isChanged('category', $category);
584
        $this->category = $category;
585
586
        return $this;
587
    }
588
589
    /**
590
     * Get category.
591
     *
592
     * @return \Mautic\CategoryBundle\Entity\Category
593
     */
594
    public function getCategory()
595
    {
596
        return $this->category;
597
    }
598
599
    /**
600
     * Set isPreferenceCenter.
601
     *
602
     * @param bool $isPreferenceCenter
603
     *
604
     * @return Page
605
     */
606
    public function setIsPreferenceCenter($isPreferenceCenter)
607
    {
608
        $this->isChanged('isPreferenceCenter', $isPreferenceCenter);
609
        $this->isPreferenceCenter = $isPreferenceCenter;
610
611
        return $this;
612
    }
613
614
    /**
615
     * Get isPreferenceCenter.
616
     *
617
     * @return bool
618
     */
619
    public function getIsPreferenceCenter()
620
    {
621
        return $this->isPreferenceCenter;
622
    }
623
624
    /**
625
     * Set noIndex.
626
     *
627
     * @param bool $noIndex
628
     */
629
    public function setNoIndex($noIndex)
630
    {
631
        $this->isChanged('noIndex', $noIndex);
632
        $this->noIndex = $noIndex;
633
    }
634
635
    /**
636
     * Get noIndex.
637
     *
638
     * @return bool
639
     */
640
    public function getNoIndex()
641
    {
642
        return $this->noIndex;
643
    }
644
645
    /**
646
     * Set sessionId.
647
     *
648
     * @param string $id
649
     *
650
     * @return Page
651
     */
652
    public function setSessionId($id)
653
    {
654
        $this->sessionId = $id;
655
656
        return $this;
657
    }
658
659
    /**
660
     * Get sessionId.
661
     *
662
     * @return string
663
     */
664
    public function getSessionId()
665
    {
666
        return $this->sessionId;
667
    }
668
669
    /**
670
     * Set template.
671
     *
672
     * @param string $template
673
     *
674
     * @return Page
675
     */
676
    public function setTemplate($template)
677
    {
678
        $this->isChanged('template', $template);
679
        $this->template = $template;
680
681
        return $this;
682
    }
683
684
    /**
685
     * Get template.
686
     *
687
     * @return string
688
     */
689
    public function getTemplate()
690
    {
691
        return $this->template;
692
    }
693
694
    /**
695
     * @param $prop
696
     * @param $val
697
     */
698
    protected function isChanged($prop, $val)
699
    {
700
        $getter  = 'get'.ucfirst($prop);
701
        $current = $this->$getter();
702
703
        if ('translationParent' == $prop || 'variantParent' == $prop || 'category' == $prop) {
704
            $currentId = ($current) ? $current->getId() : '';
705
            $newId     = ($val) ? $val->getId() : null;
706
            if ($currentId != $newId) {
707
                $this->changes[$prop] = [$currentId, $newId];
708
            }
709
        } else {
710
            parent::isChanged($prop, $val);
711
        }
712
    }
713
714
    /**
715
     * Set uniqueHits.
716
     *
717
     * @param int $uniqueHits
718
     *
719
     * @return Page
720
     */
721
    public function setUniqueHits($uniqueHits)
722
    {
723
        $this->uniqueHits = $uniqueHits;
724
725
        return $this;
726
    }
727
728
    /**
729
     * Get uniqueHits.
730
     *
731
     * @return int
732
     */
733
    public function getUniqueHits($includeVariants = false)
734
    {
735
        return ($includeVariants) ? $this->getAccumulativeVariantCount('getUniqueHits') : $this->uniqueHits;
736
    }
737
738
    /**
739
     * @param bool $includeVariants
740
     *
741
     * @return int|mixed
742
     */
743
    public function getVariantHits($includeVariants = false)
744
    {
745
        return ($includeVariants) ? $this->getAccumulativeVariantCount('getVariantHits') : $this->variantHits;
746
    }
747
748
    /**
749
     * @param mixed $variantHits
750
     */
751
    public function setVariantHits($variantHits)
752
    {
753
        $this->variantHits = $variantHits;
754
    }
755
756
    /**
757
     * @return mixed
758
     */
759
    public function getCustomHtml()
760
    {
761
        return $this->customHtml;
762
    }
763
764
    /**
765
     * @param mixed $customHtml
766
     */
767
    public function setCustomHtml($customHtml)
768
    {
769
        $this->customHtml = $customHtml;
770
    }
771
}
772