From 15dc4e4779cb687f50c9c1a1549816a11785be3f Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 25 Apr 2023 21:29:57 -0700 Subject: [PATCH] Added fieldset sync Signed-off-by: snipe --- .../Api/CustomFieldsetsController.php | 12 +++++++++++ .../Controllers/CustomFieldsetsController.php | 21 ++++++++++++++----- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/app/Http/Controllers/Api/CustomFieldsetsController.php b/app/Http/Controllers/Api/CustomFieldsetsController.php index 27da7733cd..12436ebbfa 100644 --- a/app/Http/Controllers/Api/CustomFieldsetsController.php +++ b/app/Http/Controllers/Api/CustomFieldsetsController.php @@ -94,6 +94,18 @@ class CustomFieldsetsController extends Controller $fieldset->fill($request->all()); if ($fieldset->save()) { + // Sync fieldset with auto_add_to_fieldsets + $fields = CustomField::select('id')->where('auto_add_to_fieldsets', '=', '1')->get(); + + if ($fields->count() > 0) { + + foreach ($fields as $field) { + $field_ids[] = $field->id; + } + + $fieldset->fields()->sync($field_ids); + } + return response()->json(Helper::formatStandardApiResponse('success', $fieldset, trans('admin/custom_fields/message.fieldset.create.success'))); } diff --git a/app/Http/Controllers/CustomFieldsetsController.php b/app/Http/Controllers/CustomFieldsetsController.php index 8c14502285..dbee97b776 100644 --- a/app/Http/Controllers/CustomFieldsetsController.php +++ b/app/Http/Controllers/CustomFieldsetsController.php @@ -93,16 +93,27 @@ class CustomFieldsetsController extends Controller { $this->authorize('create', CustomField::class); - $cfset = new CustomFieldset([ + $fieldset = new CustomFieldset([ 'name' => e($request->get('name')), 'user_id' => Auth::user()->id, ]); - $validator = Validator::make($request->all(), $cfset->rules); - if ($validator->passes()) { - $cfset->save(); + $validator = Validator::make($request->all(), $fieldset->rules); - return redirect()->route('fieldsets.show', [$cfset->id]) + if ($validator->passes()) { + $fieldset->save(); + + // Sync fieldset with auto_add_to_fieldsets + $fields = CustomField::select('id')->where('auto_add_to_fieldsets', '=', '1')->get(); + if ($fields->count() > 0) { + foreach ($fields as $field) { + $field_ids[] = $field->id; + } + + $fieldset->fields()->sync($field_ids); + } + + return redirect()->route('fieldsets.show', [$fieldset->id]) ->with('success', trans('admin/custom_fields/message.fieldset.create.success')); }