PluginProbe
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration / 1.30
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration v1.30
2.1.0 2.0.15 2.0.12 2.0.10 2.0.4 2.0.1 2.0.0 1.95.3 1.95.2 1.95 1.91.6 trunk 1.11 1.12 1.13 1.20 1.21 1.22 1.23 1.30 1.31 1.32 1.35 1.40 1.41 All 42 releases
fluent-boards / app / Hooks / Handlers / ScheduleHandler.php

ScheduleHandler.php in FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration 1.30, at app/Hooks/Handlers/ScheduleHandler.php

521 lines 19.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentBoards\App\Hooks\Handlers;
4
5 use FluentBoards\App\Models\Board;
6 use FluentBoards\App\Models\Comment;
7 use FluentBoards\App\Models\Meta;
8 use FluentBoards\App\Models\Task;
9 use FluentBoards\App\Models\User;
10 use FluentBoards\App\Services\Constant;
11 use FluentBoards\App\Services\Helper;
12
13 class ScheduleHandler
14 {
15 public function sendEmailForComment(
16 $commentId,
17 $usersToSendEmail,
18 $current_user_id
19 ) {
20 try {
21 $comment = Comment::find($commentId) ?? null;
22 if ( ! $comment) {
23 return;
24 }
25 if ('comment' != $comment->type) {
26 return;
27 }
28
29 $task = Task::findOrFail($comment->task_id);
30 if ( ! $task) {
31 return;
32 }
33 // $assignees = $task->assignees;
34 $board = $task->board;
35 $page_url = fluent_boards_page_url();
36
37 $user = $task->user($comment->created_by);
38
39 $userData = $this->getUserData($current_user_id);
40
41 $boardUrl = $page_url.'boards/'.$board->id;
42 $taskUrl = $page_url.'boards/'.$board->id.'/tasks/'.$task->id.'-'
43 .substr($task->title, 0, 10);
44
45 $userLinkTag = '<strong>'.htmlspecialchars($user->display_name)
46 .'</strong>';
47 $taskLinkTag = '<a target="_blank" href="'
48 .htmlspecialchars($taskUrl).'">'
49 .htmlspecialchars($task->title).'</a>';
50 $boardLinkTag = '<a target="_blank" href="'
51 .htmlspecialchars($boardUrl).'">'
52 .htmlspecialchars($board->title).'</a>';
53
54 $data = [
55 'body' => 'commented on '.$taskLinkTag.' on '
56 .$boardLinkTag.' board.',
57 'pre_header' => 'New comment added on task',
58 'show_footer' => true,
59 'comment' => $comment->description,
60 'userData' => $userData,
61 'site_url' => site_url(),
62 'site_title' => get_bloginfo('name'),
63 'site_logo' => fluent_boards_site_logo(),
64 ];
65
66 $mailSubject = 'new comment added on task';
67 $message = Helper::loadView('emails.comment2', $data);
68 $headers = ['Content-Type: text/html; charset=UTF-8'];
69
70 foreach ($usersToSendEmail as $email) {
71 \wp_mail($email, $mailSubject, $message, $headers);
72 }
73 } catch (\Exception $e) {
74 // do nothing // better to log here
75 }
76 }
77
78 public function sendEmailForMention($commentId, $usersToSendEmail, $current_user_id)
79 {
80 try {
81 $comment = Comment::find($commentId) ?? null;
82 if ( ! $comment) {
83 return;
84 }
85 if ('comment' != $comment->type) {
86 return;
87 }
88
89 $task = Task::findOrFail($comment->task_id);
90 if ( ! $task) {
91 return;
92 }
93 // $assignees = $task->assignees;
94 $board = $task->board;
95 $page_url = fluent_boards_page_url();
96
97 $user = $task->user($comment->created_by);
98
99 $userData = $this->getUserData($current_user_id);
100
101 $boardUrl = $page_url.'boards/'.$board->id;
102 $taskUrl = $page_url.'boards/'.$board->id.'/tasks/'.$task->id.'-'
103 .substr($task->title, 0, 10);
104
105 $userLinkTag = '<strong>'.htmlspecialchars($user->display_name)
106 .'</strong>';
107 $taskLinkTag = '<a target="_blank" href="'
108 .htmlspecialchars($taskUrl).'">'
109 .htmlspecialchars($task->title).'</a>';
110 $boardLinkTag = '<a target="_blank" href="'
111 .htmlspecialchars($boardUrl).'">'
112 .htmlspecialchars($board->title).'</a>';
113
114 $commentLink = $taskUrl . '?comment='.$comment->id;
115
116 $data = [
117 'body' => 'mentioned you in a comment on '.$taskLinkTag.' on '
118 .$boardLinkTag.' board.',
119 'comment_link' => $commentLink,
120 'pre_header' => 'You are mentioned in a comment',
121 'show_footer' => true,
122 'comment' => $comment->description,
123 'userData' => $userData,
124 'site_url' => site_url(),
125 'site_title' => get_bloginfo('name'),
126 'site_logo' => fluent_boards_site_logo(),
127 ];
128
129 $mailSubject = 'You are mentioned in a comment';
130 $message = Helper::loadView('emails.comment2', $data);
131 $headers = ['Content-Type: text/html; charset=UTF-8'];
132
133 foreach ($usersToSendEmail as $email) {
134 \wp_mail($email, $mailSubject, $message, $headers);
135 }
136 } catch (\Exception $e) {
137 // do nothing // better to log here
138 }
139 }
140
141 public function sendEmailForAddAssignee(
142 $taskId,
143 $newAssigneeId,
144 $current_user_id
145 ) {
146 try {
147 $task = Task::findOrFail($taskId);
148 $board = $task->board;
149 $assignee = User::findOrFail($newAssigneeId);
150
151 $userData = $this->getUserData($current_user_id);
152
153 $page_url = fluent_boards_page_url();
154 $boardUrl = $page_url.'boards/'.$board->id;
155 $boardLinkTag = '<a target="_blank" href="'
156 .htmlspecialchars($boardUrl).'">'
157 .htmlspecialchars($board->title).'</a>';
158
159 if (null == $task->parent_id) {
160 // this is a task
161 $taskUrl = $page_url.'boards/'.$board->id.'/tasks/'
162 .$task->id.'-'.substr($task->title, 0, 10);
163 $taskLinkTag = '<a target="_blank" href="'
164 .htmlspecialchars($taskUrl).'">'
165 .htmlspecialchars($task->title).'</a>';
166 $data = [
167 'body' => 'has assigned you to task '.$taskLinkTag
168 .' on the board '.$boardLinkTag,
169 'pre_header' => 'you have been assigned to task',
170 'show_footer' => true,
171 'userData' => $userData,
172 'site_url' => site_url(),
173 'site_title' => get_bloginfo('name'),
174 'site_logo' => fluent_boards_site_logo(),
175 ];
176 } else {
177 // this is a subtask
178 $task = $task->parentTask($task->parent_id);
179
180 $taskUrl = $page_url.'boards/'.$board->id.'/tasks/'
181 .$task->id.'-'.substr($task->title, 0, 10);
182 $taskLinkTag = '<a target="_blank" href="'
183 .htmlspecialchars($taskUrl).'">'
184 .htmlspecialchars($task->title).'</a>';
185
186 $data = [
187 'body' => 'has assigned you to subtask <strong>'
188 .$task->title.'</strong> of task '
189 .$taskLinkTag.' on the board '
190 .$boardLinkTag,
191 'pre_header' => 'you have been assigned to subtask',
192 'show_footer' => true, 'user' => $assignee,
193 'userData' => $userData,
194 'site_url' => site_url(),
195 'site_title' => get_bloginfo('name'),
196 'site_logo' => fluent_boards_site_logo(),
197 ];
198 }
199
200 $mailSubject = 'You have been assigned to task';
201 $message = Helper::loadView('emails.assignee2', $data);
202 $headers = ['Content-Type: text/html; charset=UTF-8'];
203
204
205 \wp_mail($assignee->user_email, $mailSubject, $message, $headers);
206
207 } catch (\Exception $e) {
208 throw new \Exception('Error in sending mail to new assignees', 1);
209 }
210 }
211
212 public function sendEmailForRemoveAssignee(
213 $taskId,
214 $newAssigneeId,
215 $current_user_id
216 ) {
217 try {
218 $task = Task::findOrFail($taskId);
219 $board = $task->board;
220 $assignee = User::findOrFail($newAssigneeId);
221
222 $userData = $this->getUserData($current_user_id);
223
224 $page_url = fluent_boards_page_url();
225
226 $boardUrl = $page_url.'boards/'.$board->id;
227 $boardLinkTag = '<a target="_blank" href="'
228 .htmlspecialchars($boardUrl).'">'
229 .htmlspecialchars($board->title).'</a>';
230
231 if (null == $task->parent_id) {
232 // this is a task
233 $taskUrl = $page_url.'boards/'.$board->id.'/tasks/'
234 .$task->id.'-'.substr($task->title, 0, 10);
235 $taskLinkTag = '<a target="_blank" href="'
236 .htmlspecialchars($taskUrl).'">'
237 .htmlspecialchars($task->title).'</a>';
238 $data = [
239 'body' => 'has removed you from task '.$taskLinkTag
240 .' on the board '.$boardLinkTag,
241 'pre_header' => 'you have been removed from task',
242 'show_footer' => true,
243 'userData' => $userData,
244 'site_url' => site_url(),
245 'site_title' => get_bloginfo('name'),
246 'site_logo' => fluent_boards_site_logo(),
247 ];
248 } else {
249 // this is a subtask
250 $task = $task->parentTask($task->parent_id);
251
252 $taskUrl = $page_url.'boards/'.$board->id.'/tasks/'
253 .$task->id.'-'.substr($task->title, 0, 10);
254 $taskLinkTag = '<a target="_blank" href="'
255 .htmlspecialchars($taskUrl).'">'
256 .htmlspecialchars($task->title).'</a>';
257
258 $data = [
259 'body' => 'has removed you from subtask <strong>'
260 .$task->title.'</strong> of task '
261 .$taskLinkTag.' on the board '
262 .$boardLinkTag,
263 'pre_header' => 'you have been removed from subtask',
264 'show_footer' => true,
265 'userData' => $userData,
266 ];
267 }
268
269 $mailSubject = 'You have been removed from task';
270 $message = Helper::loadView('emails.assignee2', $data);
271 $headers = ['Content-Type: text/html; charset=UTF-8'];
272
273 \wp_mail($assignee->user_email, $mailSubject, $message, $headers);
274
275 } catch (\Exception $e) {
276 throw new \Exception('Error in sending mail to new assignees', 1);
277 }
278 }
279
280
281 public function sendEmailForChangeStage(
282 $taskId,
283 $newAssigneeEmails,
284 $current_user_id
285 ) {
286 try {
287 $task = Task::findOrFail($taskId);
288 $board = $task->board;
289
290 $userData = $this->getUserData($current_user_id);
291
292 $page_url = fluent_boards_page_url();
293 $boardUrl = $page_url.'boards/'.$board->id;
294 $boardLinkTag = '<a target="_blank" href="'
295 .htmlspecialchars($boardUrl).'">'
296 .htmlspecialchars($board->title).'</a>';
297
298 $taskUrl = $page_url.'boards/'.$board->id.'/tasks/'.$task->id
299 .'-'.substr($task->title, 0, 10);
300 $taskLinkTag = '<a target="_blank" href="'
301 .htmlspecialchars($taskUrl).'">'
302 .htmlspecialchars($task->title).'</a>';
303
304 $data = [
305 'body' => 'has moved '.$taskLinkTag.' task to <strong>'
306 .$task->stage->title
307 .'</strong> stage of board '.$boardLinkTag,
308 'pre_header' => 'Task stage has been changed',
309 'show_footer' => true,
310 'userData' => $userData,
311 'site_url' => site_url(),
312 'site_title' => get_bloginfo('name'),
313 'site_logo' => fluent_boards_site_logo(),
314 ];
315
316
317 $mailSubject = 'Task stage has been changed';
318 $message = Helper::loadView('emails.assignee2', $data);
319 $headers = ['Content-Type: text/html; charset=UTF-8'];
320
321 foreach ($newAssigneeEmails as $assignee_email) {
322 \wp_mail($assignee_email, $mailSubject, $message, $headers);
323 }
324 } catch (\Exception $e) {
325 throw new \Exception('Error in sending mail to new assignees', 1);
326 }
327 }
328
329 public function sendEmailForDueDateUpdate(
330 $taskId,
331 $newAssigneeEmails,
332 $current_user_id
333 ) {
334 try {
335 $task = Task::findOrFail($taskId);
336 $board = $task->board;
337
338 $userData = $this->getUserData($current_user_id);
339
340 $page_url = fluent_boards_page_url();
341 $boardUrl = $page_url.'boards/'.$board->id;
342 $boardLinkTag = '<a target="_blank" href="'
343 .htmlspecialchars($boardUrl).'">'
344 .htmlspecialchars($board->title).'</a>';
345
346 $taskUrl = $page_url.'boards/'.$board->id.'/tasks/'.$task->id
347 .'-'.substr($task->title, 0, 10);
348 $taskLinkTag = '<a target="_blank" href="'
349 .htmlspecialchars($taskUrl).'">'
350 .htmlspecialchars($task->title).'</a>';
351
352 $data = [
353 'body' => 'has updated due date of '.$taskLinkTag
354 .' task to <strong>'.$task->due_at
355 .'</strong> of board '.$boardLinkTag,
356 'pre_header' => 'Task due date has been changed',
357 'show_footer' => true,
358 'userData' => $userData,
359 'site_url' => site_url(),
360 'site_title' => get_bloginfo('name'),
361 'site_logo' => fluent_boards_site_logo(),
362 ];
363
364
365 $mailSubject = 'Task due date has been changed';
366 $message = Helper::loadView('emails.assignee2', $data);
367 $headers = ['Content-Type: text/html; charset=UTF-8'];
368
369 foreach ($newAssigneeEmails as $assignee_email) {
370 \wp_mail($assignee_email, $mailSubject, $message, $headers);
371 }
372 } catch (\Exception $e) {
373 throw new \Exception('Error in sending mail to new assignees', 1);
374 }
375 }
376
377 public function sendEmailForArchivedTask(
378 $taskId,
379 $newAssigneeEmails,
380 $current_user_id
381 ) {
382 try {
383 $task = Task::findOrFail($taskId);
384 $board = $task->board;
385
386 $userData = $this->getUserData($current_user_id);
387
388 $page_url = fluent_boards_page_url();
389 $boardUrl = $page_url.'boards/'.$board->id;
390 $boardLinkTag = '<a target="_blank" href="'
391 .htmlspecialchars($boardUrl).'">'
392 .htmlspecialchars($board->title).'</a>';
393
394 $taskUrl = $page_url.'boards/'.$board->id.'/tasks/'.$task->id
395 .'-'.substr($task->title, 0, 10);
396 $taskLinkTag = '<a target="_blank" href="'
397 .htmlspecialchars($taskUrl).'">'
398 .htmlspecialchars($task->title).'</a>';
399
400 $data = [
401 'body' => 'has archived '.$taskLinkTag.' task of board '
402 .$boardLinkTag,
403 'pre_header' => 'Task has been archived',
404 'show_footer' => true,
405 'userData' => $userData,
406 'site_url' => site_url(),
407 'site_title' => get_bloginfo('name'),
408 'site_logo' => fluent_boards_site_logo(),
409 ];
410
411
412 $mailSubject = 'Task has been archived';
413 $message = Helper::loadView('emails.assignee2', $data);
414 $headers = ['Content-Type: text/html; charset=UTF-8'];
415
416 foreach ($newAssigneeEmails as $assignee_email) {
417 \wp_mail($assignee_email, $mailSubject, $message, $headers);
418 }
419 } catch (\Exception $e) {
420 throw new \Exception('Error in sending mail to new assignees', 1);
421 }
422 }
423
424 private function getUserData($userId)
425 {
426 $currentUser = User::findOrFail($userId);
427 $gravaterPhoto = fluent_boards_user_avatar($currentUser->user_email,
428 $currentUser->display_name);
429
430 return [
431 'display_name' => $currentUser->display_name,
432 'photo' => $gravaterPhoto,
433 ];
434 }
435
436 public function sendInvitationViaEmail($boardId, $email, $current_user_id)
437 {
438 try {
439 $userData = $this->getUserData($current_user_id);
440
441 $board = Board::findOrFail($boardId) ?? null;
442 if (!$board) {
443 return;
444 }
445
446 $page_url = fluent_boards_page_url();
447
448 $hashCode = $this->hashGenerate(20);
449
450 $siteUrl = add_query_arg( array(
451 'fbs' => 1,
452 'invitation' => 'board',
453 'email' => $email,
454 'hash' => $hashCode,
455 'bid' => $boardId
456 ), site_url('index.php') );
457
458 $data = [
459 'body' => 'has invited you to join board: '. $board->title ,
460 'pre_header' => __('join board invitation in fluent boards', 'fluent-boards'),
461 'btn_title' => __('Join Board', 'fluent-boards'),
462 'show_footer' => true,
463 'userData' => $userData,
464 'boardLink' => $siteUrl,
465 'site_url' => site_url(),
466 'site_title' => get_bloginfo('name'),
467 'site_logo' => fluent_boards_site_logo(),
468 ];
469
470 $mailSubject = 'Invitation for joining board';
471 $message = Helper::loadView('emails.invite-external', $data);
472 $headers = ['Content-Type: text/html; charset=UTF-8'];
473
474 $this->saveHashByEmail($boardId, $email, $hashCode);
475
476 \wp_mail($email, $mailSubject, $message, $headers);
477 } catch (\Exception $e) {
478 // do nothing // better to log here
479 }
480 }
481
482 private function hashGenerate($chars)
483 {
484 $data = '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcefghijklmnopqrstuvwxyz';
485 return substr(str_shuffle($data), 0, $chars);
486 }
487
488 private function saveHashByEmail($boardId, $email, $hash)
489 {
490 $this->deleteHashCode($boardId, $email);
491
492 $fbs_meta = new Meta();
493 $fbs_meta->object_id = $boardId;
494 $fbs_meta->object_type = Constant::OBJECT_TYPE_BOARD;
495 $fbs_meta->key = Constant::BOARD_INVITATION;
496 $fbs_meta->value = ['email' => $email, 'hash' => $hash];
497 $fbs_meta->save();
498 }
499
500 private function deleteHashCode($boardId, $email)
501 {
502 $activeHashCodes = $this->getActiveHashCodes($boardId);
503
504 foreach ($activeHashCodes as $savedHash) {
505 $value = maybe_unserialize($savedHash->value);
506 if($value['email'] == $email){
507 Meta::where('id', $savedHash->id)->delete();
508 }
509 }
510 }
511
512 private function getActiveHashCodes($boardId)
513 {
514 return Meta::query()->where('object_id', $boardId)
515 ->where('object_type', Constant::OBJECT_TYPE_BOARD)
516 ->where('key', Constant::BOARD_INVITATION)
517 ->get();
518 }
519
520 }
521