PluginProbe
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration / 1.13
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration v1.13
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.13, at app/Hooks/Handlers/ScheduleHandler.php

458 lines 17.4 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' => 'comment added in fluent 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 sendEmailForAddAssignee(
79 $taskId,
80 $newAssigneeId,
81 $current_user_id
82 ) {
83 try {
84 $task = Task::findOrFail($taskId);
85 $board = $task->board;
86 $assignee = User::findOrFail($newAssigneeId);
87
88 $userData = $this->getUserData($current_user_id);
89
90 $page_url = fluent_boards_page_url();
91 $boardUrl = $page_url.'boards/'.$board->id;
92 $boardLinkTag = '<a target="_blank" href="'
93 .htmlspecialchars($boardUrl).'">'
94 .htmlspecialchars($board->title).'</a>';
95
96 if (null == $task->parent_id) {
97 // this is a task
98 $taskUrl = $page_url.'boards/'.$board->id.'/tasks/'
99 .$task->id.'-'.substr($task->title, 0, 10);
100 $taskLinkTag = '<a target="_blank" href="'
101 .htmlspecialchars($taskUrl).'">'
102 .htmlspecialchars($task->title).'</a>';
103 $data = [
104 'body' => 'has assigned you to task '.$taskLinkTag
105 .' on the board '.$boardLinkTag,
106 'pre_header' => 'you have been assigned to task',
107 'show_footer' => true,
108 'userData' => $userData,
109 'site_url' => site_url(),
110 'site_title' => get_bloginfo('name'),
111 'site_logo' => fluent_boards_site_logo(),
112 ];
113 } else {
114 // this is a subtask
115 $task = $task->parentTask($task->parent_id);
116
117 $taskUrl = $page_url.'boards/'.$board->id.'/tasks/'
118 .$task->id.'-'.substr($task->title, 0, 10);
119 $taskLinkTag = '<a target="_blank" href="'
120 .htmlspecialchars($taskUrl).'">'
121 .htmlspecialchars($task->title).'</a>';
122
123 $data = [
124 'body' => 'has assigned you to subtask <strong>'
125 .$task->title.'</strong> of task '
126 .$taskLinkTag.' on the board '
127 .$boardLinkTag,
128 'pre_header' => 'you have been assigned to subtask',
129 'show_footer' => true, 'user' => $assignee,
130 'userData' => $userData,
131 'site_url' => site_url(),
132 'site_title' => get_bloginfo('name'),
133 'site_logo' => fluent_boards_site_logo(),
134 ];
135 }
136
137 $mailSubject = 'You have been assigned to task';
138 $message = Helper::loadView('emails.assignee2', $data);
139 $headers = ['Content-Type: text/html; charset=UTF-8'];
140
141
142 \wp_mail($assignee->user_email, $mailSubject, $message, $headers);
143
144 } catch (\Exception $e) {
145 throw new \Exception('Error in sending mail to new assignees', 1);
146 }
147 }
148
149 public function sendEmailForRemoveAssignee(
150 $taskId,
151 $newAssigneeId,
152 $current_user_id
153 ) {
154 try {
155 $task = Task::findOrFail($taskId);
156 $board = $task->board;
157 $assignee = User::findOrFail($newAssigneeId);
158
159 $userData = $this->getUserData($current_user_id);
160
161 $page_url = fluent_boards_page_url();
162
163 $boardUrl = $page_url.'boards/'.$board->id;
164 $boardLinkTag = '<a target="_blank" href="'
165 .htmlspecialchars($boardUrl).'">'
166 .htmlspecialchars($board->title).'</a>';
167
168 if (null == $task->parent_id) {
169 // this is a task
170 $taskUrl = $page_url.'boards/'.$board->id.'/tasks/'
171 .$task->id.'-'.substr($task->title, 0, 10);
172 $taskLinkTag = '<a target="_blank" href="'
173 .htmlspecialchars($taskUrl).'">'
174 .htmlspecialchars($task->title).'</a>';
175 $data = [
176 'body' => 'has removed you from task '.$taskLinkTag
177 .' on the board '.$boardLinkTag,
178 'pre_header' => 'you have been removed from task',
179 'show_footer' => true,
180 'userData' => $userData,
181 'site_url' => site_url(),
182 'site_title' => get_bloginfo('name'),
183 'site_logo' => fluent_boards_site_logo(),
184 ];
185 } else {
186 // this is a subtask
187 $task = $task->parentTask($task->parent_id);
188
189 $taskUrl = $page_url.'boards/'.$board->id.'/tasks/'
190 .$task->id.'-'.substr($task->title, 0, 10);
191 $taskLinkTag = '<a target="_blank" href="'
192 .htmlspecialchars($taskUrl).'">'
193 .htmlspecialchars($task->title).'</a>';
194
195 $data = [
196 'body' => 'has removed you from subtask <strong>'
197 .$task->title.'</strong> of task '
198 .$taskLinkTag.' on the board '
199 .$boardLinkTag,
200 'pre_header' => 'you have been removed from subtask',
201 'show_footer' => true,
202 'userData' => $userData,
203 ];
204 }
205
206 $mailSubject = 'You have been removed from task';
207 $message = Helper::loadView('emails.assignee2', $data);
208 $headers = ['Content-Type: text/html; charset=UTF-8'];
209
210 \wp_mail($assignee->user_email, $mailSubject, $message, $headers);
211
212 } catch (\Exception $e) {
213 throw new \Exception('Error in sending mail to new assignees', 1);
214 }
215 }
216
217
218 public function sendEmailForChangeStage(
219 $taskId,
220 $newAssigneeEmails,
221 $current_user_id
222 ) {
223 try {
224 $task = Task::findOrFail($taskId);
225 $board = $task->board;
226
227 $userData = $this->getUserData($current_user_id);
228
229 $page_url = fluent_boards_page_url();
230 $boardUrl = $page_url.'boards/'.$board->id;
231 $boardLinkTag = '<a target="_blank" href="'
232 .htmlspecialchars($boardUrl).'">'
233 .htmlspecialchars($board->title).'</a>';
234
235 $taskUrl = $page_url.'boards/'.$board->id.'/tasks/'.$task->id
236 .'-'.substr($task->title, 0, 10);
237 $taskLinkTag = '<a target="_blank" href="'
238 .htmlspecialchars($taskUrl).'">'
239 .htmlspecialchars($task->title).'</a>';
240
241 $data = [
242 'body' => 'has moved '.$taskLinkTag.' task to <strong>'
243 .$task->stage->title
244 .'</strong> stage of board '.$boardLinkTag,
245 'pre_header' => 'Task stage has been changed',
246 'show_footer' => true,
247 'userData' => $userData,
248 'site_url' => site_url(),
249 'site_title' => get_bloginfo('name'),
250 'site_logo' => fluent_boards_site_logo(),
251 ];
252
253
254 $mailSubject = 'Task stage has been changed';
255 $message = Helper::loadView('emails.assignee2', $data);
256 $headers = ['Content-Type: text/html; charset=UTF-8'];
257
258 foreach ($newAssigneeEmails as $assignee_email) {
259 \wp_mail($assignee_email, $mailSubject, $message, $headers);
260 }
261 } catch (\Exception $e) {
262 throw new \Exception('Error in sending mail to new assignees', 1);
263 }
264 }
265
266 public function sendEmailForDueDateUpdate(
267 $taskId,
268 $newAssigneeEmails,
269 $current_user_id
270 ) {
271 try {
272 $task = Task::findOrFail($taskId);
273 $board = $task->board;
274
275 $userData = $this->getUserData($current_user_id);
276
277 $page_url = fluent_boards_page_url();
278 $boardUrl = $page_url.'boards/'.$board->id;
279 $boardLinkTag = '<a target="_blank" href="'
280 .htmlspecialchars($boardUrl).'">'
281 .htmlspecialchars($board->title).'</a>';
282
283 $taskUrl = $page_url.'boards/'.$board->id.'/tasks/'.$task->id
284 .'-'.substr($task->title, 0, 10);
285 $taskLinkTag = '<a target="_blank" href="'
286 .htmlspecialchars($taskUrl).'">'
287 .htmlspecialchars($task->title).'</a>';
288
289 $data = [
290 'body' => 'has updated due date of '.$taskLinkTag
291 .' task to <strong>'.$task->due_at
292 .'</strong> of board '.$boardLinkTag,
293 'pre_header' => 'Task due date has been changed',
294 'show_footer' => true,
295 'userData' => $userData,
296 'site_url' => site_url(),
297 'site_title' => get_bloginfo('name'),
298 'site_logo' => fluent_boards_site_logo(),
299 ];
300
301
302 $mailSubject = 'Task due date has been changed';
303 $message = Helper::loadView('emails.assignee2', $data);
304 $headers = ['Content-Type: text/html; charset=UTF-8'];
305
306 foreach ($newAssigneeEmails as $assignee_email) {
307 \wp_mail($assignee_email, $mailSubject, $message, $headers);
308 }
309 } catch (\Exception $e) {
310 throw new \Exception('Error in sending mail to new assignees', 1);
311 }
312 }
313
314 public function sendEmailForArchivedTask(
315 $taskId,
316 $newAssigneeEmails,
317 $current_user_id
318 ) {
319 try {
320 $task = Task::findOrFail($taskId);
321 $board = $task->board;
322
323 $userData = $this->getUserData($current_user_id);
324
325 $page_url = fluent_boards_page_url();
326 $boardUrl = $page_url.'boards/'.$board->id;
327 $boardLinkTag = '<a target="_blank" href="'
328 .htmlspecialchars($boardUrl).'">'
329 .htmlspecialchars($board->title).'</a>';
330
331 $taskUrl = $page_url.'boards/'.$board->id.'/tasks/'.$task->id
332 .'-'.substr($task->title, 0, 10);
333 $taskLinkTag = '<a target="_blank" href="'
334 .htmlspecialchars($taskUrl).'">'
335 .htmlspecialchars($task->title).'</a>';
336
337 $data = [
338 'body' => 'has archived '.$taskLinkTag.' task of board '
339 .$boardLinkTag,
340 'pre_header' => 'Task has been archived',
341 'show_footer' => true,
342 'userData' => $userData,
343 'site_url' => site_url(),
344 'site_title' => get_bloginfo('name'),
345 'site_logo' => fluent_boards_site_logo(),
346 ];
347
348
349 $mailSubject = 'Task has been archived';
350 $message = Helper::loadView('emails.assignee2', $data);
351 $headers = ['Content-Type: text/html; charset=UTF-8'];
352
353 foreach ($newAssigneeEmails as $assignee_email) {
354 \wp_mail($assignee_email, $mailSubject, $message, $headers);
355 }
356 } catch (\Exception $e) {
357 throw new \Exception('Error in sending mail to new assignees', 1);
358 }
359 }
360
361 private function getUserData($userId)
362 {
363 $currentUser = User::findOrFail($userId);
364 $gravaterPhoto = fluent_boards_user_avatar($currentUser->user_email,
365 $currentUser->display_name);
366
367 return [
368 'display_name' => $currentUser->display_name,
369 'photo' => $gravaterPhoto,
370 ];
371 }
372
373 public function sendInvitationViaEmail($boardId, $email, $current_user_id)
374 {
375 try {
376 $userData = $this->getUserData($current_user_id);
377
378 $board = Board::findOrFail($boardId) ?? null;
379 if (!$board) {
380 return;
381 }
382
383 $page_url = fluent_boards_page_url();
384
385 $hashCode = $this->hashGenerate(20);
386
387 $siteUrl = add_query_arg( array(
388 'fbs' => 1,
389 'invitation' => 'board',
390 'email' => $email,
391 'hash' => $hashCode,
392 'bid' => $boardId
393 ), site_url('index.php') );
394
395 $data = [
396 'body' => 'has invited you to join board: '. $board->title ,
397 'pre_header' => __('join board invitation in fluent boards', 'fluent-boards'),
398 'btn_title' => __('Join Board', 'fluent-boards'),
399 'show_footer' => true,
400 'userData' => $userData,
401 'boardLink' => $siteUrl,
402 'site_url' => site_url(),
403 'site_title' => get_bloginfo('name'),
404 'site_logo' => fluent_boards_site_logo(),
405 ];
406
407 $mailSubject = 'Invitation for joining board';
408 $message = Helper::loadView('emails.invite-external', $data);
409 $headers = ['Content-Type: text/html; charset=UTF-8'];
410
411 $this->saveHashByEmail($boardId, $email, $hashCode);
412
413 \wp_mail($email, $mailSubject, $message, $headers);
414 } catch (\Exception $e) {
415 // do nothing // better to log here
416 }
417 }
418
419 private function hashGenerate($chars)
420 {
421 $data = '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcefghijklmnopqrstuvwxyz';
422 return substr(str_shuffle($data), 0, $chars);
423 }
424
425 private function saveHashByEmail($boardId, $email, $hash)
426 {
427 $this->deleteHashCode($boardId, $email);
428
429 $fbs_meta = new Meta();
430 $fbs_meta->object_id = $boardId;
431 $fbs_meta->object_type = Constant::OBJECT_TYPE_BOARD;
432 $fbs_meta->key = Constant::BOARD_INVITATION;
433 $fbs_meta->value = ['email' => $email, 'hash' => $hash];
434 $fbs_meta->save();
435 }
436
437 private function deleteHashCode($boardId, $email)
438 {
439 $activeHashCodes = $this->getActiveHashCodes($boardId);
440
441 foreach ($activeHashCodes as $savedHash) {
442 $value = maybe_unserialize($savedHash->value);
443 if($value['email'] == $email){
444 Meta::where('id', $savedHash->id)->delete();
445 }
446 }
447 }
448
449 private function getActiveHashCodes($boardId)
450 {
451 return Meta::query()->where('object_id', $boardId)
452 ->where('object_type', Constant::OBJECT_TYPE_BOARD)
453 ->where('key', Constant::BOARD_INVITATION)
454 ->get();
455 }
456
457 }
458