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 / UserService.php

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

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