diff --git a/app/Http/Controllers/Api/CustomFieldsController.php b/app/Http/Controllers/Api/CustomFieldsController.php index fb0493046f..da27ba02d2 100644 --- a/app/Http/Controllers/Api/CustomFieldsController.php +++ b/app/Http/Controllers/Api/CustomFieldsController.php @@ -125,6 +125,38 @@ class CustomFieldsController extends Controller } + public function associate(Request $request, $field_id) + { + $this->authorize('edit', CustomFieldset::class); + $field = CustomField::findOrFail($field_id); + + $fieldset_id = $request->input('fieldset_id'); + foreach ($field->fieldset as $fieldset) { + if ($fieldset->id == $fieldset_id) { + return response()->json(Helper::formatStandardApiResponse('success', $fieldset, trans('admin/custom_fields/message.fieldset.update.success'))); + } + } + + $fieldset = CustomFieldset::findOrFail($fieldset_id); + $fieldset->fields()->attach($field->id, ["required" => ($request->input('required') == "on"), "order" => $request->input('order', $fieldset->fields->count())]); + return response()->json(Helper::formatStandardApiResponse('success', $fieldset, trans('admin/custom_fields/message.fieldset.update.success'))); + } + + public function disassociate(Request $request, $field_id) + { + $this->authorize('edit', CustomFieldset::class); + $field = CustomField::findOrFail($field_id); + + $fieldset_id = $request->input('fieldset_id'); + foreach ($field->fieldset as $fieldset) { + if ($fieldset->id == $fieldset_id) { + $fieldset->fields()->detach($field->id); + return response()->json(Helper::formatStandardApiResponse('success', $fieldset, trans('admin/custom_fields/message.fieldset.update.success'))); + } + } + $fieldset = CustomFieldset::findOrFail($fieldset_id); + return response()->json(Helper::formatStandardApiResponse('success', $fieldset, trans('admin/custom_fields/message.fieldset.update.success'))); + } /** * Delete a custom field. diff --git a/app/Http/Controllers/Api/CustomFieldsetsController.php b/app/Http/Controllers/Api/CustomFieldsetsController.php index 3b82d0e5eb..915518e945 100644 --- a/app/Http/Controllers/Api/CustomFieldsetsController.php +++ b/app/Http/Controllers/Api/CustomFieldsetsController.php @@ -126,7 +126,7 @@ class CustomFieldsetsController extends Controller { $this->authorize('delete', CustomFieldset::class); $fieldset = CustomFieldset::findOrFail($id); - + $modelsCount = $fieldset->models->count(); $fieldsCount = $fieldset->fields->count(); @@ -141,7 +141,23 @@ class CustomFieldsetsController extends Controller return response()->json(Helper::formatStandardApiResponse('error', null, 'Unspecified error')); - + + } + + /** + * Return JSON containing a list of fields belonging to a fieldset. + * + * @author [V. Cordes] [] + * @since [v4.1.10] + * @param $fieldsetId + * @return string JSON + */ + public function fields($id) + { + $this->authorize('view', CustomFieldset::class); + $set = CustomFieldset::findOrFail($id); + $fields = $set->fields->get(); + return (new CustomFieldsTransformer)->transformCustomFields($fields, $fields->count()); } } diff --git a/routes/api.php b/routes/api.php index 36646956c3..5b40657a74 100644 --- a/routes/api.php +++ b/routes/api.php @@ -219,11 +219,32 @@ Route::group(['prefix' => 'v1','namespace' => 'Api'], function () { 'uses' => 'CustomFieldsController@postReorder' ] ); + Route::post('{field}/associate', + [ + 'as' => 'api.customfields.associate', + 'uses' => 'CustomFieldsController@associate' + ] + ); + Route::post('{field}/disassociate', + [ + 'as' => 'api.customfields.disassociate', + 'uses' => 'CustomFieldsController@disassociate' + ] + ); }); // Fields group /*--- Fieldsets API ---*/ + Route::group(['prefix' => 'fieldsets'], function () { + Route::get('{fieldset}/fields', + [ + 'as' => 'api.fieldsets.fields', + 'uses' => 'CustomFieldsetsController@fields' + ] + ); + }); + Route::resource('fieldsets', 'CustomFieldsetsController', [ 'names' => @@ -240,7 +261,6 @@ Route::group(['prefix' => 'v1','namespace' => 'Api'], function () { ); // Custom fieldset resource - /*--- Groups API ---*/ Route::resource('groups', 'GroupsController',