Completed
Push — staging ( ea50c8...34bcbe )
by Woeler
71:27 queued 58:41
created

Form::getFormAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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\FormBundle\Entity;
13
14
use Doctrine\Common\Collections\ArrayCollection;
15
use Doctrine\ORM\Mapping as ORM;
16
use Mautic\ApiBundle\Serializer\Driver\ApiMetadataDriver;
17
use Mautic\CoreBundle\Doctrine\Mapping\ClassMetadataBuilder;
18
use Mautic\CoreBundle\Entity\FormEntity;
19
use Mautic\CoreBundle\Helper\InputHelper;
20
use Symfony\Component\Validator\Constraints as Assert;
21
use Symfony\Component\Validator\Mapping\ClassMetadata;
22
23
/**
24
 * Class Form.
25
 */
26
class Form extends FormEntity
27
{
28
    /**
29
     * @var int
30
     */
31
    private $id;
32
33
    /**
34
     * @var string
35
     */
36
    private $name;
37
38
    /**
39
     * @var string
40
     */
41
    private $formAttributes;
42
43
    /**
44
     * @var string
45
     */
46
    private $description;
47
48
    /**
49
     * @var string
50
     */
51
    private $alias;
52
53
    /**
54
     * @var \Mautic\CategoryBundle\Entity\Category
55
     **/
56
    private $category;
57
58
    /**
59
     * @var string
60
     */
61
    private $cachedHtml;
62
63
    /**
64
     * @var string
65
     */
66
    private $postAction = 'return';
67
68
    /**
69
     * @var string
70
     */
71
    private $postActionProperty;
72
73
    /**
74
     * @var \DateTime
75
     */
76
    private $publishUp;
77
78
    /**
79
     * @var \DateTime
80
     */
81
    private $publishDown;
82
83
    /**
84
     * @var ArrayCollection
85
     */
86
    private $fields;
87
88
    /**
89
     * @var ArrayCollection
90
     */
91
    private $actions;
92
93
    /**
94
     * @var string
95
     */
96
    private $template;
97
98
    /**
99
     * @var bool
100
     */
101
    private $inKioskMode = false;
102
103
    /**
104
     * @var bool
105
     */
106
    private $renderStyle = false;
107
108
    /**
109
     * @ORM\OneToMany(targetEntity="Submission", mappedBy="form", fetch="EXTRA_LAZY")
110
     * @ORM\OrderBy({"dateSubmitted" = "DESC"})
111
     *
112
     * @var ArrayCollection
113
     */
114
    private $submissions;
115
116
    /**
117
     * @var int
118
     */
119
    public $submissionCount;
120
121
    /**
122
     * @var string
123
     */
124
    private $formType;
125
126
    /**
127
     * @var bool
128
     */
129
    private $noIndex;
130
131
    /**
132
     * This var is used to cache the result once gained from the loop.
133
     *
134
     * @var bool
135
     */
136
    private $usesProgressiveProfiling = null;
137
138
    public function __clone()
139
    {
140
        $this->id = null;
141
142
        parent::__clone();
143
    }
144
145
    /**
146
     * Construct.
147
     */
148
    public function __construct()
149
    {
150
        $this->fields      = new ArrayCollection();
151
        $this->actions     = new ArrayCollection();
152
        $this->submissions = new ArrayCollection();
153
    }
154
155
    /**
156
     * @param ORM\ClassMetadata $metadata
157
     */
158
    public static function loadMetadata(ORM\ClassMetadata $metadata)
159
    {
160
        $builder = new ClassMetadataBuilder($metadata);
161
162
        $builder->setTable('forms')
163
            ->setCustomRepositoryClass('Mautic\FormBundle\Entity\FormRepository');
164
165
        $builder->addIdColumns();
166
167
        $builder->addField('alias', 'string');
168
169
        $builder->addNullableField('formAttributes', 'string', 'form_attr');
170
171
        $builder->addCategory();
172
173
        $builder->createField('cachedHtml', 'text')
174
            ->columnName('cached_html')
175
            ->nullable()
176
            ->build();
177
178
        $builder->createField('postAction', 'string')
179
            ->columnName('post_action')
180
            ->build();
181
182
        $builder->createField('postActionProperty', 'string')
183
            ->columnName('post_action_property')
184
            ->nullable()
185
            ->build();
186
187
        $builder->addPublishDates();
188
189
        $builder->createOneToMany('fields', 'Field')
190
            ->setIndexBy('id')
191
            ->setOrderBy(['order' => 'ASC'])
192
            ->mappedBy('form')
193
            ->cascadeAll()
194
            ->fetchExtraLazy()
195
            ->build();
196
197
        $builder->createOneToMany('actions', 'Action')
198
            ->setIndexBy('id')
199
            ->setOrderBy(['order' => 'ASC'])
200
            ->mappedBy('form')
201
            ->cascadeAll()
202
            ->fetchExtraLazy()
203
            ->build();
204
205
        $builder->createField('template', 'string')
206
            ->nullable()
207
            ->build();
208
209
        $builder->createField('inKioskMode', 'boolean')
210
            ->columnName('in_kiosk_mode')
211
            ->nullable()
212
            ->build();
213
214
        $builder->createField('renderStyle', 'boolean')
215
            ->columnName('render_style')
216
            ->nullable()
217
            ->build();
218
219
        $builder->createOneToMany('submissions', 'Submission')
220
            ->setOrderBy(['dateSubmitted' => 'DESC'])
221
            ->mappedBy('form')
222
            ->fetchExtraLazy()
223
            ->build();
224
225
        $builder->addNullableField('formType', 'string', 'form_type');
226
227
        $builder->createField('noIndex', 'boolean')
228
            ->columnName('no_index')
229
            ->nullable()
230
            ->build();
231
    }
232
233
    /**
234
     * @param ClassMetadata $metadata
235
     */
236
    public static function loadValidatorMetadata(ClassMetadata $metadata)
237
    {
238
        $metadata->addPropertyConstraint('name', new Assert\NotBlank([
239
            'message' => 'mautic.core.name.required',
240
            'groups'  => ['form'],
241
        ]));
242
243
        $metadata->addPropertyConstraint('postActionProperty', new Assert\NotBlank([
244
            'message' => 'mautic.form.form.postactionproperty_message.notblank',
245
            'groups'  => ['messageRequired'],
246
        ]));
247
248
        $metadata->addPropertyConstraint('postActionProperty', new Assert\NotBlank([
249
            'message' => 'mautic.form.form.postactionproperty_redirect.notblank',
250
            'groups'  => ['urlRequired'],
251
        ]));
252
253
        $metadata->addPropertyConstraint('postActionProperty', new Assert\Url([
254
            'message' => 'mautic.form.form.postactionproperty_redirect.notblank',
255
            'groups'  => ['urlRequiredPassTwo'],
256
        ]));
257
258
        $metadata->addPropertyConstraint('formType', new Assert\Choice([
259
            'choices' => ['standalone', 'campaign'],
260
        ]));
261
    }
262
263
    /**
264
     * @param \Symfony\Component\Form\Form $form
265
     *
266
     * @return array
267
     */
268
    public static function determineValidationGroups(\Symfony\Component\Form\Form $form)
269
    {
270
        $data   = $form->getData();
271
        $groups = ['form'];
272
273
        $postAction = $data->getPostAction();
274
275
        if ($postAction == 'message') {
276
            $groups[] = 'messageRequired';
277
        } elseif ($postAction == 'redirect') {
278
            $groups[] = 'urlRequired';
279
        }
280
281
        return $groups;
282
    }
283
284
    /**
285
     * Prepares the metadata for API usage.
286
     *
287
     * @param $metadata
288
     */
289
    public static function loadApiMetadata(ApiMetadataDriver $metadata)
290
    {
291
        $metadata->setGroupPrefix('form')
292
            ->addListProperties(
293
                [
294
                    'id',
295
                    'name',
296
                    'alias',
297
                    'category',
298
                ]
299
            )
300
            ->addProperties(
301
                [
302
                    'description',
303
                    'cachedHtml',
304
                    'publishUp',
305
                    'publishDown',
306
                    'fields',
307
                    'actions',
308
                    'template',
309
                    'inKioskMode',
310
                    'renderStyle',
311
                    'formType',
312
                    'postAction',
313
                    'postActionProperty',
314
                    'noIndex',
315
                    'formAttributes',
316
                ]
317
            )
318
            ->build();
319
    }
320
321
    /**
322
     * @param $prop
323
     * @param $val
324
     */
325
    protected function isChanged($prop, $val)
326
    {
327
        if ($prop == 'actions' || $prop == 'fields') {
328
            //changes are already computed so just add them
329
            $this->changes[$prop][$val[0]] = $val[1];
330
        } else {
331
            parent::isChanged($prop, $val);
332
        }
333
    }
334
335
    /**
336
     * Get id.
337
     *
338
     * @return int
339
     */
340
    public function getId()
341
    {
342
        return $this->id;
343
    }
344
345
    /**
346
     * Set name.
347
     *
348
     * @param string $name
349
     *
350
     * @return Form
351
     */
352
    public function setName($name)
353
    {
354
        $this->isChanged('name', $name);
355
        $this->name = $name;
356
357
        return $this;
358
    }
359
360
    /**
361
     * Get name.
362
     *
363
     * @return string
364
     */
365
    public function getName()
366
    {
367
        return $this->name;
368
    }
369
370
    /**
371
     * Set description.
372
     *
373
     * @param string $description
374
     *
375
     * @return Form
376
     */
377
    public function setDescription($description)
378
    {
379
        $this->isChanged('description', $description);
380
        $this->description = $description;
381
382
        return $this;
383
    }
384
385
    /**
386
     * Get description.
387
     *
388
     * @return string
389
     */
390
    public function getDescription($truncate = false, $length = 45)
391
    {
392
        if ($truncate) {
393
            if (strlen($this->description) > $length) {
394
                return substr($this->description, 0, $length).'...';
395
            }
396
        }
397
398
        return $this->description;
399
    }
400
401
    /**
402
     * Set cachedHtml.
403
     *
404
     * @param string $cachedHtml
405
     *
406
     * @return Form
407
     */
408
    public function setCachedHtml($cachedHtml)
409
    {
410
        $this->cachedHtml = $cachedHtml;
411
412
        return $this;
413
    }
414
415
    /**
416
     * Get cachedHtml.
417
     *
418
     * @return string
419
     */
420
    public function getCachedHtml()
421
    {
422
        return $this->cachedHtml;
423
    }
424
425
    /**
426
     * Get render style.
427
     *
428
     * @return string
429
     */
430
    public function getRenderStyle()
431
    {
432
        return $this->renderStyle;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->renderStyle returns the type boolean which is incompatible with the documented return type string.
Loading history...
433
    }
434
435
    /**
436
     * Set postAction.
437
     *
438
     * @param string $postAction
439
     *
440
     * @return Form
441
     */
442
    public function setPostAction($postAction)
443
    {
444
        $this->isChanged('postAction', $postAction);
445
        $this->postAction = $postAction;
446
447
        return $this;
448
    }
449
450
    /**
451
     * Get postAction.
452
     *
453
     * @return string
454
     */
455
    public function getPostAction()
456
    {
457
        return $this->postAction;
458
    }
459
460
    /**
461
     * Set postActionProperty.
462
     *
463
     * @param string $postActionProperty
464
     *
465
     * @return Form
466
     */
467
    public function setPostActionProperty($postActionProperty)
468
    {
469
        $this->isChanged('postActionProperty', $postActionProperty);
470
        $this->postActionProperty = $postActionProperty;
471
472
        return $this;
473
    }
474
475
    /**
476
     * Get postActionProperty.
477
     *
478
     * @return string
479
     */
480
    public function getPostActionProperty()
481
    {
482
        return $this->postActionProperty;
483
    }
484
485
    /**
486
     * Get result count.
487
     */
488
    public function getResultCount()
489
    {
490
        return count($this->submissions);
491
    }
492
493
    /**
494
     * Set publishUp.
495
     *
496
     * @param \DateTime $publishUp
497
     *
498
     * @return Form
499
     */
500
    public function setPublishUp($publishUp)
501
    {
502
        $this->isChanged('publishUp', $publishUp);
503
        $this->publishUp = $publishUp;
504
505
        return $this;
506
    }
507
508
    /**
509
     * Get publishUp.
510
     *
511
     * @return \DateTime
512
     */
513
    public function getPublishUp()
514
    {
515
        return $this->publishUp;
516
    }
517
518
    /**
519
     * Set publishDown.
520
     *
521
     * @param \DateTime $publishDown
522
     *
523
     * @return Form
524
     */
525
    public function setPublishDown($publishDown)
526
    {
527
        $this->isChanged('publishDown', $publishDown);
528
        $this->publishDown = $publishDown;
529
530
        return $this;
531
    }
532
533
    /**
534
     * Get publishDown.
535
     *
536
     * @return \DateTime
537
     */
538
    public function getPublishDown()
539
    {
540
        return $this->publishDown;
541
    }
542
543
    /**
544
     * Add a field.
545
     *
546
     * @param       $key
547
     * @param Field $field
548
     *
549
     * @return Form
550
     */
551
    public function addField($key, Field $field)
552
    {
553
        if ($changes = $field->getChanges()) {
554
            $this->isChanged('fields', [$key, $changes]);
555
        }
556
        $this->fields[$key] = $field;
557
558
        return $this;
559
    }
560
561
    /**
562
     * Remove a field.
563
     *
564
     * @param       $key
565
     * @param Field $field
566
     */
567
    public function removeField($key, Field $field)
568
    {
569
        if ($changes = $field->getChanges()) {
570
            $this->isChanged('fields', [$key, $changes]);
571
        }
572
        $this->fields->removeElement($field);
573
    }
574
575
    /**
576
     * Get fields.
577
     *
578
     * @return \Doctrine\Common\Collections\Collection|Field[]
579
     */
580
    public function getFields()
581
    {
582
        return $this->fields;
583
    }
584
585
    /**
586
     * Get array of field aliases.
587
     *
588
     * @return array
589
     */
590
    public function getFieldAliases()
591
    {
592
        $aliases = [];
593
        $fields  = $this->getFields();
594
595
        if ($fields) {
596
            foreach ($fields as $field) {
597
                $aliases[] = $field->getAlias();
598
            }
599
        }
600
601
        return $aliases;
602
    }
603
604
    /**
605
     * Set alias.
606
     *
607
     * @param string $alias
608
     *
609
     * @return Form
610
     */
611
    public function setAlias($alias)
612
    {
613
        $this->isChanged('alias', $alias);
614
        $this->alias = $alias;
615
616
        return $this;
617
    }
618
619
    /**
620
     * Get alias.
621
     *
622
     * @return string
623
     */
624
    public function getAlias()
625
    {
626
        return $this->alias;
627
    }
628
629
    /**
630
     * Add submissions.
631
     *
632
     * @param Submission $submissions
633
     *
634
     * @return Form
635
     */
636
    public function addSubmission(Submission $submissions)
637
    {
638
        $this->submissions[] = $submissions;
639
640
        return $this;
641
    }
642
643
    /**
644
     * Remove submissions.
645
     *
646
     * @param Submission $submissions
647
     */
648
    public function removeSubmission(Submission $submissions)
649
    {
650
        $this->submissions->removeElement($submissions);
651
    }
652
653
    /**
654
     * Get submissions.
655
     *
656
     * @return \Doctrine\Common\Collections\Collection|Submission[]
657
     */
658
    public function getSubmissions()
659
    {
660
        return $this->submissions;
661
    }
662
663
    /**
664
     * Add actions.
665
     *
666
     * @param        $key
667
     * @param Action $action
668
     *
669
     * @return Form
670
     */
671
    public function addAction($key, Action $action)
672
    {
673
        if ($changes = $action->getChanges()) {
674
            $this->isChanged('actions', [$key, $changes]);
675
        }
676
        $this->actions[$key] = $action;
677
678
        return $this;
679
    }
680
681
    /**
682
     * Remove action.
683
     *
684
     * @param Action $action
685
     */
686
    public function removeAction(Action $action)
687
    {
688
        $this->actions->removeElement($action);
689
    }
690
691
    /**
692
     * Removes all actions.
693
     */
694
    public function clearActions()
695
    {
696
        $this->actions = new ArrayCollection();
697
    }
698
699
    /**
700
     * Get actions.
701
     *
702
     * @return \Doctrine\Common\Collections\Collection
703
     */
704
    public function getActions()
705
    {
706
        return $this->actions;
707
    }
708
709
    /**
710
     * @return mixed
711
     */
712
    public function getCategory()
713
    {
714
        return $this->category;
715
    }
716
717
    /**
718
     * @param mixed $category
719
     */
720
    public function setCategory($category)
721
    {
722
        $this->category = $category;
723
    }
724
725
    /**
726
     * @return mixed
727
     */
728
    public function getTemplate()
729
    {
730
        return $this->template;
731
    }
732
733
    /**
734
     * @param mixed $template
735
     */
736
    public function setTemplate($template)
737
    {
738
        $this->template = $template;
739
    }
740
741
    /**
742
     * @return mixed
743
     */
744
    public function getInKioskMode()
745
    {
746
        return $this->inKioskMode;
747
    }
748
749
    /**
750
     * @param mixed $inKioskMode
751
     */
752
    public function setInKioskMode($inKioskMode)
753
    {
754
        $this->inKioskMode = $inKioskMode;
755
    }
756
757
    /**
758
     * @param mixed $renderStyle
759
     */
760
    public function setRenderStyle($renderStyle)
761
    {
762
        $this->renderStyle = $renderStyle;
763
    }
764
765
    /**
766
     * @return mixed
767
     */
768
    public function isInKioskMode()
769
    {
770
        return $this->getInKioskMode();
771
    }
772
773
    /**
774
     * @return mixed
775
     */
776
    public function getFormType()
777
    {
778
        return $this->formType;
779
    }
780
781
    /**
782
     * @param mixed $formType
783
     *
784
     * @return Form
785
     */
786
    public function setFormType($formType)
787
    {
788
        $this->formType = $formType;
789
790
        return $this;
791
    }
792
793
    /**
794
     * Set noIndex.
795
     *
796
     * @param bool $noIndex
797
     */
798
    public function setNoIndex($noIndex)
799
    {
800
        $this->isChanged('noIndex', $noIndex);
801
        $this->noIndex = $noIndex;
802
    }
803
804
    /**
805
     * Get noIndex.
806
     *
807
     * @return bool
808
     */
809
    public function getNoIndex()
810
    {
811
        return $this->noIndex;
812
    }
813
814
    /**
815
     * @param string $formAttributes
816
     *
817
     * @return Form
818
     */
819
    public function setFormAttributes($formAttributes)
820
    {
821
        $this->isChanged('formAttributes', $formAttributes);
822
        $this->formAttributes = $formAttributes;
823
824
        return $this;
825
    }
826
827
    /**
828
     * @return string
829
     */
830
    public function getFormAttributes()
831
    {
832
        return $this->formAttributes;
833
    }
834
835
    /**
836
     * @return bool
837
     */
838
    public function isStandalone()
839
    {
840
        return $this->formType != 'campaign';
841
    }
842
843
    /**
844
     * Generate a form name for HTML attributes.
845
     */
846
    public function generateFormName()
847
    {
848
        $name = strtolower(
849
            InputHelper::alphanum(
850
                InputHelper::transliterate(
851
                    $this->name
852
                )
853
            )
854
        );
855
856
        return (empty($name)) ? 'form-'.$this->id : $name;
857
    }
858
859
    /**
860
     * Check if some Progressive Profiling setting is turned on on any of the form fields.
861
     *
862
     * @return bool
863
     */
864
    public function usesProgressiveProfiling()
865
    {
866
        if ($this->usesProgressiveProfiling !== null) {
867
            return $this->usesProgressiveProfiling;
868
        }
869
870
        // Progressive profiling must be turned off in the kiosk mode
871
        if ($this->getInKioskMode() === false) {
872
            // Search for a field with a progressive profiling setting on
873
            foreach ($this->fields->toArray() as $field) {
874
                if ($field->getShowWhenValueExists() === false || $field->getShowAfterXSubmissions() > 0) {
875
                    $this->usesProgressiveProfiling = true;
876
877
                    return $this->usesProgressiveProfiling;
878
                }
879
            }
880
        }
881
882
        $this->usesProgressiveProfiling = false;
883
884
        return $this->usesProgressiveProfiling;
885
    }
886
}
887