diff --git a/app/Livewire/CustomFieldSetDefaultValuesForModel.php b/app/Livewire/CustomFieldSetDefaultValuesForModel.php index a4a9f9fe71..0ca733eb24 100644 --- a/app/Livewire/CustomFieldSetDefaultValuesForModel.php +++ b/app/Livewire/CustomFieldSetDefaultValuesForModel.php @@ -2,6 +2,8 @@ namespace App\Livewire; +use App\Models\CustomField; +use Livewire\Attributes\Computed; use Livewire\Component; use App\Models\CustomFieldset; @@ -12,37 +14,95 @@ class CustomFieldSetDefaultValuesForModel extends Component public $add_default_values; public $fieldset_id; - public $fields; public $model_id; - public function mount() + public array $selectedValues = []; + + public function mount($model_id = null) { - if(is_null($this->model_id)){ - return; - } - - $this->model = AssetModel::find($this->model_id); // It's possible to do some clever route-model binding here, but let's keep it simple, shall we? - $this->fieldset_id = $this->model->fieldset_id; + $this->model_id = $model_id; + $this->fieldset_id = $this->model?->fieldset_id; + $this->add_default_values = ($this->model?->defaultValues->count() > 0); - $this->fields = null; - - if ($fieldset = CustomFieldset::find($this->fieldset_id)) { - $this->fields = CustomFieldset::find($this->fieldset_id)->fields; - } - - $this->add_default_values = ($this->model->defaultValues->count() > 0); + $this->initializeSelectedValuesArray(); + $this->populatedSelectedValuesArray(); } - public function updatedFieldsetId() + #[Computed] + public function model() { - if (CustomFieldset::find($this->fieldset_id)) { - $this->fields = CustomFieldset::find($this->fieldset_id)->fields; + return AssetModel::find($this->model_id); + } + + #[Computed] + public function fields() + { + $customFieldset = CustomFieldset::find($this->fieldset_id); + + if ($customFieldset) { + return $customFieldset?->fields; } - + + return collect(); } public function render() { return view('livewire.custom-field-set-default-values-for-model'); } + + /** + * Livewire property binding plays nicer with arrays when it knows + * which keys will be present instead of them being + * dynamically added (this is especially true for checkboxes). + * + * Let's go ahead and initialize selectedValues with all the potential keys (custom field db_columns). + * + * @return void + */ + private function initializeSelectedValuesArray(): void + { + CustomField::all()->each(function ($field) { + $this->selectedValues[$field->db_column] = null; + + if ($field->element === 'checkbox') { + $this->selectedValues[$field->db_column] = []; + } + }); + } + + /** + * Populate the selectedValues array with the + * default values or old input for each field. + * + * @return void + */ + private function populatedSelectedValuesArray(): void + { + $this->fields->each(function ($field) { + $this->selectedValues[$field->db_column] = $this->getSelectedValueForField($field); + }); + } + + private function getSelectedValueForField(CustomField $field) + { + $defaultValue = $field->defaultValue($this->model_id); + + // if old() contains a value for default_values that means + // the user has submitted the form and we were redirected + // back with the old input. + // Let's use what they had previously set. + if (old('default_values')) { + $defaultValue = old('default_values.' . $field->id); + } + + // on first load the default value for checkboxes will be + // a comma-separated string but if we're loading the page + // with old input then it was already parsed into an array. + if ($field->element === 'checkbox' && is_string($defaultValue)) { + $defaultValue = explode(', ', $defaultValue); + } + + return $defaultValue; + } } diff --git a/resources/views/account/view-assets.blade.php b/resources/views/account/view-assets.blade.php index b973ad2bd0..96b95484a6 100755 --- a/resources/views/account/view-assets.blade.php +++ b/resources/views/account/view-assets.blade.php @@ -427,7 +427,7 @@ {{ trans('general.purchase_cost') }} @endcan - + {{ trans('admin/hardware/form.eol_date') }} diff --git a/resources/views/livewire/custom-field-set-default-values-for-model.blade.php b/resources/views/livewire/custom-field-set-default-values-for-model.blade.php index d2118e5004..8f68644099 100644 --- a/resources/views/livewire/custom-field-set-default-values-for-model.blade.php +++ b/resources/views/livewire/custom-field-set-default-values-for-model.blade.php @@ -9,18 +9,20 @@ {!! $errors->first('custom_fieldset', '') !!}
+ @if ($fieldset_id) + {{ Form::checkbox('add_default_values', 1, old('add_default_values', $add_default_values), ['data-livewire-component' => $this->getId(), 'id' => 'add_default_values', 'wire:model.live' => 'add_default_values', 'disabled' => $this->fields->isEmpty()]) }} + {{ trans('admin/models/general.add_default_values') }} + + @endif
- @if ($this->add_default_values ) {{-- 'if the checkbox is enabled *AND* there are more than 0 fields in the fieldsset' --}} - @if ($fields) + @if ($add_default_values) + @if ($this->fields) - @foreach ($fields as $field) -
+ @foreach ($this->fields as $field) +
@@ -30,7 +32,17 @@
- +
@@ -38,22 +50,39 @@ @elseif ($field->element == "text") - + @elseif($field->element == "textarea") - + @elseif($field->element == "listbox") - @foreach(explode("\r\n", $field->field_values) as $field_value) - + @endforeach @@ -61,16 +90,30 @@ @elseif($field->element == "radio") @foreach(explode("\r\n", $field->field_values) as $field_value) - + @endforeach @elseif($field->element == "checkbox") @foreach(explode("\r\n", $field->field_values) as $field_value) -
+ @endforeach - - @endif + + @endif @endif