PluginProbe
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration / 1.21
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration v1.21
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 1.45 All 41 releases
fluent-boards / app / Services / NotificationService.php

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

204 lines 6.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
11 class NotificationService
12 {
13 public function getAllNotifications($per_page, $page)
14 {
15 $user = wp_get_current_user();
16
17 $notifications = Notification::where('object_type', Constant::OBJECT_TYPE_BOARD_NOTIFICATION)
18 ->whereHas('users', function($q) use ($user) {
19 $q->where('user_id', $user->ID);
20 })->with('activitist', 'task')->orderBy('created_at', 'desc')->paginate($per_page, ['*'], 'page', $page);
21
22 foreach ($notifications as $notification){
23 $notification->read = $notification->checkReadOrNot();
24 }
25
26 return $notifications;
27 }
28
29 /**
30 * @throws \Exception
31 */
32 public function getAllUnreadNotifications($per_page, $page)
33 {
34 $userId = get_current_user_id();
35 if (!$userId) {
36 throw new \Exception('You are not allowed to do that', 403);
37 }
38 $unreadNotifications = Notification::where('object_type', Constant::OBJECT_TYPE_BOARD_NOTIFICATION)
39 ->whereHas('users', function($q) use ($userId) {
40 $q->where('user_id', $userId)
41 ->whereNull('marked_read_at');
42 })->with('activitist', 'task')->orderBy('created_at', 'desc')->paginate($per_page, ['*'], 'page', $page);
43 return $unreadNotifications;
44 }
45
46 public function markAllRead()
47 {
48 $user = wp_get_current_user();
49
50 $userNotifications = NotificationUser::where('user_id', $user->ID)
51 ->with('notification')
52 ->get();
53
54 foreach ($userNotifications as $data)
55 {
56 $data->marked_read_at = current_time('mysql');
57 $data->save();
58 }
59 }
60
61 public function markNotificationRead($notificationId)
62 {
63 $user = wp_get_current_user();
64
65 $notification = NotificationUser::where('user_id', $user->ID)
66 ->where('notification_id', $notificationId)
67 ->first();
68 $notification->marked_read_at = current_time('mysql');
69 $notification->save();
70 return $notification;
71 }
72
73 public function newNotificationNumber()
74 {
75 $user = wp_get_current_user();
76
77 $unread_notifications = Notification::query()->where('object_type', Constant::OBJECT_TYPE_BOARD_NOTIFICATION)
78 ->whereHas('users', function($q) use ($user) {
79 $q->where('user_id', $user->ID)
80 ->whereNull('marked_read_at');
81 });
82
83 return $unread_notifications->count();
84 }
85
86 public function isCurrentUserObservingTask($task)
87 {
88 $currentUserId = get_current_user_id();
89 $observers = $task->watchers()->get()->pluck('ID');
90
91 foreach ($observers as $id){
92 if($id == $currentUserId)
93 return true;
94 }
95
96 return false;
97 }
98
99 public function getBoardNotificationSettingsOfUser($id, $userId)
100 {
101 $boardSettings = Relation::where('object_type', Constant::OBJECT_TYPE_BOARD_USER)
102 ->where('object_id', $id)
103 ->where('foreign_id', $userId)
104 ->first();
105
106 return $boardSettings;
107 }
108
109 public function getGlobalNotificationSettingsOfUser($userId)
110 {
111 $settings = Meta::where('object_type', Constant::OBJECT_TYPE_USER)
112 ->where('object_id', $userId)
113 ->where('key', Constant::USER_GLOBAL_NOTIFICATIONS)
114 ->first();
115
116 return $settings;
117 }
118
119 public function updateBoardNotificationSettings($newSettings, $id)
120 {
121 $userId = get_current_user_id();
122 $boardSettings = $this->getBoardNotificationSettingsOfUser($id, $userId);
123 if(empty($boardSettings)){
124 return;
125 }
126 foreach ($newSettings as $index => $setting)
127 {
128 $newSettings[$index] = $setting == 'true' ? true : false;
129 }
130 $boardSettings->preferences = $newSettings;
131 $boardSettings->save();
132
133 }
134
135 public function filterAssigneeToSendEmail($taskId, $emailPurpose){
136 $task = Task::findOrFail($taskId);
137 $watchers = $task->watchers;
138 $currentUserId = get_current_user_id();
139
140 $wathersToSendEmail = array();
141
142 foreach ($watchers as $watcher){
143 if($watcher->ID != $currentUserId)
144 {
145 if($this->checkIfEmailEnable($watcher->ID, $emailPurpose, $task->board_id))
146 {
147 $wathersToSendEmail[] = $watcher->user_email;
148 }
149 }
150 }
151
152 return $wathersToSendEmail;
153 }
154
155 public function checkIfEmailEnable($userId, $emailPurpose, $boardId)
156 {
157 if(
158 $this->checkIfEmailEnabled($boardId, $userId, $emailPurpose)
159 ){
160 return true;
161 }else{
162 return false;
163 }
164 }
165
166 public function checkIfEmailEnabled($boardId, $userId, $purpose)
167 {
168 $boardSettings = $this->getBoardNotificationSettingsOfUser($boardId, $userId);
169
170 if($boardSettings){
171 $preferences = maybe_unserialize($boardSettings['preferences']);
172 if(!array_key_exists($purpose, $preferences)){
173 $preferences[$purpose] = true;
174 $boardSettings->preferences = $preferences;
175 $boardSettings->save();
176 return true;
177 }
178 return $preferences[$purpose];
179 }
180
181 return false;
182 }
183
184 public function checkIfEmailEnabledGlobally($userId, $purpose)
185 {
186 $globalSettings = $this->getGlobalNotificationSettingsOfUser($userId);
187
188 if($globalSettings){
189 $preferences = maybe_unserialize($globalSettings['value']);
190 if(!array_key_exists($purpose, $preferences)){
191 $preferences[$purpose] = true;
192 $globalSettings->preferences = $preferences;
193 $globalSettings->save();
194 return true;
195 }
196 return $preferences[$purpose];
197 }
198
199 return true;
200 }
201 }
202
203
204