From 60989d67664a14c407d861e9b9b678a02fcf296a Mon Sep 17 00:00:00 2001 From: Lukas Kraic Date: Wed, 4 Jun 2025 15:17:55 +0200 Subject: [PATCH] Fix Codacy warnings --- app/Http/Controllers/ViewAssetsController.php | 37 ++++++++++++------- app/Models/User.php | 14 +++---- resources/views/account/view-assets.blade.php | 1 + 3 files changed, 32 insertions(+), 20 deletions(-) diff --git a/app/Http/Controllers/ViewAssetsController.php b/app/Http/Controllers/ViewAssetsController.php index a54dff0881..49503f370f 100755 --- a/app/Http/Controllers/ViewAssetsController.php +++ b/app/Http/Controllers/ViewAssetsController.php @@ -26,6 +26,27 @@ use Exception; */ class ViewAssetsController extends Controller { + /** + * Extract custom fields that should be displayed in user view. + * + * @param User $user + * @return array + */ + private function extractCustomFields(User $user): array + { + $fieldArray = []; + foreach ($user->assets as $asset) { + if ($asset->model && $asset->model->fieldset) { + foreach ($asset->model->fieldset->fields as $field) { + if ($field->display_in_user_view == '1') { + $fieldArray[$field->db_column] = $field->name; + } + } //end foreach + } + } //end foreach + return array_unique($fieldArray); + } + /** * Show user's assigned assets with optional manager view functionality. * @@ -59,7 +80,7 @@ class ViewAssetsController extends Controller } else { // User has no subordinates, so they only see themselves $subordinates = collect([$authUser]); - } + } //end if } // If the user has subordinates and a user_id is provided in the request @@ -90,22 +111,12 @@ class ViewAssetsController extends Controller } // Process custom fields for the user being viewed - $field_array = []; - foreach ($userToView->assets as $asset) { - if ($asset->model && $asset->model->fieldset) { - foreach ($asset->model->fieldset->fields as $field) { - if ($field->display_in_user_view == '1') { - $field_array[$field->db_column] = $field->name; - } - } - } - } - array_unique($field_array); // Remove duplicate field names + $fieldArray = $this->extractCustomFields($userToView); // Pass the necessary data to the view return view('account/view-assets', [ 'user' => $userToView, // Use 'user' for compatibility with the existing view - 'field_array' => $field_array, + 'field_array' => $fieldArray, 'settings' => $settings, 'subordinates' => $subordinates, 'selectedUserId' => $selectedUserId diff --git a/app/Models/User.php b/app/Models/User.php index 7856e5e53f..43ff8fdf1f 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -964,7 +964,7 @@ class User extends SnipeModel implements AuthenticatableContract, AuthorizableCo * @param bool $includeSelf Include the current user in the results * @return \Illuminate\Support\Collection */ - public function getAllSubordinates($includeSelf = false) + public function getAllSubordinates($includeSelf=false) { $subordinates = collect(); if ($includeSelf) { @@ -981,21 +981,21 @@ class User extends SnipeModel implements AuthenticatableContract, AuthorizableCo * Recursive helper function to fetch subordinates. * * @param User $manager - * @param \Illuminate\Support\Collection $subordinatesCollection + * @param \Illuminate\Support\Collection $subs */ - protected function fetchSubordinatesRecursive(User $manager, \Illuminate\Support\Collection &$subordinatesCollection) + protected function fetchSubordinatesRecursive(User $manager, \Illuminate\Support\Collection &$subs) { // Eager load 'managesUsers' to prevent N+1 queries in recursion $directSubordinates = $manager->managesUsers()->with('managesUsers')->get(); foreach ($directSubordinates as $directSubordinate) { // Add subordinate if not already in the collection - if (!$subordinatesCollection->contains('id', $directSubordinate->id)) { - $subordinatesCollection->push($directSubordinate); + if (!$subs->contains('id', $directSubordinate->id)) { + $subs->push($directSubordinate); // Recursive call for this subordinate's subordinates - $this->fetchSubordinatesRecursive($directSubordinate, $subordinatesCollection); + $this->fetchSubordinatesRecursive($directSubordinate, $subs); } - } + } //end foreach } /** diff --git a/resources/views/account/view-assets.blade.php b/resources/views/account/view-assets.blade.php index 4c356778cf..742fab66b3 100755 --- a/resources/views/account/view-assets.blade.php +++ b/resources/views/account/view-assets.blade.php @@ -14,6 +14,7 @@
+ @csrf