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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
|
<?php
namespace Model;
use Core\Translator;
/**
* Notification model
*
* @package model
* @author Frederic Guillot
*/
class Notification extends Base
{
/**
* SQL table name
*
* @var string
*/
const TABLE = 'user_has_notifications';
/**
* User filters
*
* @var integer
*/
const FILTER_NONE = 1;
const FILTER_ASSIGNEE = 2;
const FILTER_CREATOR = 3;
const FILTER_BOTH = 4;
/**
* Send overdue tasks
*
* @access public
*/
public function sendOverdueTaskNotifications()
{
$tasks = $this->taskFinder->getOverdueTasks();
$projects = array();
foreach ($this->groupByColumn($tasks, 'project_id') as $project_id => $project_tasks) {
// Get the list of users that should receive notifications for each projects
$users = $this->notification->getUsersWithNotificationEnabled($project_id);
foreach ($users as $user) {
$this->sendUserOverdueTaskNotifications($user, $project_tasks);
}
}
return $tasks;
}
/**
* Send overdue tasks for a given user
*
* @access public
* @param array $user
* @param array $tasks
*/
public function sendUserOverdueTaskNotifications(array $user, array $tasks)
{
$user_tasks = array();
foreach ($tasks as $task) {
if ($this->notification->shouldReceiveNotification($user, array('task' => $task))) {
$user_tasks[] = $task;
}
}
if (! empty($user_tasks)) {
$this->sendEmailNotification(
$user,
Task::EVENT_OVERDUE,
array('tasks' => $user_tasks, 'project_name' => $tasks[0]['project_name'])
);
}
}
/**
* Send notifications to people
*
* @access public
* @param string $event_name
* @param array $event_data
*/
public function sendNotifications($event_name, array $event_data)
{
$logged_user_id = $this->userSession->isLogged() ? $this->userSession->getId() : 0;
$users = $this->notification->getUsersWithNotificationEnabled($event_data['task']['project_id'], $logged_user_id);
foreach ($users as $user) {
if ($this->shouldReceiveNotification($user, $event_data)) {
$this->sendEmailNotification($user, $event_name, $event_data);
}
}
// Restore locales
$this->config->setupTranslations();
}
/**
* Send email notification to someone
*
* @access public
* @param array $user User
* @param string $event_name
* @param array $event_data
*/
public function sendEmailNotification(array $user, $event_name, array $event_data)
{
// Use the user language otherwise use the application language (do not use the session language)
if (! empty($user['language'])) {
Translator::load($user['language']);
}
else {
Translator::load($this->config->get('application_language', 'en_US'));
}
$this->emailClient->send(
$user['email'],
$user['name'] ?: $user['username'],
$this->getMailSubject($event_name, $event_data),
$this->getMailContent($event_name, $event_data)
);
}
/**
* Return true if the user should receive notification
*
* @access public
* @param array $user
* @param array $event_data
* @return boolean
*/
public function shouldReceiveNotification(array $user, array $event_data)
{
$filters = array(
'filterNone',
'filterAssignee',
'filterCreator',
'filterBoth',
);
foreach ($filters as $filter) {
if ($this->$filter($user, $event_data)) {
return $this->filterProject($user, $event_data);
}
}
return false;
}
/**
* Return true if the user will receive all notifications
*
* @access public
* @param array $user
* @param array $event_data
* @return boolean
*/
public function filterNone(array $user, array $event_data)
{
return $user['notifications_filter'] == self::FILTER_NONE;
}
/**
* Return true if the user is the assignee and selected the filter "assignee"
*
* @access public
* @param array $user
* @param array $event_data
* @return boolean
*/
public function filterAssignee(array $user, array $event_data)
{
return $user['notifications_filter'] == self::FILTER_ASSIGNEE && $event_data['task']['owner_id'] == $user['id'];
}
/**
* Return true if the user is the creator and enabled the filter "creator"
*
* @access public
* @param array $user
* @param array $event_data
* @return boolean
*/
public function filterCreator(array $user, array $event_data)
{
return $user['notifications_filter'] == self::FILTER_CREATOR && $event_data['task']['creator_id'] == $user['id'];
}
/**
* Return true if the user is the assignee or the creator and selected the filter "both"
*
* @access public
* @param array $user
* @param array $event_data
* @return boolean
*/
public function filterBoth(array $user, array $event_data)
{
return $user['notifications_filter'] == self::FILTER_BOTH &&
($event_data['task']['creator_id'] == $user['id'] || $event_data['task']['owner_id'] == $user['id']);
}
/**
* Return true if the user want to receive notification for the selected project
*
* @access public
* @param array $user
* @param array $event_data
* @return boolean
*/
public function filterProject(array $user, array $event_data)
{
$projects = $this->db->table(self::TABLE)->eq('user_id', $user['id'])->findAllByColumn('project_id');
if (! empty($projects)) {
return in_array($event_data['task']['project_id'], $projects);
}
return true;
}
/**
* Get a list of people with notifications enabled
*
* @access public
* @param integer $project_id Project id
* @param array $exclude_user_id User id to exclude
* @return array
*/
public function getUsersWithNotificationEnabled($project_id, $exclude_user_id = 0)
{
if ($this->projectPermission->isEverybodyAllowed($project_id)) {
return $this->db
->table(User::TABLE)
->columns(User::TABLE.'.id', User::TABLE.'.username', User::TABLE.'.name', User::TABLE.'.email', User::TABLE.'.language', User::TABLE.'.notifications_filter')
->eq('notifications_enabled', '1')
->neq('email', '')
->neq(User::TABLE.'.id', $exclude_user_id)
->findAll();
}
return $this->db
->table(ProjectPermission::TABLE)
->columns(User::TABLE.'.id', User::TABLE.'.username', User::TABLE.'.name', User::TABLE.'.email', User::TABLE.'.language', User::TABLE.'.notifications_filter')
->join(User::TABLE, 'id', 'user_id')
->eq('project_id', $project_id)
->eq('notifications_enabled', '1')
->neq('email', '')
->neq(User::TABLE.'.id', $exclude_user_id)
->findAll();
}
/**
* Get the mail content for a given template name
*
* @access public
* @param string $event_name Event name
* @param array $event_data Event data
* @return string
*/
public function getMailContent($event_name, array $event_data)
{
return $this->template->render(
'notification/'.str_replace('.', '_', $event_name),
$event_data + array('application_url' => $this->config->get('application_url'))
);
}
/**
* Get the mail subject for a given template name
*
* @access public
* @param string $event_name Event name
* @param array $event_data Event data
* @return string
*/
public function getMailSubject($event_name, array $event_data)
{
switch ($event_name) {
case Task::EVENT_CREATE:
$subject = $this->getStandardMailSubject(e('New attachment'), $event_data);
break;
case Comment::EVENT_CREATE:
$subject = $this->getStandardMailSubject(e('New comment'), $event_data);
break;
case Comment::EVENT_UPDATE:
$subject = $this->getStandardMailSubject(e('Comment updated'), $event_data);
break;
case Subtask::EVENT_CREATE:
$subject = $this->getStandardMailSubject(e('New subtask'), $event_data);
break;
case Subtask::EVENT_UPDATE:
$subject = $this->getStandardMailSubject(e('Subtask updated'), $event_data);
break;
case Task::EVENT_CREATE:
$subject = $this->getStandardMailSubject(e('New task'), $event_data);
break;
case Task::EVENT_UPDATE:
$subject = $this->getStandardMailSubject(e('Task updated'), $event_data);
break;
case Task::EVENT_CLOSE:
$subject = $this->getStandardMailSubject(e('Task closed'), $event_data);
break;
case Task::EVENT_OPEN:
$subject = $this->getStandardMailSubject(e('Task opened'), $event_data);
break;
case Task::EVENT_MOVE_COLUMN:
$subject = $this->getStandardMailSubject(e('Column Change'), $event_data);
break;
case Task::EVENT_MOVE_POSITION:
$subject = $this->getStandardMailSubject(e('Position Change'), $event_data);
break;
case Task::EVENT_ASSIGNEE_CHANGE:
$subject = $this->getStandardMailSubject(e('Assignee Change'), $event_data);
break;
case Task::EVENT_OVERDUE:
$subject = e('[%s] Overdue tasks', $event_data['project_name']);
break;
default:
$subject = e('Notification');
}
return $subject;
}
/**
* Get the mail subject for a given label
*
* @access private
* @param string $label Label
* @param array $data Template data
* @return string
*/
private function getStandardMailSubject($label, array $data)
{
return sprintf('[%s][%s] %s (#%d)', $data['task']['project_name'], $label, $data['task']['title'], $data['task']['id']);
}
/**
* Save settings for the given user
*
* @access public
* @param integer $user_id User id
* @param array $values Form values
*/
public function saveSettings($user_id, array $values)
{
// Delete all selected projects
$this->db->table(self::TABLE)->eq('user_id', $user_id)->remove();
if (isset($values['notifications_enabled']) && $values['notifications_enabled'] == 1) {
// Activate notifications
$this->db->table(User::TABLE)->eq('id', $user_id)->update(array(
'notifications_enabled' => '1',
'notifications_filter' => empty($values['notifications_filter']) ? self::FILTER_BOTH : $values['notifications_filter'],
));
// Save selected projects
if (! empty($values['projects'])) {
foreach ($values['projects'] as $project_id => $checkbox_value) {
$this->db->table(self::TABLE)->insert(array(
'user_id' => $user_id,
'project_id' => $project_id,
));
}
}
}
else {
// Disable notifications
$this->db->table(User::TABLE)->eq('id', $user_id)->update(array(
'notifications_enabled' => '0'
));
}
}
/**
* Read user settings to display the form
*
* @access public
* @param integer $user_id User id
* @return array
*/
public function readSettings($user_id)
{
$values = $this->db->table(User::TABLE)->eq('id', $user_id)->columns('notifications_enabled', 'notifications_filter')->findOne();
$projects = $this->db->table(self::TABLE)->eq('user_id', $user_id)->findAllByColumn('project_id');
foreach ($projects as $project_id) {
$values['project_'.$project_id] = true;
}
return $values;
}
}
|