Issues (3627)

app/bundles/CoreBundle/Entity/FormEntity.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\CoreBundle\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\Helper\DateTimeHelper;
18
use Mautic\UserBundle\Entity\User;
19
20
class FormEntity extends CommonEntity
21
{
22
    /**
23
     * @var bool
24
     */
25
    private $isPublished = true;
26
27
    /**
28
     * @var \DateTime|null
29
     */
30
    private $dateAdded;
31
32
    /**
33
     * @var int|null
34
     */
35
    private $createdBy;
36
37
    /**
38
     * @var string|null
39
     */
40
    private $createdByUser;
41
42
    /**
43
     * @var \DateTime|null
44
     */
45
    private $dateModified;
46
47
    /**
48
     * var null|int.
49
     */
50
    private $modifiedBy;
51
52
    /**
53
     * @var string|null
54
     */
55
    private $modifiedByUser;
56
57
    /**
58
     * @var \DateTime|null
59
     */
60
    private $checkedOut;
61
62
    /**
63
     * @var int|null
64
     */
65
    private $checkedOutBy;
66
67
    /**
68
     * @var string|null
69
     */
70
    private $checkedOutByUser;
71
72
    /**
73
     * @var array
74
     */
75
    protected $changes = [];
76
77
    /**
78
     * @var bool
79
     */
80
    protected $new = false;
81
82
    /**
83
     * @var int|null
84
     */
85
    public $deletedId;
86
87
    public static function loadMetadata(ORM\ClassMetadata $metadata)
88
    {
89
        $builder = new ClassMetadataBuilder($metadata);
90
91
        $builder->setMappedSuperClass();
92
93
        $builder->createField('isPublished', 'boolean')
94
            ->columnName('is_published')
95
            ->build();
96
97
        $builder->addDateAdded(true);
98
99
        $builder->createField('createdBy', 'integer')
100
            ->columnName('created_by')
101
            ->nullable()
102
            ->build();
103
104
        $builder->createField('createdByUser', 'string')
105
            ->columnName('created_by_user')
106
            ->nullable()
107
            ->build();
108
109
        $builder->createField('dateModified', 'datetime')
110
            ->columnName('date_modified')
111
            ->nullable()
112
            ->build();
113
114
        $builder->createField('modifiedBy', 'integer')
115
            ->columnName('modified_by')
116
            ->nullable()
117
            ->build();
118
119
        $builder->createField('modifiedByUser', 'string')
120
            ->columnName('modified_by_user')
121
            ->nullable()
122
            ->build();
123
124
        $builder->createField('checkedOut', 'datetime')
125
            ->columnName('checked_out')
126
            ->nullable()
127
            ->build();
128
129
        $builder->createField('checkedOutBy', 'integer')
130
            ->columnName('checked_out_by')
131
            ->nullable()
132
            ->build();
133
134
        $builder->createField('checkedOutByUser', 'string')
135
            ->columnName('checked_out_by_user')
136
            ->nullable()
137
            ->build();
138
    }
139
140
    /**
141
     * Prepares the metadata for API usage.
142
     *
143
     * @param $metadata
144
     */
145
    public static function loadApiMetadata(ApiMetadataDriver $metadata)
146
    {
147
        $metadata->setGroupPrefix('publish')
148
            ->addListProperties(
149
                [
150
                    'isPublished',
151
                    'dateAdded',
152
                    'dateModified',
153
                ]
154
            )
155
            ->addProperties(
156
                [
157
                    'createdBy',
158
                    'createdByUser',
159
                    'dateModified',
160
                    'modifiedBy',
161
                    'modifiedByUser',
162
                ]
163
            )
164
            ->build();
165
    }
166
167
    /**
168
     * Clear dates on clone.
169
     */
170
    public function __clone()
171
    {
172
        $this->dateAdded    = null;
173
        $this->dateModified = null;
174
        $this->checkedOut   = null;
175
        $this->isPublished  = false;
176
        $this->changes      = [];
177
    }
178
179
    /**
180
     * Check publish status with option to check against category, publish up and down dates.
181
     *
182
     * @param bool $checkPublishStatus
183
     * @param bool $checkCategoryStatus
184
     *
185
     * @return bool
186
     */
187
    public function isPublished($checkPublishStatus = true, $checkCategoryStatus = true)
188
    {
189
        if ($checkPublishStatus && method_exists($this, 'getPublishUp')) {
190
            $status = $this->getPublishStatus();
191
            if ('published' == $status) {
192
                //check to see if there is a category to check
193
                if ($checkCategoryStatus && method_exists($this, 'getCategory')) {
194
                    $category = $this->getCategory();
195
                    if (null !== $category && !$category->isPublished()) {
196
                        return false;
197
                    }
198
                }
199
            }
200
201
            return ('published' == $status) ? true : false;
202
        }
203
204
        return $this->getIsPublished();
205
    }
206
207
    /**
208
     * Set dateAdded.
209
     *
210
     * @param \DateTime $dateAdded
211
     *
212
     * @return $this
213
     */
214
    public function setDateAdded($dateAdded)
215
    {
216
        $this->dateAdded = $dateAdded;
217
218
        return $this;
219
    }
220
221
    /**
222
     * Get dateAdded.
223
     *
224
     * @return \DateTime
225
     */
226
    public function getDateAdded()
227
    {
228
        return $this->dateAdded;
229
    }
230
231
    /**
232
     * Set dateModified.
233
     *
234
     * @param \DateTime $dateModified
235
     *
236
     * @return $this
237
     */
238
    public function setDateModified($dateModified)
239
    {
240
        $this->isChanged('dateModified', $dateModified);
241
        $this->dateModified = $dateModified;
242
243
        return $this;
244
    }
245
246
    /**
247
     * Get dateModified.
248
     *
249
     * @return \DateTime
250
     */
251
    public function getDateModified()
252
    {
253
        return $this->dateModified;
254
    }
255
256
    /**
257
     * Set checkedOut.
258
     *
259
     * @param \DateTime $checkedOut
260
     *
261
     * @return $this
262
     */
263
    public function setCheckedOut($checkedOut)
264
    {
265
        $this->checkedOut = $checkedOut;
266
267
        return $this;
268
    }
269
270
    /**
271
     * Get checkedOut.
272
     *
273
     * @return \DateTime
274
     */
275
    public function getCheckedOut()
276
    {
277
        return $this->checkedOut;
278
    }
279
280
    /**
281
     * Set createdBy.
282
     *
283
     * @param User $createdBy
284
     *
285
     * @return $this
286
     */
287
    public function setCreatedBy($createdBy = null)
288
    {
289
        if (null != $createdBy && !$createdBy instanceof User) {
290
            $this->createdBy = $createdBy;
291
        } else {
292
            $this->createdBy = (null != $createdBy) ? $createdBy->getId() : null;
293
            if (null != $createdBy) {
294
                $this->createdByUser = $createdBy->getName();
295
            }
296
        }
297
298
        return $this;
299
    }
300
301
    /**
302
     * Get createdBy.
303
     *
304
     * @return int
305
     */
306
    public function getCreatedBy()
307
    {
308
        return $this->createdBy;
309
    }
310
311
    /**
312
     * Set modifiedBy.
313
     *
314
     * @param User $modifiedBy
315
     *
316
     * @return mixed
317
     */
318
    public function setModifiedBy($modifiedBy = null)
319
    {
320
        if (null != $modifiedBy && !$modifiedBy instanceof User) {
321
            $this->modifiedBy = $modifiedBy;
322
        } else {
323
            $this->modifiedBy = (null != $modifiedBy) ? $modifiedBy->getId() : null;
324
325
            if (null != $modifiedBy) {
326
                $this->modifiedByUser = $modifiedBy->getName();
327
            }
328
        }
329
330
        return $this;
331
    }
332
333
    /**
334
     * Get modifiedBy.
335
     *
336
     * @return User
337
     */
338
    public function getModifiedBy()
339
    {
340
        return $this->modifiedBy;
341
    }
342
343
    /**
344
     * Set checkedOutBy.
345
     *
346
     * @param User $checkedOutBy
347
     *
348
     * @return mixed
349
     */
350
    public function setCheckedOutBy($checkedOutBy = null)
351
    {
352
        if (null != $checkedOutBy && !$checkedOutBy instanceof User) {
353
            $this->checkedOutBy = $checkedOutBy;
354
        } else {
355
            $this->checkedOutBy = (null != $checkedOutBy) ? $checkedOutBy->getId() : null;
356
357
            if (null != $checkedOutBy) {
358
                $this->checkedOutByUser = $checkedOutBy->getName();
359
            }
360
        }
361
362
        return $this;
363
    }
364
365
    /**
366
     * Get checkedOutBy.
367
     *
368
     * @return User
369
     */
370
    public function getCheckedOutBy()
371
    {
372
        return $this->checkedOutBy;
373
    }
374
375
    /**
376
     * Set isPublished.
377
     *
378
     * @param bool $isPublished
379
     *
380
     * @return $this
381
     */
382
    public function setIsPublished($isPublished)
383
    {
384
        $this->isChanged('isPublished', (bool) $isPublished);
385
386
        $this->isPublished = (bool) $isPublished;
387
388
        return $this;
389
    }
390
391
    /**
392
     * Get isPublished.
393
     *
394
     * @return bool
395
     */
396
    public function getIsPublished()
397
    {
398
        return $this->isPublished;
399
    }
400
401
    /**
402
     * Check the publish status of an entity based on publish up and down datetimes.
403
     *
404
     * @return string early|expired|published|unpublished
405
     *
406
     * @throws \BadMethodCallException
407
     */
408
    public function getPublishStatus()
409
    {
410
        $dt      = new DateTimeHelper();
411
        $current = $dt->getLocalDateTime();
412
        if (!$this->isPublished(false)) {
413
            return 'unpublished';
414
        }
415
416
        $status = 'published';
417
        if (method_exists($this, 'getPublishUp')) {
418
            $up = $this->getPublishUp();
0 ignored issues
show
The method getPublishUp() does not exist on Mautic\CoreBundle\Entity\FormEntity. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

418
            /** @scrutinizer ignore-call */ 
419
            $up = $this->getPublishUp();
Loading history...
419
            if (!empty($up) && $current < $up) {
420
                $status = 'pending';
421
            }
422
        }
423
        if (method_exists($this, 'getPublishDown')) {
424
            $down = $this->getPublishDown();
0 ignored issues
show
The method getPublishDown() does not exist on Mautic\CoreBundle\Entity\FormEntity. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

424
            /** @scrutinizer ignore-call */ 
425
            $down = $this->getPublishDown();
Loading history...
425
            if (!empty($down) && $current >= $down) {
426
                $status = 'expired';
427
            }
428
        }
429
430
        return $status;
431
    }
432
433
    /**
434
     * @return bool
435
     */
436
    public function isNew()
437
    {
438
        if ($this->new) {
439
            return true;
440
        }
441
442
        if (!method_exists($this, 'getId')) {
443
            return true;
444
        }
445
446
        return !$this->getId();
447
    }
448
449
    /**
450
     * Set this entity as new in case it has to be saved prior to the events.
451
     */
452
    public function setNew()
453
    {
454
        $this->new = true;
455
    }
456
457
    /**
458
     * @return string
459
     */
460
    public function getCheckedOutByUser()
461
    {
462
        return $this->checkedOutByUser;
463
    }
464
465
    /**
466
     * @return string
467
     */
468
    public function getCreatedByUser()
469
    {
470
        return $this->createdByUser;
471
    }
472
473
    /**
474
     * @return string
475
     */
476
    public function getModifiedByUser()
477
    {
478
        return $this->modifiedByUser;
479
    }
480
481
    /**
482
     * @param mixed $createdByUser
483
     *
484
     * @return $this
485
     */
486
    public function setCreatedByUser($createdByUser)
487
    {
488
        $this->createdByUser = $createdByUser;
489
490
        return $this;
491
    }
492
493
    /**
494
     * @param mixed $modifiedByUser
495
     *
496
     * @return $this
497
     */
498
    public function setModifiedByUser($modifiedByUser)
499
    {
500
        $this->modifiedByUser = $modifiedByUser;
501
502
        return $this;
503
    }
504
505
    /**
506
     * @param mixed $checkedOutByUser
507
     *
508
     * @return $this
509
     */
510
    public function setCheckedOutByUser($checkedOutByUser)
511
    {
512
        $this->checkedOutByUser = $checkedOutByUser;
513
514
        return $this;
515
    }
516
}
517