|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* A task that sends a reminder email to users assigned to a workflow that has |
|
4
|
|
|
* not been actioned for n days. |
|
5
|
|
|
* |
|
6
|
|
|
* @package advancedworkflow |
|
7
|
|
|
*/ |
|
8
|
|
|
class WorkflowReminderTask extends BuildTask { |
|
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
protected $title = 'Workflow Reminder Task'; |
|
11
|
|
|
protected $description = 'Sends out workflow reminder emails to stale workflow instances'; |
|
12
|
|
|
|
|
13
|
|
|
public function run($request) { |
|
14
|
|
|
$sent = 0; |
|
15
|
|
|
if (WorkflowInstance::get()->count()) { // Don't attempt the filter if no instances -- prevents a crash |
|
16
|
|
|
$active = WorkflowInstance::get() |
|
17
|
|
|
->innerJoin('WorkflowDefinition', '"DefinitionID" = "WorkflowDefinition"."ID"') |
|
18
|
|
|
->filter(array('WorkflowStatus' => array('Active', 'Paused'), 'RemindDays:GreaterThan' => '0')); |
|
19
|
|
|
$active->filter(array('RemindDays:GreaterThan' => '0')); |
|
20
|
|
|
if ($active) foreach ($active as $instance) { |
|
21
|
|
|
$edited = strtotime($instance->LastEdited); |
|
22
|
|
|
$days = $instance->Definition()->RemindDays; |
|
23
|
|
|
|
|
24
|
|
|
if ($edited + $days * 3600 * 24 > time()) { |
|
25
|
|
|
continue; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
$email = new Email(); |
|
29
|
|
|
$bcc = ''; |
|
|
|
|
|
|
30
|
|
|
$members = $instance->getAssignedMembers(); |
|
31
|
|
|
$target = $instance->getTarget(); |
|
32
|
|
|
|
|
33
|
|
|
if (!$members || !count($members)) continue; |
|
34
|
|
|
|
|
35
|
|
|
$email->setSubject("Workflow Reminder: $instance->Title"); |
|
36
|
|
|
$email->setBcc(implode(', ', $members->column('Email'))); |
|
37
|
|
|
$email->setTemplate('WorkflowReminderEmail'); |
|
38
|
|
|
$email->populateTemplate(array( |
|
39
|
|
|
'Instance' => $instance, |
|
40
|
|
|
'Link' => $target instanceof SiteTree ? "admin/show/$target->ID" : '' |
|
41
|
|
|
)); |
|
42
|
|
|
|
|
43
|
|
|
$email->send(); |
|
44
|
|
|
$sent++; |
|
45
|
|
|
|
|
46
|
|
|
$instance->LastEdited = time(); |
|
47
|
|
|
$instance->write(); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
echo "Sent $sent workflow reminder emails.\n"; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
} |
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.