PluginProbe
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration / 2.0.0
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration v2.0.0
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 / NotificationHandler.php

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

241 lines 9.9 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\Task;
6 use FluentBoards\App\Models\User;
7 use FluentBoards\App\Models\Board;
8 use FluentBoards\App\Models\Notification;
9 use FluentBoards\App\Services\Constant;
10
11 class NotificationHandler
12 {
13 /**
14 * Add a generic comment notification for watchers who were not explicitly mentioned.
15 *
16 * Mentioned users receive the higher-priority mention notification separately, so they
17 * should not also receive a generic comment notification for the same comment.
18 *
19 * @param \FluentBoards\App\Models\Comment $comment
20 * @return void
21 */
22 public function addCommentNotification($comment)
23 {
24 if($comment->task_id){
25 $task = Task::findOrFail($comment->task_id);
26 $mentionedUserIds = $this->getMentionedUserIdsFromComment($comment);
27 $userIdsWhoGetNotification = $this->findUsersWhoWillGetNotification($task);
28 $userIdsWhoGetNotification = $this->excludeMentionedUsersFromNotificationRecipients($userIdsWhoGetNotification, $mentionedUserIds);
29 if(count($userIdsWhoGetNotification) > 0){
30 $plainDescription = wp_strip_all_tags($comment->description);
31 $action = $comment->parent_id ? 'task_reply_added' : 'comment_created';
32 $message = $plainDescription;
33 $settings = [ 'comment_id' => $comment->parent_id ?? $comment->id ];
34 $notification = $this->createNotification($task, Constant::OBJECT_TYPE_BOARD_NOTIFICATION, $action, $message, $settings);
35 $notification->users()->attach($userIdsWhoGetNotification);
36 }
37 }
38 }
39
40 /**
41 * Get normalized mentioned user IDs from a comment's stored settings.
42 *
43 * @param \FluentBoards\App\Models\Comment $comment
44 * @return array
45 */
46 private function getMentionedUserIdsFromComment($comment)
47 {
48 $settings = $comment->settings;
49
50 if (!is_array($settings) || empty($settings['mentioned_id']) || !is_array($settings['mentioned_id'])) {
51 return [];
52 }
53
54 return array_values(array_filter(array_map('intval', $settings['mentioned_id'])));
55 }
56
57 /**
58 * Remove mentioned users from generic notification recipients.
59 *
60 * @param array $recipientIds
61 * @param array $mentionedUserIds
62 * @return array
63 */
64 private function excludeMentionedUsersFromNotificationRecipients($recipientIds, $mentionedUserIds)
65 {
66 $mentionedUserIds = array_filter(array_map('intval', $mentionedUserIds));
67
68 if (!$mentionedUserIds) {
69 return array_values($recipientIds);
70 }
71
72 return array_values(array_filter($recipientIds, function ($recipientId) use ($mentionedUserIds) {
73 return !in_array((int) $recipientId, $mentionedUserIds, true);
74 }));
75 }
76
77 public function mentionInCommentNotification($comment, $mentionIds)
78 {
79 if($comment){
80 $task = Task::findOrFail($comment->task_id);
81 $plainDescription = wp_strip_all_tags($comment->description);
82 $action = 'task_comment_mentioned';
83 $message = $plainDescription;
84 $settings = [ 'comment_id' => $comment->parent_id ?? $comment->id ];
85 $notification = $this->createNotification($task, Constant::OBJECT_TYPE_BOARD_NOTIFICATION, $action, $message, $settings);
86 $notification->users()->attach($mentionIds);
87 }
88 }
89
90 public function addSubtaskNotification($parentTask, $subTask)
91 {
92 $userIdsWhoGetNotification = $this->findUsersWhoWillGetNotification($parentTask);
93 if(count($userIdsWhoGetNotification) > 0){
94 $board_id = $subTask->board_id;
95 $action = 'subtask_added';
96 $message = $subTask->title ;
97 $notification = $this->createNotification($parentTask, Constant::OBJECT_TYPE_BOARD_NOTIFICATION, $action, $message);
98 $notification->users()->attach($userIdsWhoGetNotification);
99 }
100 }
101
102 public function changeDueDateNotification($task, $oldDate)
103 {
104 $userIdsWhoGetNotification = $this->findUsersWhoWillGetNotification($task);
105 if(count($userIdsWhoGetNotification) > 0){
106 $action = 'task_due_date_changed';
107 $message = date_i18n('F j, Y, g:i a', strtotime($task->due_at));
108 $notification = $this->createNotification($task, Constant::OBJECT_TYPE_BOARD_NOTIFICATION, $action, $message);
109 $notification->users()->attach($userIdsWhoGetNotification);
110 }
111 }
112
113 public function changeStartDateNotification($task, $oldDate)
114 {
115 $userIdsWhoGetNotification = $this->findUsersWhoWillGetNotification($task);
116 if(count($userIdsWhoGetNotification) > 0){
117 $action = 'task_start_date_changed';
118 $message = date_i18n('F j, Y, g:i a', strtotime($task->started_at));
119 $notification = $this->createNotification($task, Constant::OBJECT_TYPE_BOARD_NOTIFICATION, $action, $message);
120 $notification->users()->attach($userIdsWhoGetNotification);
121 }
122 }
123
124 public function changeStageNotification($task, $oldStageId)
125 {
126 $userIdsWhoGetNotification = $this->findUsersWhoWillGetNotification($task);
127 if(count($userIdsWhoGetNotification) > 0){
128 $action = 'task_moved_to_new_stage';
129 $message = $task->stage->title;
130 $notification = $this->createNotification($task, Constant::OBJECT_TYPE_BOARD_NOTIFICATION, $action, $message);
131 $notification->users()->attach($userIdsWhoGetNotification);
132 }
133 }
134
135 public function changePriorityNotification($task, $oldPriority)
136 {
137 $userIdsWhoGetNotification = $this->findUsersWhoWillGetNotification($task);
138 if(count($userIdsWhoGetNotification) > 0){
139 $action = 'task_priority_changed';
140 $message = $task->priority;
141 $notification = $this->createNotification($task, Constant::OBJECT_TYPE_BOARD_NOTIFICATION, $action, $message);
142 $notification->users()->attach($userIdsWhoGetNotification);
143 }
144 }
145
146 public function taskArchiveNotification($task)
147 {
148 $userIdsWhoGetNotification = $this->findUsersWhoWillGetNotification($task);
149 if(count($userIdsWhoGetNotification) > 0){
150 if ($task->archived_at) {
151 $action = 'board_task_archived';
152 $message = $task->title;
153 } else {
154 $action = 'board_task_restored';
155 $message = $task->title;
156 }
157 $notification = $this->createNotification($task, Constant::OBJECT_TYPE_BOARD_NOTIFICATION, $action, $message);
158 $notification->users()->attach($userIdsWhoGetNotification);
159 }
160 }
161
162 public function changeTitleOrDescriptionNotification($task, $col, $oldTask)
163 {
164 $userIdsWhoGetNotification = $this->findUsersWhoWillGetNotification($task);
165 if(count($userIdsWhoGetNotification) > 0){
166 if($col == 'title'){
167 $action = 'task_title_updated';
168 $message = $task->title;
169 }else{
170 $action = 'task_description_updated';
171 $message = $task->description;
172 }
173 $notification = $this->createNotification($task, Constant::OBJECT_TYPE_BOARD_NOTIFICATION, $action, $message);
174 $notification->users()->attach($userIdsWhoGetNotification);
175 }
176 }
177
178 public function changeBoardNotification($task, $oldBoardId)
179 {
180 $userIdsWhoGetNotification = $this->findUsersWhoWillGetNotification($task);
181 if(count($userIdsWhoGetNotification) > 0){
182 $new_board_id = Task::findOrFail($task->board_id)->board_id;
183 $new_board_title = Board::findOrFail($new_board_id)->title;
184 // translators: %1$s is the task title, %2$s is the board title
185 $message = sprintf(__('moved %1$s task to %2$s board.','fluent-boards'), $task->title, $new_board_title);
186 $notification = $this->createNotification($oldBoardId, Constant::OBJECT_TYPE_BOARD_NOTIFICATION, $message);
187 $notification->users()->attach($userIdsWhoGetNotification);
188 }
189 }
190
191 public function assigneeAddedNotification($task, $newAssigneeId)
192 {
193 if($newAssigneeId != get_current_user_id()){
194 $action = 'task_assignee_changed';
195 $message = __('has added you as an assignee.','fluent-boards');
196 $notification = $this->createNotification($task, Constant::OBJECT_TYPE_BOARD_NOTIFICATION, $action, $message);
197 $notification->users()->attach($newAssigneeId);
198 }
199 }
200
201 public function assigneeRemovedNotification($task, $newAssigneeId)
202 {
203 if($newAssigneeId != get_current_user_id()){
204 $action = 'task_assignee_changed';
205 $message = __('has removed you as an assignee.','fluent-boards');
206 $notification = $this->createNotification($task, Constant::OBJECT_TYPE_BOARD_NOTIFICATION, $action, $message);
207 $notification->users()->attach($newAssigneeId);
208 }
209 }
210
211 public function createNotification($task, $objectType, $action, $description, $settings = null)
212 {
213 $data = [
214 'object_id' => $task->board_id,
215 'object_type' => $objectType,
216 'task_id' => $task->id,
217 'action' => $action,
218 'description' => $description,
219 'settings' => $settings
220 ];
221 $currentUser = get_current_user_id();
222 if($currentUser == 0) {
223 $data['activity_by'] = $task->created_by;
224 }
225 return Notification::create($data);
226 }
227
228 public function findUsersWhoWillGetNotification($task)
229 {
230 $currentUserId = get_current_user_id();
231 $watchersOfTask = $task->watchers;
232 $watchersExceptCurrentUser = [];
233 foreach($watchersOfTask as $watcher){
234 if($watcher->ID != $currentUserId){
235 $watchersExceptCurrentUser[] = $watcher->ID;
236 }
237 }
238 return $watchersExceptCurrentUser;
239 }
240 }
241