mirror of
https://github.com/snipe/snipe-it.git
synced 2024-12-25 05:34:06 -08:00
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:
parent
b346556caa
commit
7b596c750d
|
@ -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.
|
* Delete a custom field.
|
||||||
|
|
|
@ -126,7 +126,7 @@ class CustomFieldsetsController extends Controller
|
||||||
{
|
{
|
||||||
$this->authorize('delete', CustomFieldset::class);
|
$this->authorize('delete', CustomFieldset::class);
|
||||||
$fieldset = CustomFieldset::findOrFail($id);
|
$fieldset = CustomFieldset::findOrFail($id);
|
||||||
|
|
||||||
$modelsCount = $fieldset->models->count();
|
$modelsCount = $fieldset->models->count();
|
||||||
$fieldsCount = $fieldset->fields->count();
|
$fieldsCount = $fieldset->fields->count();
|
||||||
|
|
||||||
|
@ -141,7 +141,23 @@ class CustomFieldsetsController extends Controller
|
||||||
return response()->json(Helper::formatStandardApiResponse('error', null, 'Unspecified error'));
|
return response()->json(Helper::formatStandardApiResponse('error', null, 'Unspecified error'));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -219,11 +219,32 @@ Route::group(['prefix' => 'v1','namespace' => 'Api'], function () {
|
||||||
'uses' => 'CustomFieldsController@postReorder'
|
'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
|
}); // Fields group
|
||||||
|
|
||||||
|
|
||||||
/*--- Fieldsets API ---*/
|
/*--- Fieldsets API ---*/
|
||||||
|
|
||||||
|
Route::group(['prefix' => 'fieldsets'], function () {
|
||||||
|
Route::get('{fieldset}/fields',
|
||||||
|
[
|
||||||
|
'as' => 'api.fieldsets.fields',
|
||||||
|
'uses' => 'CustomFieldsetsController@fields'
|
||||||
|
]
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
Route::resource('fieldsets', 'CustomFieldsetsController',
|
Route::resource('fieldsets', 'CustomFieldsetsController',
|
||||||
[
|
[
|
||||||
'names' =>
|
'names' =>
|
||||||
|
@ -240,7 +261,6 @@ Route::group(['prefix' => 'v1','namespace' => 'Api'], function () {
|
||||||
); // Custom fieldset resource
|
); // Custom fieldset resource
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*--- Groups API ---*/
|
/*--- Groups API ---*/
|
||||||
|
|
||||||
Route::resource('groups', 'GroupsController',
|
Route::resource('groups', 'GroupsController',
|
||||||
|
|
Loading…
Reference in a new issue