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

201 lines 6.1 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 foreach ($newSettings as $index => $setting)
124 {
125 $newSettings[$index] = $setting == 'true' ? true : false;
126 }
127 $boardSettings->preferences = $newSettings;
128 $boardSettings->save();
129
130 }
131
132 public function filterAssigneeToSendEmail($taskId, $emailPurpose){
133 $task = Task::findOrFail($taskId);
134 $watchers = $task->watchers;
135 $currentUserId = get_current_user_id();
136
137 $wathersToSendEmail = array();
138
139 foreach ($watchers as $watcher){
140 if($watcher->ID != $currentUserId)
141 {
142 if($this->checkIfEmailEnable($watcher->ID, $emailPurpose, $task->board_id))
143 {
144 $wathersToSendEmail[] = $watcher->user_email;
145 }
146 }
147 }
148
149 return $wathersToSendEmail;
150 }
151
152 public function checkIfEmailEnable($userId, $emailPurpose, $boardId)
153 {
154 if(
155 $this->checkIfEmailEnabled($boardId, $userId, $emailPurpose)
156 ){
157 return true;
158 }else{
159 return false;
160 }
161 }
162
163 public function checkIfEmailEnabled($boardId, $userId, $purpose)
164 {
165 $boardSettings = $this->getBoardNotificationSettingsOfUser($boardId, $userId);
166
167 if($boardSettings){
168 $preferences = maybe_unserialize($boardSettings['preferences']);
169 if(!array_key_exists($purpose, $preferences)){
170 $preferences[$purpose] = true;
171 $boardSettings->preferences = $preferences;
172 $boardSettings->save();
173 return true;
174 }
175 return $preferences[$purpose];
176 }
177
178 return false;
179 }
180
181 public function checkIfEmailEnabledGlobally($userId, $purpose)
182 {
183 $globalSettings = $this->getGlobalNotificationSettingsOfUser($userId);
184
185 if($globalSettings){
186 $preferences = maybe_unserialize($globalSettings['value']);
187 if(!array_key_exists($purpose, $preferences)){
188 $preferences[$purpose] = true;
189 $globalSettings->preferences = $preferences;
190 $globalSettings->save();
191 return true;
192 }
193 return $preferences[$purpose];
194 }
195
196 return true;
197 }
198 }
199
200
201