-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
EmailTaskHistory.php
274 lines (232 loc) · 13.8 KB
/
EmailTaskHistory.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php
namespace Kanboard\Plugin\KanboardEmailHistory\Action;
use Kanboard\Model\TaskModel;
use Kanboard\Model\CommentModel;
use Kanboard\Model\ProjectModel;
use Kanboard\Model\UserMetadataModel;
use Kanboard\Action\Base;
class EmailTaskHistory extends Base
{
public function getDescription()
{
return t('EmailTaskHistory > Send task description and complete comment history on task closure');
}
public function getCompatibleEvents()
{
return array(
TaskModel::EVENT_CLOSE,
);
}
public function getActionRequiredParameters()
{
return array(
'email_subject' => t('Email Subject'),
'send_to' => array(
'assignee' => t('Send to Assignee'),
'creator' => t('Send to Creator'),
'both' => t('Assignee & Creator'),
'assignee_project_email' => t('Assignee & Project Email'),
'creator_project_email' => t('Creator & Project Email'),
'project_email' => t('Project Email'),
'all' => t('Assignee, Creator & Project Email')
),
'check_box_include_task_title' => t('Include the Task Title and ID in the subject line?'),
'check_box_include_project_name' => t('Include the Project Name in the subject line?'),
'check_box_include_identifier' => t('Include the Project Identifier in the subject line?'),
);
}
public function getEventRequiredParameters()
{
return array(
'task',
// These are event parameters and the task close event does not have a comment parameter
);
}
public function doAction(array $data)
{
// TASK COMMENTS
// here is where we will pull all the comments from a task using the same method in the TaskViewController, note we would need to add the user metadata model up above in "use"
$commentSortingDirection = $this->userMetadataCacheDecorator->get(UserMetadataModel::KEY_COMMENT_SORTING_DIRECTION, 'ASC');
$comments = $this->commentModel->getAll($data['task']['id'], $commentSortingDirection);
$historySent = FALSE;
$project = $this->projectModel->getById($data['task']['project_id']);
$subject_text = $this->getParam('email_subject');
$subject_text_fallback = t('Activity Report');
// SUBJECT OPTIONS
if ($this->getParam('check_box_include_task_title') && (!$this->getParam('check_box_include_project_name')) && (!$this->getParam('check_box_include_identifier'))) {
/////// TASK TITLE only
// Subject becomes: `subject` `task title` `task id`
if ($subject_text == null) {
$subject = $subject_text_fallback . ": " . $data['task']['title'] . " (#" . $data['task']['id'] . ")";
} else {
$subject = $subject_text . ": " . $data['task']['title'] . " (#" . $data['task']['id'] . ")";
}
} elseif (!$this->getParam('check_box_include_task_title') && ($this->getParam('check_box_include_project_name')) && (!$this->getParam('check_box_include_identifier'))) {
/////// PROJECT NAME only
// Subject becomes: `subject` `project name`
$project = $this->projectModel->getById($data['task']['project_id']);
if ($subject_text == null) {
$subject = $subject_text_fallback . ": " . $project['name'];
} else {
$subject = $subject_text . ": " . $project['name'];
}
} elseif (!$this->getParam('check_box_include_task_title') && (!$this->getParam('check_box_include_project_name')) && ($this->getParam('check_box_include_identifier'))) {
/////// PROJECT IDENTIFIER only
// Subject becomes: `subject` `project identifier`
$project = $this->projectModel->getById($data['task']['project_id']);
if ($subject_text == null) {
$subject = $subject_text_fallback . ": " . $project['identifier'];
} else {
$subject = $subject_text . ": " . $project['identifier'];
}
} elseif ($this->getParam('check_box_include_task_title') && (!$this->getParam('check_box_include_project_name')) && ($this->getParam('check_box_include_identifier'))) {
/////// PROJECT IDENTIFIER + TITLE
// Subject becomes: `subject` `project identifier` `task title` `task id`
$project = $this->projectModel->getById($data['task']['project_id']);
if ($subject_text == null) {
$subject = $subject_text_fallback . ": " . $project['identifier'] . " " . $data['task']['title'] . " (#" . $data['task']['id'] . ")";
} else {
$subject = $subject_text . ": " . $project['identifier'] . " " . $data['task']['title'] . " (#" . $data['task']['id'] . ")";
}
} elseif (!$this->getParam('check_box_include_task_title') && ($this->getParam('check_box_include_project_name')) && ($this->getParam('check_box_include_identifier'))) {
/////// PROJECT NAME + PROJECT IDENTIFIER
// Subject becomes: `subject` `project identifier`
$project = $this->projectModel->getById($data['task']['project_id']);
if ($subject_text == null) {
$subject = $subject_text_fallback . ": " . $project['name'] . " " . $project['identifier'];
} else {
$subject = $subject_text . ": " . $project['name'] . " " . $project['identifier'];
}
} elseif ($this->getParam('check_box_include_task_title') && ($this->getParam('check_box_include_project_name')) && (!$this->getParam('check_box_include_identifier'))) {
/////// PROJECT NAME + TITLE
// Subject becomes: `subject` `project name` `task title` `task id`
$project = $this->projectModel->getById($data['task']['project_id']);
if ($subject_text == null) {
$subject = $subject_text_fallback . ": " . $project['name'] . " " . $data['task']['title'] . " (#" . $data['task']['id'] . ")";
} else {
$subject = $subject_text . ": " . $project['name'] . " " . $data['task']['title'] . " (#" . $data['task']['id'] . ")";
}
} elseif ($this->getParam('check_box_include_task_title') && ($this->getParam('check_box_include_project_name')) && ($this->getParam('check_box_include_identifier'))) {
/////// PROJECT NAME + PROJECT IDENTIFIER + TITLE
// Subject becomes: `subject` `project name` `project identifier` `task title` `task id`
$project = $this->projectModel->getById($data['task']['project_id']);
if ($subject_text == null) {
$subject = $subject_text_fallback . ": " . $project['name'] . " " .$project['identifier'] . " " . $data['task']['title'] . " (#" . $data['task']['id'] . ")";
} else {
$subject = $subject_text . ": " . $project['name'] . " " .$project['identifier'] . " " . $data['task']['title'] . " (#" . $data['task']['id'] . ")";
}
} else {
/////// NO SELECTION
// Subject becomes: `subject`
$subject = $subject_text_fallback;
}
// CONSTRUCT EMAIL - SEND TO SELECTED OR ALL
if ($this->getParam('send_to') !== null) {
$send_to = $this->getParam('send_to');
} else {
$send_to = 'all';
}
// CONSTRUCT EMAIL - SEND TO 'ASSIGNEE' 'ASSIGNEE & PROJECT EMAIL' OR 'BOTH' OR 'ALL'
if ($send_to == 'assignee'|| $send_to == 'assignee_project_email' || $send_to == 'both' || $send_to == 'all') {
$user = $this->userModel->getById($data['task']['owner_id']);
if (! empty($user['email'])) {
$myHTML = $this->template->render('kanboardEmailHistory:notification/task_create', array('task' => $data['task']));
$myHTML = $myHTML . $this->template->render('kanboardEmailHistory:task_comments/show', array(
'task' => $data['task'],
'comments' => $comments,
'project' => $this->projectModel->getById($data['task']['project_id']),
'editable' => false,
));
$myHTML = $myHTML . $this->template->render('kanboardEmailHistory:notification/footer', array('task' => $data['task']));
// Send email
$this->emailClient->send(
$user['email'],
$user['name'] ?: $user['username'],
$subject,
$myHTML
);
// Add comment to task to show an email has been fired
$this->commentModel->create( array(
'comment' => t('TASK CLOSED: A copy of the task history has been emailed to @'.$user['username'].' (Task Assignee) [Subject: '. $subject .']'),
'user_id' => $user['id'],
'task_id' => $data['task']['id'],
));
$historySent = TRUE;
// An easy way to test code is to use error_log - disabled by default
error_log("KanboardEmailHistory > Email Sent to Task Assignee",0);
} else {
error_log("KanboardEmailHistory > Email NOT Sent to Task Assignee - No assigned email address",0);
}
}
// CONSTRUCT EMAIL - SEND TO 'CREATOR' 'CREATOR & PROJECT EMAIL' OR 'BOTH' OR 'ALL'
if ($send_to == 'creator' || $send_to == 'creator_project_email' || $send_to == 'both' || $send_to == 'all') {
$user = $this->userModel->getById($data['task']['creator_id']);
if (! empty($user['email'])) {
$myHTML = $this->template->render('kanboardEmailHistory:notification/task_create', array('task' => $data['task']));
$myHTML = $myHTML . $this->template->render('kanboardEmailHistory:task_comments/show', array(
'task' => $data['task'],
'comments' => $comments,
'project' => $this->projectModel->getById($data['task']['project_id']),
'editable' => false,
));
$myHTML = $myHTML . $this->template->render('kanboardEmailHistory:notification/footer', array('task' => $data['task']));
// Send email
$this->emailClient->send(
$user['email'],
$user['name'] ?: $user['username'],
$subject,
$myHTML
);
// Add comment to task to show an email has been fired
$this->commentModel->create( array(
'comment' => t('TASK CLOSED: A copy of the task history has been emailed to @'.$user['username'].' (Task Creator) [Subject: '. $subject .']'),
'user_id' => $user['id'],
'task_id' => $data['task']['id'],
));
$historySent = TRUE;
// An easy way to test code is to use error_log - disabled by default
error_log("KanboardEmailHistory > Email Sent to Task Creator",0);
} else {
error_log("KanboardEmailHistory > Email NOT Sent to Task Creator - No assigned email address",0);
}
}
// CONSTRUCT EMAIL - SEND TO 'PROJECT_EMAIL' OR 'ALL'
if ($send_to == 'project_email' || $send_to == 'assignee_project_email' || $send_to == 'creator_project_email' || $send_to == 'all') {
$user = $this->userModel->getById($data['task']['creator_id']);
$project = $this->projectModel->getById($data['task']['project_id']);
if (! empty($project['email'])) {
$myHTML = $this->template->render('kanboardEmailHistory:notification/task_create', array('task' => $data['task']));
$myHTML = $myHTML . $this->template->render('kanboardEmailHistory:task_comments/show', array(
'task' => $data['task'],
'comments' => $comments,
'project' => $this->projectModel->getById($data['task']['project_id']),
'editable' => false,
));
$myHTML = $myHTML . $this->template->render('kanboardEmailHistory:notification/footer', array('task' => $data['task']));
// Send email
$this->emailClient->send(
$project['email'],
$user['name'] ?: $user['username'],
$subject,
$myHTML
);
// Add comment to task to show an email has been fired
$this->commentModel->create( array(
'comment' => t('TASK CLOSED: A copy of the task history has been emailed to the project email address [Subject: '. $subject .']'),
'user_id' => $user['id'],
'task_id' => $data['task']['id'],
));
$historySent = TRUE;
// An easy way to test code is to use error_log - disabled by default
error_log("KanboardEmailHistory > Email Sent to Project Email Address",0);
} else {
error_log("KanboardEmailHistory > Email NOT Sent to Project Email Address - No assigned email address",0);
}
}
return $historySent;
}
public function hasRequiredCondition(array $data)
{
return true;
}
}