PluginProbe
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration / 1.95.2
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration v1.95.2
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 / UserService.php

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

382 lines 13.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\Services;
4
5 use FluentBoards\App\Models\Activity;
6 use FluentBoards\App\Models\Meta;
7 use FluentBoards\App\Models\Task;
8 use FluentBoards\App\Models\User;
9 use FluentBoards\App\Models\Board;
10 use FluentBoards\App\Models\Relation;
11 use FluentBoards\Framework\Support\Arr;
12
13
14 class UserService
15 {
16 public function allFluentBoardsUsers($boardId = null)
17 {
18 // Get the global admins first
19 $adminUserIds = Meta::query()->where('object_type', Constant::FLUENT_BOARD_ADMIN)
20 ->get()->pluck('object_id')->toArray();
21
22 $boardObjects = Relation::where('object_type', 'board_user');
23
24 if ($adminUserIds) {
25 $boardObjects = $boardObjects->whereNotIn('foreign_id', $adminUserIds);
26 }
27
28 if ($boardId) {
29 $boardObjects = $boardObjects->where('object_id', $boardId);
30 }
31
32 $boardObjects = $boardObjects->whereNotIn('foreign_id', $adminUserIds)
33 ->get();
34
35 $boardUserMaps = [];
36
37 $accessBoardIds = [];
38
39 foreach ($boardObjects as $boardObject) {
40 if (!isset($boardUserMaps[$boardObject->foreign_id])) {
41 $boardUserMaps[$boardObject->foreign_id] = [];
42 }
43
44 $accessBoardIds[] = $boardObject->object_id;
45
46 $boardUserMaps[$boardObject->foreign_id][] = [
47 'board_id' => $boardObject->object_id,
48 'role' => Arr::get($boardObject->settings, 'is_admin')
49 ? 'admin'
50 : (Arr::has($boardObject->settings, 'is_viewer_only') && Arr::get($boardObject->settings, 'is_viewer_only')
51 ? 'viewer'
52 : 'member')
53 ];
54 }
55
56 $accessBoardIds = array_unique($accessBoardIds);
57 $allUserIds = array_unique(array_merge($adminUserIds, array_keys($boardUserMaps)));
58
59 $users = get_users([
60 'include' => $allUserIds
61 ]);
62
63 $boardUsers = [];
64 $allBoards = Board::query()->whereIn('id', $accessBoardIds)->get()->keyBy('id');
65
66 foreach ($users as $user) {
67
68 $boards = Arr::get($boardUserMaps, $user->ID, []);
69
70 $formattedBoars = [];
71
72 foreach ($boards as $board) {
73 if (empty($allBoards[$board['board_id']])) {
74 continue;
75 }
76
77 $boardModel = $allBoards[$board['board_id']];
78 $formattedBoars[] = [
79 'id' => $boardModel->id,
80 'title' => $boardModel->title,
81 'role' => $board['role']
82 ];
83 }
84
85 $name = trim($user->first_name . ' ' . $user->last_name);
86
87 if (!$name) {
88 $name = $user->display_name;
89 }
90
91 $photo = fluent_boards_user_avatar($user->user_email, $name);
92
93 $boardUsers[] = [
94 'ID' => $user->ID,
95 'display_name' => $user->display_name,
96 'photo' => $photo,
97 'email' => $user->user_email,
98 'boards' => $formattedBoars,
99 'is_super' => in_array($user->ID, $adminUserIds),
100 'is_wpadmin' => $user->has_cap('manage_options')
101 ];
102 }
103
104 usort($boardUsers, function ($a, $b) {
105 return strcmp($a['display_name'], $b['display_name']);
106 });
107
108 return $boardUsers;
109 }
110
111 public function memberAssociatedTaskUsers($userId)
112 {
113 $user = User::find($userId);
114 $boards = $user->whichBoards->pluck('id');
115 $boardUsers = Relation::whereIn('object_id', $boards)
116 ->where('object_type', Constant::OBJECT_TYPE_BOARD_USER)
117 ->pluck('foreign_id')->toArray();
118 $uniqueUsersIds = array_unique($boardUsers);
119 $uniqueUsers = User::whereIn('ID', $uniqueUsersIds)->with('whichBoards')->get();
120
121 $userWiseBoardDesignation = Relation::query()->whereIn('foreign_id', $uniqueUsersIds)
122 ->where('object_type', Constant::OBJECT_TYPE_BOARD_USER)->get();
123
124 $data = array();
125 $data['userWiseBoardDesignation'] = $userWiseBoardDesignation;
126
127 foreach ($uniqueUsers as &$uniqueUser) {
128 if (user_can($uniqueUser['ID'], 'manage_options') && PermissionManager::isFluentBoardsAdmin($uniqueUser['ID'])) {
129 $uniqueUser['is_super'] = true;
130 $uniqueUser['is_wpadmin'] = true;
131 } elseif (user_can($uniqueUser['ID'], 'manage_options')) {
132 $uniqueUser['is_wpadmin'] = true;
133 $uniqueUser['is_super'] = false;
134 } elseif (PermissionManager::isFluentBoardsAdmin($uniqueUser['ID'])) {
135 $uniqueUser['is_super'] = true;
136 $uniqueUser['is_wpadmin'] = false;
137 } else {
138 $uniqueUser['all_boards'] = Arr::get($uniqueUser, 'boards');
139 $uniqueUser['is_super'] = false;
140 $uniqueUser['is_wpadmin'] = false;
141 }
142 }
143
144 $data['uniqueUsers'] = $uniqueUsers;
145
146 return $data;
147 }
148
149 public function searchFluentBoardsUser($search_input)
150 {
151
152 $boardUsers = User::whereHas('whichBoards', function ($query) use ($search_input) {
153 $query->where('display_name', 'like', '%' . $search_input . '%');
154 })
155 ->where('display_name', 'like', '%' . $search_input . '%')
156 ->with('whichBoards')
157 ->get()
158 ->toArray();
159
160 foreach ($boardUsers as &$boardUser) {
161 if (user_can($boardUser['ID'], 'manage_options') && PermissionManager::isFluentBoardsAdmin($boardUser['ID'])) {
162 $boardUser['is_super'] = true;
163 $boardUser['is_wpadmin'] = true;
164 } elseif (user_can($boardUser['ID'], 'manage_options')) {
165 $boardUser['is_wpadmin'] = true;
166 $boardUser['is_super'] = false;
167 } elseif (PermissionManager::isFluentBoardsAdmin($boardUser['ID'])) {
168 $boardUser['is_super'] = true;
169 $boardUser['is_wpadmin'] = false;
170 } else {
171 $boardUser['all_boards'] = Arr::get($boardUser, 'boards');
172 $boardUser['is_super'] = false;
173 $boardUser['is_wpadmin'] = false;
174 }
175 }
176
177 return $boardUsers;
178 }
179
180 public function searchMemberUser($search_input, $userId)
181 {
182 $user = User::find($userId);
183 $boards = $user->boards->pluck('id');
184 $boardUsers = Relation::whereIn('board_id', $boards)->orWhere('board_id', null)->where('status', 'ACTIVE')->pluck('user_id')->toArray();
185 $uniqueUsersIds = array_unique($boardUsers);
186 // $uniqueUsers = User::whereIn('ID', $uniqueUsersIds)->with('boards')->get();
187
188 $searchResult = User::query()->whereIn('ID', $uniqueUsersIds)->with('boards')
189 ->where('display_name', 'like', '%' . $search_input . '%')
190 ->take(20)->get();
191
192 foreach ($searchResult as &$uniqueUser) {
193 if (PermissionManager::isAdmin($uniqueUser['ID'])) {
194 $uniqueUser['all_boards'] = Board::query()->where('is_archived', 0)->get();
195 $isFluentBoardsAdmin = Relation::where('user_id', $uniqueUser['ID'])
196 ->whereNull('board_id')
197 ->where('status', 'ACTIVE')
198 ->first();
199 if ($isFluentBoardsAdmin) {
200 $uniqueUser['is_super'] = true;
201 $uniqueUser['is_wpadmin'] = false;
202 } else {
203 $uniqueUser['is_super'] = false;
204 $uniqueUser['is_wpadmin'] = true;
205 }
206 } else {
207 $uniqueUser['all_boards'] = $uniqueUser['boards'];
208 $uniqueUser['is_super'] = false;
209 $uniqueUser['is_wpadmin'] = false;
210 }
211 }
212
213 return $searchResult;
214 }
215
216
217 public function getMemberAssociatedTasks($user_id, $requestData)
218 {
219 $taskType = $requestData['taskType'] ?? null;
220 $boardIds = !empty($requestData['boardIds']) ? $requestData['boardIds'] : [];
221 $orderBy = $requestData['orderBy'] ?? 'created_at';
222 $order = strtoupper($requestData['order'] ?? 'ASC');
223 $per_page = $requestData['per_page'] ?? 15;
224 $page = $requestData['page'] ?? 1;
225 $user = User::find($user_id);
226
227 $loggedInUserId = get_current_user_id();
228
229 $allowedBoardIds = PermissionManager::getBoardIdsForUser($loggedInUserId);
230
231 if (!$user || empty($allowedBoardIds)) {
232 return [
233 'tasks' => [],
234 'paginationInfo' => [
235 'current_page' => 1,
236 'last_page' => 1,
237 'total' => 0,
238 ],
239 ];
240 }
241
242 if($taskType == 'assigned') {
243 $tasksQuery = $user->assignedTasks()->with(['stage', 'board'])->whereNull('archived_at')->whereNull('parent_id')->whereIn('board_id', $allowedBoardIds);
244 } else if($taskType == 'mentioned') {
245 $tasksQuery = $user->mentionedTasks()->with(['stage', 'board'])->whereNull('archived_at')->whereNull('parent_id')->whereIn('board_id', $allowedBoardIds);
246 } else {
247 // Get the task assigned to the user
248 $tasksQuery = $user->tasks()->with(['stage', 'board'])->whereNull('archived_at') ->whereIn('board_id', $allowedBoardIds);
249
250 switch ($taskType) {
251 case 'upcoming':
252 $tasksQuery->upcoming()
253 ->whereIn('board_id', $allowedBoardIds);
254 break;
255 case 'due_today':
256 $tasksQuery->dueToday()
257 ->whereIn('board_id', $allowedBoardIds);
258 break;
259 case 'overdue':
260 $tasksQuery->overdue()
261 ->whereIn('board_id', $allowedBoardIds);
262 break;
263 case 'completed':
264 $tasksQuery->where('status', 'closed')
265 ->whereIn('board_id', $allowedBoardIds);
266 break;
267 default:
268 $tasksQuery->whereNull('due_at')
269 ->whereIn('board_id', $allowedBoardIds);
270 break;
271 }
272 }
273
274
275
276 $currentUserId = get_current_user_id();
277 if ($currentUserId != $user->ID && !PermissionManager::isAdmin()) {
278 $currentUser = User::find($currentUserId);
279 $currentUserBoardIds = $currentUser->boards->pluck('id')->toArray();
280
281 if (empty($boardIds)) {
282 $boardIds = $currentUserBoardIds;
283 } else {
284 // Get the intersection of the two arrays
285 $boardIds = array_intersect($boardIds, $currentUserBoardIds);
286 }
287 }
288
289 if (!empty($boardIds)) {
290 $tasksQuery->whereIn('board_id', $boardIds);
291 }
292
293 $sortOptions = ['priority', 'due_at', 'position', 'created_at', 'title'];
294 $orderOptions = ['ASC', 'DESC'];
295
296 // Validate order and orderBy parameters
297 if (!in_array($order, $orderOptions) || !in_array($orderBy, $sortOptions)) {
298 throw new \Exception(esc_html__('Invalid sort or orderBy parameter', 'fluent-boards'));
299 }
300
301 // Apply ordering based on the specified order and orderBy
302 if ($orderBy === 'priority') {
303 $tasksQuery->orderByRaw("FIELD(priority, 'High', 'Medium', 'Low') {$order}");
304 } else if ($orderBy === 'due_at') {
305 $tasksQuery->orderByRaw("ISNULL(due_at), due_at {$order}");
306 } else {
307 $tasksQuery->orderBy($orderBy, $order);
308 }
309
310 $tasks = $tasksQuery->paginate($per_page, ['*'], 'page', $page);
311
312 return [
313 'tasks' => $tasks->values()->toArray(),
314 'paginationInfo' => [
315 'current_page' => $tasks->currentPage(),
316 'last_page' => $tasks->lastPage(),
317 'total' => $tasks->total(),
318 ],
319 ];
320 }
321
322
323 public function getMemberRelatedAcitivies($user_id, $page)
324 {
325 $activities = Activity::query()->where('created_by', $user_id)
326 ->orderBy('created_at', 'desc')
327 ->with('user')->paginate(40, ['*'], 'page', $page);
328
329 $activitiesToShow = array();
330
331 foreach ($activities as $activity) {
332 if ($activity->object_type == Constant::ACTIVITY_BOARD) {
333 $activity->load('board');
334 // if($activity->settings && $activity->settings['task_id']){
335 // $activity->task = Task::findOrFail($activity->settings['task_id']);
336 // }
337 if (PermissionManager::userHasPermission($activity->board_id, get_current_user_id())) {
338 $activitiesToShow[] = $activity;
339 }
340 } elseif ($activity->object_type == Constant::ACTIVITY_TASK) {
341 $activity->load('task');
342 if ($activity->task && PermissionManager::userHasPermission($activity->task->board_id, get_current_user_id())) {
343 $activitiesToShow[] = $activity;
344 }
345 }
346 }
347 return [
348 'activities' => $activitiesToShow,
349 'pagination' => $activities->toArray(),
350 ];
351 }
352
353 private function getTaskById($taskId)
354 {
355 return Task::findOrFail($taskId);
356 }
357
358 public function getMemberBoards($user_id)
359 {
360 if(!$user_id) {
361 return [];
362 }
363 $boardIds = [];
364 $boards = [];
365 $user = User::find($user_id);
366 $currentUserId = get_current_user_id();
367 if($currentUserId != $user->ID) {
368 if(!PermissionManager::isAdmin()){
369 $currentUser = User::find($currentUserId);
370 $currentUserBoardIds = $currentUser->whichBoards->pluck('id')->toArray();
371 $boardIds = $currentUserBoardIds;
372 }
373 }
374 if (!empty($boardIds)) {
375 $boards = $user->whichBoards()->whereIn('fbs_boards.id', $boardIds)->get();
376 } else {
377 $boards = $user->whichBoards;
378 }
379 return $boards;
380 }
381 }
382