GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#336)
by
unknown
02:01
created

getIsUnPublishScheduled()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 38 and the first side effect is on line 29.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
namespace Symbiote\AdvancedWorkflow\Extensions;
4
5
use SilverStripe\Control\Controller;
6
use SilverStripe\Core\Injector\Injector;
7
use SilverStripe\Core\Manifest\ModuleLoader;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\Forms\DatetimeField;
10
use SilverStripe\Forms\HeaderField;
11
use SilverStripe\Forms\LiteralField;
12
use SilverStripe\ORM\ArrayList;
13
use SilverStripe\ORM\DataExtension;
14
use SilverStripe\ORM\DataQuery;
15
use SilverStripe\ORM\FieldType\DBDatetime;
16
use SilverStripe\ORM\Queries\SQLSelect;
17
use SilverStripe\Security\Member;
18
use SilverStripe\Security\Permission;
19
use SilverStripe\View\Requirements;
20
use SilverStripe\Versioned\Versioned;
21
use Symbiote\AdvancedWorkflow\Forms\AWRequiredFields;
22
use Symbiote\AdvancedWorkflow\Jobs\WorkflowPublishTargetJob;
23
use Symbiote\AdvancedWorkflow\Services\WorkflowService;
24
use Symbiote\QueuedJobs\DataObjects\QueuedJobDescriptor;
25
use Symbiote\QueuedJobs\Services\QueuedJobService;
26
27
// Queued jobs descriptor is required for this extension
28
if (!class_exists(QueuedJobDescriptor::class)) {
29
    return;
30
}
31
32
/**
33
 * Adds embargo period and expiry dates to content items
34
 *
35
 * @author [email protected]
36
 * @license BSD License http://silverstripe.org/bsd-license/
37
 */
38
class WorkflowEmbargoExpiryExtension extends DataExtension
39
{
40
    private static $db = array(
0 ignored issues
show
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
41
        'DesiredPublishDate' => 'DBDatetime',
42
        'DesiredUnPublishDate' => 'DBDatetime',
43
        'PublishOnDate' => 'DBDatetime',
44
        'UnPublishOnDate' => 'DBDatetime',
45
        'AllowEmbargoedEditing' => 'Boolean',
46
    );
47
48
    private static $has_one = array(
0 ignored issues
show
Unused Code introduced by
The property $has_one is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
49
        'PublishJob' => QueuedJobDescriptor::class,
50
        'UnPublishJob' => QueuedJobDescriptor::class,
51
    );
52
53
    private static $dependencies = array(
0 ignored issues
show
Unused Code introduced by
The property $dependencies is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
54
        'workflowService' => '%$' . WorkflowService::class,
55
    );
56
57
    private static $defaults = array(
0 ignored issues
show
Unused Code introduced by
The property $defaults is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
58
        'AllowEmbargoedEditing' => true
59
    );
60
61
    // This "config" option, might better be handled in _config
62
    public static $showTimePicker = true;
63
64
    /**
65
     * @var WorkflowService
66
     */
67
    protected $workflowService;
68
69
    /**
70
     * Is a workflow in effect?
71
     *
72
     * @var bool
73
     */
74
    public $isWorkflowInEffect = false;
75
76
    /**
77
     * A basic extended validation routine method return format
78
     *
79
     * @var array
80
     */
81
    public static $extendedMethodReturn = array(
82
        'fieldName' => null,
83
        'fieldField' => null,
84
        'fieldMsg' => null,
85
        'fieldValid' => true,
86
    );
87
88
    /**
89
     * @param FieldList $fields
90
     */
91
    public function updateCMSFields(FieldList $fields)
92
    {
93
        // requirements
94
        // ------------
95
96
        Requirements::add_i18n_javascript('symbiote/silverstripe-advancedworkflow:client/lang');
97
98
        // Add timepicker functionality
99
        // @see https://github.com/trentrichardson/jQuery-Timepicker-Addon
100
        Requirements::css(
101
            'symbiote/silverstripe-advancedworkflow:thirdparty/javascript/jquery-ui/timepicker/' .
102
            'jquery-ui-timepicker-addon.css'
103
        );
104
        Requirements::css('symbiote/silverstripe-advancedworkflow:client/dist/styles/advancedworkflow.css');
105
        Requirements::javascript(
106
            'symbiote/silverstripe-advancedworkflow:thirdparty/javascript/jquery-ui/timepicker/' .
107
            'jquery-ui-sliderAccess.js'
108
        );
109
        Requirements::javascript(
110
            'symbiote/silverstripe-advancedworkflow:thirdparty/javascript/jquery-ui/timepicker/' .
111
            'jquery-ui-timepicker-addon.js'
112
        );
113
        Requirements::javascript('symbiote/silverstripe-advancedworkflow:client/dist/js/advancedworkflow.js');
114
115
        // Fields
116
        // ------
117
118
        // we never show these explicitly in admin
119
        $fields->removeByName('PublishJobID');
120
        $fields->removeByName('UnPublishJobID');
121
122
        $this->setIsWorkflowInEffect();
123
124
        $fields->findOrMakeTab(
125
            'Root.PublishingSchedule',
126
            _t('WorkflowEmbargoExpiryExtension.TabTitle', 'Publishing Schedule')
127
        );
128
        if ($this->getIsWorkflowInEffect()) {
129
            // add fields we want in this context
130
            $fields->addFieldsToTab('Root.PublishingSchedule', array(
131
                HeaderField::create(
132
                    'PublishDateHeader',
133
                    _t('WorkflowEmbargoExpiryExtension.REQUESTED_PUBLISH_DATE_H3', 'Expiry and Embargo'),
134
                    3
135
                ),
136
                LiteralField::create('PublishDateIntro', $this->getIntroMessage('PublishDateIntro')),
137
                $dt = DatetimeField::create(
138
                    'DesiredPublishDate',
139
                    _t('WorkflowEmbargoExpiryExtension.REQUESTED_PUBLISH_DATE', 'Requested publish date')
140
                )->setRightTitle(
141
                    _t(
142
                        'WorkflowEmbargoExpiryExtension.REQUESTED_PUBLISH_DATE_RIGHT_TITLE',
143
                        'To request this page to be <strong>published immediately</strong> leave the date and ' .
144
                        'time fields blank'
145
                    )
146
                ),
147
                $ut = DatetimeField::create(
148
                    'DesiredUnPublishDate',
149
                    _t(
150
                        'WorkflowEmbargoExpiryExtension.REQUESTED_UNPUBLISH_DATE',
151
                        'Requested un-publish date'
152
                    )
153
                )->setRightTitle(
154
                    _t(
155
                        'WorkflowEmbargoExpiryExtension.REQUESTED_UNPUBLISH_DATE_RIGHT_TITLE',
156
                        'To request this page to <strong>never expire</strong>' .
157
                        ' leave the date and time fields blank'
158
                    )
159
                ),
160
                DatetimeField::create(
161
                    'PublishOnDate',
162
                    _t('WorkflowEmbargoExpiryExtension.PUBLISH_ON', 'Scheduled publish date')
163
                )->setDisabled(true),
164
                DatetimeField::create(
165
                    'UnPublishOnDate',
166
                    _t('WorkflowEmbargoExpiryExtension.UNPUBLISH_ON', 'Scheduled un-publish date')
167
                )->setDisabled(true)
168
            ));
169
        } else {
170
            // remove fields that have been automatically added that we don't want
171
            $fields->removeByName('DesiredPublishDate');
172
            $fields->removeByName('DesiredUnPublishDate');
173
174
            // add fields we want in this context
175
            $fields->addFieldsToTab('Root.PublishingSchedule', array(
176
                HeaderField::create(
177
                    'PublishDateHeader',
178
                    _t('WorkflowEmbargoExpiryExtension.REQUESTED_PUBLISH_DATE_H3', 'Expiry and Embargo'),
179
                    3
180
                ),
181
                LiteralField::create('PublishDateIntro', $this->getIntroMessage('PublishDateIntro')),
182
                $dt = DatetimeField::create(
183
                    'PublishOnDate',
184
                    _t('WorkflowEmbargoExpiryExtension.PUBLISH_ON', 'Scheduled publish date')
185
                ),
186
                $ut = DatetimeField::create(
187
                    'UnPublishOnDate',
188
                    _t('WorkflowEmbargoExpiryExtension.UNPUBLISH_ON', 'Scheduled un-publish date')
189
                ),
190
            ));
191
        }
192
193
        // Enable a jQuery-UI timepicker widget
194
        // @todo re-validate this with new DatetimeField API
195
        if (self::$showTimePicker) {
196
            $dt->addExtraClass('hasTimePicker');
197
            $ut->addExtraClass('hasTimePicker');
198
        }
199
    }
200
201
    /**
202
     * Clears any existing publish job against this dataobject
203
     */
204
    public function clearPublishJob()
205
    {
206
        $job = $this->owner->PublishJob();
207
        if ($job && $job->exists()) {
208
            $job->delete();
209
        }
210
        $this->owner->PublishJobID = 0;
211
    }
212
213
    /**
214
     * Clears any existing unpublish job
215
     */
216
    public function clearUnPublishJob()
217
    {
218
        // Cancel any in-progress unpublish job
219
        $job = $this->owner->UnPublishJob();
220
        if ($job && $job->exists()) {
221
            $job->delete();
222
        }
223
        $this->owner->UnPublishJobID = 0;
224
    }
225
226
    /**
227
     * Ensure the existence of a publish job at the specified time
228
     *
229
     * @param int $when Timestamp to start this job, or null to start immediately
230
     */
231 View Code Duplication
    protected function ensurePublishJob($when)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
232
    {
233
        // Check if there is a prior job
234
        if ($this->owner->PublishJobID) {
235
            $job = $this->owner->PublishJob();
236
            // Use timestamp for sake of comparison.
237
            if ($job && $job->exists() && DBDatetime::create()->setValue($job->StartAfter)->getTimestamp() == $when) {
238
                return;
239
            }
240
            $this->clearPublishJob();
241
        }
242
243
        // Create a new job with the specified schedule
244
        $job = new WorkflowPublishTargetJob($this->owner, 'publish');
245
        $this->owner->PublishJobID = Injector::inst()->get(QueuedJobService::class)
246
            ->queueJob($job, $when ? date('Y-m-d H:i:s', $when) : null);
247
    }
248
249
    /**
250
     * Ensure the existence of an unpublish job at the specified time
251
     *
252
     * @param int $when Timestamp to start this job, or null to start immediately
253
     */
254 View Code Duplication
    protected function ensureUnPublishJob($when)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
255
    {
256
        // Check if there is a prior job
257
        if ($this->owner->UnPublishJobID) {
258
            $job = $this->owner->UnPublishJob();
259
            // Use timestamp for sake of comparison.
260
            if ($job && $job->exists() && DBDatetime::create()->setValue($job->StartAfter)->getTimestamp() == $when) {
261
                return;
262
            }
263
            $this->clearUnPublishJob();
264
        }
265
266
        // Create a new job with the specified schedule
267
        $job = new WorkflowPublishTargetJob($this->owner, 'unpublish');
268
        $this->owner->UnPublishJobID = Injector::inst()->get(QueuedJobService::class)
269
            ->queueJob($job, $when ? date('Y-m-d H:i:s', $when) : null);
270
    }
271
272
    public function onBeforeDuplicate($original, $doWrite)
0 ignored issues
show
Unused Code introduced by
The parameter $original is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $doWrite is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
273
    {
274
        $clone = $this->owner;
275
276
        $clone->PublishOnDate = null;
277
        $clone->UnPublishOnDate = null;
278
        $clone->clearPublishJob();
279
        $clone->clearUnPublishJob();
280
    }
281
282
    /**
283
     * {@see PublishItemWorkflowAction} for approval of requested publish dates
284
     */
285
    public function onBeforeWrite()
286
    {
287
        parent::onBeforeWrite();
288
289
        // only operate on staging content for this extension; otherwise, you
290
        // need to publish the page to be able to set a 'future' publish...
291
        // while the same could be said for the unpublish, the 'publish' state
292
        // is the one that must be avoided so we allow setting the 'unpublish'
293
        // date for as-yet-not-published content.
294
        if (Versioned::get_stage() === Versioned::LIVE) {
295
            return;
296
        }
297
298
        /*
299
         * Without checking if there's actually a workflow in effect, simply saving
300
         * as draft, would clear the Scheduled Publish & Unpublish date fields, which we obviously
301
         * don't want during a workflow: These date fields should be treated as a content
302
         * change that also requires approval (where such an approval step exists).
303
         *
304
         * - Check to see if we've got 'desired' publish/unpublish date(s).
305
         * - Check if there's a workflow attached to this content
306
         * - Reset values if it's safe to do so
307
         */
308
        if (!$this->getIsWorkflowInEffect()) {
309
            $resetPublishOnDate = $this->owner->DesiredPublishDate && $this->owner->PublishOnDate;
310
            if ($resetPublishOnDate) {
311
                $this->owner->PublishOnDate = null;
312
            }
313
314
            $resetUnPublishOnDate = $this->owner->DesiredUnPublishDate && $this->owner->UnPublishOnDate;
315
            if ($resetUnPublishOnDate) {
316
                $this->owner->UnPublishOnDate = null;
317
            }
318
        }
319
320
        // Jobs can only be queued for records that already exist
321
        if (!$this->owner->ID) {
322
            return;
323
        }
324
325
        // Check requested dates of publish / unpublish, and whether the page should have already been unpublished
326
        $now = DBDatetime::now()->getTimestamp();
327
        $publishTime = $this->owner->dbObject('PublishOnDate')->getTimestamp();
328
        $unPublishTime = $this->owner->dbObject('UnPublishOnDate')->getTimestamp();
329
330
        // We should have a publish job if:
331
        // if no unpublish or publish time, then the Workflow Publish Action will publish without a job
332
        if ((!$unPublishTime && $publishTime) // the unpublish date is not set
333
            || (
334
                $unPublishTime > $now // unpublish date has not passed
335
                && ($publishTime && ($publishTime < $unPublishTime))
336
                // publish date not set or happens before unpublish date
337
            )
338
        ) {
339
            // Trigger time immediately if passed
340
            $this->ensurePublishJob($publishTime < $now ? null : $publishTime);
341
        } else {
342
            $this->clearPublishJob();
343
        }
344
345
        // We should have an unpublish job if:
346
        if ($unPublishTime // we have an unpublish date
347
            &&
348
            $publishTime < $unPublishTime // publish date is before to unpublish date
349
        ) {
350
            // Trigger time immediately if passed
351
            $this->ensureUnPublishJob($unPublishTime < $now ? null : $unPublishTime);
352
        } else {
353
            $this->clearUnPublishJob();
354
        }
355
    }
356
357
    /**
358
     * Add badges to the site tree view to show that a page has been scheduled for publishing or unpublishing
359
     *
360
     * @param $flags
361
     */
362
    public function updateStatusFlags(&$flags)
363
    {
364
        $embargo = $this->getIsPublishScheduled();
365
        $expiry = $this->getIsUnPublishScheduled();
366
367
        if ($embargo || $expiry) {
368
            unset($flags['addedtodraft'], $flags['modified']);
369
        }
370
371
        if ($embargo && $expiry) {
372
            $flags['embargo_expiry'] = array(
373
                'text' => _t('WorkflowEmbargoExpiryExtension.BADGE_PUBLISH_UNPUBLISH', 'Embargo+Expiry'),
374
                'title' => sprintf(
375
                    '%s: %s, %s: %s',
376
                    _t('WorkflowEmbargoExpiryExtension.PUBLISH_ON', 'Scheduled publish date'),
377
                    $this->owner->PublishOnDate,
378
                    _t('WorkflowEmbargoExpiryExtension.UNPUBLISH_ON', 'Scheduled un-publish date'),
379
                    $this->owner->UnPublishOnDate
380
                ),
381
            );
382
        } elseif ($embargo) {
383
            $flags['embargo'] = array(
384
                'text' => _t('WorkflowEmbargoExpiryExtension.BADGE_PUBLISH', 'Embargo'),
385
                'title' => sprintf(
386
                    '%s: %s',
387
                    _t('WorkflowEmbargoExpiryExtension.PUBLISH_ON', 'Scheduled publish date'),
388
                    $this->owner->PublishOnDate
389
                ),
390
            );
391
        } elseif ($expiry) {
392
            $flags['expiry'] = array(
393
                'text' => _t('WorkflowEmbargoExpiryExtension.BADGE_UNPUBLISH', 'Expiry'),
394
                'title' => sprintf(
395
                    '%s: %s',
396
                    _t('WorkflowEmbargoExpiryExtension.UNPUBLISH_ON', 'Scheduled un-publish date'),
397
                    $this->owner->UnPublishOnDate
398
                ),
399
            );
400
        }
401
    }
402
403
    /*
404
     * Define an array of message-parts for use by {@link getIntroMessage()}
405
     *
406
     * @param string $key
407
     * @return array
408
     */
409
    public function getIntroMessageParts($key)
410
    {
411
        $parts = array(
412
            'PublishDateIntro' => array(
413
                'INTRO' => _t(
414
                    'WorkflowEmbargoExpiryExtension.REQUESTED_PUBLISH_DATE_INTRO',
415
                    'Enter a date and/or time to specify embargo and expiry dates.'
416
                ),
417
                'BULLET_1' => _t(
418
                    'WorkflowEmbargoExpiryExtension.REQUESTED_PUBLISH_DATE_INTRO_BULLET_1',
419
                    'These settings won\'t take effect until any approval actions are run'
420
                ),
421
                'BULLET_2' => _t(
422
                    'WorkflowEmbargoExpiryExtension.REQUESTED_PUBLISH_DATE_INTRO_BULLET_2',
423
                    'If an embargo is already set, adding a new one prior to that date\'s passing will overwrite it'
424
                )
425
            )
426
        );
427
        // If there's no effective workflow, no need for the first bullet-point
428
        if (!$this->getIsWorkflowInEffect()) {
429
            $parts['PublishDateIntro']['BULLET_1'] = false;
430
        }
431
        return $parts[$key];
432
    }
433
434
    /*
435
     * Display some messages to the user, a little more complex that a simple one-liner
436
     *
437
     * @param string $key
438
     * @return string
439
     */
440
    public function getIntroMessage($key)
441
    {
442
        $msg = $this->getIntroMessageParts($key);
443
        $curr = Controller::curr();
444
        $msg = $curr->customise($msg)->renderWith('Includes/embargoIntro');
445
        return $msg;
446
    }
447
448
    /*
449
     * Validate
450
     */
451
    public function getCMSValidator()
452
    {
453
        $required = new AWRequiredFields();
454
        $required->setCaller($this);
455
        return $required;
456
    }
457
458
    /**
459
     * This is called in the AWRequiredFields class, this validates whether an Embargo and Expiry are not equal and that
460
     * Embargo is before Expiry, returning the appropriate message when it fails.
461
     *
462
     * @param $data
463
     * @return array
464
     */
465
    public function extendedRequiredFieldsEmbargoExpiry($data)
466
    {
467
        $response = array(
468
            'fieldName' => 'DesiredUnPublishDate[date]',
469
            'fieldField' => null,
470
            'fieldMsg' => null,
471
            'fieldValid' => true
472
        );
473
474
        if (isset($data['DesiredPublishDate'], $data['DesiredUnPublishDate'])) {
475
            $publish = DBDatetime::create()->setValue($data['DesiredPublishDate'])->getTimestamp();
476
            $unpublish = DBDatetime::create()->setValue($data['DesiredUnPublishDate'])->getTimestamp();
477
478
            // the times are the same
479
            if ($publish && $unpublish && $publish == $unpublish) {
480
                $response = array_merge($response, array(
481
                    'fieldMsg' => _t(
482
                        'WorkflowEmbargoExpiryExtension.INVALIDSAMEEMBARGOEXPIRY',
483
                        'The publish date and unpublish date cannot be the same.'
484
                    ),
485
                    'fieldValid' => false
486
                ));
487
            } elseif ($publish && $unpublish && $publish > $unpublish) {
488
                $response = array_merge($response, array(
489
                    'fieldMsg' => _t(
490
                        'WorkflowEmbargoExpiryExtension.INVALIDEXPIRY',
491
                        'The unpublish date cannot be before the publish date.'
492
                    ),
493
                    'fieldValid' => false
494
                ));
495
            }
496
        }
497
498
        return $response;
499
    }
500
501
    /**
502
     * Format a date according to member/user preferences
503
     *
504
     * @param string $date
505
     * @return string $date
506
     */
507
    public function getUserDate($date)
508
    {
509
        $date = DBDatetime::create()->setValue($date);
510
        $member = Security::getCurrentUser();
511
        return $date->FormatFromSettings($member);
512
    }
513
514
    /*
515
     * Sets property as boolean true|false if an effective workflow is found or not
516
     */
517
    public function setIsWorkflowInEffect()
518
    {
519
        // if there is a workflow applied, we can't set the publishing date directly, only the 'desired' publishing date
520
        $effective = $this->getWorkflowService()->getDefinitionFor($this->owner);
521
        $this->isWorkflowInEffect = $effective ? true : false;
522
    }
523
524
    public function getIsWorkflowInEffect()
525
    {
526
        return $this->isWorkflowInEffect;
527
    }
528
529
    /**
530
     * Returns whether a publishing date has been set and is after the current date
531
     *
532
     * @return bool
533
     */
534 View Code Duplication
    public function getIsPublishScheduled()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
535
    {
536
        if (!$this->owner->PublishOnDate) {
537
            return false;
538
        }
539
        $now = DBDatetime::now()->getTimestamp();
540
        $publish = $this->owner->dbObject('PublishOnDate')->getTimestamp();
541
542
        return $now < $publish;
543
    }
544
545
    /**
546
     * Returns whether an unpublishing date has been set and is after the current date
547
     *
548
     * @return bool
549
     */
550 View Code Duplication
    public function getIsUnPublishScheduled()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
551
    {
552
        if (!$this->owner->UnPublishOnDate) {
553
            return false;
554
        }
555
        $now = DBDatetime::now()->getTimestamp();
556
        $unpublish = $this->owner->dbObject('UnPublishOnDate')->getTimestamp();
557
558
        return $now < $unpublish;
559
    }
560
561
    /**
562
     * Add edit check for when publishing has been scheduled and if any workflow definitions want the item to be
563
     * disabled.
564
     *
565
     * @param Member $member
566
     * @return bool|null
567
     */
568
    public function canEdit($member)
569
    {
570
        if (!Permission::check('EDIT_EMBARGOED_WORKFLOW') && // not given global/override permission to edit
571
            !$this->owner->AllowEmbargoedEditing) { // item flagged as not editable
572
            $publishTime = $this->owner->dbObject('PublishOnDate');
573
574
            if ($publishTime && $publishTime->InFuture() || // when scheduled publish date is in the future
575
                // when there isn't a publish date, but a Job is in place
576
                // (publish immediately, but queued jobs is waiting)
577
                (!$publishTime && $this->owner->PublishJobID != 0)
578
            ) {
579
                return false;
580
            }
581
        }
582
    }
583
584
    /**
585
     * Set the workflow service instance
586
     *
587
     * @param WorkflowService $workflowService
588
     * @return $this
589
     */
590
    public function setWorkflowService(WorkflowService $workflowService)
591
    {
592
        $this->workflowService = $workflowService;
593
        return $this;
594
    }
595
596
    /**
597
     * Get the workflow service instance
598
     *
599
     * @return WorkflowService
600
     */
601
    public function getWorkflowService()
602
    {
603
        return $this->workflowService;
604
    }
605
}
606