Conditions | 17 |
Paths | 16 |
Total Lines | 109 |
Code Lines | 69 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
233 | private function buildUserFoldersList(array $userInfo): array |
||
234 | { |
||
235 | //Build tree |
||
236 | $tree = new NestedTree(prefixTable('nested_tree'), 'id', 'parent_id', 'title'); |
||
237 | |||
238 | // Start by adding the manually added folders |
||
239 | $allowedFolders = array_map('intval', explode(";", $userInfo['groupes_visibles'])); |
||
240 | $readOnlyFolders = []; |
||
241 | $allowedFoldersByRoles = []; |
||
242 | $restrictedFoldersForItems = []; |
||
243 | $foldersLimited = []; |
||
244 | $foldersLimitedFull = []; |
||
245 | $restrictedItems = []; |
||
246 | $personalFolders = []; |
||
247 | |||
248 | $userFunctionId = explode(";", $userInfo['fonction_id']); |
||
249 | |||
250 | // Get folders from the roles |
||
251 | if (count($userFunctionId) > 0) { |
||
252 | $rows = DB::query( |
||
253 | 'SELECT * |
||
254 | FROM ' . prefixTable('roles_values') . ' |
||
255 | WHERE role_id IN %li AND type IN ("W", "ND", "NE", "NDNE", "R")', |
||
256 | $userFunctionId |
||
257 | ); |
||
258 | foreach ($rows as $record) { |
||
259 | if ($record['type'] === 'R') { |
||
260 | array_push($readOnlyFolders, $record['folder_id']); |
||
261 | } elseif (in_array($record['folder_id'], $allowedFolders) === false) { |
||
262 | array_push($allowedFoldersByRoles, $record['folder_id']); |
||
263 | } |
||
264 | } |
||
265 | $allowedFoldersByRoles = array_unique($allowedFoldersByRoles); |
||
266 | $readOnlyFolders = array_unique($readOnlyFolders); |
||
267 | // Clean arrays |
||
268 | foreach ($allowedFoldersByRoles as $value) { |
||
269 | $key = array_search($value, $readOnlyFolders); |
||
270 | if ($key !== false) { |
||
271 | unset($readOnlyFolders[$key]); |
||
272 | } |
||
273 | } |
||
274 | } |
||
275 | |||
276 | // Does this user is allowed to see other items |
||
277 | $inc = 0; |
||
278 | $rows = DB::query( |
||
279 | 'SELECT id, id_tree |
||
280 | FROM ' . prefixTable('items') . ' |
||
281 | WHERE restricted_to LIKE %s'. |
||
282 | (count($userFunctionId) > 0 ? ' AND id_tree NOT IN %li' : ''), |
||
283 | $userInfo['id'], |
||
284 | count($userFunctionId) > 0 ? $userFunctionId : DB::sqleval('0') |
||
285 | ); |
||
286 | foreach ($rows as $record) { |
||
287 | // Exclude restriction on item if folder is fully accessible |
||
288 | $restrictedFoldersForItems[$inc] = $record['id_tree']; |
||
289 | ++$inc; |
||
290 | } |
||
291 | |||
292 | // Check for the users roles if some specific rights exist on items |
||
293 | $rows = DB::query( |
||
294 | 'SELECT i.id_tree, r.item_id |
||
295 | FROM ' . prefixTable('items') . ' AS i |
||
296 | INNER JOIN ' . prefixTable('restriction_to_roles') . ' AS r ON (r.item_id=i.id) |
||
297 | WHERE '.(count($userFunctionId) > 0 ? ' id_tree NOT IN %li AND ' : '').' i.id_tree != "" |
||
298 | ORDER BY i.id_tree ASC', |
||
299 | count($userFunctionId) > 0 ? $userFunctionId : DB::sqleval('0') |
||
300 | ); |
||
301 | foreach ($rows as $record) { |
||
302 | $foldersLimited[$record['id_tree']][$inc] = $record['item_id']; |
||
303 | //array_push($foldersLimitedFull, $record['item_id']); |
||
304 | array_push($restrictedItems, $record['item_id']); |
||
305 | array_push($foldersLimitedFull, $record['id_tree']); |
||
306 | ++$inc; |
||
307 | } |
||
308 | |||
309 | // Add all personal folders |
||
310 | $rows = DB::queryFirstRow( |
||
311 | 'SELECT id |
||
312 | FROM ' . prefixTable('nested_tree') . ' |
||
313 | WHERE title = %i AND personal_folder = 1'. |
||
314 | (count($userFunctionId) > 0 ? ' AND id NOT IN %li' : ''), |
||
315 | $userInfo['id'], |
||
316 | count($userFunctionId) > 0 ? $userFunctionId : DB::sqleval('0') |
||
317 | ); |
||
318 | if (empty($rows['id']) === false) { |
||
319 | array_push($personalFolders, $rows['id']); |
||
320 | // get all descendants |
||
321 | $ids = $tree->getDescendants($rows['id'], false, false, true); |
||
322 | foreach ($ids as $id) { |
||
323 | array_push($personalFolders, $id); |
||
324 | } |
||
325 | } |
||
326 | |||
327 | // All folders visibles |
||
328 | return [ |
||
329 | 'folders' => array_unique( |
||
330 | array_filter( |
||
331 | array_merge( |
||
332 | $allowedFolders, |
||
333 | $foldersLimitedFull, |
||
334 | $allowedFoldersByRoles, |
||
335 | $restrictedFoldersForItems, |
||
336 | $readOnlyFolders, |
||
337 | $personalFolders |
||
338 | ) |
||
339 | ) |
||
340 | ), |
||
341 | 'items' => array_unique($restrictedItems), |
||
342 | ]; |
||
345 | } |