/
WorkflowRequest.php
937 lines (834 loc) · 32.2 KB
/
WorkflowRequest.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
<?php
/**
* A "workflow request" represents a full review process for one set of changes to a single page.
* Only one workflow request can be active for any given page; however, a page may have a number
* of historical, closed workflow requests.
*
* The WorkflowRequest object shouldn't be directly edited. Instead, you call "workflow step"
* methods on the object, that will update the object appropriately.
*
* To create or retrieve a WorkflowRequest object, call {@link SiteTreeCMSWorkflow::openOrNewWorkflowRequest()}
* or {@link SiteTreeCMSWorkflow::openWorkflowRequest()} on the relevant {@link SiteTree} object.
*
* The following examples show how a workflow can be created.
*
* Request publication:
* <code>
* $wf = $page->openOrNewWorkflowRequest('WorkflowPublicationRequest')
* $wf->request("Can you please publish this page");
* </code>
*
* Reject changes:
* <code>
* $wf = $page->openWorkflowRequest()
* $wf->deny("It's not acceptable. Please correct the spelling.");
* </code>
*
* Approve changes:
* <code>
* $wf = $page->openWorkflowRequest()
* $wf->approve("Thanks, looks good now");
* </code>
*
* Make the changes 'go live' changes:
* <code>
* $wf = $page->openWorkflowRequest()
* $wf->action();
* </code>
*
* {@link WorkflowRequest::Changes()} will provide a list of the changes that the workflow has gone through,
* suitable for presentation as a discussion thread attached to the page.
*
* @package cmsworkflow
*/
class WorkflowRequest extends DataObject implements i18nEntityProvider
{
public static $db = array(
// @todo AwaitingReview
'Status' => "Enum('AwaitingApproval,Approved,Scheduled,Completed,Denied,Cancelled,AwaitingEdit','AwaitingApproval')",
'EmbargoDate' => 'SS_Datetime'
// actioned is true/false whether the change has actually happened on live
);
public static $has_one = array(
'Author' => 'Member',
'Approver' => 'Member',
'Publisher' => 'Member',
'Page' => 'SiteTree'
);
public static $has_many = array(
'Changes' => 'WorkflowRequestChange',
);
public static $many_many = array(
'Approvers' => 'Member'
);
public static $allow_deny = true;
/**
* Control who gets alerts for certain events
* data structure is fairly self-explanitory
* self::$alerts[CLASS][EVENT][USERROLE] = boolean
* Not all event/role combinations are neccessairily
* implemented by all ApprovalPaths.
*/
public static $alerts = null;
public static $enable_all_alerts = false;
protected $memberIdsEmailed = array();
/**
* Factory method setting up a new WorkflowRequest with associated
* state. Sets relations to publishers and authors,
*
* @param SiteTree $page
* @param Member $member The user requesting publication
* @param DataObjectSet $approvers Approvers assigned to this request.
* @return boolean|WorkflowPublicationRequest
*/
public static function create_for_page($page, $author = null, $approvers = null)
{
user_error('WorkflowRequest::create_for_page() - Abstract method, please implement in subclass', E_USER_ERROR);
}
/**
* Should we send an email to the following group under
* these circumstances. Default to false.
*
*/
public static function should_send_alert($class, $event, $group)
{
self::load_default_alerts();
if (self::$enable_all_alerts) {
return true;
}
if (!isset(self::$alerts[$class])) {
return false;
}
if (!isset(self::$alerts[$class][$event])) {
return false;
}
if (!isset(self::$alerts[$class][$event][$group])) {
return false;
}
return self::$alerts[$class][$event][$group];
}
public static function set_alert($class, $event, $group, $notify)
{
self::load_default_alerts();
if (!isset(self::$alerts[$class])) {
self::$alerts[$class] = array();
}
if (!isset(self::$alerts[$class][$event])) {
self::$alerts[$class][$event] = array();
}
self::$alerts[$class][$event][$group] = $notify;
}
public static function load_default_alerts()
{
if (self::$alerts === null) {
if (singleton('WorkflowRequest')->hasExtension('WorkflowTwoStepRequest')) {
self::$alerts = WorkflowTwoStepRequest::$default_alerts;
}
if (singleton('WorkflowRequest')->hasExtension('WorkflowThreeStepRequest')) {
self::$alerts = WorkflowThreeStepRequest::$default_alerts;
}
}
}
/**
* Set this to true if publishers and admins can request new workflows.
* This can be useful, for example for a publisher to modify
* embargo and expiry in a workflow, they need to edit this in the workflow.
* Or if they want to create a workflow for a change so it is tracked.
*/
protected static $publisher_can_create_wf_requests = true;
public function set_publisher_can_create_wf_requests($val)
{
self::$publisher_can_create_wf_requests = $val;
}
/**
* @ignore
*/
protected static $force_publishers_to_use_workflow = false;
/**
* Set this to true to force publishers to use the "Request publication" button, rather than
* "Save & Publish". Workflow admins won't be affected by this change.
*/
public static function set_force_publishers_to_use_workflow($val)
{
self::$force_publishers_to_use_workflow = $val;
}
/**
* Approve this request, notify interested parties
* and close it. Used by {@link LeftAndMainCMSWorkflow}
* and {@link SiteTreeCMSWorkflow}.
*
* @param Member $author
* @return boolean
*/
public function request($comment, $member = null)
{
if (!$member) {
$member = Member::currentUser();
}
$this->Status = 'AwaitingApproval';
$this->write();
$this->addNewChange($comment, $this->Status, $member);
$this->notifyAwaitingApproval($comment);
return _t('SiteTreeCMSWorkflow.CHANGEREQUESTED', 'Requested this change. Emailed %s.');
}
/**
* Comment on a workflow item without changing the status
*/
public function comment($comment, $member = null, $notify = true)
{
if (!$member) {
$member = Member::currentUser();
}
// Switch to handle both 2 step & 3 step
$page = $this->Page();
$isWorkflowParticipant = $page->canEdit($member) || $page->canPublish($member) || $page->canView($member);
if ($page->hasMethod('canApprove')) {
$isWorkflowParticipant = $isWorkflowParticipant || $page->canApprove($member);
}
// Don't let people who aren't workflow participants comment
if (!$isWorkflowParticipant) {
return false;
}
$this->addNewChange($comment, null, $member);
if ($notify) {
$this->notifyComment($comment);
}
return _t('SiteTreeCMSWorkflow.COMMENT_MESSAGE',
'Commented on this workflow request. Emailed %s.');
}
/**
* Request an edit to this page before it can be published.
*
* @param Member $author
* @return boolean
*/
public function requestedit($comment, $member = null, $notify = true)
{
if (!$member) {
$member = Member::currentUser();
}
if (!$this->Page()->canRequestEdit($member)) {
return false;
}
// "publisher" in this sense means "deny-author"
$this->PublisherID = $member->ID;
$this->write();
// open the request and notify interested parties
$this->Status = 'AwaitingEdit';
$this->write();
$this->addNewChange($comment, $this->Status, $member);
if ($notify) {
$this->notifyAwaitingEdit($comment);
}
return _t('SiteTreeCMSWorkflow.CHANGEREQUESTED', 'Requested this change. Emailed %s.');
}
/**
* Deny this request, notify interested parties
* and close it. Used by {@link LeftAndMainCMSWorkflow}
* and {@link SiteTreeCMSWorkflow}.
*
* @param Member $author
* @return boolean
*/
public function deny($comment, $member = null, $notify = true)
{
if (!self::$allow_deny) {
return false;
}
if (!$member) {
$member = Member::currentUser();
}
if (!$this->Page()->canDenyRequests($member)) {
return false;
}
// "publisher" in this sense means "deny-author"
$this->ApproverID = $member->ID;
$this->ActionerID = $member->ID;
$this->Actioned = true;
$this->write();
// open the request and notify interested parties
$this->Status = 'Denied';
$this->write();
// revert page to live (which might undo independent changes by other authors)
if (Versioned::get_one_by_stage('SiteTree', 'Live', "\"SiteTree_Live\".\"ID\" = ".$this->Page()->ID)) {
$this->Page()->doRevertToLive();
}
$this->addNewChange($comment, $this->Status, $member);
if ($notify) {
$this->notifyDenied($comment);
}
return _t('SiteTreeCMSWorkflow.DENYPUBLICATION_MESSAGE', 'Denied workflow request, and reset content. Emailed %s');
}
/**
* Cancel this request, notify interested parties
* and close it. Used by {@link LeftAndMainCMSWorkflow}
* and {@link SiteTreeCMSWorkflow}.
*
* @param Member $author
* @return boolean
*/
public function cancel($comment, $member = null, $notify = true)
{
if (!$member) {
$member = Member::currentUser();
}
if (!$this->Page()->canEdit()) {
return false;
}
// "publisher" in this sense means "deny-author"
$this->ApproverID = $member->ID;
$this->ActionerID = $member->ID;
$this->Actioned = true;
$this->write();
// open the request and notify interested parties
$this->Status = 'Cancelled';
$this->write();
$this->addNewChange($comment, $this->Status, $member);
if ($notify) {
$this->notifyCancelled($comment);
}
return _t('SiteTreeCMSWorkflow.CANCELREQUEST_MESSAGE', 'Cancelled workflow request. Emailed %s');
}
/**
* Create a new {@link WorkflowRequestChange} with the current
* page status and versions, and link it to this object.
*
* @return WorkflowRequestChange
*/
public function addNewChange($comment, $status, $member)
{
$bt = defined('DB::USE_ANSI_SQL') ? "\"" : "`";
$change = new WorkflowRequestChange();
$change->AuthorID = $member->ID;
$change->Status = $status;
$change->Comment = $comment;
$page = $this->Page();
$draftPage = Versioned::get_one_by_stage('SiteTree', 'Draft', "{$bt}SiteTree{$bt}.{$bt}ID{$bt} = $page->ID", false, "\"Created\" DESC");
// draftpage might not exist for pages "deleted from stage"
if ($draftPage) {
$change->PageDraftVersion = $draftPage->Version;
}
$livePage = Versioned::get_one_by_stage('SiteTree', 'Live', "{$bt}SiteTree{$bt}.{$bt}ID{$bt} = $page->ID", false, "\"Created\" DESC");
// livepage might not exist for pages which have never been published
if ($livePage) {
$change->PageLiveVersion = $livePage->Version;
}
$change->write();
$this->Changes()->add($change);
return $change;
}
public function getCMSFields()
{
$fields = parent::getCMSFields();
$diffLinkTitle = _t('SiteTreeCMSWorkflow.DIFFERENCESLINK', 'Show differences to live');
$tf = $fields->dataFieldByName('Changes');
$tf->setFieldList(array(
'Created' => $this->fieldLabel('Created'),
'Author.Title' => $this->fieldLabel('Author'),
'Comment' => $this->fieldLabel('Comment'),
'StatusDescription' => $this->fieldLabel('Status'),
'DiffLinkToLastPublished' => _t('SiteTreeCMSWorkflow.DIFFERENCESTOLIVECOLUMN', 'Differences to live'),
'DiffLinkContentToPrevious' => _t('SiteTreeCMSWorkflow.DIFFERENCESTHISCHANGECOLUMN', 'Differences in this change'),
));
$tf->setFieldFormatting(array(
"DiffLinkToLastPublished" => '<a href=\"$value\" target=\"_blank\" class=\"externallink\">Show</a>',
// "DiffLinkToPrevious" => '<a href=\"$value\" target=\"_blank\" class=\"externallink\">Show</a>'
));
$fields->replaceField(
'Status',
new ReadonlyField('StatusDescription', $this->fieldLabel('Status'), $this->StatusDescription)
);
return $fields;
}
public function ApprovalDate()
{
$bt = defined('DB::USE_ANSI_SQL') ? "\"" : "`";
$change = DataObject::get_one('WorkflowRequestChange', "{$bt}WorkflowRequestID{$bt} = {$this->ID} AND {$bt}Status{$bt} = 'Approved'", "{$bt}ID{$bt} DESC");
return $change ? $change->Created : null;
}
public function getCMSDetailFields()
{
$fields = $this->getFrontEndFields();
$fields->insertBefore(
$titleField = new ReadonlyField(
'RequestTitleField',
$this->fieldLabel('Title'),
$this->getTitle()
),
'Status'
);
$fields->push(
$showDifferencesField = new ReadonlyField(
'ShowDifferencesLink',
false,
sprintf(
'<a href="%s">%s</a>',
$this->DiffLinkToLastPublished,
_t('SiteTreeCMSWorkflow.DIFFERENCESTOLIVECOLUMN', 'Differences to live')
)
)
);
$showDifferencesField->dontEscape = true;
$fields->replaceField(
'Status',
new ReadonlyField(
'StatusDescription',
$this->fieldLabel('Status'),
$this->StatusDescription
)
);
return $fields;
}
/**
* Return the field used for setting Embargo/Expiry date.
* returns false if the field cant be used in this context
*/
public function EmbargoField()
{
$datetimeField = Object::create('DatetimeField', 'EmbargoDate', 'Embargo Date', $this->EmbargoDate);
$datetimeField->getDateField()->setConfig('showcalendar', true);
$datetimeField->getTimeField()->setConfig('showdropdown', true);
$datetimeField->getDateField()->setConfig('dateformat', 'dd/MM/YYYY');
$datetimeField->getTimeField()->setConfig('timeformat', 'HH:mm');
return $datetimeField;
}
public function ExpiryField()
{
$datetimeField = Object::create('DatetimeField', 'ExpiryDate', 'Expiry Date', $this->ExpiryDate);
$datetimeField->getDateField()->setConfig('showcalendar', true);
$datetimeField->getTimeField()->setConfig('showdropdown', true);
$datetimeField->getDateField()->setConfig('dateformat', 'dd/MM/YYYY');
$datetimeField->getTimeField()->setConfig('timeformat', 'HH:mm');
return $datetimeField;
}
public function getEmbargoDate()
{
return $this->getField('EmbargoDate') != '0000-00-00 00:00:00' && $this->getField('EmbargoDate') != null ? $this->getField('EmbargoDate') : null;
}
public function getExpiryDate()
{
return $this->ExpiryDate();
}
public function ExpiryDate()
{
return $this->Page()->ExpiryDate != '0000-00-00 00:00:00' && $this->Page()->ExpiryDate != null ? $this->Page()->ExpiryDate : null;
}
public function WorkflowTimezone()
{
return date('T').', where is it currently '.date('r');
}
/**
* Return true/false whether we can currently change the PublishAt time
*/
public function CanChangeEmbargoExpiry()
{
return $this->Status == 'AwaitingApproval';
}
public function notifyDenied($comment)
{
$emailsToSend = array();
$userWhoDenied = Member::currentUser();
if (WorkflowRequest::should_send_alert(get_class($this->owner), 'deny', 'publisher')) {
$publishers = $this->owner->Page()->PublisherMembers();
foreach ($publishers as $publisher) {
$emailsToSend[] = array($userWhoDenied, $publisher);
}
}
if (WorkflowRequest::should_send_alert(get_class($this->owner), 'deny', 'approver') && $this->Page()->hasMethod('ApproverMembers')) {
$approvers = $this->owner->Page()->ApproverMembers();
foreach ($approvers as $approver) {
$emailsToSend[] = array($userWhoDenied, $approver);
}
}
if (WorkflowRequest::should_send_alert(get_class($this->owner), 'deny', 'author')) {
$emailsToSend[] = array($userWhoDenied, $this->owner->Author());
}
if (count($emailsToSend)) {
foreach ($emailsToSend as $email) {
if ($email[1]->ID == Member::currentUserID()) {
continue;
}
$this->owner->sendNotificationEmail(
$email[0], // sender
$email[1], // recipient
$comment,
'denied the request'
);
}
}
}
public function notifyCancelled($comment)
{
$emailsToSend = array();
$userWhoCancelled = Member::currentUser();
if (WorkflowRequest::should_send_alert(get_class($this->owner), 'cancel', 'publisher')) {
$publishers = $this->owner->Page()->PublisherMembers();
foreach ($publishers as $publisher) {
$emailsToSend[] = array($userWhoCancelled, $publisher);
}
}
if (WorkflowRequest::should_send_alert(get_class($this->owner), 'cancel', 'approver') && $this->Page()->hasMethod('ApproverMembers')) {
$approvers = $this->owner->Page()->ApproverMembers();
foreach ($approvers as $approver) {
$emailsToSend[] = array($userWhoCancelled, $approver);
}
}
if (WorkflowRequest::should_send_alert(get_class($this->owner), 'cancel', 'author')) {
$emailsToSend[] = array($userWhoCancelled, $this->owner->Author());
}
if (count($emailsToSend)) {
foreach ($emailsToSend as $email) {
if ($email[1]->ID == Member::currentUserID()) {
continue;
}
$this->owner->sendNotificationEmail(
$email[0], // sender
$email[1], // recipient
$comment,
'cancelled changes'
);
}
}
}
public function notifyAwaitingEdit($comment)
{
$emailsToSend = array();
$userWhoRequestedEdits = Member::currentUser();
if (WorkflowRequest::should_send_alert(get_class($this->owner), 'requestedit', 'publisher')) {
$publishers = $this->owner->Page()->PublisherMembers();
foreach ($publishers as $publisher) {
$emailsToSend[] = array($userWhoRequestedEdits, $publisher);
}
}
if (WorkflowRequest::should_send_alert(get_class($this->owner), 'requestedit', 'approver') && $this->Page()->hasMethod('ApproverMembers')) {
$approvers = $this->owner->Page()->ApproverMembers();
foreach ($approvers as $approver) {
$emailsToSend[] = array($userWhoRequestedEdits, $approver);
}
}
if (WorkflowRequest::should_send_alert(get_class($this->owner), 'requestedit', 'author')) {
$emailsToSend[] = array($userWhoRequestedEdits, $this->owner->Author());
}
if (count($emailsToSend)) {
foreach ($emailsToSend as $email) {
if ($email[1]->ID == Member::currentUserID()) {
continue;
}
$this->owner->sendNotificationEmail(
$email[0], // sender
$email[1], // recipient
$comment,
'requested edit'
);
}
}
}
public function sendNotificationEmail($sender, $recipient, $comment, $requestedAction, $template = null)
{
if (!$recipient->Email) {
return;
}
$this->addMemberEmailed($recipient);
if (!$template) {
$template = 'WorkflowGenericEmail';
}
if (class_exists('Subsite')) {
$subject = sprintf(_t('WorkflowRequest.EMAIL_SUBJECT_SITENAME', 'CMS Workflow: %s - Page: %s - Status: %s'), SiteConfig::current_site_config()->Title, $this->Page()->Title, self::get_status_description($this->Status));
} else {
$subject = sprintf(_t('WorkflowRequest.EMAIL_SUBJECT', 'Website Workflow - Page: %s - Status: %s'), $this->Page()->Title, self::get_status_description($this->Status));
}
$email = new Email();
$email->setTo($recipient->Email);
$email->setFrom(($sender->Email) ? $sender->Email : Email::getAdminEmail());
$email->setTemplate($template);
$email->setSubject($subject);
$email->populateTemplate(array(
"PageCMSLink" => "admin/show/".$this->Page()->ID,
"Recipient" => $recipient,
"Sender" => $sender,
"Page" => $this->Page(),
"StageSiteLink" => $this->Page()->Link()."?stage=stage",
"LiveSiteLink" => $this->Page()->Link()."?stage=live",
"Workflow" => $this,
"Comment" => $comment,
'RequestedAction' => strtolower($requestedAction),
"ActionOnPage" => $this->ActionOnPage()
));
return $email->send();
}
/**
* Work out the phrase of what has happened to the page. This is sensitive
* to the type of request, the person making the change and the action
* they invoked. This allows us to send more descriptive emails. The cases
* specifically handled:
* - if an author has deleted a page => "deleted the page"
* - if a publisher or approver has denied deletion of a page =>
* "undeleted the page"
* - otherwise => "made changes to"
* @todo Make the generation of this syntax use translatable.
* @todo Get the generation out of here. The message that is displayed for
* any given operation should be generated by the operation, not
* centralised here. This logic assumes a certain syntax of the
* sentence, which assumes a modification to the page.
*/
protected function ActionOnPage()
{
if ($this->ClassName == "WorkflowDeletionRequest") {
if ($this->Status == "Denied") {
return "undeleted the page";
}
if ($this->Status == "AwaitingApproval") {
return "deleted the page";
}
}
return "made changes to";
}
/**
* Add a member to the 'i've emailed them' list
*
* @param Member $member
*/
final public function addMemberEmailed(Member $member)
{
$this->memberIdsEmailed[] = (int)$member->ID;
}
/**
* Get a list of people emails this http request
*
* @return DataObjectSet
*/
final public function getMembersEmailed()
{
$doSet = new DataObjectSet();
foreach (array_unique($this->memberIdsEmailed) as $id) {
$doSet->push(DataObject::get_by_id('Member', $id));
}
return $doSet;
}
/**
* Clear the list of people emailed this http request
*
* @return void
*/
final public function clearMembersEmailed()
{
$this->memberIdsEmailed = array();
}
/**
* Returns a {@link DataDifferencer} object representing the changes. Has
* some nasty logic to make it so that only changes that are made through
* fields that are exposed by the CMS are tracked.
*/
public function Diff()
{
$diff = new DataDifferencer($this->fromRecord(), $this->toRecord());
//// This is commented out, pending a solution to infinite loop.
//// Looping is called, as getCMSFields() calls updateCMSFields() which renders .ss template, which refers to this fn, $Diff.
// $dataObjectFields = array_keys($this->fromRecord()->record);
// asort($dataObjectFields);
// $cmsFields = array();
//
// foreach($this->fromRecord()->getCMSFields()->dataFields() as $f) {
// if (!($f instanceof HiddenField)) $cmsFields[] = $f->Name();
// }
//
// $cmsFields[] = 'LastEdited';
// $cmsFields[] = 'Sort';
// $cmsFields[] = 'Created';
// $cmsFields[] = 'Status';
// $cmsFields[] = 'ProvideComments';
//
// $diff->ignoreFields(array_diff($dataObjectFields, $cmsFields));
return $diff;
}
/**
* Returns the old record that will be replaced by this publication.
*/
public function fromRecord()
{
$bt = defined('DB::USE_ANSI_SQL') ? "\"" : "`";
return Versioned::get_one_by_stage('SiteTree', 'Live', "{$bt}SiteTree_Live{$bt}.{$bt}ID{$bt} = {$this->PageID}", true, "\"Created\" DESC");
}
/**
* Returns the new record for which publication is being requested.
*/
public function toRecord()
{
return $this->Page();
}
/**
* Is the workflow request still pending.
* Important for creation of new workflow requests
* as there should be only one open request
* per page at any given point in time.
*
* @return boolean
*/
public function isOpen()
{
return (!in_array($this->Status, array('Approved', 'Denied')));
}
/**
* Returns a CMS link to see differences made in the request
*
* @return string URL
*/
protected function getDiffLinkToLastPublished()
{
$bt = defined('DB::USE_ANSI_SQL') ? "\"" : "`";
// Get the completed request change and ask it
$completedChange = DataObject::get_one('WorkflowRequestChange', "{$bt}WorkflowRequestID{$bt} = {$this->ID} AND {$bt}Status{$bt} = 'Completed'");
if (!$completedChange) {
return false;
}
return $completedChange->getDiffLinkToLastPublished();
}
/**
* Determines if a request can be created by an author for a specific page.
* Add custom authentication checks by subclassing this method.
*
* @param Member $member
* @param SiteTree $page
* @return boolean
*/
public static function can_create($member = null, $page)
{
if (!$member && $member !== false) {
$member = Member::currentUser();
}
return $page->canEdit($member);
}
/**
* Get all publication requests by a specific author
*
* @param String $class The base class of the requests to fetch
* @param Member $author The author who created the request
* @return DataObjectSet
*/
public static function get_by_author($class, $author, $status = null)
{
$bt = defined('DB::USE_ANSI_SQL') ? "\"" : "`";
if ($status) {
$statusStr = "'" . (is_array($status) ? implode("','", $status) : $status) . "'";
}
$classes = (array)ClassInfo::subclassesFor($class);
$classes[] = $class;
$classesSQL = implode("','", $classes);
// build filter
$filter = "{$bt}Member{$bt}.{$bt}ID{$bt} = {$author->ID}
AND {$bt}WorkflowRequest{$bt}.{$bt}ClassName{$bt} IN ('$classesSQL')
";
if ($status) {
$filter .= "AND {$bt}WorkflowRequest{$bt}.{$bt}Status{$bt} IN (" . $statusStr . ")";
}
return DataObject::get(
"SiteTree",
$filter,
"{$bt}SiteTree{$bt}.{$bt}LastEdited{$bt} DESC",
"LEFT JOIN {$bt}WorkflowRequest{$bt} ON {$bt}WorkflowRequest{$bt}.{$bt}PageID{$bt} = {$bt}SiteTree{$bt}.{$bt}ID{$bt} " .
"LEFT JOIN {$bt}Member{$bt} ON {$bt}Member{$bt}.{$bt}ID{$bt} = {$bt}WorkflowRequest{$bt}.{$bt}AuthorID{$bt}"
);
}
/**
* Get publication requests from all users
* @param string $class WorkflowRequest subclass
* @param array $status One or more stati from the $Status property
* @return DataObjectSet
*/
public static function get($class, $status = null)
{
$bt = defined('DB::USE_ANSI_SQL') ? "\"" : "`";
if ($status) {
$statusStr = implode(',', $status);
}
$classes = (array)ClassInfo::subclassesFor($class);
$classes[] = $class;
$classesSQL = implode("','", $classes);
// build filter
$filter = "{$bt}WorkflowRequest{$bt}.{$bt}ClassName{$bt} IN ('$classesSQL')";
if ($status) {
$filter .= "AND {$bt}WorkflowRequest{$bt}.{$bt}Status{$bt} IN ('" . Convert::raw2sql($statusStr) . "')";
}
return DataObject::get(
"SiteTree",
$filter,
"{$bt}SiteTree{$bt}.{$bt}LastEdited{$bt} DESC",
"LEFT JOIN {$bt}WorkflowRequest{$bt} ON {$bt}WorkflowRequest{$bt}.{$bt}PageID{$bt} = {$bt}SiteTree{$bt}.{$bt}ID{$bt}"
);
}
/**
* @return string
*/
public function getTitle()
{
$title = _t("{$this->class}.TITLE");
if (!$title) {
$title = _t('WorkflowRequest.TITLE');
}
return $title;
}
/**
* @return string Translated $Status property
*/
public function getStatusDescription()
{
return self::get_status_description($this->Status);
}
public static function get_status_description($status)
{
switch ($status) {
case 'Open':
return _t('SiteTreeCMSWorkflow.STATUS_OPEN', 'Open');
case 'Approved':
return _t('SiteTreeCMSWorkflow.STATUS_APPROVED', 'Approved');
case 'Scheduled':
return _t('SiteTreeCMSWorkflow.STATUS_SCHEDULED', 'Scheduled for Publishing');
case 'Completed':
return _t('SiteTreeCMSWorkflow.STATUS_COMPLETED', 'Completed');
case 'AwaitingApproval':
return _t('SiteTreeCMSWorkflow.STATUS_AWAITINGAPPROVAL', 'Awaiting Approval');
case 'AwaitingEdit':
return _t('SiteTreeCMSWorkflow.STATUS_AWAITINGEDIT', 'Awaiting Edit');
case 'Denied':
return _t('SiteTreeCMSWorkflow.STATUS_DENIED', 'Denied');
case 'Cancelled':
return _t('SiteTreeCMSWorkflow.STATUS_CANCELLED', 'Cancelled');
default:
return _t('SiteTreeCMSWorkflow.STATUS_'.strtoupper($status), $status);
}
}
public function fieldLabels()
{
$labels = parent::fieldLabels();
$labels['Status'] = _t('SiteTreeCMSWorkflow.FIELDLABEL_STATUS', "Status");
$labels['Author'] = _t('SiteTreeCMSWorkflow.FIELDLABEL_AUTHOR', "Author");
$labels['Publisher'] = _t('SiteTreeCMSWorkflow.FIELDLABEL_PUBLISHER', "Publisher");
$labels['Page'] = _t('SiteTreeCMSWorkflow.FIELDLABEL_PAGE', "Page");
$labels['Publishers'] = _t('SiteTreeCMSWorkflow.FIELDLABEL_PUBLISHERS', "Publishers");
return $labels;
}
// @codeCoverageIgnoreStart
public function provideI18nEntities()
{
$entities = array();
$entities['WorkflowRequest.EMAIL_SUBJECT_GENERIC'] = array(
"The workflow status of the \"%s\" page has changed",
PR_MEDIUM,
'Email subject with page title'
);
$entities['WorkflowRequest.TITLE'] = array(
"Workflow Request",
PR_MEDIUM,
'Title for this request, shown e.g. in the workflow status overview for a page'
);
return $entities;
}
// @codeCoverageIgnoreEnd
public function setSchedule()
{
if ($this->EmbargoDate) {
$this->Status = 'Scheduled';
$this->write();
}
}
}