1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Symbiote\AdvancedWorkflow\Tasks; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Dev\BuildTask; |
6
|
|
|
use SilverStripe\CMS\Model\SiteTree; |
7
|
|
|
use SilverStripe\Control\Email\Email; |
8
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime; |
9
|
|
|
use Symbiote\AdvancedWorkflow\DataObjects\WorkflowDefinition; |
10
|
|
|
use Symbiote\AdvancedWorkflow\DataObjects\WorkflowInstance; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* A task that sends a reminder email to users assigned to a workflow that has |
14
|
|
|
* not been actioned for n days. |
15
|
|
|
* |
16
|
|
|
* @package advancedworkflow |
17
|
|
|
*/ |
18
|
|
|
class WorkflowReminderTask extends BuildTask |
19
|
|
|
{ |
20
|
|
|
protected $title = 'Workflow Reminder Task'; |
21
|
|
|
protected $description = 'Sends out workflow reminder emails to stale workflow instances'; |
22
|
|
|
|
23
|
|
|
private static $segment = 'WorkflowReminderTask'; |
|
|
|
|
24
|
|
|
|
25
|
|
|
public function run($request) |
26
|
|
|
{ |
27
|
|
|
$sent = 0; |
28
|
|
|
if (WorkflowInstance::get()->count()) { // Don't attempt the filter if no instances -- prevents a crash |
29
|
|
|
$active = WorkflowInstance::get() |
30
|
|
|
->innerJoin('WorkflowDefinition', '"DefinitionID" = "WorkflowDefinition"."ID"') |
31
|
|
|
->filter(array( |
32
|
|
|
'WorkflowStatus' => array('Active', 'Paused') |
33
|
|
|
))->where('RemindDays > 0'); |
34
|
|
|
|
35
|
|
|
if ($active->exists()) { |
36
|
|
|
foreach ($active as $instance) { |
37
|
|
|
$edited = strtotime($instance->LastEdited); |
38
|
|
|
$days = $instance->Definition()->RemindDays; |
39
|
|
|
|
40
|
|
|
if ($edited + $days * 3600 * 24 > DBDatetime::now()->getTimestamp()) { |
41
|
|
|
continue; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$email = new Email(); |
45
|
|
|
$bcc = ''; |
|
|
|
|
46
|
|
|
$members = $instance->getAssignedMembers(); |
47
|
|
|
$target = $instance->getTarget(); |
48
|
|
|
|
49
|
|
|
if (!$members || !count($members)) { |
50
|
|
|
continue; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$email->setSubject("Workflow Reminder: $instance->Title"); |
54
|
|
|
$email->setBCC($members->column('Email')); |
55
|
|
|
$email->setHTMLTemplate('email\\WorkflowReminderEmail'); |
56
|
|
|
$email->setData(array( |
57
|
|
|
'Instance' => $instance, |
58
|
|
|
'Link' => $target instanceof SiteTree ? "admin/show/$target->ID" : '', |
59
|
|
|
'Diff' => $instance->getTargetDiff() |
60
|
|
|
)); |
61
|
|
|
|
62
|
|
|
$email->send(); |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
$sent++; |
66
|
|
|
|
67
|
|
|
$instance->LastEdited = DBDatetime::now()->getTimestamp(); |
68
|
|
|
$instance->write(); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
echo "Sent $sent workflow reminder emails.\n"; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|