Merge remote-tracking branch 'origin/develop'
Some checks failed
CodeQL Security Scan / CodeQL Security Scan (javascript) (push) Has been cancelled
Codacy Security Scan / Codacy Security Scan (push) Has been cancelled
Docker images (Alpine) / docker (push) Has been cancelled
Docker images / docker (push) Has been cancelled
Tests in MySQL / PHP ${{ matrix.php-version }} (8.1) (push) Has been cancelled
Tests in MySQL / PHP ${{ matrix.php-version }} (8.2) (push) Has been cancelled
Tests in MySQL / PHP ${{ matrix.php-version }} (8.3) (push) Has been cancelled
Tests in SQLite / PHP ${{ matrix.php-version }} (8.1.1) (push) Has been cancelled

This commit is contained in:
snipe 2024-09-11 12:25:28 +01:00
commit 7e14f8c6d6
3 changed files with 143 additions and 40 deletions

View file

@ -2,6 +2,8 @@
namespace App\Livewire; namespace App\Livewire;
use App\Models\CustomField;
use Livewire\Attributes\Computed;
use Livewire\Component; use Livewire\Component;
use App\Models\CustomFieldset; use App\Models\CustomFieldset;
@ -12,37 +14,95 @@ class CustomFieldSetDefaultValuesForModel extends Component
public $add_default_values; public $add_default_values;
public $fieldset_id; public $fieldset_id;
public $fields;
public $model_id; public $model_id;
public function mount() public array $selectedValues = [];
public function mount($model_id = null)
{ {
if(is_null($this->model_id)){ $this->model_id = $model_id;
return; $this->fieldset_id = $this->model?->fieldset_id;
} $this->add_default_values = ($this->model?->defaultValues->count() > 0);
$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->fields = null; $this->initializeSelectedValuesArray();
$this->populatedSelectedValuesArray();
if ($fieldset = CustomFieldset::find($this->fieldset_id)) {
$this->fields = CustomFieldset::find($this->fieldset_id)->fields;
}
$this->add_default_values = ($this->model->defaultValues->count() > 0);
} }
public function updatedFieldsetId() #[Computed]
public function model()
{ {
if (CustomFieldset::find($this->fieldset_id)) { return AssetModel::find($this->model_id);
$this->fields = CustomFieldset::find($this->fieldset_id)->fields; }
#[Computed]
public function fields()
{
$customFieldset = CustomFieldset::find($this->fieldset_id);
if ($customFieldset) {
return $customFieldset?->fields;
} }
return collect();
} }
public function render() public function render()
{ {
return view('livewire.custom-field-set-default-values-for-model'); 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;
}
} }

View file

@ -427,7 +427,7 @@
{{ trans('general.purchase_cost') }} {{ trans('general.purchase_cost') }}
</th> </th>
@endcan @endcan
<th class="col-md-2" data-switchable="true" data-visible="false"> <th class="col-md-2" data-switchable="true" data-visible="true">
{{ trans('admin/hardware/form.eol_date') }} {{ trans('admin/hardware/form.eol_date') }}
</th> </th>
<th class="col-md-2" data-switchable="true" data-visible="false"> <th class="col-md-2" data-switchable="true" data-visible="false">

View file

@ -9,18 +9,20 @@
{!! $errors->first('custom_fieldset', '<span class="alert-msg" aria-hidden="true"><br><i class="fas fa-times"></i> :message</span>') !!} {!! $errors->first('custom_fieldset', '<span class="alert-msg" aria-hidden="true"><br><i class="fas fa-times"></i> :message</span>') !!}
</div> </div>
<div class="col-md-3"> <div class="col-md-3">
@if ($fieldset_id)
<label class="form-control"> <label class="form-control">
{{ 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']) }} {{ 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') }} {{ trans('admin/models/general.add_default_values') }}
</label> </label>
@endif
</div> </div>
</div> </div>
@if ($this->add_default_values ) {{-- 'if the checkbox is enabled *AND* there are more than 0 fields in the fieldsset' --}} @if ($add_default_values)
@if ($fields) @if ($this->fields)
@foreach ($fields as $field) @foreach ($this->fields as $field)
<div class="form-group"> <div class="form-group" wire:key="field-{{ $field->id }}">
<label class="col-md-3 control-label{{ $errors->has($field->name) ? ' has-error' : '' }}">{{ $field->name }}</label> <label class="col-md-3 control-label{{ $errors->has($field->name) ? ' has-error' : '' }}">{{ $field->name }}</label>
@ -30,7 +32,17 @@
<div class="input-group col-md-4" style="padding-left: 0px;"> <div class="input-group col-md-4" style="padding-left: 0px;">
<div class="input-group date" data-provide="datepicker" data-date-format="yyyy-mm-dd" data-autoclose="true"> <div class="input-group date" data-provide="datepicker" data-date-format="yyyy-mm-dd" data-autoclose="true">
<input type="text" class="form-control" placeholder="{{ trans('general.select_date') }}" name="default_values[{{ $field->id }}]" id="default-value{{ $field->id }}" value="{{ $field->defaultValue($model_id) }}"> <input
type="text"
class="form-control"
placeholder="{{ trans('general.select_date') }}"
name="default_values[{{ $field->id }}]"
id="default-value{{ $field->id }}"
wire:model="selectedValues.{{ $field->db_column }}"
{{-- catch the onchange event and dispatch an InputEvent ourselves so Livewire can react to it... --}}
{{-- https://laracasts.com/discuss/channels/livewire/livewire-and-bootstrap-datepicker?page=1&replyId=623122--}}
onchange="this.dispatchEvent(new InputEvent('input'))"
>
<span class="input-group-addon"><x-icon type="calendar" /></span> <span class="input-group-addon"><x-icon type="calendar" /></span>
</div> </div>
</div> </div>
@ -38,22 +50,39 @@
@elseif ($field->element == "text") @elseif ($field->element == "text")
<input class="form-control" type="text" value="{{ $field->defaultValue($model_id) }}" id="default-value{{ $field->id }}" name="default_values[{{ $field->id }}]"> <input
class="form-control"
type="text"
id="default-value{{ $field->id }}"
name="default_values[{{ $field->id }}]"
wire:model="selectedValues.{{ $field->db_column }}"
/>
@elseif($field->element == "textarea") @elseif($field->element == "textarea")
<textarea class="form-control" style="width: 100%;" id="default-value{{ $field->id }}" name="default_values[{{ $field->id }}]">{{ $field->defaultValue($model_id) }}</textarea> <textarea
class="form-control"
style="width: 100%;"
id="default-value{{ $field->id }}"
name="default_values[{{ $field->id }}]"
wire:model="selectedValues.{{ $field->db_column }}"
></textarea>
@elseif($field->element == "listbox") @elseif($field->element == "listbox")
<select class="form-control" name="default_values[{{ $field->id }}]"> <select class="form-control" name="default_values[{{ $field->id }}]" wire:model="selectedValues.{{ $field->db_column }}">
<option value=""></option> <option value=""></option>
@foreach(explode("\r\n", $field->field_values) as $field_value) @foreach(explode("\r\n", $field->field_values) as $field_value)
<option value="{{$field_value}}" {{ $field->defaultValue($model_id) == $field_value ? 'selected="selected"': '' }}>{{ $field_value }}</option> <option
value="{{$field_value}}"
wire:key="listbox-{{ $field_value }}"
>
{{ $field_value }}
</option>
@endforeach @endforeach
</select> </select>
@ -61,16 +90,30 @@
@elseif($field->element == "radio") @elseif($field->element == "radio")
@foreach(explode("\r\n", $field->field_values) as $field_value) @foreach(explode("\r\n", $field->field_values) as $field_value)
<label class="col-md-3 form-control" for="{{ str_slug($field_value) }}"> <label class="col-md-3 form-control" for="{{ $field->db_column }}_{{ str_slug($field_value) }}" wire:key="radio-{{ $field_value }}">
<input id="{{ str_slug($field_value) }}" aria-label="{{ str_slug($field->name) }}" type='radio' name="default_values[{{ $field->id }}]" value="{{$field_value}}" {{ $field->defaultValue($model_id) == $field_value ? 'checked="checked"': '' }} />{{ $field_value }} <input
</label> id="{{ $field->db_column }}_{{ str_slug($field_value) }}"
aria-label="{{ str_slug($field->name) }}"
type="radio"
name="default_values[{{ $field->id }}]"
value="{{$field_value}}"
wire:model="selectedValues.{{ $field->db_column }}"
/>{{ $field_value }}
</label>
@endforeach @endforeach
@elseif($field->element == "checkbox") @elseif($field->element == "checkbox")
@foreach(explode("\r\n", $field->field_values) as $field_value) @foreach(explode("\r\n", $field->field_values) as $field_value)
<label class="col-md-3 form-control" for="{{ str_slug($field_value) }}"> <label class="col-md-3 form-control" for="{{ $field->db_column }}_{{ str_slug($field_value) }}" wire:key="checkbox-{{ $field_value }}">
<input id="{{ str_slug($field_value) }}" type="checkbox" aria-label="{{ str_slug($field->name) }}" name="default_values[{{ $field->id }}][]" value="{{ $field_value }}"{{ in_array($field_value, explode(', ',$field->defaultValue($model_id))) ? ' checked="checked"': '' }}> {{ $field_value }} <input
id="{{ $field->db_column }}_{{ str_slug($field_value) }}"
type="checkbox"
aria-label="{{ str_slug($field->name) }}"
name="default_values[{{ $field->id }}][]"
value="{{ $field_value }}"
wire:model="selectedValues.{{ $field->db_column }}"
> {{ $field_value }}
</label> </label>
@endforeach @endforeach
@ -80,12 +123,12 @@
Unknown field element: {{ $field->element }} Unknown field element: {{ $field->element }}
</span> </span>
@endif @endif
</div>
</div> </div>
</div>
@endforeach @endforeach
</div>
@endif @endif
@endif @endif
</span> </span>