|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
if (class_exists('AbstractQueuedJob')) { |
|
4
|
|
|
/** |
|
5
|
|
|
* @author <[email protected]> |
|
6
|
|
|
* @license BSD License http://www.silverstripe.org/bsd-license |
|
7
|
|
|
*/ |
|
8
|
|
|
class WorkflowReminderJob extends AbstractQueuedJob { |
|
|
|
|
|
|
9
|
|
|
const DEFAULT_REPEAT = 600; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* |
|
13
|
|
|
* @var QueuedJobService |
|
14
|
|
|
*/ |
|
15
|
|
|
public $queuedJobService; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct($repeatInterval = 0) { |
|
18
|
|
|
if (!$this->repeatInterval) { |
|
19
|
|
|
$this->repeatInterval = $repeatInterval ? $repeatInterval : self::DEFAULT_REPEAT; |
|
20
|
|
|
$this->totalSteps = 2; |
|
21
|
|
|
$this->currentStep = 1; |
|
22
|
|
|
} |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function getTitle() { |
|
26
|
|
|
return _t('AdvancedWorkflow.WORKFLOW_REMINDER_JOB', 'Workflow Reminder Job'); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* We only want one instance of this job ever |
|
31
|
|
|
* |
|
32
|
|
|
* @return string |
|
33
|
|
|
*/ |
|
34
|
|
|
public function getSignature() { |
|
35
|
|
|
return md5($this->getTitle()); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function process() { |
|
39
|
|
|
$sent = 0; |
|
40
|
|
|
$filter = array( |
|
41
|
|
|
'WorkflowStatus' => array('Active', 'Paused'), |
|
42
|
|
|
'Definition.RemindDays:GreaterThan' => 0 |
|
43
|
|
|
); |
|
44
|
|
|
|
|
45
|
|
|
$active = WorkflowInstance::get()->filter($filter); |
|
46
|
|
|
|
|
47
|
|
|
foreach ($active as $instance) { |
|
48
|
|
|
$edited = strtotime($instance->LastEdited); |
|
49
|
|
|
$days = $instance->Definition()->RemindDays; |
|
50
|
|
|
|
|
51
|
|
|
if ($edited + ($days * 3600 * 24) > time()) { |
|
52
|
|
|
continue; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$email = new Email(); |
|
56
|
|
|
$bcc = ''; |
|
|
|
|
|
|
57
|
|
|
$members = $instance->getAssignedMembers(); |
|
58
|
|
|
$target = $instance->getTarget(); |
|
59
|
|
|
|
|
60
|
|
|
if (!$members || !count($members)) { |
|
61
|
|
|
continue; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$email->setSubject("Workflow Reminder: $instance->Title"); |
|
65
|
|
|
$email->setBcc(implode(', ', $members->column('Email'))); |
|
66
|
|
|
$email->setTemplate('WorkflowReminderEmail'); |
|
67
|
|
|
$email->populateTemplate(array( |
|
68
|
|
|
'Instance' => $instance, |
|
69
|
|
|
'Link' => $target instanceof SiteTree ? "admin/show/$target->ID" : '' |
|
70
|
|
|
)); |
|
71
|
|
|
|
|
72
|
|
|
$email->send(); |
|
73
|
|
|
$sent++; |
|
74
|
|
|
|
|
75
|
|
|
// add a comment to the workflow if possible |
|
76
|
|
|
$action = $instance->CurrentAction(); |
|
77
|
|
|
|
|
78
|
|
|
$currentComment = $action->Comment; |
|
79
|
|
|
$action->Comment = sprintf(_t('AdvancedWorkflow.JOB_REMINDER_COMMENT', '%s: Reminder email sent\n\n'), date('Y-m-d H:i:s')) . $currentComment; |
|
80
|
|
|
try { |
|
81
|
|
|
$action->write(); |
|
82
|
|
|
} catch (Exception $ex) { |
|
83
|
|
|
SS_Log::log($ex, SS_Log::WARN); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$instance->LastEdited = time(); |
|
87
|
|
|
try { |
|
88
|
|
|
$instance->write(); |
|
89
|
|
|
} catch (Exception $ex) { |
|
90
|
|
|
SS_Log::log($ex, SS_Log::WARN); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$this->currentStep = 2; |
|
95
|
|
|
$this->isComplete = true; |
|
96
|
|
|
|
|
97
|
|
|
$nextDate = date('Y-m-d H:i:s', time() + $this->repeatInterval); |
|
98
|
|
|
$this->queuedJobService->queueJob(new WorkflowReminderJob($this->repeatInterval), $nextDate); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.