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