From 8f64261c68236b661fd9374a47e157b9fed319ec Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 28 May 2026 13:14:57 +0100 Subject: [PATCH] Trait-ify custom fields --- app/Models/Asset.php | 73 ++------------------------- app/Models/Traits/HasCustomFields.php | 66 ++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 69 deletions(-) create mode 100644 app/Models/Traits/HasCustomFields.php diff --git a/app/Models/Asset.php b/app/Models/Asset.php index 3f79efad32..9250649b7f 100644 --- a/app/Models/Asset.php +++ b/app/Models/Asset.php @@ -8,6 +8,7 @@ use App\Helpers\Helper; use App\Http\Traits\UniqueUndeletedTrait; use App\Models\Traits\Acceptable; use App\Models\Traits\CompanyableTrait; +use App\Models\Traits\HasCustomFields; use App\Models\Traits\HasUploads; use App\Models\Traits\Loggable; use App\Models\Traits\Requestable; @@ -20,7 +21,6 @@ use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Database\Eloquent\SoftDeletes; -use Illuminate\Support\Facades\Crypt; use Illuminate\Support\Facades\Gate; use Illuminate\Support\Facades\Storage; use Watson\Validating\ValidatingTrait; @@ -37,6 +37,7 @@ class Asset extends Depreciable protected $with = ['model', 'adminuser', 'location', 'company']; use CompanyableTrait; + use HasCustomFields; use HasFactory; use HasUploads; use Loggable; @@ -252,41 +253,9 @@ class Asset extends Depreciable $this->attributes['expected_checkin'] = $value; } - public function customFieldValidationRules() + protected function getCustomFieldset(): ?CustomFieldset { - - $customFieldValidationRules = []; - - if (($this->model) && ($this->model->fieldset)) { - - foreach ($this->model->fieldset->fields as $field) { - - // this just casts booleans that may come through as strings to an actual boolean type - // adding !$field->field_encrypted because when the encrypted value comes through it - // screws things up for the encrypted validation rules (and the encrypted string - // is not a valid boolean type) - if ($field->format == 'BOOLEAN' && ! $field->field_encrypted) { - $this->{$field->db_column} = filter_var($this->{$field->db_column}, FILTER_VALIDATE_BOOLEAN); - } - } - - $customFieldValidationRules += $this->model->fieldset->validation_rules(); - } - - return $customFieldValidationRules; - - } - - /** - * This handles the custom field validation for assets - * - * @var array - */ - public function save(array $params = []) - { - $this->rules += $this->customFieldValidationRules(); - - return parent::save($params); + return $this->model?->fieldset ?? null; } public function getDisplayNameAttribute() @@ -608,40 +577,6 @@ class Asset extends Depreciable return $this->rules; } - public function customFieldsForCheckinCheckout($checkin_checkout) - { - // Check to see if any of the custom fields were included on the form and if they have any values - if (($this->model) && ($this->model->fieldset) && ($this->model->fieldset->fields)) { - - foreach ($this->model->fieldset->fields as $field) { - - if (($field->{$checkin_checkout} == 1) && (request()->has($field->db_column))) { - - if ($field->field_encrypted == '1') { - - if (Gate::allows('assets.view.encrypted_custom_fields')) { - if (is_array(request()->input($field->db_column))) { - $this->{$field->db_column} = Crypt::encrypt(implode(', ', request()->input($field->db_column))); - } else { - $this->{$field->db_column} = Crypt::encrypt(request()->input($field->db_column)); - } - } - - } else { - - if (is_array(request()->input($field->db_column))) { - $this->{$field->db_column} = implode(', ', request()->input($field->db_column)); - } else { - $this->{$field->db_column} = request()->input($field->db_column); - } - - } - } - } - } - - } - public function manufacturer() { return $this->hasOneThrough(Manufacturer::class, AssetModel::class, 'id', 'id', 'model_id', 'manufacturer_id'); diff --git a/app/Models/Traits/HasCustomFields.php b/app/Models/Traits/HasCustomFields.php new file mode 100644 index 0000000000..28615a174c --- /dev/null +++ b/app/Models/Traits/HasCustomFields.php @@ -0,0 +1,66 @@ +getCustomFieldset(); + + if (! $fieldset) { + return []; + } + + foreach ($fieldset->fields as $field) { + if ($field->format === 'BOOLEAN' && ! $field->field_encrypted) { + $this->{$field->db_column} = filter_var($this->{$field->db_column}, FILTER_VALIDATE_BOOLEAN); + } + } + + return $fieldset->validation_rules(); + } + + public function save(array $params = []) + { + $this->rules += $this->customFieldValidationRules(); + + return parent::save($params); + } + + public function customFieldsForCheckinCheckout(string $checkin_checkout): void + { + $fieldset = $this->getCustomFieldset(); + + if (! $fieldset?->fields) { + return; + } + + foreach ($fieldset->fields as $field) { + if (($field->{$checkin_checkout} == 1) && request()->has($field->db_column)) { + if ($field->field_encrypted == '1') { + if (Gate::allows('assets.view.encrypted_custom_fields')) { + $value = request()->input($field->db_column); + $this->{$field->db_column} = Crypt::encrypt(is_array($value) ? implode(', ', $value) : $value); + } + } else { + $value = request()->input($field->db_column); + $this->{$field->db_column} = is_array($value) ? implode(', ', $value) : $value; + } + } + } + } +}