mirror of
https://github.com/snipe/snipe-it.git
synced 2024-11-09 23:24:06 -08:00
* add fields api * change route names
This commit is contained in:
parent
409f5cc4fd
commit
94cf1f8741
|
@ -2,11 +2,14 @@
|
|||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Helpers\Helper;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Transformers\CustomFieldsTransformer;
|
||||
use App\Models\CustomField;
|
||||
use App\Models\CustomFieldset;
|
||||
use Illuminate\Http\Request;
|
||||
use Validator;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class CustomFieldsController extends Controller
|
||||
{
|
||||
|
@ -27,6 +30,81 @@ class CustomFieldsController extends Controller
|
|||
$total = count($fields);
|
||||
return (new CustomFieldsTransformer)->transformCustomFields($fields, $total);
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows the given field
|
||||
* @author [V. Cordes] [<volker@fdatek.de>]
|
||||
* @param int $id
|
||||
* @since [v4.1.10]
|
||||
* @return View
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
$this->authorize('show', CustomField::class);
|
||||
if ($field = CustomField::find($id)) {
|
||||
return (new CustomFieldsTransformer)->transformCustomField($field);
|
||||
}
|
||||
|
||||
return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/custom_fields/message.field.invalid')), 200);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified field
|
||||
*
|
||||
* @author [V. Cordes] [<volker@fdatek.de>]
|
||||
* @since [v4.1.10]
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
$this->authorize('edit', CustomField::class);
|
||||
$field = CustomField::findOrFail($id);
|
||||
$data = $request->all();
|
||||
|
||||
$validator = Validator::make($data, $field->validationRules());
|
||||
if ($validator->fails()) {
|
||||
return response()->json(Helper::formatStandardApiResponse('error', null, $validator->errors()));
|
||||
}
|
||||
|
||||
$field->fill($data);
|
||||
|
||||
if ($field->save()) {
|
||||
return response()->json(Helper::formatStandardApiResponse('success', $field, trans('admin/custom_fields/message.field.update.success')));
|
||||
}
|
||||
|
||||
return response()->json(Helper::formatStandardApiResponse('error', null, $field->getErrors()));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Store a newly created field.
|
||||
*
|
||||
* @author [V. Cordes] [<volker@fdatek.de>]
|
||||
* @since [v4.1.10]
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$this->authorize('create', CustomField::class);
|
||||
$field = new CustomField;
|
||||
|
||||
$data = $request->all();
|
||||
$validator = Validator::make($data, $field->validationRules());
|
||||
if ($validator->fails()) {
|
||||
return response()->json(Helper::formatStandardApiResponse('error', null, $validator->errors()));
|
||||
}
|
||||
$field->fill($data);
|
||||
|
||||
if ($field->save()) {
|
||||
return response()->json(Helper::formatStandardApiResponse('success', $field, trans('admin/custom_fields/message.field.create.success')));
|
||||
}
|
||||
return response()->json(Helper::formatStandardApiResponse('error', null, $field->getErrors()));
|
||||
|
||||
}
|
||||
|
||||
public function postReorder(Request $request, $id)
|
||||
{
|
||||
$fieldset = CustomFieldset::find($id);
|
||||
|
@ -62,7 +140,7 @@ class CustomFieldsController extends Controller
|
|||
if ($field->fieldset->count() >0) {
|
||||
return response()->json(Helper::formatStandardApiResponse('error', null, 'Field is in use.'));
|
||||
}
|
||||
|
||||
|
||||
$field->delete();
|
||||
return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/custom_fields/message.field.delete.success')));
|
||||
|
||||
|
|
|
@ -22,6 +22,7 @@ class CustomFieldsTransformer
|
|||
{
|
||||
|
||||
$array = [
|
||||
'id' => $field->id,
|
||||
'name' => e($field->name),
|
||||
'db_column_name' => e($field->db_column_name()),
|
||||
'format' => e($field->format),
|
||||
|
|
|
@ -8,6 +8,7 @@ use App\Http\Traits\UniqueUndeletedTrait;
|
|||
use ForceUTF8\Encoding;
|
||||
use EasySlugger\Utf8Slugger;
|
||||
use Patchwork\Utf8;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class CustomField extends Model
|
||||
{
|
||||
|
@ -29,8 +30,18 @@ class CustomField extends Model
|
|||
"BOOLEAN" => "boolean",
|
||||
];
|
||||
|
||||
public $rules = [
|
||||
"name" => "required|unique:custom_fields"
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'element',
|
||||
'format',
|
||||
'field_values',
|
||||
'field_encrypted',
|
||||
'help_text'
|
||||
];
|
||||
|
||||
// This is confusing, since it's actually the custom fields table that
|
||||
|
@ -160,7 +171,7 @@ class CustomField extends Model
|
|||
public function getFormatAttribute($value)
|
||||
{
|
||||
foreach (self::$PredefinedFormats as $name => $pattern) {
|
||||
if ($pattern === $value) {
|
||||
if ($pattern === $value || $name === $value) {
|
||||
return $name;
|
||||
}
|
||||
}
|
||||
|
@ -248,4 +259,26 @@ class CustomField extends Model
|
|||
|
||||
return substr($long_slug, 0, 50) . '_' . $id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get validation rules for custom fields to use with Validator
|
||||
* @author [V. Cordes] [<volker@fdatek.de>]
|
||||
* @param int $id
|
||||
* @since [v4.1.10]
|
||||
* @return Array
|
||||
*/
|
||||
public function validationRules()
|
||||
{
|
||||
return [
|
||||
"name" => "required|unique:custom_fields",
|
||||
"element" => [
|
||||
"required",
|
||||
Rule::in(['text', 'listbox'])
|
||||
],
|
||||
'format' => [
|
||||
Rule::in(array_merge(array_keys(CustomField::$PredefinedFormats), CustomField::$PredefinedFormats))
|
||||
],
|
||||
'field_encrypted' => "nullable|boolean"
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -200,6 +200,18 @@ Route::group(['prefix' => 'v1','namespace' => 'Api'], function () {
|
|||
|
||||
/*--- Fields API ---*/
|
||||
|
||||
Route::resource('fields', 'CustomFieldsController', [
|
||||
'names' => [
|
||||
'index' => 'api.customfields.index',
|
||||
'show' => 'api.customfields.show',
|
||||
'store' => 'api.customfields.store',
|
||||
'update' => 'api.customfields.update',
|
||||
'destroy' => 'api.customfields.destroy'
|
||||
],
|
||||
'except' => [ 'create', 'edit' ],
|
||||
'parameters' => [ 'field' => 'field_id' ]
|
||||
]);
|
||||
|
||||
Route::group(['prefix' => 'fields'], function () {
|
||||
Route::post('fieldsets/{id}/order',
|
||||
[
|
||||
|
@ -207,13 +219,6 @@ Route::group(['prefix' => 'v1','namespace' => 'Api'], function () {
|
|||
'uses' => 'CustomFieldsController@postReorder'
|
||||
]
|
||||
);
|
||||
|
||||
Route::get('/',
|
||||
[
|
||||
'as' => 'api.customfields.index',
|
||||
'uses' => 'CustomFieldsController@index'
|
||||
]
|
||||
);
|
||||
}); // Fields group
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue