2017-07-12 17:51:22 -07:00
|
|
|
<?php
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2017-07-12 17:51:22 -07:00
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
|
|
|
|
use App\Helpers\Helper;
|
2019-03-13 20:12:03 -07:00
|
|
|
use App\Http\Controllers\Controller;
|
2017-07-12 17:51:22 -07:00
|
|
|
use App\Http\Transformers\CustomFieldsetsTransformer;
|
2019-03-13 20:12:03 -07:00
|
|
|
use App\Http\Transformers\CustomFieldsTransformer;
|
|
|
|
use App\Models\CustomFieldset;
|
2023-05-06 06:36:09 -07:00
|
|
|
use App\Models\CustomField;
|
2019-03-13 20:12:03 -07:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Redirect;
|
|
|
|
use View;
|
2017-07-12 17:51:22 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This controller handles all actions related to Custom Asset Fieldsets for
|
|
|
|
* the Snipe-IT Asset Management application.
|
|
|
|
*
|
|
|
|
* @todo Improve documentation here.
|
|
|
|
* @todo Check for raw DB queries and try to convert them to query builder statements
|
|
|
|
* @version v2.0
|
|
|
|
* @author [Brady Wetherington] [<uberbrady@gmail.com>]
|
|
|
|
* @author [Josh Gibson]
|
|
|
|
*/
|
|
|
|
class CustomFieldsetsController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
2021-06-10 13:15:52 -07:00
|
|
|
* Shows the given fieldset and its fields
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @author [Josh Gibson]
|
|
|
|
* @param int $id
|
|
|
|
* @since [v1.8]
|
|
|
|
* @return View
|
|
|
|
*/
|
2017-07-12 17:51:22 -07:00
|
|
|
public function index()
|
|
|
|
{
|
2023-01-26 17:05:41 -08:00
|
|
|
$this->authorize('index', CustomField::class);
|
2018-07-24 22:40:05 -07:00
|
|
|
$fieldsets = CustomFieldset::withCount('fields as fields_count', 'models as models_count')->get();
|
2017-07-12 17:51:22 -07:00
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
return (new CustomFieldsetsTransformer)->transformCustomFieldsets($fieldsets, $fieldsets->count());
|
2017-07-12 17:51:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-06-10 13:15:52 -07:00
|
|
|
* Shows the given fieldset and its fields
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @author [Josh Gibson]
|
|
|
|
* @param int $id
|
|
|
|
* @since [v1.8]
|
|
|
|
* @return View
|
|
|
|
*/
|
2017-07-12 17:51:22 -07:00
|
|
|
public function show($id)
|
|
|
|
{
|
2023-01-26 17:05:41 -08:00
|
|
|
$this->authorize('view', CustomField::class);
|
2017-07-12 17:51:22 -07:00
|
|
|
if ($fieldset = CustomFieldset::find($id)) {
|
|
|
|
return (new CustomFieldsetsTransformer)->transformCustomFieldset($fieldset);
|
|
|
|
}
|
|
|
|
|
|
|
|
return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/custom_fields/message.fieldset.does_not_exist')), 200);
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
/**
|
2017-07-12 19:34:34 -07:00
|
|
|
* Update the specified resource in storage.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v4.0]
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function update(Request $request, $id)
|
|
|
|
{
|
2023-01-26 17:05:41 -08:00
|
|
|
$this->authorize('update', CustomField::class);
|
2017-07-12 19:34:34 -07:00
|
|
|
$fieldset = CustomFieldset::findOrFail($id);
|
|
|
|
$fieldset->fill($request->all());
|
|
|
|
|
|
|
|
if ($fieldset->save()) {
|
|
|
|
return response()->json(Helper::formatStandardApiResponse('success', $fieldset, trans('admin/custom_fields/message.fieldset.update.success')));
|
|
|
|
}
|
|
|
|
|
|
|
|
return response()->json(Helper::formatStandardApiResponse('error', null, $fieldset->getErrors()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v4.0]
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function store(Request $request)
|
|
|
|
{
|
2023-01-26 17:05:41 -08:00
|
|
|
$this->authorize('create', CustomField::class);
|
2017-07-12 19:34:34 -07:00
|
|
|
$fieldset = new CustomFieldset;
|
|
|
|
$fieldset->fill($request->all());
|
|
|
|
|
|
|
|
if ($fieldset->save()) {
|
2023-04-25 21:29:57 -07:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2017-07-12 19:34:34 -07:00
|
|
|
return response()->json(Helper::formatStandardApiResponse('success', $fieldset, trans('admin/custom_fields/message.fieldset.create.success')));
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
return response()->json(Helper::formatStandardApiResponse('error', null, $fieldset->getErrors()));
|
2017-07-12 19:34:34 -07:00
|
|
|
}
|
|
|
|
|
2017-07-12 19:22:58 -07:00
|
|
|
/**
|
|
|
|
* Delete a custom fieldset.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v4.0]
|
|
|
|
* @return Redirect
|
|
|
|
*/
|
|
|
|
public function destroy($id)
|
|
|
|
{
|
2023-01-26 17:05:41 -08:00
|
|
|
$this->authorize('delete', CustomField::class);
|
2017-07-12 19:22:58 -07:00
|
|
|
$fieldset = CustomFieldset::findOrFail($id);
|
2018-01-22 13:14:04 -08:00
|
|
|
|
2017-07-12 19:22:58 -07:00
|
|
|
$modelsCount = $fieldset->models->count();
|
|
|
|
$fieldsCount = $fieldset->fields->count();
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
if (($modelsCount > 0) || ($fieldsCount > 0)) {
|
2017-07-12 19:22:58 -07:00
|
|
|
return response()->json(Helper::formatStandardApiResponse('error', null, 'Fieldset is in use.'));
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
if ($fieldset->delete()) {
|
|
|
|
return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/custom_fields/message.fieldset.delete.success')));
|
|
|
|
}
|
2017-07-12 20:41:20 -07:00
|
|
|
|
|
|
|
return response()->json(Helper::formatStandardApiResponse('error', null, 'Unspecified error'));
|
2018-01-22 13:14:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
2023-01-26 17:05:41 -08:00
|
|
|
$this->authorize('view', CustomField::class);
|
2018-01-22 13:14:04 -08:00
|
|
|
$set = CustomFieldset::findOrFail($id);
|
2018-04-23 21:16:55 -07:00
|
|
|
$fields = $set->fields;
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2018-01-22 13:14:04 -08:00
|
|
|
return (new CustomFieldsTransformer)->transformCustomFields($fields, $fields->count());
|
2017-07-12 19:22:58 -07:00
|
|
|
}
|
|
|
|
|
2018-04-23 21:16:55 -07:00
|
|
|
/**
|
|
|
|
* Return JSON containing a list of fields belonging to a fieldset with the
|
|
|
|
* default values for a given model
|
|
|
|
*
|
|
|
|
* @param $modelId
|
|
|
|
* @param $fieldsetId
|
|
|
|
* @return string JSON
|
|
|
|
*/
|
|
|
|
public function fieldsWithDefaultValues($fieldsetId, $modelId)
|
|
|
|
{
|
2023-01-26 17:05:41 -08:00
|
|
|
$this->authorize('view', CustomField::class);
|
2018-04-23 21:16:55 -07:00
|
|
|
|
|
|
|
$set = CustomFieldset::findOrFail($fieldsetId);
|
|
|
|
|
|
|
|
$fields = $set->fields;
|
|
|
|
|
|
|
|
return (new CustomFieldsTransformer)->transformCustomFieldsWithDefaultValues($fields, $modelId, $fields->count());
|
|
|
|
}
|
2017-07-12 17:51:22 -07:00
|
|
|
}
|