|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Symbiote\AdvancedWorkflow\Jobs; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Psr\Log\LoggerInterface; |
|
7
|
|
|
use SilverStripe\CMS\Model\SiteTree; |
|
8
|
|
|
use SilverStripe\Control\Email\Email; |
|
9
|
|
|
use Symbiote\AdvancedWorkflow\DataObjects\WorkflowInstance; |
|
10
|
|
|
use Symbiote\QueuedJobs\Services\AbstractQueuedJob; |
|
11
|
|
|
use Symbiote\QueuedJobs\Services\QueuedJobService; |
|
12
|
|
|
|
|
13
|
|
|
if (!class_exists(AbstractQueuedJob::class)) { |
|
14
|
|
|
return; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @author <[email protected]> |
|
19
|
|
|
* @license BSD License http://www.silverstripe.org/bsd-license |
|
20
|
|
|
*/ |
|
21
|
|
|
class WorkflowReminderJob extends AbstractQueuedJob |
|
22
|
|
|
{ |
|
23
|
|
|
const DEFAULT_REPEAT = 600; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* |
|
27
|
|
|
* @var QueuedJobService |
|
28
|
|
|
*/ |
|
29
|
|
|
public $queuedJobService; |
|
30
|
|
|
|
|
31
|
|
|
public function __construct($repeatInterval = 0) |
|
32
|
|
|
{ |
|
33
|
|
|
if (!$this->repeatInterval) { |
|
34
|
|
|
$this->repeatInterval = $repeatInterval ? $repeatInterval : self::DEFAULT_REPEAT; |
|
35
|
|
|
$this->totalSteps = 2; |
|
36
|
|
|
$this->currentStep = 1; |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function getTitle() |
|
41
|
|
|
{ |
|
42
|
|
|
return _t('AdvancedWorkflow.WORKFLOW_REMINDER_JOB', 'Workflow Reminder Job'); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* We only want one instance of this job ever |
|
47
|
|
|
* |
|
48
|
|
|
* @return string |
|
49
|
|
|
*/ |
|
50
|
|
|
public function getSignature() |
|
51
|
|
|
{ |
|
52
|
|
|
return md5($this->getTitle()); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function process() |
|
56
|
|
|
{ |
|
57
|
|
|
$sent = 0; |
|
58
|
|
|
$filter = [ |
|
59
|
|
|
'WorkflowStatus' => ['Active', 'Paused'], |
|
60
|
|
|
'Definition.RemindDays:GreaterThan' => 0 |
|
61
|
|
|
]; |
|
62
|
|
|
|
|
63
|
|
|
$active = WorkflowInstance::get()->filter($filter); |
|
64
|
|
|
|
|
65
|
|
|
foreach ($active as $instance) { |
|
66
|
|
|
$edited = strtotime($instance->LastEdited); |
|
67
|
|
|
$days = $instance->Definition()->RemindDays; |
|
68
|
|
|
|
|
69
|
|
|
if ($edited + ($days * 3600 * 24) > time()) { |
|
70
|
|
|
continue; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$email = Email::create(); |
|
74
|
|
|
$bcc = ''; |
|
|
|
|
|
|
75
|
|
|
$members = $instance->getAssignedMembers(); |
|
76
|
|
|
$target = $instance->getTarget(); |
|
77
|
|
|
|
|
78
|
|
|
if (!$members || !$members->exists()) { |
|
79
|
|
|
continue; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$email->setSubject("Workflow Reminder: $instance->Title"); |
|
83
|
|
|
$email->setBcc(implode(', ', $members->column('Email'))); |
|
84
|
|
|
$email->setHTMLTemplate('WorkflowReminderEmail'); |
|
85
|
|
|
$email->setData(array( |
|
86
|
|
|
'Instance' => $instance, |
|
87
|
|
|
'Link' => $target instanceof SiteTree ? "admin/show/$target->ID" : '' |
|
88
|
|
|
)); |
|
89
|
|
|
|
|
90
|
|
|
try { |
|
91
|
|
|
$email->send(); |
|
92
|
|
|
} catch (Exception $ex) { |
|
93
|
|
|
Injector::inst()->get(LoggerInterface::class)->warning($ex->getMessage()); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$sent++; |
|
97
|
|
|
|
|
98
|
|
|
// add a comment to the workflow if possible |
|
99
|
|
|
$action = $instance->CurrentAction(); |
|
100
|
|
|
|
|
101
|
|
|
$currentComment = $action->Comment; |
|
102
|
|
|
$action->Comment = sprintf(_t( |
|
103
|
|
|
'AdvancedWorkflow.JOB_REMINDER_COMMENT', |
|
104
|
|
|
'%s: Reminder email sent\n\n' |
|
105
|
|
|
), date('Y-m-d H:i:s')) . $currentComment; |
|
106
|
|
|
try { |
|
107
|
|
|
$action->write(); |
|
108
|
|
|
} catch (Exception $ex) { |
|
109
|
|
|
Injector::inst()->get(LoggerInterface::class)->warning($ex->getMessage()); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
$instance->LastEdited = time(); |
|
113
|
|
|
try { |
|
114
|
|
|
$instance->write(); |
|
115
|
|
|
} catch (Exception $ex) { |
|
116
|
|
|
Injector::inst()->get(LoggerInterface::class)->warning($ex->getMessage()); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
$this->currentStep = 2; |
|
121
|
|
|
$this->isComplete = true; |
|
122
|
|
|
|
|
123
|
|
|
$nextDate = date('Y-m-d H:i:s', time() + $this->repeatInterval); |
|
124
|
|
|
$this->queuedJobService->queueJob(new WorkflowReminderJob($this->repeatInterval), $nextDate); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.