-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathTaskEmailDue.php
213 lines (187 loc) · 7.09 KB
/
TaskEmailDue.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
<?php
namespace Kanboard\Plugin\SendEmailCreator\Action;
use Kanboard\Model\TaskModel;
use Kanboard\Model\TaskMetadataModel;
use Kanboard\Action\Base;
/**
* Email a task notification of impending due date
*/
class TaskEmailDue extends Base
{
/**
* Get automatic action description
*
* @access public
* @return string
*/
public function getDescription()
{
return t('Send email notification of impending due date');
}
/**
* Get the list of compatible events
*
* @access public
* @return array
*/
public function getCompatibleEvents()
{
return array(
TaskModel::EVENT_DAILY_CRONJOB,
);
}
/**
* Get the required parameter for the action (defined by the user)
*
* @access public
* @return array
*/
public function getActionRequiredParameters()
{
return array(
'subject' => t('Email subject'),
'duration' => t('Duration in days'),
'send_to' => array('assignee' => t('Send to Assignee'), 'creator' => t('Send to Creator'), 'both' => t('Send to Both')),
'column_id' => t('Choose a column to ignore, when a task is in this column, no emails will be sent.')
);
}
/**
* Get the required parameter for the event
*
* @access public
* @return string[]
*/
public function getEventRequiredParameters()
{
return array('tasks');
}
/**
* Check if the event data meet the action condition
*
* @access public
* @param array $data Event data dictionary
* @return bool
*/
public function hasRequiredCondition(array $data)
{
return count($data['tasks']) > 0;
}
public function makeDefaultSubject($task)
{
$project = $this->projectModel->getById($task['project_id']);
$remaining = $task['date_due'] - time();
$days_to_due = 0;
$seconds_to_due = 0;
$hours_to_due = 0;
if ($remaining > 0) {
$days_to_due = floor($remaining / 86400);
$seconds_to_due = $remaining % 86400;
$hours_to_due = $seconds_to_due > 0 ? floor($seconds_to_due / 3600) : 0;
}
$time_part = '';
if ($days_to_due > 0) {
$time_part .= $days_to_due . ' day' . ($days_to_due == 1 ? '' : 's');
if ($days_to_due < 2 && $hours_to_due > 0) {
$time_part .= ' and ' . $hours_to_due . ' hour' . ($hours_to_due == 1 ? '' : 's');
}
} elseif ($hours_to_due > 0) {
$time_part = $hours_to_due . ' hour' . ($hours_to_due == 1 ? '' : 's');
}
$subject = '[' . $project['name'] . '][' . $task['title'] . '] ' . ($time_part ? 'Due in ' . $time_part : 'Task is due');
//print "\n".$subject."\n";
return $subject;
}
public function isTimeToSendEmail($project, $task)
{
// Change $verbose to true while debugging
$verbose = false;
$verbose_prefix = $verbose ? "isTimeToSendEmail() - Task \"{$project['name']}::{$task['title']}({$task['id']})\" " : "";
// Don't send if the task doesn't have a due date
if ($task['date_due'] == 0) {
$verbose && print "\n{$verbose_prefix}doesn't have a due date; Not time to send.";
return false;
}
// Don't send if the task itself isn't due soon enough
(!empty($this->getParam('duration'))) ? $max_duration = $this->getParam('duration') * 86400 : $max_duration = 0;
$duration = $task['date_due'] - time();
if ($duration >= $max_duration) {
$verbose && print "\n{$verbose_prefix}isn't due soon enough ($duration v. $max_duration); Not time to send.";
return false;
}
// Don't send if we've already sent too recently
$minimum_email_span = 86400;
$last_emailed = $this->taskMetadataModel->get($task['id'], 'task_last_emailed_toassignee', time() - 86400);
$last_email_span = time() - $last_emailed;
if ($last_email_span < $minimum_email_span) {
$verbose && print "\n{$verbose_prefix}has already been emailed about too recently ($last_email_span v. $minimum_email_span); Not time to send.";
return false;
}
//
$verbose && print "\n{$verbose_prefix}Sending email!";
return true;
}
public function doAction(array $data)
{
$results = array();
if ($this->getParam('send_to') !== null) {
$send_to = $this->getParam('send_to');
} else {
$send_to = 'both';
}
if ($send_to == 'assignee' || $send_to == 'both') {
foreach ($data['tasks'] as $task) {
$project = $this->projectModel->getById($task['project_id']);
// Only email for active projects
if ($project['is_active'] && $task['column_id'] != $this->getParam('column_id')) {
// Decide if it's time to send an email
$is_time_to_send = $this->isTimeToSendEmail($project, $task);
if ($is_time_to_send) {
$user = $this->userModel->getById($task['owner_id']);
if (! empty($user['email'])) {
$results[] = $this->sendEmail($task['id'], $user);
$this->taskMetadataModel->save($task['id'], ['task_last_emailed_toassignee' => time()]);
}
}
}
}
}
if ($send_to == 'creator' || $send_to == 'both') {
foreach ($data['tasks'] as $task) {
$project = $this->projectModel->getById($task['project_id']);
// Only email for active projects
if ($project['is_active'] && $task['column_id'] != $this->getParam('column_id')) {
// Only email is enough time has passed since the last one was sent
$is_time_to_send = $this->isTimeToSendEmail($project, $task);
if ($is_time_to_send) {
$user = $this->userModel->getById($task['creator_id']);
if (! empty($user['email'])) {
$results[] = $this->sendEmail($task['id'], $user);
$this->taskMetadataModel->save($task['id'], ['task_last_emailed_tocreator' => time()]);
}
}
}
}
}
return in_array(true, $results, true);
}
/**
* Send email
*
* @access private
* @param integer $task_id
* @param array $user
* @return boolean
*/
private function sendEmail($task_id, array $user)
{
$task = $this->taskFinderModel->getDetails($task_id);
$subject = $this->getParam('subject') ?: $this->makeDefaultSubject($task);
$this->emailClient->send(
$user['email'],
$user['name'] ?: $user['username'],
$subject,
$this->template->render('notification/task_create', array('task' => $task))
);
return true;
}
}