Conditions | 34 |
Paths | 241 |
Total Lines | 188 |
Code Lines | 122 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
228 | private function createFolder($params, $parentFolderData) |
||
229 | { |
||
230 | extract($params); |
||
231 | extract($parentFolderData); |
||
232 | if ( |
||
233 | (int) $isPersonal === 1 |
||
234 | || (int) $user_is_admin === 1 |
||
235 | || ((int) $user_is_manager === 1 || (int) $user_can_manage_all_users === 1) |
||
236 | || (isset($SETTINGS['enable_user_can_create_folders']) === true |
||
237 | && (int) $SETTINGS['enable_user_can_create_folders'] == 1) |
||
238 | || ((int) $user_can_create_root_folder && null !== $user_can_create_root_folder && (int) $user_can_create_root_folder === 1) |
||
239 | ) { |
||
240 | //create folder |
||
241 | DB::insert( |
||
242 | prefixTable('nested_tree'), |
||
243 | array( |
||
244 | 'parent_id' => $parent_id, |
||
245 | 'title' => $title, |
||
246 | 'personal_folder' => $isPersonal, |
||
247 | 'renewal_period' => isset($duration) === true && (int) $duration !== 0 ? $duration : 0, |
||
248 | 'bloquer_creation' => isset($create_auth_without) === true && (int) $create_auth_without === 1 ? '1' : $parentBloquerCreation, |
||
249 | 'bloquer_modification' => isset($edit_auth_without) === true && (int) $edit_auth_without === 1 ? '1' : $parentBloquerModification, |
||
250 | 'fa_icon' => empty($icon) === true ? TP_DEFAULT_ICON : $icon, |
||
251 | 'fa_icon_selected' => empty($icon_selected) === true ? TP_DEFAULT_ICON_SELECTED : $icon_selected, |
||
252 | 'categories' => '', |
||
253 | ) |
||
254 | ); |
||
255 | $newId = DB::insertId(); |
||
256 | |||
257 | //Add complexity |
||
258 | DB::insert( |
||
259 | prefixTable('misc'), |
||
260 | array( |
||
261 | 'type' => 'complex', |
||
262 | 'intitule' => $newId, |
||
263 | 'valeur' => $complexity, |
||
264 | ) |
||
265 | ); |
||
266 | |||
267 | // ensure categories are set |
||
268 | handleFoldersCategories( |
||
269 | [$newId] |
||
270 | ); |
||
271 | |||
272 | // Update timestamp |
||
273 | DB::update( |
||
274 | prefixTable('misc'), |
||
275 | array( |
||
276 | 'valeur' => time(), |
||
277 | ), |
||
278 | 'type = %s AND intitule = %s', |
||
279 | 'timestamp', |
||
280 | 'last_folder_change' |
||
281 | ); |
||
282 | |||
283 | // Load tree |
||
284 | $tree = new NestedTree(prefixTable('nested_tree'), 'id', 'parent_id', 'title'); |
||
285 | |||
286 | // rebuild tree |
||
287 | $tree->rebuild(); |
||
288 | |||
289 | |||
290 | // --> build json tree if not Admin |
||
291 | if ($user_is_admin === 0) { |
||
292 | // Get path |
||
293 | $path = ''; |
||
294 | $tree_path = $tree->getPath(0, false); |
||
295 | foreach ($tree_path as $fld) { |
||
296 | $path .= empty($path) === true ? $fld->title : '/'.$fld->title; |
||
297 | } |
||
298 | $new_json = [ |
||
299 | "path" => $path, |
||
300 | "id" => $newId, |
||
301 | "level" => count($tree_path), |
||
302 | "title" => $title, |
||
303 | "disabled" => 0, |
||
304 | "parent_id" => $parent_id, |
||
305 | "perso" => $isPersonal, |
||
306 | "is_visible_active" => 0, |
||
307 | ]; |
||
308 | |||
309 | // update cache_tree |
||
310 | $cache_tree = DB::queryfirstrow( |
||
311 | 'SELECT increment_id, folders, visible_folders |
||
312 | FROM ' . prefixTable('cache_tree').' WHERE user_id = %i', |
||
313 | (int) $user_id |
||
314 | ); |
||
315 | if (empty($cache_tree) === true) { |
||
316 | DB::insert( |
||
317 | prefixTable('cache_tree'), |
||
318 | array( |
||
319 | 'user_id' => $user_id, |
||
320 | 'folders' => json_encode($newId), |
||
321 | 'visible_folders' => json_encode($new_json), |
||
322 | 'timestamp' => time(), |
||
323 | 'data' => '[{}]', |
||
324 | ) |
||
325 | ); |
||
326 | } else { |
||
327 | $a_folders = is_null($cache_tree['folders']) === true ? [] : json_decode($cache_tree['folders'], true); |
||
328 | array_push($a_folders, $newId); |
||
329 | $a_visible_folders = is_null($cache_tree['visible_folders']) === true || empty($cache_tree['visible_folders']) === true ? [] : json_decode($cache_tree['visible_folders'], true); |
||
330 | array_push($a_visible_folders, $new_json); |
||
331 | DB::update( |
||
332 | prefixTable('cache_tree'), |
||
333 | array( |
||
334 | 'folders' => json_encode($a_folders), |
||
335 | 'visible_folders' => json_encode($a_visible_folders), |
||
336 | 'timestamp' => time(), |
||
337 | ), |
||
338 | 'increment_id = %i', |
||
339 | (int) $cache_tree['increment_id'] |
||
340 | ); |
||
341 | } |
||
342 | } |
||
343 | // <-- end - build json tree |
||
344 | |||
345 | // Create expected groups access rights based upon option selected |
||
346 | if ( |
||
347 | isset($SETTINGS['subfolder_rights_as_parent']) === true |
||
348 | && (int) $SETTINGS['subfolder_rights_as_parent'] === 1 |
||
349 | ) { |
||
350 | //If it is a subfolder, then give access to it for all roles that allows the parent folder |
||
351 | $rows = DB::query('SELECT role_id, type FROM ' . prefixTable('roles_values') . ' WHERE folder_id = %i', $parent_id); |
||
352 | foreach ($rows as $record) { |
||
353 | //add access to this subfolder |
||
354 | DB::insert( |
||
355 | prefixTable('roles_values'), |
||
356 | array( |
||
357 | 'role_id' => $record['role_id'], |
||
358 | 'folder_id' => $newId, |
||
359 | 'type' => $record['type'], |
||
360 | ) |
||
361 | ); |
||
362 | } |
||
363 | } elseif ((int) $user_is_admin !== 1) { |
||
364 | // If not admin and no option enabled |
||
365 | // then provide expected rights based upon user's roles |
||
366 | foreach (explode(';', $user_roles) as $role) { |
||
367 | if (empty($role) === false) { |
||
368 | DB::insert( |
||
369 | prefixTable('roles_values'), |
||
370 | array( |
||
371 | 'role_id' => $role, |
||
372 | 'folder_id' => $newId, |
||
373 | 'type' => $access_rights, |
||
374 | ) |
||
375 | ); |
||
376 | } |
||
377 | } |
||
378 | } |
||
379 | |||
380 | // if parent folder has Custom Fields Categories then add to this child one too |
||
381 | $rows = DB::query('SELECT id_category FROM ' . prefixTable('categories_folders') . ' WHERE id_folder = %i', $parent_id); |
||
382 | foreach ($rows as $record) { |
||
383 | //add CF Category to this subfolder |
||
384 | DB::insert( |
||
385 | prefixTable('categories_folders'), |
||
386 | array( |
||
387 | 'id_category' => $record['id_category'], |
||
388 | 'id_folder' => $newId, |
||
389 | ) |
||
390 | ); |
||
391 | } |
||
392 | |||
393 | // clear cache cache for each user that have at least one similar role as the current user |
||
394 | $usersWithSimilarRoles = empty($user_roles) === false ? getUsersWithRoles( |
||
395 | explode(";", $user_roles) |
||
396 | ) : []; |
||
397 | foreach ($usersWithSimilarRoles as $user) { |
||
398 | // delete cache tree |
||
399 | DB::delete( |
||
400 | prefixTable('cache_tree'), |
||
401 | 'user_id = %i', |
||
402 | $user |
||
403 | ); |
||
404 | } |
||
405 | return array( |
||
406 | 'error' => false, |
||
407 | 'message' => '', |
||
408 | 'newId' => $newId, |
||
409 | ); |
||
410 | |||
411 | } else { |
||
412 | return array( |
||
413 | 'error' => true, |
||
414 | 'message' => $lang->get('error_not_allowed_to'), |
||
415 | 'newId' => $newId, |
||
416 | ); |
||
428 | } |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.