snipe-it/app/Livewire/CustomFieldSetDefaultValuesForModel.php

68 lines
1.5 KiB
PHP
Raw Normal View History

<?php
2024-05-29 12:07:48 -07:00
namespace App\Livewire;
2024-08-15 10:32:33 -07:00
use App\Models\CustomField;
2024-08-14 11:01:51 -07:00
use Illuminate\Support\Collection;
use Livewire\Attributes\Computed;
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-14 11:01:51 -07:00
public Collection $cachedValues;
public function mount($model_id = null)
{
$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
$this->cachedValues = collect();
$this->fields->each(function ($field) {
$this->cachedValues->put($field->db_column, $field->defaultValue($this->model_id));
});
}
2024-08-15 10:43:41 -07:00
public function getFieldValue(CustomField $field)
2024-08-15 10:32:33 -07:00
{
return $this->cachedValues->get($field->db_column);
}
public function updateFieldValue($dbColumn, $updatedValue): void
{
$this->cachedValues->put($dbColumn, $updatedValue);
}
#[Computed]
public function model()
{
return AssetModel::find($this->model_id);
}
#[Computed]
2024-08-13 17:30:53 -07:00
public function fields()
{
2024-08-14 11:01:51 -07:00
$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');
}
}