2021-03-18 16:12:39 -07:00
|
|
|
<?php
|
|
|
|
|
2024-05-29 12:07:48 -07:00
|
|
|
namespace App\Livewire;
|
2021-03-18 16:12:39 -07:00
|
|
|
|
2024-08-22 12:54:54 -07:00
|
|
|
use App\Models\CustomField;
|
2024-08-13 17:01:35 -07:00
|
|
|
use Livewire\Attributes\Computed;
|
2021-03-18 16:12:39 -07:00
|
|
|
use Livewire\Component;
|
|
|
|
|
|
|
|
use App\Models\CustomFieldset;
|
|
|
|
use App\Models\AssetModel;
|
|
|
|
|
|
|
|
class CustomFieldSetDefaultValuesForModel extends Component
|
|
|
|
{
|
|
|
|
public $add_default_values;
|
|
|
|
|
|
|
|
public $fieldset_id;
|
|
|
|
public $model_id;
|
|
|
|
|
2024-08-20 15:38:43 -07:00
|
|
|
public array $selectedValues = [];
|
2024-08-14 11:01:51 -07:00
|
|
|
|
2024-08-13 17:01:35 -07:00
|
|
|
public function mount($model_id = null)
|
2021-03-18 16:12:39 -07:00
|
|
|
{
|
2024-08-13 17:01:35 -07:00
|
|
|
$this->model_id = $model_id;
|
|
|
|
$this->fieldset_id = $this->model?->fieldset_id;
|
|
|
|
$this->add_default_values = ($this->model?->defaultValues->count() > 0);
|
2024-08-14 11:01:51 -07:00
|
|
|
|
2024-08-22 17:04:04 -07:00
|
|
|
$this->initializeSelectedValuesArray();
|
|
|
|
$this->populatedSelectedValuesArray();
|
2024-08-13 17:01:35 -07:00
|
|
|
}
|
2021-11-15 21:09:35 -08:00
|
|
|
|
2024-08-13 17:01:35 -07:00
|
|
|
#[Computed]
|
|
|
|
public function model()
|
|
|
|
{
|
|
|
|
return AssetModel::find($this->model_id);
|
2021-09-30 16:21:23 -07:00
|
|
|
}
|
|
|
|
|
2024-08-13 17:01:35 -07:00
|
|
|
#[Computed]
|
2024-08-13 17:30:53 -07:00
|
|
|
public function fields()
|
2021-09-30 16:21:23 -07:00
|
|
|
{
|
2024-08-14 11:01:51 -07:00
|
|
|
$customFieldset = CustomFieldset::find($this->fieldset_id);
|
|
|
|
|
|
|
|
if ($customFieldset) {
|
|
|
|
return $customFieldset?->fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
return collect();
|
2021-09-30 16:21:23 -07:00
|
|
|
}
|
|
|
|
|
2021-03-18 16:12:39 -07:00
|
|
|
public function render()
|
|
|
|
{
|
|
|
|
return view('livewire.custom-field-set-default-values-for-model');
|
|
|
|
}
|
2024-08-22 12:54:54 -07:00
|
|
|
|
2024-08-22 17:04:04 -07:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-08-22 16:03:30 -07:00
|
|
|
private function getSelectedValueForField(CustomField $field)
|
2024-08-22 12:54:54 -07:00
|
|
|
{
|
|
|
|
$defaultValue = $field->defaultValue($this->model_id);
|
|
|
|
|
2024-08-22 13:20:04 -07:00
|
|
|
// 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')) {
|
2024-08-22 12:54:54 -07:00
|
|
|
$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)) {
|
2024-08-22 12:58:35 -07:00
|
|
|
$defaultValue = explode(', ', $defaultValue);
|
2024-08-22 12:54:54 -07:00
|
|
|
}
|
2024-08-22 12:58:35 -07:00
|
|
|
|
2024-08-22 16:03:30 -07:00
|
|
|
return $defaultValue;
|
2024-08-22 12:54:54 -07:00
|
|
|
}
|
2021-03-18 16:12:39 -07:00
|
|
|
}
|