-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEmailTaskHistory.php
229 lines (188 loc) · 11.3 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
<?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(
'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_title' => t('Include the Task Title and ID in the subject line?'),
'check_box_include_project' => t('Include the Project Name in the subject line?'),
'check_box_include_project_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_id = $this->projectModel->getById($data['task']['project_id']);
// SUBJECT OPTIONS
if ($this->getParam('check_box_include_title') == true ) {
// TASK TITLE // Subject becomes: `subject` `task title` `task id`
$subject = $this->getParam('subject') . ": " . $data['task']['title'] . " (#" . $data['task']['id'] . ")";
} elseif ($this->getParam('check_box_include_project') == true ) {
// PROJECT NAME // Subject becomes: `subject` `project name` `task title` `task id`
$project = $this->projectModel->getById($project_id);
$subject = $this->getParam('subject') . ": " . $project['name'] . " " . $data['task']['title'] . " (#" . $data['task']['id'] . ")";
} elseif ($this->getParam('check_box_include_title') == true && $this->getParam('check_box_include_project_identifier')) {
// PROJECT IDENTIFIER // Subject becomes: `subject` `project identifier`
$project = $this->projectModel->getById($project_id);
$subject = $this->getParam('subject') . ": " . $project['identifier'];
} elseif ($this->getParam('check_box_include_title') == true && $this->getParam('check_box_include_project_identifier')) {
// PROJECT IDENTIFIER + TITLE // Subject becomes: `subject` `project identifier` `task title` `task id`
$project = $this->projectModel->getById($project_id);
$subject = $this->getParam('subject') . ": " . $project['identifier'] . " " . $data['task']['title'] . " (#" . $data['task']['id'] . ")";
} elseif ($this->getParam('check_box_include_project') == true && $this->getParam('check_box_include_project_identifier')) {
// PROJECT NAME + PROJECT IDENTIFIER // Subject becomes: `subject` `project identifier`
$project = $this->projectModel->getById($project_id);
$subject = $this->getParam('subject') . ": " . $project['name'] . " " . $project['identifier'];
} elseif ($this->getParam('check_box_include_title') == true && $this->getParam('check_box_include_project')) {
// PROJECT NAME + TITLE // Subject becomes: `subject` `project name` `task title` `task id`
$project = $this->projectModel->getById($project_id);
$subject = $this->getParam('subject') . ": " . $project['name'] . " " . $data['task']['title'] . " (#" . $data['task']['id'] . ")";
} elseif ($this->getParam('check_box_include_title') == true && $this->getParam('check_box_include_project_identifier') && $this->getParam('check_box_include_project')) {
// PROJECT NAME + PROJECT IDENTIFIER + TITLE // Subject becomes: `subject` `project name` `project identifier` `task title` `task id`
$project = $this->projectModel->getById($project_id);
$subject = $this->getParam('subject') . ": " . $project['name'] . " " .$project['identifier'] . " " . $data['task']['title'] . " (#" . $data['task']['id'] . ")";
} else {
// NO SELECTION // Subject becomes: `subject`
$subject = $this->getParam('subject');
}
// 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 history emailed to the task assignee @'.$user['username'].' with subject "'. $subject).'".',
'user_id' => $user['id'],
'task_id' => $data['task']['id'],
));
$historySent = TRUE;
// An easy way to test code is to use error_log
error_log("KanboardEmailHistory > Email Sent to Task Assignee",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('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
error_log("KanboardEmailHistory > Email Sent to Task Creator",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('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
error_log("KanboardEmailHistory > Email Sent to Project Email Address",0);
}
}
return $historySent;
}
public function hasRequiredCondition(array $data)
{
return true;
}
}