/
WorkflowEmbargoExpiryExtension.php
367 lines (323 loc) · 11.6 KB
/
WorkflowEmbargoExpiryExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
<?php
/**
* Adds embargo period and expiry dates to content items
*
* @author marcus@silverstripe.com.au
* @license BSD License http://silverstripe.org/bsd-license/
*/
class WorkflowEmbargoExpiryExtension extends DataExtension {
private static $db = array(
'DesiredPublishDate' => 'SS_Datetime',
'DesiredUnPublishDate' => 'SS_Datetime',
'PublishOnDate' => 'SS_Datetime',
'UnPublishOnDate' => 'SS_Datetime',
);
private static $has_one = array(
'PublishJob' => 'QueuedJobDescriptor',
'UnPublishJob' => 'QueuedJobDescriptor',
);
private static $dependencies = array(
'workflowService' => '%$WorkflowService',
);
// This "config" option, might better be handled in _config
public static $showTimePicker = true;
/**
* @var WorkflowService
*/
public $workflowService;
/**
* Is a workflow in effect?
*
* @var bool
*/
public $isWorkflowInEffect = false;
/**
* A basic extended validation routine method return format
*
* @var array
*/
public static $extendedMethodReturn = array(
'fieldName' =>null,
'fieldField'=>null,
'fieldMsg' =>null,
'fieldValid'=>true
);
/**
* @param FieldList $fields
*/
public function updateCMSFields(FieldList $fields) {
// requirements
// ------------
Requirements::add_i18n_javascript(ADVANCED_WORKFLOW_DIR . '/javascript/lang');
// Add timepicker functionality
// @see https://github.com/trentrichardson/jQuery-Timepicker-Addon
Requirements::css(
ADVANCED_WORKFLOW_DIR . '/thirdparty/javascript/jquery-ui/timepicker/jquery-ui-timepicker-addon.css'
);
Requirements::css(ADVANCED_WORKFLOW_DIR . '/css/WorkflowCMS.css');
Requirements::javascript(
ADVANCED_WORKFLOW_DIR . '/thirdparty/javascript/jquery-ui/timepicker/jquery-ui-sliderAccess.js'
);
Requirements::javascript(
ADVANCED_WORKFLOW_DIR . '/thirdparty/javascript/jquery-ui/timepicker/jquery-ui-timepicker-addon.js'
);
Requirements::javascript(ADVANCED_WORKFLOW_DIR . '/javascript/WorkflowField.js');
// Fields
// ------
// we never show these explicitly in admin
$fields->removeByName('PublishJobID');
$fields->removeByName('UnPublishJobID');
$this->setIsWorkflowInEffect();
$fields->findOrMakeTab(
'Root.PublishingSchedule',
_t('WorkflowEmbargoExpiryExtension.TabTitle', 'Publishing Schedule')
);
if ($this->getIsWorkflowInEffect()) {
// add fields we want in this context
$fields->addFieldsToTab('Root.PublishingSchedule', array(
HeaderField::create(
'PublishDateHeader',
_t('WorkflowEmbargoExpiryExtension.REQUESTED_PUBLISH_DATE_H3', 'Expiry and Embargo'),
3
),
LiteralField::create('PublishDateIntro', $this->getIntroMessage('PublishDateIntro')),
$dt = Datetimefield::create(
'DesiredPublishDate',
_t('WorkflowEmbargoExpiryExtension.REQUESTED_PUBLISH_DATE', 'Requested publish date')
),
$ut = Datetimefield::create(
'DesiredUnPublishDate',
_t('WorkflowEmbargoExpiryExtension.REQUESTED_UNPUBLISH_DATE', 'Requested un-publish date')
),
Datetimefield::create(
'PublishOnDate',
_t('WorkflowEmbargoExpiryExtension.PUBLISH_ON', 'Scheduled publish date')
)->setDisabled(true),
Datetimefield::create(
'UnPublishOnDate',
_t('WorkflowEmbargoExpiryExtension.UNPUBLISH_ON', 'Scheduled un-publish date')
)->setDisabled(true)
));
} else {
// remove fields that have been automatically added that we don't want
$fields->removeByName('DesiredPublishDate');
$fields->removeByName('DesiredUnPublishDate');
// add fields we want in this context
$fields->addFieldsToTab('Root.PublishingSchedule', array(
HeaderField::create(
'PublishDateHeader',
_t('WorkflowEmbargoExpiryExtension.REQUESTED_PUBLISH_DATE_H3', 'Expiry and Embargo'),
3
),
LiteralField::create('PublishDateIntro', $this->getIntroMessage('PublishDateIntro')),
$dt = Datetimefield::create(
'PublishOnDate',
_t('WorkflowEmbargoExpiryExtension.PUBLISH_ON', 'Scheduled publish date')
),
$ut = Datetimefield::create(
'UnPublishOnDate',
_t('WorkflowEmbargoExpiryExtension.UNPUBLISH_ON', 'Scheduled un-publish date')
),
));
}
$dt->getDateField()->setConfig('showcalendar', true);
$ut->getDateField()->setConfig('showcalendar', true);
if ($dt->getTimeField()->getConfig('timeformat') === null) {
$dt->getTimeField()->setConfig('timeformat', 'HH:mm:ss');
$ut->getTimeField()->setConfig('timeformat', 'HH:mm:ss');
}
// Enable a jQuery-UI timepicker widget
if(self::$showTimePicker) {
$dt->getTimeField()->addExtraClass('hasTimePicker');
$ut->getTimeField()->addExtraClass('hasTimePicker');
}
}
/**
* Clears any existing publish job against this dataobject
*/
protected function clearPublishJob() {
$job = $this->owner->PublishJob();
if($job && $job->exists()) {
$job->delete();
}
$this->owner->PublishJobID = 0;
}
/**
* Clears any existing unpublish job
*/
protected function clearUnPublishJob() {
// Cancel any in-progress unpublish job
$job = $this->owner->UnPublishJob();
if ($job && $job->exists()) {
$job->delete();
}
$this->owner->UnPublishJobID = 0;
}
/**
* Ensure the existence of a publish job at the specified time
*
* @param int $when Timestamp to start this job, or null to start immediately
*/
protected function ensurePublishJob($when) {
// Check if there is a prior job
if($this->owner->PublishJobID) {
$job = $this->owner->PublishJob();
// Use timestamp for sake of comparison.
if($job && $job->exists() && strtotime($job->StartAfter) == $when) {
return;
}
$this->clearPublishJob();
}
$version = null;
if ($this->owner->hasField('Version')) {
$version = $this->owner->getField('Version');
}
// Create a new job with the specified schedule
$job = new WorkflowPublishTargetJob($this->owner, 'publish', $version);
$this->owner->PublishJobID = Injector::inst()->get('QueuedJobService')
->queueJob($job, $when ? date('Y-m-d H:i:s', $when) : null);
}
/**
* Ensure the existence of an unpublish job at the specified time
*
* @param int $when Timestamp to start this job, or null to start immediately
*/
protected function ensureUnPublishJob($when) {
// Check if there is a prior job
if($this->owner->UnPublishJobID) {
$job = $this->owner->UnPublishJob();
// Use timestamp for sake of comparison.
if($job && $job->exists() && strtotime($job->StartAfter) == $when) {
return;
}
$this->clearUnPublishJob();
}
// Create a new job with the specified schedule
$job = new WorkflowPublishTargetJob($this->owner, 'unpublish');
$this->owner->UnPublishJobID = Injector::inst()->get('QueuedJobService')
->queueJob($job, $when ? date('Y-m-d H:i:s', $when) : null);
}
/**
* {@see PublishItemWorkflowAction} for approval of requested publish dates
*/
public function onBeforeWrite() {
parent::onBeforeWrite();
// if we've been duplicated, the old job IDs will be hanging around, so explicitly clear
if (!$this->owner->ID) {
$this->owner->PublishJobID = 0;
$this->owner->UnPublishJobID = 0;
}
// only operate on staging content for this extension; otherwise, you
// need to publish the page to be able to set a 'future' publish...
// while the same could be said for the unpublish, the 'publish' state
// is the one that must be avoided so we allow setting the 'unpublish'
// date for as-yet-not-published content.
if (Versioned::current_stage() === 'Live') return;
/*
* Without checking if there's actually a workflow in effect, simply saving
* as draft, would clear the Scheduled Publish & Unpublish date fields, which we obviously
* don't want during a workflow: These date fields should be treated as a content
* change that also requires approval (where such an approval step exists).
*
* - Check to see if we've got 'desired' publish/unpublish date(s).
* - Check if there's a workflow attached to this content
* - Reset values if it's safe to do so
*/
$resetPublishOnDate = $this->owner->DesiredPublishDate && $this->owner->PublishOnDate;
if ($resetPublishOnDate && !$this->getIsWorkflowInEffect()) {
$this->owner->PublishOnDate = '';
}
$resetUnPublishOnDate = $this->owner->DesiredUnPublishDate && $this->owner->UnPublishOnDate;
if ($resetUnPublishOnDate && !$this->getIsWorkflowInEffect()) {
$this->owner->UnPublishOnDate = '';
}
// Jobs can only be queued for records that already exist
if(!$this->owner->ID) return;
// Check requested dates of publish / unpublish, and whether the page should have already been unpublished
$now = strtotime(SS_Datetime::now()->getValue());
$publishTime = strtotime($this->owner->PublishOnDate);
$unPublishTime = strtotime($this->owner->UnPublishOnDate);
// We should have a publish job if:
if($publishTime && ( // We have a date
$unPublishTime < $publishTime // it occurs after an unpublish date (or there is no unpublish)
|| $unPublishTime > $now // or the unpublish date hasn't passed
)) {
// Trigger time immediately if passed
$this->ensurePublishJob($publishTime < $now ? null : $publishTime);
} else {
$this->clearPublishJob();
}
// We should have an unpublish job if:
if($unPublishTime && ( // we have a date
$publishTime < $unPublishTime // it occurs after a publish date (or there is no publish)
|| $publishTime > $now // or the publish date hasn't passed
)) {
// Trigger time immediately if passed
$this->ensureUnPublishJob($unPublishTime < $now ? null : $unPublishTime);
} else {
$this->clearUnPublishJob();
}
}
/*
* Define an array of message-parts for use by {@link getIntroMessage()}
*
* @param string $key
* @return array
*/
public function getIntroMessageParts($key) {
$parts = array(
'PublishDateIntro' => array(
'INTRO'=>_t('WorkflowEmbargoExpiryExtension.REQUESTED_PUBLISH_DATE_INTRO','Enter a date and/or time to specify embargo and expiry dates.'),
'BULLET_1'=>_t('WorkflowEmbargoExpiryExtension.REQUESTED_PUBLISH_DATE_INTRO_BULLET_1','These settings won\'t take effect until any approval actions are run'),
'BULLET_2'=>_t('WorkflowEmbargoExpiryExtension.REQUESTED_PUBLISH_DATE_INTRO_BULLET_2','If an embargo is already set, adding a new one prior to that date\'s passing will overwrite it')
)
);
// If there's no effective workflow, no need for the first bullet-point
if(!$this->getIsWorkflowInEffect()) {
$parts['PublishDateIntro']['BULLET_1'] = false;
}
return $parts[$key];
}
/*
* Display some messages to the user, a little more complex that a simple one-liner
*
* @param string $key
* @return string
*/
public function getIntroMessage($key) {
$msg = $this->getIntroMessageParts($key);
$curr = Controller::curr();
$msg = $curr->customise($msg)->renderWith('embargoIntro');
return $msg;
}
/*
* Validate
*/
public function getCMSValidator() {
$required = new AWRequiredFields();
$required->setCaller($this);
return $required;
}
/*
* Format a date according to member/user preferences
*
* @param string $date
* @return string $date
*/
public function getUserDate($date) {
$date = new Zend_Date($date);
$member = Member::currentUser();
return $date->toString($member->getDateFormat().' '.$member->getTimeFormat());
}
/*
* Sets property as boolean true|false if an effective workflow is found or not
*/
public function setIsWorkflowInEffect() {
// if there is a workflow applied, we can't set the publishing date directly, only the 'desired' publishing date
$effective = $this->workflowService->getDefinitionFor($this->owner);
$this->isWorkflowInEffect = $effective?true:false;
}
public function getIsWorkflowInEffect() {
return $this->isWorkflowInEffect;
}
}