2016-03-25 01:18:05 -07:00
|
|
|
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use Redirect;
|
2018-12-06 20:05:04 -08:00
|
|
|
use App\Helpers\Helper;
|
|
|
|
use App\Models\CustomField;
|
|
|
|
use App\Models\CustomFieldset;
|
2018-07-24 19:35:26 -07:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2018-12-06 20:05:04 -08:00
|
|
|
use Illuminate\Support\Facades\Input;
|
|
|
|
use App\Http\Requests\CustomFieldRequest;
|
2018-07-24 19:35:26 -07:00
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2016-04-07 13:21:09 -07:00
|
|
|
/**
|
|
|
|
* This controller handles all actions related to Custom Asset Fields 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>]
|
|
|
|
*/
|
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
class CustomFieldsController extends Controller
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
2018-07-24 19:35:26 -07:00
|
|
|
* Returns a view with a listing of custom fields.
|
|
|
|
*
|
|
|
|
* @author [Brady Wetherington] [<uberbrady@gmail.com>]
|
|
|
|
* @since [v1.8]
|
|
|
|
* @return \Illuminate\Support\Facades\View
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
2016-12-15 04:09:40 -08:00
|
|
|
public function index()
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2018-07-12 18:28:20 -07:00
|
|
|
$this->authorize('view', CustomField::class);
|
2016-12-14 09:56:23 -08:00
|
|
|
|
|
|
|
$fieldsets = CustomFieldset::with("fields", "models")->get();
|
|
|
|
$fields = CustomField::with("fieldset")->get();
|
2018-12-06 20:05:04 -08:00
|
|
|
|
2017-06-09 16:44:03 -07:00
|
|
|
return view("custom_fields.index")->with("custom_fieldsets", $fieldsets)->with("custom_fields", $fields);
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-07 13:21:09 -07:00
|
|
|
/**
|
2018-07-24 19:35:26 -07:00
|
|
|
* Returns a view with a form to create a new custom field.
|
|
|
|
*
|
|
|
|
* @see CustomFieldsController::storeField()
|
|
|
|
* @author [Brady Wetherington] [<uberbrady@gmail.com>]
|
|
|
|
* @since [v1.8]
|
|
|
|
* @return \Illuminate\Support\Facades\View
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
2016-12-15 19:17:07 -08:00
|
|
|
public function create()
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2018-07-12 18:28:20 -07:00
|
|
|
$this->authorize('create', CustomField::class);
|
2017-01-25 04:34:11 -08:00
|
|
|
|
2018-12-06 20:05:04 -08:00
|
|
|
return view("custom_fields.fields.edit",[
|
2019-01-15 13:56:56 -08:00
|
|
|
'predefinedFormats' => Helper::predefined_formats(),
|
|
|
|
'customFormat' => ''
|
2018-12-06 20:05:04 -08:00
|
|
|
])->with('field', new CustomField());
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
2016-04-07 13:21:09 -07:00
|
|
|
|
|
|
|
/**
|
2018-07-24 19:35:26 -07:00
|
|
|
* Validates and stores a new custom field.
|
|
|
|
*
|
|
|
|
* @see CustomFieldsController::createField()
|
|
|
|
* @author [Brady Wetherington] [<uberbrady@gmail.com>]
|
|
|
|
* @since [v1.8]
|
|
|
|
* @return Redirect
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
2017-11-04 17:06:14 -07:00
|
|
|
public function store(CustomFieldRequest $request)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2018-07-12 18:28:20 -07:00
|
|
|
$this->authorize('create', CustomField::class);
|
|
|
|
|
2016-08-25 17:08:08 -07:00
|
|
|
$field = new CustomField([
|
2017-01-26 04:52:11 -08:00
|
|
|
"name" => $request->get("name"),
|
|
|
|
"element" => $request->get("element"),
|
|
|
|
"help_text" => $request->get("help_text"),
|
|
|
|
"field_values" => $request->get("field_values"),
|
|
|
|
"field_encrypted" => $request->get("field_encrypted", 0),
|
2018-03-25 13:46:57 -07:00
|
|
|
"show_in_email" => $request->get("show_in_email", 0),
|
2018-07-24 19:35:26 -07:00
|
|
|
"user_id" => Auth::id()
|
2016-08-25 17:08:08 -07:00
|
|
|
]);
|
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2018-07-24 22:51:31 -07:00
|
|
|
if ($request->filled("custom_format")) {
|
2016-08-25 17:08:08 -07:00
|
|
|
$field->format = e($request->get("custom_format"));
|
2016-03-25 01:18:05 -07:00
|
|
|
} else {
|
2016-08-25 17:08:08 -07:00
|
|
|
$field->format = e($request->get("format"));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
2017-11-04 17:06:14 -07:00
|
|
|
if ($field->save()) {
|
2018-07-24 19:35:26 -07:00
|
|
|
|
2017-11-04 17:06:14 -07:00
|
|
|
return redirect()->route("fields.index")->with("success", trans('admin/custom_fields/message.field.create.success'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2017-11-04 17:06:14 -07:00
|
|
|
|
2018-07-24 19:35:26 -07:00
|
|
|
return redirect()->back()->withInput()
|
|
|
|
->with('error', trans('admin/custom_fields/message.field.create.error'));
|
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
2016-07-28 08:10:15 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Detach a custom field from a fieldset.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v3.0]
|
|
|
|
* @return Redirect
|
2018-07-24 19:35:26 -07:00
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
2016-07-28 08:10:15 -07:00
|
|
|
*/
|
|
|
|
public function deleteFieldFromFieldset($field_id, $fieldset_id)
|
|
|
|
{
|
|
|
|
$field = CustomField::find($field_id);
|
|
|
|
|
2018-07-12 18:28:20 -07:00
|
|
|
$this->authorize('update', $field);
|
|
|
|
|
2016-07-28 08:10:15 -07:00
|
|
|
if ($field->fieldset()->detach($fieldset_id)) {
|
2018-07-24 19:35:26 -07:00
|
|
|
return redirect()->route('fieldsets.show', ['fieldset' => $fieldset_id])
|
|
|
|
->with("success", trans('admin/custom_fields/message.field.delete.success'));
|
2016-07-28 08:10:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->back()->withErrors(['message' => "Field is in-use"]);
|
|
|
|
}
|
|
|
|
|
2016-04-07 13:21:09 -07:00
|
|
|
/**
|
2018-07-24 19:35:26 -07:00
|
|
|
* Delete a custom field.
|
|
|
|
*
|
|
|
|
* @author [Brady Wetherington] [<uberbrady@gmail.com>]
|
|
|
|
* @since [v1.8]
|
|
|
|
* @return Redirect
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
2016-12-15 19:17:07 -08:00
|
|
|
public function destroy($field_id)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2016-08-25 17:08:08 -07:00
|
|
|
$field = CustomField::find($field_id);
|
2016-07-28 08:10:15 -07:00
|
|
|
|
2018-07-12 18:28:20 -07:00
|
|
|
$this->authorize('delete', $field);
|
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
if ($field->fieldset->count()>0) {
|
2016-04-28 21:06:41 -07:00
|
|
|
return redirect()->back()->withErrors(['message' => "Field is in-use"]);
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2018-07-24 19:35:26 -07:00
|
|
|
$field->delete();
|
|
|
|
return redirect()->route("fields.index")
|
|
|
|
->with("success", trans('admin/custom_fields/message.field.delete.success'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2018-07-24 19:35:26 -07:00
|
|
|
* Return a view to edit a custom field
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @param int $id
|
|
|
|
* @since [v4.0]
|
|
|
|
* @return \Illuminate\Support\Facades\View
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
2016-03-25 01:18:05 -07:00
|
|
|
public function edit($id)
|
|
|
|
{
|
2017-01-25 04:34:11 -08:00
|
|
|
$field = CustomField::find($id);
|
2018-07-12 18:28:20 -07:00
|
|
|
|
|
|
|
$this->authorize('update', $field);
|
|
|
|
|
2018-12-06 20:05:04 -08:00
|
|
|
$customFormat = '';
|
|
|
|
if((stripos($field->format, 'regex') === 0) && ($field->format !== CustomField::PREDEFINED_FORMATS['MAC'])) {
|
|
|
|
$customFormat = $field->format;
|
|
|
|
}
|
|
|
|
|
|
|
|
return view("custom_fields.fields.edit",[
|
|
|
|
'field' => $field,
|
|
|
|
'customFormat' => $customFormat,
|
|
|
|
'predefinedFormats' => Helper::predefined_formats()
|
|
|
|
]);
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2018-07-24 19:35:26 -07:00
|
|
|
* Store the updated field
|
|
|
|
*
|
|
|
|
* @todo Allow encrypting/decrypting if encryption status changes
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @param int $id
|
|
|
|
* @since [v4.0]
|
|
|
|
* @return Redirect
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
2017-11-04 17:06:14 -07:00
|
|
|
public function update(CustomFieldRequest $request, $id)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2017-01-25 04:34:11 -08:00
|
|
|
$field = CustomField::find($id);
|
2018-12-06 20:05:04 -08:00
|
|
|
|
2018-07-12 18:28:20 -07:00
|
|
|
$this->authorize('update', $field);
|
|
|
|
|
2018-12-06 20:05:04 -08:00
|
|
|
$field->name = e($request->get("name"));
|
|
|
|
$field->element = e($request->get("element"));
|
|
|
|
$field->field_values = e($request->get("field_values"));
|
|
|
|
$field->user_id = Auth::id();
|
|
|
|
$field->help_text = $request->get("help_text");
|
2018-05-03 08:06:28 -07:00
|
|
|
$field->show_in_email = $request->get("show_in_email", 0);
|
2017-01-25 04:34:11 -08:00
|
|
|
|
2018-12-06 20:05:04 -08:00
|
|
|
if ($request->get('format') == 'CUSTOM REGEX') {
|
2017-01-25 04:34:11 -08:00
|
|
|
$field->format = e($request->get("custom_format"));
|
|
|
|
} else {
|
|
|
|
$field->format = e($request->get("format"));
|
|
|
|
}
|
|
|
|
|
2017-01-25 04:52:47 -08:00
|
|
|
if ($field->save()) {
|
2017-01-25 04:34:11 -08:00
|
|
|
return redirect()->route("fields.index")->with("success", trans('admin/custom_fields/message.field.update.success'));
|
|
|
|
}
|
|
|
|
|
2017-11-04 17:06:14 -07:00
|
|
|
return redirect()->back()->withInput()->with('error', trans('admin/custom_fields/message.field.update.error'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-11 18:33:59 -08:00
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|