From df72f92bc01c6c6199ffa865b01767d09fe857ec Mon Sep 17 00:00:00 2001 From: snipe Date: Mon, 5 Apr 2021 20:16:06 -0700 Subject: [PATCH] Brings PR #6744 up to parity with previous changes - Set custom field's default value when creating a new asset using the API Signed-off-by: snipe --- app/Http/Controllers/Api/AssetsController.php | 35 +++++++++++++++---- 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/app/Http/Controllers/Api/AssetsController.php b/app/Http/Controllers/Api/AssetsController.php index ef9a681ef7..8ac4307a55 100644 --- a/app/Http/Controllers/Api/AssetsController.php +++ b/app/Http/Controllers/Api/AssetsController.php @@ -478,13 +478,36 @@ class AssetsController extends Controller $model = AssetModel::find($request->get('model_id')); if (($model) && ($model->fieldset)) { foreach ($model->fieldset->fields as $field) { - if ($field->field_encrypted=='1') { - if (Gate::allows('admin')) { - $asset->{$field->convertUnicodeDbSlug()} = \Crypt::encrypt($request->input($field->convertUnicodeDbSlug())); - } - } else { - $asset->{$field->convertUnicodeDbSlug()} = $request->input($field->convertUnicodeDbSlug()); + + // Set the field value based on what was sent in the request + $field_val = $request->input($field->convertUnicodeDbSlug(), null); + + // If input value is null, use custom field's default value + if ($field_val == null) { + \Log::debug('Field value for '.$field->convertUnicodeDbSlug().' is null'); + $field_val = $field->defaultValue($request->get('model_id')); + \Log::debug('Use the default fieldset value of '.$field->defaultValue($request->get('model_id'))); } + + // if the field is set to encrypted, make sure we encrypt the value + if ($field->field_encrypted == '1') { + + \Log::debug('This model field is encrypted in this fieldset.'); + + if (Gate::allows('admin')) { + + // If input value is null, use custom field's default value + if (($field_val == null) && ($request->has('model_id')!='')){ + $field_val = \Crypt::encrypt($field->defaultValue($request->get('model_id'))); + } else { + $field_val = \Crypt::encrypt($request->input($field->convertUnicodeDbSlug())); + } + } + } + + + $asset->{$field->convertUnicodeDbSlug()} = $field_val; + } }