Issues (3627)

app/bundles/LeadBundle/Entity/LeadField.php (1 issue)

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\LeadBundle\Entity;
13
14
use Doctrine\ORM\Mapping as ORM;
15
use Mautic\ApiBundle\Serializer\Driver\ApiMetadataDriver;
16
use Mautic\CoreBundle\Doctrine\Mapping\ClassMetadataBuilder;
17
use Mautic\CoreBundle\Entity\FormEntity;
18
use Mautic\LeadBundle\Form\Validator\Constraints\FieldAliasKeyword;
19
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
20
use Symfony\Component\Validator\Constraints as Assert;
21
use Symfony\Component\Validator\Context\ExecutionContextInterface;
22
use Symfony\Component\Validator\Mapping\ClassMetadata;
23
24
/**
25
 * Class LeadField.
26
 */
27
class LeadField extends FormEntity
28
{
29
    /**
30
     * @var int
31
     */
32
    private $id;
33
34
    /**
35
     * @var string
36
     */
37
    private $label;
38
39
    /**
40
     * @var string
41
     */
42
    private $alias;
43
44
    /**
45
     * @var string
46
     */
47
    private $type = 'text';
48
49
    /**
50
     * @var string
51
     */
52
    private $group = 'core';
53
54
    /**
55
     * @var string
56
     */
57
    private $defaultValue;
58
59
    /**
60
     * @var bool
61
     */
62
    private $isRequired = false;
63
64
    /**
65
     * @var bool
66
     */
67
    private $isFixed = false;
68
69
    /**
70
     * @var bool
71
     */
72
    private $isVisible = true;
73
74
    /**
75
     * @var bool
76
     */
77
    private $isShortVisible = true;
78
79
    /**
80
     * @var bool
81
     */
82
    private $isListable = true;
83
84
    /**
85
     * @var bool
86
     */
87
    private $isPubliclyUpdatable = false;
88
89
    /**
90
     * @var bool
91
     */
92
    private $isUniqueIdentifer = false;
93
94
    /**
95
     * Workaround for incorrectly spelled $isUniqueIdentifer.
96
     *
97
     * @var bool
98
     */
99
    private $isUniqueIdentifier = false;
100
101
    /**
102
     * @var int
103
     */
104
    private $order = 1;
105
106
    /**
107
     * @var string
108
     */
109
    private $object = 'lead';
110
111
    /**
112
     * @var array
113
     */
114
    private $properties = [];
115
116
    public function __clone()
117
    {
118
        $this->id = null;
119
120
        parent::__clone();
121
    }
122
123
    public static function loadMetadata(ORM\ClassMetadata $metadata)
124
    {
125
        $builder = new ClassMetadataBuilder($metadata);
126
        $builder->addLifecycleEvent('identifierWorkaround', 'postLoad');
127
128
        $builder->setTable('lead_fields')
129
            ->setCustomRepositoryClass(LeadFieldRepository::class)
130
            ->addIndex(['object'], 'search_by_object');
131
132
        $builder->addId();
133
134
        $builder->addField('label', 'string');
135
136
        $builder->addField('alias', 'string');
137
138
        $builder->createField('type', 'string')
139
            ->length(50)
140
            ->build();
141
142
        $builder->createField('group', 'string')
143
            ->columnName('field_group')
144
            ->nullable()
145
            ->build();
146
147
        $builder->createField('defaultValue', 'string')
148
            ->columnName('default_value')
149
            ->nullable()
150
            ->build();
151
152
        $builder->createField('isRequired', 'boolean')
153
            ->columnName('is_required')
154
            ->build();
155
156
        $builder->createField('isFixed', 'boolean')
157
            ->columnName('is_fixed')
158
            ->build();
159
160
        $builder->createField('isVisible', 'boolean')
161
            ->columnName('is_visible')
162
            ->build();
163
164
        $builder->createField('isShortVisible', 'boolean')
165
            ->columnName('is_short_visible')
166
            ->build();
167
168
        $builder->createField('isListable', 'boolean')
169
            ->columnName('is_listable')
170
            ->build();
171
172
        $builder->createField('isPubliclyUpdatable', 'boolean')
173
            ->columnName('is_publicly_updatable')
174
            ->build();
175
176
        $builder->addNullableField('isUniqueIdentifer', 'boolean', 'is_unique_identifer');
177
178
        $builder->createField('order', 'integer')
179
            ->columnName('field_order')
180
            ->nullable()
181
            ->build();
182
183
        $builder->createField('object', 'string')
184
            ->nullable()
185
            ->build();
186
187
        $builder->createField('properties', 'array')
188
            ->nullable()
189
            ->build();
190
    }
191
192
    public static function loadValidatorMetadata(ClassMetadata $metadata)
193
    {
194
        $metadata->addPropertyConstraint('label', new Assert\NotBlank(
195
            ['message' => 'mautic.lead.field.label.notblank']
196
        ));
197
198
        $metadata->addConstraint(new UniqueEntity([
199
            'fields'  => ['alias'],
200
            'message' => 'mautic.lead.field.alias.unique',
201
        ]));
202
203
        $metadata->addConstraint(new Assert\Callback([
204
            'callback' => function (LeadField $field, ExecutionContextInterface $context) {
205
                $violations = $context->getValidator()->validate($field, [new FieldAliasKeyword()]);
206
207
                if ($violations->count() > 0) {
208
                    $context->buildViolation($violations->get(0)->getMessage())
209
                        ->atPath('alias')
210
                        ->addViolation();
211
                }
212
            },
213
        ]));
214
    }
215
216
    /**
217
     * Prepares the metadata for API usage.
218
     *
219
     * @param $metadata
220
     */
221
    public static function loadApiMetadata(ApiMetadataDriver $metadata)
222
    {
223
        $metadata->setGroupPrefix('leadField')
224
            ->addListProperties(
225
                [
226
                    'id',
227
                    'label',
228
                    'alias',
229
                    'type',
230
                    'group',
231
                    'order',
232
                    'object',
233
                ]
234
            )
235
            ->addProperties(
236
                [
237
                    'defaultValue',
238
                    'isRequired',
239
                    'isPubliclyUpdatable',
240
                    'isUniqueIdentifier',
241
                    'properties',
242
                ]
243
            )
244
            ->build();
245
    }
246
247
    /**
248
     * Get id.
249
     *
250
     * @return int
251
     */
252
    public function getId()
253
    {
254
        return $this->id;
255
    }
256
257
    /**
258
     * Set label.
259
     *
260
     * @param string $label
261
     *
262
     * @return LeadField
263
     */
264
    public function setLabel($label)
265
    {
266
        $this->isChanged('label', $label);
267
        $this->label = $label;
268
269
        return $this;
270
    }
271
272
    /**
273
     * Get label.
274
     *
275
     * @return string
276
     */
277
    public function getLabel()
278
    {
279
        return $this->label;
280
    }
281
282
    /**
283
     * Proxy function to setLabel().
284
     *
285
     * @param string $label
286
     *
287
     * @return LeadField
288
     */
289
    public function setName($label)
290
    {
291
        $this->isChanged('label', $label);
292
293
        return $this->setLabel($label);
294
    }
295
296
    /**
297
     * Proxy function for getLabel().
298
     *
299
     * @return string
300
     */
301
    public function getName()
302
    {
303
        return $this->getLabel();
304
    }
305
306
    /**
307
     * Set type.
308
     *
309
     * @param string $type
310
     *
311
     * @return LeadField
312
     */
313
    public function setType($type)
314
    {
315
        $this->isChanged('type', $type);
316
        $this->type = $type;
317
318
        return $this;
319
    }
320
321
    /**
322
     * Get type.
323
     *
324
     * @return string
325
     */
326
    public function getType()
327
    {
328
        return $this->type;
329
    }
330
331
    /**
332
     * Set defaultValue.
333
     *
334
     * @param string $defaultValue
335
     *
336
     * @return LeadField
337
     */
338
    public function setDefaultValue($defaultValue)
339
    {
340
        $this->isChanged('defaultValue', $defaultValue);
341
        $this->defaultValue = $defaultValue;
342
343
        return $this;
344
    }
345
346
    /**
347
     * Get defaultValue.
348
     *
349
     * @return string
350
     */
351
    public function getDefaultValue()
352
    {
353
        return $this->defaultValue;
354
    }
355
356
    /**
357
     * Set isRequired.
358
     *
359
     * @param bool $isRequired
360
     *
361
     * @return LeadField
362
     */
363
    public function setIsRequired($isRequired)
364
    {
365
        $this->isChanged('isRequired', $isRequired);
366
        $this->isRequired = $isRequired;
367
368
        return $this;
369
    }
370
371
    /**
372
     * Get isRequired.
373
     *
374
     * @return bool
375
     */
376
    public function getIsRequired()
377
    {
378
        return $this->isRequired;
379
    }
380
381
    /**
382
     * Proxy to getIsRequired().
383
     *
384
     * @return bool
385
     */
386
    public function isRequired()
387
    {
388
        return $this->getIsRequired();
389
    }
390
391
    /**
392
     * Set isFixed.
393
     *
394
     * @param bool $isFixed
395
     *
396
     * @return LeadField
397
     */
398
    public function setIsFixed($isFixed)
399
    {
400
        $this->isFixed = $isFixed;
401
402
        return $this;
403
    }
404
405
    /**
406
     * Get isFixed.
407
     *
408
     * @return bool
409
     */
410
    public function getIsFixed()
411
    {
412
        return $this->isFixed;
413
    }
414
415
    /**
416
     * Proxy to getIsFixed().
417
     *
418
     * @return bool
419
     */
420
    public function isFixed()
421
    {
422
        return $this->getIsFixed();
423
    }
424
425
    /**
426
     * Set properties.
427
     *
428
     * @param string $properties
429
     *
430
     * @return LeadField
431
     */
432
    public function setProperties($properties)
433
    {
434
        $this->isChanged('properties', $properties);
435
        $this->properties = $properties;
0 ignored issues
show
Documentation Bug introduced by
It seems like $properties of type string is incompatible with the declared type array of property $properties.

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...
436
437
        return $this;
438
    }
439
440
    /**
441
     * Get properties.
442
     *
443
     * @return string
444
     */
445
    public function getProperties()
446
    {
447
        return $this->properties;
448
    }
449
450
    /**
451
     * Set order.
452
     *
453
     * @param int $order
454
     *
455
     * @return LeadField
456
     */
457
    public function setOrder($order)
458
    {
459
        $this->isChanged('order', $order);
460
        $this->order = $order;
461
462
        return $this;
463
    }
464
465
    /**
466
     * Get object.
467
     *
468
     * @return string
469
     */
470
    public function getObject()
471
    {
472
        return $this->object;
473
    }
474
475
    /**
476
     * Set object.
477
     *
478
     * @param int $object
479
     *
480
     * @return LeadField
481
     */
482
    public function setObject($object)
483
    {
484
        $this->isChanged('object', $object);
485
        $this->object = $object;
486
487
        return $this;
488
    }
489
490
    /**
491
     * Get order.
492
     *
493
     * @return int
494
     */
495
    public function getOrder()
496
    {
497
        return $this->order;
498
    }
499
500
    /**
501
     * Set isVisible.
502
     *
503
     * @param bool $isVisible
504
     *
505
     * @return LeadField
506
     */
507
    public function setIsVisible($isVisible)
508
    {
509
        $this->isChanged('isVisible', $isVisible);
510
        $this->isVisible = $isVisible;
511
512
        return $this;
513
    }
514
515
    /**
516
     * Get isVisible.
517
     *
518
     * @return bool
519
     */
520
    public function getIsVisible()
521
    {
522
        return $this->isVisible;
523
    }
524
525
    /**
526
     * Proxy to getIsVisible().
527
     *
528
     * @return bool
529
     */
530
    public function isVisible()
531
    {
532
        return $this->getIsVisible();
533
    }
534
535
    /**
536
     * Set isShortVisible.
537
     *
538
     * @param bool $isShortVisible
539
     *
540
     * @return LeadField
541
     */
542
    public function setIsShortVisible($isShortVisible)
543
    {
544
        $this->isChanged('isShortVisible', $isShortVisible);
545
        $this->isShortVisible = $isShortVisible;
546
547
        return $this;
548
    }
549
550
    /**
551
     * Get isShortVisible.
552
     *
553
     * @return bool
554
     */
555
    public function getIsShortVisible()
556
    {
557
        return $this->isShortVisible;
558
    }
559
560
    /**
561
     * Proxy to getIsShortVisible().
562
     *
563
     * @return bool
564
     */
565
    public function isShortVisible()
566
    {
567
        return $this->getIsShortVisible();
568
    }
569
570
    /**
571
     * Get the unique identifer state of the field.
572
     *
573
     * @return bool
574
     */
575
    public function getIsUniqueIdentifer()
576
    {
577
        return $this->isUniqueIdentifer;
578
    }
579
580
    /**
581
     * Set the unique identifer state of the field.
582
     *
583
     * @param mixed $isUniqueIdentifer
584
     *
585
     * @return LeadField
586
     */
587
    public function setIsUniqueIdentifer($isUniqueIdentifer)
588
    {
589
        $this->isUniqueIdentifer = $this->isUniqueIdentifier = $isUniqueIdentifer;
590
591
        return $this;
592
    }
593
594
    /**
595
     * Wrapper for incorrectly spelled setIsUniqueIdentifer.
596
     *
597
     * @return bool
598
     */
599
    public function getIsUniqueIdentifier()
600
    {
601
        return $this->getIsUniqueIdentifer();
602
    }
603
604
    /**
605
     * Wrapper for incorrectly spelled setIsUniqueIdentifer.
606
     *
607
     * @param mixed $isUniqueIdentifier
608
     *
609
     * @return LeadField
610
     */
611
    public function setIsUniqueIdentifier($isUniqueIdentifier)
612
    {
613
        return $this->setIsUniqueIdentifer($isUniqueIdentifier);
614
    }
615
616
    /**
617
     * Set alias.
618
     *
619
     * @param string $alias
620
     *
621
     * @return LeadField
622
     */
623
    public function setAlias($alias)
624
    {
625
        $this->isChanged('alias', $alias);
626
        $this->alias = $alias;
627
628
        return $this;
629
    }
630
631
    /**
632
     * Get alias.
633
     *
634
     * @return string
635
     */
636
    public function getAlias()
637
    {
638
        return $this->alias;
639
    }
640
641
    /**
642
     * Set isListable.
643
     *
644
     * @param bool $isListable
645
     *
646
     * @return LeadField
647
     */
648
    public function setIsListable($isListable)
649
    {
650
        $this->isChanged('isListable', $isListable);
651
        $this->isListable = $isListable;
652
653
        return $this;
654
    }
655
656
    /**
657
     * Get isListable.
658
     *
659
     * @return bool
660
     */
661
    public function getIsListable()
662
    {
663
        return $this->isListable;
664
    }
665
666
    /**
667
     * Proxy to getIsListable().
668
     *
669
     * @return bool
670
     */
671
    public function isListable()
672
    {
673
        return $this->getIsListable();
674
    }
675
676
    /**
677
     * @return mixed
678
     */
679
    public function getGroup()
680
    {
681
        return $this->group;
682
    }
683
684
    /**
685
     * @param mixed $group
686
     */
687
    public function setGroup($group)
688
    {
689
        $this->group = $group;
690
    }
691
692
    /**
693
     * @return mixed
694
     */
695
    public function getIsPubliclyUpdatable()
696
    {
697
        return $this->isPubliclyUpdatable;
698
    }
699
700
    /**
701
     * @param mixed $isPubliclyUpdatable
702
     */
703
    public function setIsPubliclyUpdatable($isPubliclyUpdatable)
704
    {
705
        $this->isPubliclyUpdatable = (bool) $isPubliclyUpdatable;
706
    }
707
708
    /**
709
     * Workaround for mispelled isUniqueIdentifer.
710
     */
711
    public function identifierWorkaround()
712
    {
713
        $this->isUniqueIdentifier = $this->isUniqueIdentifer;
714
    }
715
}
716