PluginProbe
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration / 1.91.6
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration v1.91.6
2.1.0 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 All 42 releases
fluent-boards / app / Services / UserService.php

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

378 lines 13.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')
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 'overdue':
256 $tasksQuery->overdue()
257 ->whereIn('board_id', $allowedBoardIds);
258 break;
259 case 'completed':
260 $tasksQuery->where('status', 'closed')
261 ->whereIn('board_id', $allowedBoardIds);
262 break;
263 default:
264 $tasksQuery->whereNull('due_at')
265 ->whereIn('board_id', $allowedBoardIds);
266 break;
267 }
268 }
269
270
271
272 $currentUserId = get_current_user_id();
273 if ($currentUserId != $user->ID && !PermissionManager::isAdmin()) {
274 $currentUser = User::find($currentUserId);
275 $currentUserBoardIds = $currentUser->boards->pluck('id')->toArray();
276
277 if (empty($boardIds)) {
278 $boardIds = $currentUserBoardIds;
279 } else {
280 // Get the intersection of the two arrays
281 $boardIds = array_intersect($boardIds, $currentUserBoardIds);
282 }
283 }
284
285 if (!empty($boardIds)) {
286 $tasksQuery->whereIn('board_id', $boardIds);
287 }
288
289 $sortOptions = ['priority', 'due_at', 'position', 'created_at', 'title'];
290 $orderOptions = ['ASC', 'DESC'];
291
292 // Validate order and orderBy parameters
293 if (!in_array($order, $orderOptions) || !in_array($orderBy, $sortOptions)) {
294 throw new \Exception(esc_html__('Invalid sort or orderBy parameter', 'fluent-boards'));
295 }
296
297 // Apply ordering based on the specified order and orderBy
298 if ($orderBy === 'priority') {
299 $tasksQuery->orderByRaw("FIELD(priority, 'High', 'Medium', 'Low') {$order}");
300 } else if ($orderBy === 'due_at') {
301 $tasksQuery->orderByRaw("ISNULL(due_at), due_at {$order}");
302 } else {
303 $tasksQuery->orderBy($orderBy, $order);
304 }
305
306 $tasks = $tasksQuery->paginate($per_page, ['*'], 'page', $page);
307
308 return [
309 'tasks' => $tasks->values()->toArray(),
310 'paginationInfo' => [
311 'current_page' => $tasks->currentPage(),
312 'last_page' => $tasks->lastPage(),
313 'total' => $tasks->total(),
314 ],
315 ];
316 }
317
318
319 public function getMemberRelatedAcitivies($user_id, $page)
320 {
321 $activities = Activity::query()->where('created_by', $user_id)
322 ->orderBy('created_at', 'desc')
323 ->with('user')->paginate(40, ['*'], 'page', $page);
324
325 $activitiesToShow = array();
326
327 foreach ($activities as $activity) {
328 if ($activity->object_type == Constant::ACTIVITY_BOARD) {
329 $activity->load('board');
330 // if($activity->settings && $activity->settings['task_id']){
331 // $activity->task = Task::findOrFail($activity->settings['task_id']);
332 // }
333 if (PermissionManager::userHasPermission($activity->board_id, get_current_user_id())) {
334 $activitiesToShow[] = $activity;
335 }
336 } elseif ($activity->object_type == Constant::ACTIVITY_TASK) {
337 $activity->load('task');
338 if ($activity->task && PermissionManager::userHasPermission($activity->task->board_id, get_current_user_id())) {
339 $activitiesToShow[] = $activity;
340 }
341 }
342 }
343 return [
344 'activities' => $activitiesToShow,
345 'pagination' => $activities->toArray(),
346 ];
347 }
348
349 private function getTaskById($taskId)
350 {
351 return Task::findOrFail($taskId);
352 }
353
354 public function getMemberBoards($user_id)
355 {
356 if(!$user_id) {
357 return [];
358 }
359 $boardIds = [];
360 $boards = [];
361 $user = User::find($user_id);
362 $currentUserId = get_current_user_id();
363 if($currentUserId != $user->ID) {
364 if(!PermissionManager::isAdmin()){
365 $currentUser = User::find($currentUserId);
366 $currentUserBoardIds = $currentUser->whichBoards->pluck('id')->toArray();
367 $boardIds = $currentUserBoardIds;
368 }
369 }
370 if (!empty($boardIds)) {
371 $boards = $user->whichBoards()->whereIn('fbs_boards.id', $boardIds)->get();
372 } else {
373 $boards = $user->whichBoards;
374 }
375 return $boards;
376 }
377 }
378