From 4469db0bd3a3153ae91f802db02043ec54e82aa2 Mon Sep 17 00:00:00 2001 From: snipe Date: Mon, 5 May 2025 13:55:28 +0100 Subject: [PATCH] Livewire component for smoother check for location companies Signed-off-by: snipe --- app/Helpers/Helper.php | 20 ++++++++++++- app/Http/Controllers/SettingsController.php | 3 +- app/Livewire/LocationScopeCheck.php | 28 +++++++++++++++++++ .../lang/en-US/admin/settings/general.php | 1 + .../livewire/location-scope-check.blade.php | 11 ++++++++ resources/views/settings/general.blade.php | 14 +--------- 6 files changed, 61 insertions(+), 16 deletions(-) create mode 100644 app/Livewire/LocationScopeCheck.php create mode 100644 resources/views/livewire/location-scope-check.blade.php diff --git a/app/Helpers/Helper.php b/app/Helpers/Helper.php index c7930360bb..74296b61be 100644 --- a/app/Helpers/Helper.php +++ b/app/Helpers/Helper.php @@ -1562,6 +1562,11 @@ class Helper $locations = Location::all(); } + // Bail out early if there are no locations + if ($locations->count() == 0) { + return []; + } + foreach($locations as $location) { // in case of an update of a single location, use the newly requested company_id if ($new_company_id) { @@ -1600,14 +1605,17 @@ class Helper $items = collect([])->push($location->$keyword); } + $count = 0; foreach ($items as $item) { + if ($item && $item->company_id != $location_company) { + $mismatched[] = [ class_basename(get_class($item)), $item->id, $item->name ?? $item->asset_tag ?? $item->serial ?? $item->username, - str_replace('App\\Models\\', '', $item->assigned_type) ?? null, + $item->assigned_type ? str_replace('App\\Models\\', '', $item->assigned_type) : null, $item->company_id ?? null, $item->company->name ?? null, // $item->defaultLoc->id ?? null, @@ -1619,11 +1627,21 @@ class Helper $location_company ?? null, ]; + $count++; + + // Bail early if this is not being run via artisan + if ((!$artisan) && ($count > 0)) { + return $mismatched; + } + + + } } } } } + \Log::error(count($mismatched)); return $mismatched; } } diff --git a/app/Http/Controllers/SettingsController.php b/app/Http/Controllers/SettingsController.php index dd0b72188c..1e713c2791 100644 --- a/app/Http/Controllers/SettingsController.php +++ b/app/Http/Controllers/SettingsController.php @@ -290,8 +290,7 @@ class SettingsController extends Controller public function getSettings() : View { $setting = Setting::getSettings(); - $total_locations = count(Helper::test_locations_fmcs(false)); - return view('settings/general', compact('setting'))->with('total_locations', $total_locations); + return view('settings/general', compact('setting')); } /** diff --git a/app/Livewire/LocationScopeCheck.php b/app/Livewire/LocationScopeCheck.php new file mode 100644 index 0000000000..e176d41f0a --- /dev/null +++ b/app/Livewire/LocationScopeCheck.php @@ -0,0 +1,28 @@ +mismatched = Helper::test_locations_fmcs(false); + } + + public function mount() { + $this->setting = Setting::getSettings(); + $this->mismatched = Helper::test_locations_fmcs(false); + } + + public function render() + { + return view('livewire.location-scope-check'); + } +} diff --git a/resources/lang/en-US/admin/settings/general.php b/resources/lang/en-US/admin/settings/general.php index 74c05602fe..5d41074652 100644 --- a/resources/lang/en-US/admin/settings/general.php +++ b/resources/lang/en-US/admin/settings/general.php @@ -152,6 +152,7 @@ return [ 'full_multiple_companies_support_text' => 'Full Multiple Companies Support', 'scope_locations_fmcs_support_text' => 'Scope Locations with Full Multiple Companies Support', 'scope_locations_fmcs_support_help_text' => 'Restrict locations to their selected company.', + 'scope_locations_fmcs_check_button' => 'Check Compatibility', 'scope_locations_fmcs_support_disabled_text' => 'This option is disabled because you have conflicting locations set for :count or more items.', 'show_in_model_list' => 'Show in Model Dropdowns', 'optional' => 'optional', diff --git a/resources/views/livewire/location-scope-check.blade.php b/resources/views/livewire/location-scope-check.blade.php new file mode 100644 index 0000000000..8feb8ea25f --- /dev/null +++ b/resources/views/livewire/location-scope-check.blade.php @@ -0,0 +1,11 @@ +
+ +

+ {{ trans('admin/settings/general.scope_locations_fmcs_support_help_text') }} + {{ (count($mismatched) > 0) ? trans('admin/settings/general.scope_locations_fmcs_support_disabled_text', ['count' => count($mismatched)]) : '' }} +

+ +
\ No newline at end of file diff --git a/resources/views/settings/general.blade.php b/resources/views/settings/general.blade.php index 50399fdc8c..931fa47bcc 100644 --- a/resources/views/settings/general.blade.php +++ b/resources/views/settings/general.blade.php @@ -56,19 +56,7 @@
- - - - {!! $errors->first('scope_locations_fmcs', '') !!} -

- {{ trans('admin/settings/general.scope_locations_fmcs_support_help_text') }} - - {{ ($total_locations > 0) ? trans('admin/settings/general.scope_locations_fmcs_support_disabled_text', ['count' => $total_locations]) : '' }} - -

+