PluginProbe
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration / trunk
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration vtrunk
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 / FolderService.php

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

218 lines 6.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\Board;
6 use FluentBoards\App\Models\Folder;
7 use FluentBoards\App\Models\Relation;
8
9 class FolderService
10 {
11 public function create(array $data)
12 {
13 $background = [
14 'id' => null,
15 'is_image' => false,
16 'image_url' => null,
17 'color' => !empty($data['color']) ? sanitize_hex_color($data['color']) : null,
18 ];
19
20 $folderData = [
21 'title' => sanitize_text_field($data['title'] ?? ''),
22 'background' => $background,
23 ];
24
25 if (!empty($data['parent_id'])) {
26 $folderData['parent_id'] = absint($data['parent_id']);
27 }
28
29 return Folder::create($folderData);
30 }
31
32 public function getFolders($userId = null)
33 {
34 if (!$userId) {
35 $userId = get_current_user_id();
36 }
37
38 $boardIds = array_unique(array_map('intval', PermissionManager::getBoardIdsForUser($userId)));
39 $folderIds = [];
40
41 if ($boardIds) {
42 $folderIds = Relation::where('object_type', Constant::OBJECT_TYPE_FOLDER_BOARD)
43 ->whereIn('foreign_id', $boardIds)
44 ->pluck('object_id')
45 ->unique()
46 ->toArray();
47 }
48
49 $query = Folder::whereNull('parent_id');
50 if ($folderIds) {
51 $query->where(function ($folderQuery) use ($folderIds, $userId) {
52 $folderQuery->whereIn('id', $folderIds)
53 ->orWhere('created_by', $userId);
54 });
55 } else {
56 $query->where('created_by', $userId);
57 }
58
59 return $query->with(['subFolders' => function ($subFoldersQuery) {
60 $subFoldersQuery->orderBy('created_at', 'DESC');
61 }])
62 ->with(['boards' => function ($boardsQuery) use ($boardIds) {
63 if ($boardIds) {
64 $boardsQuery->whereIn('fbs_boards.id', $boardIds);
65 } else {
66 $boardsQuery->where('fbs_boards.id', 0);
67 }
68
69 $boardsQuery->orderBy('created_at', 'DESC');
70 }])
71 ->orderBy('created_at', 'DESC')
72 ->distinct()
73 ->get();
74 }
75
76 public function getFolderById($folderId, $data = [])
77 {
78 $folderId = absint($folderId);
79 if (!$folderId) {
80 return null;
81 }
82
83 $allowedOrderColumns = ['created_at', 'title', 'id'];
84 $order = sanitize_sql_orderby($data['order'] ?? 'created_at') ?: 'created_at';
85 if (!in_array($order, $allowedOrderColumns, true)) {
86 $order = 'created_at';
87 }
88 $orderBy = strtoupper(sanitize_text_field($data['orderBy'] ?? 'DESC')) === 'ASC' ? 'ASC' : 'DESC';
89 $searchInput = sanitize_text_field($data['searchInput'] ?? '');
90 $option = sanitize_text_field($data['option'] ?? '');
91 $boardIds = array_map('intval', PermissionManager::getBoardIdsForUser());
92
93 return Folder::where('id', $folderId)
94 ->with(['subFolders' => function ($query) {
95 $query->orderBy('created_at', 'DESC');
96 }])
97 ->with(['boards' => function ($query) use ($boardIds, $option, $order, $orderBy, $searchInput) {
98 if ($boardIds) {
99 $query->whereIn('fbs_boards.id', $boardIds);
100 } else {
101 $query->where('fbs_boards.id', 0);
102 }
103
104 if ($option) {
105 if ($option === 'archived') {
106 $query->whereNotNull('archived_at');
107 } else {
108 $query->whereNull('archived_at');
109 }
110 }
111
112 if ($searchInput) {
113 global $wpdb;
114 $query->where('title', 'like', '%' . $wpdb->esc_like($searchInput) . '%');
115 }
116
117 $query->withCount('completedTasks')->orderBy($order, $orderBy);
118 }])
119 ->orderBy('title', 'ASC')
120 ->first();
121 }
122
123 public function getBoardIdsByFolder($folderId)
124 {
125 $folderId = absint($folderId);
126 if (!$folderId) {
127 return [];
128 }
129
130 return Relation::where('object_type', Constant::OBJECT_TYPE_FOLDER_BOARD)
131 ->where('object_id', $folderId)
132 ->pluck('foreign_id')
133 ->toArray();
134 }
135
136 public function addBoardToFolder($folderId, $boardIds)
137 {
138 $folder = $this->assertCanModifyFolder($folderId);
139 $boardIds = $this->normalizeBoardIds($boardIds);
140
141 if (!$boardIds) {
142 return;
143 }
144
145 $boardIds = Board::whereIn('id', $boardIds)->pluck('id')->toArray();
146 if (!$boardIds) {
147 return;
148 }
149
150 Relation::where('object_type', Constant::OBJECT_TYPE_FOLDER_BOARD)
151 ->whereIn('foreign_id', $boardIds)
152 ->delete();
153
154 $boardPivots = [];
155 foreach ($boardIds as $boardId) {
156 $boardPivots[$boardId] = ['object_type' => Constant::OBJECT_TYPE_FOLDER_BOARD];
157 }
158
159 $folder->boards()->syncWithoutDetaching($boardPivots);
160 }
161
162 public function assertCanModifyFolder($folderId)
163 {
164 $folder = Folder::findOrFail(absint($folderId));
165 $userId = get_current_user_id();
166
167 if (!$userId || ((int) $folder->created_by !== (int) $userId && !PermissionManager::isAdmin($userId))) {
168 throw new \Exception(__('You do not have permission to modify this folder.', 'fluent-boards'));
169 }
170
171 return $folder;
172 }
173
174 public function removeBoardFromFolder($folderId, $boardId)
175 {
176 Relation::where('object_type', Constant::OBJECT_TYPE_FOLDER_BOARD)
177 ->where('object_id', absint($folderId))
178 ->where('foreign_id', absint($boardId))
179 ->delete();
180 }
181
182 public function updateFolder($folderId, $title)
183 {
184 $folder = Folder::findOrFail(absint($folderId));
185 $folder->title = sanitize_text_field($title);
186 $folder->save();
187
188 return $folder;
189 }
190
191 public function deleteFolder($folderId)
192 {
193 $folderId = absint($folderId);
194 $folder = Folder::findOrFail($folderId);
195
196 Relation::where('object_type', Constant::OBJECT_TYPE_FOLDER_BOARD)
197 ->where('object_id', $folderId)
198 ->delete();
199
200 $folder->delete();
201 }
202
203 protected function removeBoardFromPreviousFolder($boardId)
204 {
205 Relation::where('object_type', Constant::OBJECT_TYPE_FOLDER_BOARD)
206 ->where('foreign_id', absint($boardId))
207 ->delete();
208 }
209
210 protected function normalizeBoardIds($boardIds)
211 {
212 $boardIds = is_array($boardIds) ? $boardIds : [$boardIds];
213 $boardIds = array_map('absint', $boardIds);
214
215 return array_values(array_filter(array_unique($boardIds)));
216 }
217 }
218