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

439 lines 16.0 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 // public function getMemberAssociatedTasks($user_id, $requestData)
217 // {
218 // $taskType = $requestData['taskType'];
219 // $boardIds = !empty($requestData['boardIds']) ? $requestData['boardIds'] : [];
220 // $orderBy = $requestData['orderBy'];
221 // $order = $requestData['order'];
222 // $per_page = 15;
223 // $user = User::find($user_id);
224 //
225 // // get the task assigned to the user
226 // $tasksQuery = $user->tasks()->with(['stage', 'board'])->whereNull('archived_at');
227 // if ($taskType == 'upcoming') {
228 // $tasksQuery = $tasksQuery->upcoming();
229 // } elseif ($taskType == 'overdue') {
230 // $tasksQuery = $tasksQuery->overdue();
231 // } elseif ($taskType == 'completed') {
232 // $tasksQuery = $tasksQuery->where('status', 'closed');
233 // } else {
234 // $tasksQuery = $tasksQuery->whereNull('due_at');
235 // }
236 //
237 // $currentUserId = get_current_user_id();
238 // if($currentUserId != $user->ID) {
239 // if(!PermissionManager::isAdmin()){
240 // $currentUser = User::find($currentUserId);
241 // $currentUserBoardIds = $currentUser->whichBoards->pluck('id')->toArray();
242 // if (empty($boardIds)) {
243 // $boardIds = $currentUserBoardIds;
244 // } else {
245 // // Get the intersection of the two arrays
246 // $boardIds = array_intersect($boardIds, $currentUserBoardIds);
247 // }
248 // }
249 // }
250 //
251 // if($boardIds){
252 // $tasksQuery = $tasksQuery->whereIn('board_id', $boardIds);
253 // }
254 //
255 // $sortOptions = ['priority', 'due_at', 'position', 'created_at', 'title'];
256 // $orderOptions = ['ASC', 'DESC'];
257 //
258 // // Validate order and orderBy parameters
259 // if (!in_array($order, $sortOptions) || !in_array($orderBy, $orderOptions)) {
260 // throw new \Exception(__('Invalid sort or orderBy parameter', 'fluent-boards'));
261 // }
262 //
263 // // Apply ordering based on the specified order and orderBy
264 // switch ($order) {
265 // case 'priority':
266 // $tasksQuery->orderByRaw("FIELD(priority, 'High', 'Medium', 'Low') {$orderBy}");
267 // break;
268 //
269 // case 'due_at':
270 // if ($orderBy === 'ASC') {
271 // // Separate ordering for tasks with and without due dates
272 // $tasksQuery = $tasksQuery->orderBy('due_at');
273 // } else {
274 // $tasksQuery->orderBy('due_at', $orderBy);
275 // }
276 // break;
277 //
278 // default:
279 // $tasksQuery->orderBy($order, $orderBy);
280 // break;
281 // }
282 //
283 // $tasks = $tasksQuery->paginate($per_page, ['*'], 'page', $requestData->page);
284 // return [
285 // 'tasks' => $tasks->values()->toArray(),
286 // 'paginationInfo' => [
287 // 'current_page' => $tasks->currentPage(),
288 // 'last_page' => $tasks->lastPage(),
289 // 'total' => $tasks->total(),
290 // ],
291 // ];
292 //
293 // }
294 public function getMemberAssociatedTasks($user_id, $requestData)
295 {
296 $taskType = $requestData['taskType'] ?? null;
297 $boardIds = !empty($requestData['boardIds']) ? $requestData['boardIds'] : [];
298 $orderBy = $requestData['orderBy'] ?? 'created_at';
299 $order = strtoupper($requestData['order'] ?? 'ASC');
300 $per_page = $requestData['per_page'] ?? 15;
301 $page = $requestData['page'] ?? 1;
302 $user = User::find($user_id);
303
304 if (!$user) {
305 return [
306 'tasks' => [],
307 'paginationInfo' => [
308 'current_page' => 1,
309 'last_page' => 1,
310 'total' => 0,
311 ],
312 ];
313 }
314
315 // Get the task assigned to the user
316 $tasksQuery = $user->tasks()->with(['stage', 'board'])->whereNull('archived_at');
317
318 switch ($taskType) {
319 case 'upcoming':
320 $tasksQuery->upcoming();
321 break;
322 case 'overdue':
323 $tasksQuery->overdue();
324 break;
325 case 'completed':
326 $tasksQuery->where('status', 'closed');
327 break;
328 default:
329 $tasksQuery->whereNull('due_at');
330 break;
331 }
332
333 $currentUserId = get_current_user_id();
334 if ($currentUserId != $user->ID && !PermissionManager::isAdmin()) {
335 $currentUser = User::find($currentUserId);
336 $currentUserBoardIds = $currentUser->boards->pluck('id')->toArray();
337
338 if (empty($boardIds)) {
339 $boardIds = $currentUserBoardIds;
340 } else {
341 // Get the intersection of the two arrays
342 $boardIds = array_intersect($boardIds, $currentUserBoardIds);
343 }
344 }
345
346 if (!empty($boardIds)) {
347 $tasksQuery->whereIn('board_id', $boardIds);
348 }
349
350 $sortOptions = ['priority', 'due_at', 'position', 'created_at', 'title'];
351 $orderOptions = ['ASC', 'DESC'];
352
353 // Validate order and orderBy parameters
354 if (!in_array($order, $orderOptions) || !in_array($orderBy, $sortOptions)) {
355 throw new \Exception(__('Invalid sort or orderBy parameter', 'fluent-boards'));
356 }
357
358 // Apply ordering based on the specified order and orderBy
359 if ($orderBy === 'priority') {
360 $tasksQuery->orderByRaw("FIELD(priority, 'High', 'Medium', 'Low') {$order}");
361 } else if ($orderBy === 'due_at') {
362 $tasksQuery->orderByRaw("ISNULL(due_at), due_at {$order}");
363 } else {
364 $tasksQuery->orderBy($orderBy, $order);
365 }
366
367 $tasks = $tasksQuery->paginate($per_page, ['*'], 'page', $page);
368
369 return [
370 'tasks' => $tasks->values()->toArray(),
371 'paginationInfo' => [
372 'current_page' => $tasks->currentPage(),
373 'last_page' => $tasks->lastPage(),
374 'total' => $tasks->total(),
375 ],
376 ];
377 }
378
379
380 public function getMemberRelatedAcitivies($user_id, $page)
381 {
382 $activities = Activity::query()->where('created_by', $user_id)
383 ->orderBy('created_at', 'desc')
384 ->with('user')->paginate(40, ['*'], 'page', $page);
385
386 $activitiesToShow = array();
387
388 foreach ($activities as $activity) {
389 if ($activity->object_type == Constant::ACTIVITY_BOARD) {
390 $activity->load('board');
391 // if($activity->settings && $activity->settings['task_id']){
392 // $activity->task = Task::findOrFail($activity->settings['task_id']);
393 // }
394 if (PermissionManager::userHasPermission($activity->board_id, get_current_user_id())) {
395 $activitiesToShow[] = $activity;
396 }
397 } elseif ($activity->object_type == Constant::ACTIVITY_TASK) {
398 $activity->load('task');
399 if ($activity->task && PermissionManager::userHasPermission($activity->task->board_id, get_current_user_id())) {
400 $activitiesToShow[] = $activity;
401 }
402 }
403 }
404 return [
405 'activities' => $activitiesToShow,
406 'pagination' => $activities->toArray(),
407 ];
408 }
409
410 private function getTaskById($taskId)
411 {
412 return Task::findOrFail($taskId);
413 }
414
415 public function getMemberBoards($user_id)
416 {
417 if(!$user_id) {
418 return [];
419 }
420 $boardIds = [];
421 $boards = [];
422 $user = User::find($user_id);
423 $currentUserId = get_current_user_id();
424 if($currentUserId != $user->ID) {
425 if(!PermissionManager::isAdmin()){
426 $currentUser = User::find($currentUserId);
427 $currentUserBoardIds = $currentUser->whichBoards->pluck('id')->toArray();
428 $boardIds = $currentUserBoardIds;
429 }
430 }
431 if (!empty($boardIds)) {
432 $boards = $user->whichBoards()->whereIn('fbs_boards.id', $boardIds)->get();
433 } else {
434 $boards = $user->whichBoards;
435 }
436 return $boards;
437 }
438 }
439