PluginProbe
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration / 1.91.6
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration v1.91.6
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 / Services / NotificationService.php

NotificationService.php in FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration 1.91.6, at app/Services/NotificationService.php

256 lines 8.2 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\Services;
4
5 use FluentBoards\App\Models\Meta;
6 use FluentBoards\App\Models\Notification;
7 use FluentBoards\App\Models\NotificationUser;
8 use FluentBoards\App\Models\Relation;
9 use FluentBoards\App\Models\Task;
10 use FluentBoards\App\Models\User;
11
12 class NotificationService
13 {
14 public function getAllNotifications($per_page, $page, $action = 'all')
15 {
16 $user = wp_get_current_user();
17
18 $query = Notification::where('object_type', Constant::OBJECT_TYPE_BOARD_NOTIFICATION)
19 ->whereHas('users', function($q) use ($user) {
20 $q->where('user_id', $user->ID);
21 });
22
23 // Add action filter if not 'all'
24 if ($action !== 'all') {
25 $query->where('action', $action);
26 }
27
28 $notifications = $query->with('activitist', 'task')
29 ->orderBy('created_at', 'desc')
30 ->paginate($per_page, ['*'], 'page', $page);
31
32 foreach ($notifications as $notification){
33 $notification->read = $notification->checkReadOrNot();
34 }
35
36 return $notifications;
37 }
38
39 /**
40 * @throws \Exception
41 */
42 public function getAllUnreadNotifications($per_page, $page)
43 {
44 $userId = get_current_user_id();
45 if (!$userId) {
46 throw new \Exception(esc_html__('You are not allowed to do that', 'fluent-boards'), 403);
47 }
48 $unreadNotifications = Notification::where('object_type', Constant::OBJECT_TYPE_BOARD_NOTIFICATION)
49 ->whereHas('users', function($q) use ($userId) {
50 $q->where('user_id', $userId)
51 ->whereNull('marked_read_at');
52 })->with('activitist', 'task')->orderBy('created_at', 'desc')->paginate($per_page, ['*'], 'page', $page);
53 return $unreadNotifications;
54 }
55
56 public function markAllRead()
57 {
58 $user = wp_get_current_user();
59
60 $userNotifications = NotificationUser::where('user_id', $user->ID)
61 ->with('notification')
62 ->get();
63
64 foreach ($userNotifications as $data)
65 {
66 $data->marked_read_at = current_time('mysql');
67 $data->save();
68 }
69 }
70
71 public function markNotificationRead($notificationId)
72 {
73 $user = wp_get_current_user();
74
75 $notification = NotificationUser::where('user_id', $user->ID)
76 ->where('notification_id', $notificationId)
77 ->first();
78 $notification->marked_read_at = current_time('mysql');
79 $notification->save();
80 return $notification;
81 }
82
83 public function newNotificationNumber()
84 {
85 $user = wp_get_current_user();
86
87 $unread_notifications = Notification::query()->where('object_type', Constant::OBJECT_TYPE_BOARD_NOTIFICATION)
88 ->whereHas('users', function($q) use ($user) {
89 $q->where('user_id', $user->ID)
90 ->whereNull('marked_read_at');
91 });
92
93 return $unread_notifications->count();
94 }
95
96 public function isCurrentUserObservingTask($task)
97 {
98 $currentUserId = get_current_user_id();
99 $observers = $task->watchers()->get()->pluck('ID');
100
101 foreach ($observers as $id){
102 if($id == $currentUserId)
103 return true;
104 }
105
106 return false;
107 }
108
109 public function getBoardNotificationSettingsOfUser($id, $userId)
110 {
111 $boardSettings = Relation::where('object_type', Constant::OBJECT_TYPE_BOARD_USER)
112 ->where('object_id', $id)
113 ->where('foreign_id', $userId)
114 ->first();
115
116 return $boardSettings;
117 }
118
119 public function getGlobalNotificationSettingsOfUser($userId)
120 {
121 $settings = Meta::where('object_type', Constant::OBJECT_TYPE_USER)
122 ->where('object_id', $userId)
123 ->where('key', Constant::USER_GLOBAL_NOTIFICATIONS)
124 ->first();
125
126 return $settings;
127 }
128
129 public function updateBoardNotificationSettings($newSettings, $id)
130 {
131 $userId = get_current_user_id();
132 $boardSettings = $this->getBoardNotificationSettingsOfUser($id, $userId);
133 if(empty($boardSettings)){
134 return;
135 }
136 foreach ($newSettings as $index => $setting)
137 {
138 $newSettings[$index] = $setting == 'true' ? true : false;
139 }
140 $boardSettings->preferences = $newSettings;
141 $boardSettings->save();
142
143 }
144
145 public function filterAssigneeToSendEmail($taskId, $emailPurpose){
146 $task = Task::findOrFail($taskId);
147 $watchers = $task->watchers;
148 $currentUserId = get_current_user_id();
149
150 $wathersToSendEmail = array();
151
152 foreach ($watchers as $watcher){
153 if($watcher->ID != $currentUserId)
154 {
155 if($this->checkIfEmailEnable($watcher->ID, $emailPurpose, $task->board_id))
156 {
157 $wathersToSendEmail[] = $watcher->user_email;
158 }
159 }
160 }
161
162 return $wathersToSendEmail;
163 }
164
165 public function checkIfEmailEnable($userId, $emailPurpose, $boardId)
166 {
167 if(
168 $this->checkIfEmailEnabled($boardId, $userId, $emailPurpose)
169 ){
170 return true;
171 }else{
172 return false;
173 }
174 }
175
176 public function checkIfEmailEnabled($boardId, $userId, $purpose)
177 {
178 $boardSettings = $this->getBoardNotificationSettingsOfUser($boardId, $userId);
179
180 if($boardSettings){
181 $preferences = maybe_unserialize($boardSettings['preferences']);
182 if(!array_key_exists($purpose, $preferences)){
183 $preferences[$purpose] = true;
184 $boardSettings->preferences = $preferences;
185 $boardSettings->save();
186 return true;
187 }
188 return $preferences[$purpose];
189 }
190
191 return false;
192 }
193
194 public function checkIfEmailEnabledGlobally($userId, $purpose)
195 {
196 $globalSettings = $this->getGlobalNotificationSettingsOfUser($userId);
197
198 if($globalSettings){
199 $preferences = maybe_unserialize($globalSettings['value']);
200 if(!array_key_exists($purpose, $preferences)){
201 $preferences[$purpose] = true;
202 $globalSettings->preferences = $preferences;
203 $globalSettings->save();
204 return true;
205 }
206 return $preferences[$purpose];
207 }
208
209 return true;
210 }
211
212 public function mentionInComment($comment, $mentionedUserIds)
213 {
214 $uniqueIds = array_unique($mentionedUserIds);
215
216 $uniqueIds = array_filter($uniqueIds, function($value) {
217 return (int)$value !== get_current_user_id();
218 });
219
220 //sending emails to mentioned users
221 $mentionedUserEmails = User::whereIn('ID', $uniqueIds)->pluck('user_email');
222 $this->sendMailAfterMention($comment->id, $mentionedUserEmails);
223
224 //sending desktop notifications
225 do_action('fluent_boards/mention_comment_notification', $comment, $uniqueIds);
226 }
227
228 public function sendMailAfterMention($commentId, $usersToSendEmail)
229 {
230 $current_user_id = get_current_user_id();
231
232 /* this will run in background as soon as possible */
233 /* sending Model or Model Instance won't work here */
234 as_enqueue_async_action('fluent_boards/one_time_schedule_send_email_for_mention', [$commentId, $usersToSendEmail, $current_user_id], 'fluent-boards');
235 }
236
237 public function getUnreadNotificationsOfTasks($task)
238 {
239 $userId = get_current_user_id();
240 if (!$userId) {
241 throw new \Exception(esc_html__('You are not allowed to do that', 'fluent-boards'), 403);
242 }
243 $unreadNotifications = Notification::where('object_type', Constant::OBJECT_TYPE_BOARD_NOTIFICATION)
244 ->whereHas('users', function($q) use ($userId) {
245 $q->where('user_id', $userId)
246 ->whereNull('marked_read_at');
247 })->whereHas('task', function($q) use ($task) {
248 $q->where('id', $task->id);
249 })
250 ->with('activitist')->orderBy('created_at', 'desc')->count();
251 return $unreadNotifications;
252 }
253 }
254
255
256