API for (dis)associating fields with fieldsets (#4881)

* start work on fields in fieldset api

* revert CustomFieldsetsController

* fieldset associate / disassociate api

* fix variable names and payload

* fix variable name
This commit is contained in:
vcordes79 2018-01-22 22:14:04 +01:00 committed by snipe
parent b346556caa
commit 7b596c750d
3 changed files with 71 additions and 3 deletions

View file

@ -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.

View file

@ -144,4 +144,20 @@ class CustomFieldsetsController extends Controller
}
/**
* Return JSON containing a list of fields belonging to a fieldset.
*
* @author [V. Cordes] [<volker@fdatek.de>]
* @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());
}
}

View file

@ -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',