Skip to content

Commit ada8195

Browse files
authored
Merge pull request #16860 from grokability/better_location_scope_check
Livewire component for smoother check for location companies
2 parents 5e2dba5 + 83d6e9a commit ada8195

File tree

6 files changed

+60
-16
lines changed

6 files changed

+60
-16
lines changed

app/Helpers/Helper.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1562,6 +1562,11 @@ static public function test_locations_fmcs($artisan, $location_id = null, $new_c
15621562
$locations = Location::all();
15631563
}
15641564

1565+
// Bail out early if there are no locations
1566+
if ($locations->count() == 0) {
1567+
return [];
1568+
}
1569+
15651570
foreach($locations as $location) {
15661571
// in case of an update of a single location, use the newly requested company_id
15671572
if ($new_company_id) {
@@ -1600,14 +1605,17 @@ static public function test_locations_fmcs($artisan, $location_id = null, $new_c
16001605
$items = collect([])->push($location->$keyword);
16011606
}
16021607

1608+
$count = 0;
16031609
foreach ($items as $item) {
16041610

1611+
16051612
if ($item && $item->company_id != $location_company) {
1613+
16061614
$mismatched[] = [
16071615
class_basename(get_class($item)),
16081616
$item->id,
16091617
$item->name ?? $item->asset_tag ?? $item->serial ?? $item->username,
1610-
str_replace('App\\Models\\', '', $item->assigned_type) ?? null,
1618+
$item->assigned_type ? str_replace('App\\Models\\', '', $item->assigned_type) : null,
16111619
$item->company_id ?? null,
16121620
$item->company->name ?? null,
16131621
// $item->defaultLoc->id ?? null,
@@ -1619,6 +1627,15 @@ class_basename(get_class($item)),
16191627
$location_company ?? null,
16201628
];
16211629

1630+
$count++;
1631+
1632+
// Bail early if this is not being run via artisan
1633+
if ((!$artisan) && ($count > 0)) {
1634+
return $mismatched;
1635+
}
1636+
1637+
1638+
16221639
}
16231640
}
16241641
}

app/Http/Controllers/SettingsController.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,7 @@ public function index() : View
290290
public function getSettings() : View
291291
{
292292
$setting = Setting::getSettings();
293-
$total_locations = count(Helper::test_locations_fmcs(false));
294-
return view('settings/general', compact('setting'))->with('total_locations', $total_locations);
293+
return view('settings/general', compact('setting'));
295294
}
296295

297296
/**
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace App\Livewire;
4+
5+
use App\Helpers\Helper;
6+
use App\Models\Setting;
7+
use Livewire\Component;
8+
9+
class LocationScopeCheck extends Component
10+
{
11+
public $mismatched = [];
12+
public $setting;
13+
14+
public function check_locations()
15+
{
16+
$this->mismatched = Helper::test_locations_fmcs(false);
17+
}
18+
19+
public function mount() {
20+
$this->setting = Setting::getSettings();
21+
$this->mismatched = Helper::test_locations_fmcs(false);
22+
}
23+
24+
public function render()
25+
{
26+
return view('livewire.location-scope-check');
27+
}
28+
}

resources/lang/en-US/admin/settings/general.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@
152152
'full_multiple_companies_support_text' => 'Full Multiple Companies Support',
153153
'scope_locations_fmcs_support_text' => 'Scope Locations with Full Multiple Companies Support',
154154
'scope_locations_fmcs_support_help_text' => 'Restrict locations to their selected company.',
155+
'scope_locations_fmcs_check_button' => 'Check Compatibility',
155156
'scope_locations_fmcs_support_disabled_text' => 'This option is disabled because you have conflicting locations set for :count or more items.',
156157
'show_in_model_list' => 'Show in Model Dropdowns',
157158
'optional' => 'optional',
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<div>
2+
<label class="form-control{{ (count($mismatched) > 0) ? ' form-control--disabled' : '' }}">
3+
<input type="checkbox" name="scope_locations_fmcs" value="1" @checked(old('scope_locations_fmcs', $setting->scope_locations_fmcs)) aria-label="scope_locations_fmcs" {{ (count($mismatched) > 0) ? ' disabled' : '' }}/>
4+
{{ trans('admin/settings/general.scope_locations_fmcs_support_text') }}
5+
</label>
6+
<p class="help-block">
7+
{{ trans('admin/settings/general.scope_locations_fmcs_support_help_text') }}
8+
<strong>{{ (count($mismatched) > 0) ? trans('admin/settings/general.scope_locations_fmcs_support_disabled_text', ['count' => count($mismatched)]) : '' }}</strong>
9+
</p>
10+
<button class="btn btn-sm btn-default" wire:click.prevent="check_locations">{{ trans('admin/settings/general.scope_locations_fmcs_check_button') }}</button>
11+
</div>

resources/views/settings/general.blade.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,7 @@
5656
<!-- Scope Locations with Full Multiple Companies Support -->
5757
<div class="form-group {{ $errors->has('scope_locations_fmcs') ? 'error' : '' }}">
5858
<div class="col-md-8 col-md-offset-3">
59-
<label class="form-control{{ ($total_locations > 0) ? ' form-control--disabled' : '' }}">
60-
<input type="checkbox" name="scope_locations_fmcs" value="1" @checked(old('scope_locations_fmcs', $setting->scope_locations_fmcs)) aria-label="scope_locations_fmcs" {{ ($total_locations > 0) ? ' disabled' : '' }}/>
61-
{{ trans('admin/settings/general.scope_locations_fmcs_support_text') }}
62-
</label>
63-
64-
65-
{!! $errors->first('scope_locations_fmcs', '<span class="alert-msg" aria-hidden="true">:message</span>') !!}
66-
<p class="help-block">
67-
{{ trans('admin/settings/general.scope_locations_fmcs_support_help_text') }}
68-
69-
<strong>{{ ($total_locations > 0) ? trans('admin/settings/general.scope_locations_fmcs_support_disabled_text', ['count' => $total_locations]) : '' }}</strong>
70-
71-
</p>
59+
<livewire:location-scope-check />
7260
</div>
7361
</div>
7462
<!-- /.form-group -->

0 commit comments

Comments
 (0)