2016-12-15 19:17:07 -08:00
|
|
|
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use App\Models\AssetModel;
|
|
|
|
use App\Models\CustomField;
|
2019-03-13 20:12:03 -07:00
|
|
|
use App\Models\CustomFieldset;
|
2016-12-15 19:17:07 -08:00
|
|
|
use Illuminate\Http\Request;
|
2019-03-13 20:12:03 -07:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2018-07-24 19:35:26 -07:00
|
|
|
use Illuminate\Support\Facades\Input;
|
|
|
|
use Illuminate\Support\Facades\Validator;
|
2016-12-15 19:17:07 -08:00
|
|
|
use Redirect;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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>]
|
|
|
|
*/
|
|
|
|
|
|
|
|
class CustomFieldsetsController extends Controller
|
|
|
|
{
|
|
|
|
|
2021-12-30 13:16:44 -08:00
|
|
|
public function index()
|
|
|
|
{
|
|
|
|
return redirect()->route("fields.index")
|
|
|
|
->with("error", trans('admin/custom_fields/message.fieldset.does_not_exist'));
|
|
|
|
}
|
|
|
|
|
2018-07-24 19:35:26 -07:00
|
|
|
/**
|
|
|
|
* Validates and stores a new custom field.
|
|
|
|
*
|
|
|
|
* @author [Brady Wetherington] [<uberbrady@gmail.com>]
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Support\Facades\View
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
* @since [v1.8]
|
|
|
|
*/
|
2016-12-29 14:02:18 -08:00
|
|
|
public function show($id)
|
|
|
|
{
|
2018-07-24 19:35:26 -07:00
|
|
|
$cfset = CustomFieldset::with('fields')
|
|
|
|
->where('id', '=', $id)->orderBy('id', 'ASC')->first();
|
2016-12-15 19:17:07 -08:00
|
|
|
|
2018-07-12 18:28:20 -07:00
|
|
|
$this->authorize('view', $cfset);
|
|
|
|
|
2017-09-27 16:02:29 -07:00
|
|
|
if ($cfset) {
|
|
|
|
$custom_fields_list = ["" => "Add New Field to Fieldset"] + CustomField::pluck("name", "id")->toArray();
|
|
|
|
|
2018-09-06 14:28:04 -07:00
|
|
|
|
2017-09-27 16:02:29 -07:00
|
|
|
$maxid = 0;
|
2019-05-22 01:07:14 -07:00
|
|
|
foreach ($cfset->fields as $field) {
|
2017-09-27 16:02:29 -07:00
|
|
|
if ($field->pivot->order > $maxid) {
|
|
|
|
$maxid=$field->pivot->order;
|
|
|
|
}
|
|
|
|
if (isset($custom_fields_list[$field->id])) {
|
|
|
|
unset($custom_fields_list[$field->id]);
|
|
|
|
}
|
2016-12-29 14:02:18 -08:00
|
|
|
}
|
2017-09-27 16:02:29 -07:00
|
|
|
|
|
|
|
return view("custom_fields.fieldsets.view")->with("custom_fieldset", $cfset)->with("maxid", $maxid+1)->with("custom_fields_list", $custom_fields_list);
|
2016-12-15 19:17:07 -08:00
|
|
|
}
|
|
|
|
|
2018-07-24 19:35:26 -07:00
|
|
|
return redirect()->route("fields.index")
|
|
|
|
->with("error", trans('admin/custom_fields/message.fieldset.does_not_exist'));
|
2017-09-27 16:02:29 -07:00
|
|
|
|
2016-12-29 14:02:18 -08:00
|
|
|
}
|
|
|
|
|
2016-12-15 19:17:07 -08:00
|
|
|
|
|
|
|
/**
|
2018-07-24 19:35:26 -07:00
|
|
|
* Returns a view with a form for creating a new custom fieldset.
|
|
|
|
*
|
|
|
|
* @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()
|
|
|
|
{
|
2018-07-12 18:28:20 -07:00
|
|
|
$this->authorize('create', CustomFieldset::class);
|
|
|
|
|
2017-06-09 16:44:03 -07:00
|
|
|
return view("custom_fields.fieldsets.edit");
|
2016-12-15 19:17:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2018-07-24 19:35:26 -07:00
|
|
|
* Validates and stores a new custom fieldset.
|
|
|
|
*
|
|
|
|
* @author [Brady Wetherington] [<uberbrady@gmail.com>]
|
|
|
|
* @since [v1.8]
|
|
|
|
* @param Request $request
|
|
|
|
* @return Redirect
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
2016-12-15 19:17:07 -08:00
|
|
|
public function store(Request $request)
|
|
|
|
{
|
2018-07-12 18:28:20 -07:00
|
|
|
$this->authorize('create', CustomFieldset::class);
|
|
|
|
|
2018-07-24 19:35:26 -07:00
|
|
|
$cfset = new CustomFieldset([
|
2016-12-15 19:17:07 -08:00
|
|
|
"name" => e($request->get("name")),
|
2018-07-24 19:35:26 -07:00
|
|
|
"user_id" => Auth::user()->id
|
|
|
|
]);
|
2016-12-15 19:17:07 -08:00
|
|
|
|
2019-12-10 19:32:50 -08:00
|
|
|
$validator = Validator::make($request->all(), $cfset->rules);
|
2016-12-15 19:17:07 -08:00
|
|
|
if ($validator->passes()) {
|
|
|
|
$cfset->save();
|
2018-07-24 19:35:26 -07:00
|
|
|
return redirect()->route("fieldsets.show", [$cfset->id])
|
|
|
|
->with('success', trans('admin/custom_fields/message.fieldset.create.success'));
|
2016-12-15 19:17:07 -08:00
|
|
|
}
|
2018-07-24 19:35:26 -07:00
|
|
|
return redirect()->back()->withInput()->withErrors($validator);
|
2016-12-15 19:17:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* What the actual fuck, Brady?
|
|
|
|
*
|
|
|
|
* @todo Uhh, build this?
|
|
|
|
* @author [Brady Wetherington] [<uberbrady@gmail.com>]
|
|
|
|
* @param int $id
|
|
|
|
* @since [v1.8]
|
|
|
|
* @return Fuckall
|
|
|
|
*/
|
|
|
|
public function edit($id)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GET IN THE SEA BRADY.
|
|
|
|
*
|
|
|
|
* @todo Uhh, build this too?
|
|
|
|
* @author [Brady Wetherington] [<uberbrady@gmail.com>]
|
|
|
|
* @param int $id
|
|
|
|
* @since [v1.8]
|
|
|
|
* @return Fuckall
|
|
|
|
*/
|
|
|
|
public function update($id)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2018-07-24 19:35:26 -07:00
|
|
|
* Validates a custom fieldset and then deletes if it has no models associated.
|
|
|
|
*
|
|
|
|
* @author [Brady Wetherington] [<uberbrady@gmail.com>]
|
|
|
|
* @param int $id
|
|
|
|
* @since [v1.8]
|
|
|
|
* @return View
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
2016-12-15 19:17:07 -08:00
|
|
|
public function destroy($id)
|
|
|
|
{
|
|
|
|
$fieldset = CustomFieldset::find($id);
|
|
|
|
|
2018-07-12 18:28:20 -07:00
|
|
|
$this->authorize('delete', $fieldset);
|
|
|
|
|
2017-09-27 16:02:29 -07:00
|
|
|
if ($fieldset) {
|
|
|
|
$models = AssetModel::where("fieldset_id", "=", $id);
|
|
|
|
if ($models->count() == 0) {
|
|
|
|
$fieldset->delete();
|
|
|
|
return redirect()->route("fields.index")->with("success", trans('admin/custom_fields/message.fieldset.delete.success'));
|
|
|
|
}
|
2018-07-24 19:35:26 -07:00
|
|
|
return redirect()->route("fields.index")->with("error", trans('admin/custom_fields/message.fieldset.delete.in_use'));
|
2016-12-15 19:17:07 -08:00
|
|
|
}
|
2017-09-27 16:02:29 -07:00
|
|
|
|
|
|
|
return redirect()->route("fields.index")->with("error", trans('admin/custom_fields/message.fieldset.does_not_exist'));
|
|
|
|
|
|
|
|
|
2016-12-15 19:17:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Associate the custom field with a custom fieldset.
|
|
|
|
*
|
|
|
|
* @author [Brady Wetherington] [<uberbrady@gmail.com>]
|
|
|
|
* @since [v1.8]
|
|
|
|
* @return View
|
|
|
|
*/
|
2019-05-24 12:05:52 -07:00
|
|
|
public function associate(Request $request, $id)
|
2016-12-15 19:17:07 -08:00
|
|
|
{
|
|
|
|
|
|
|
|
$set = CustomFieldset::find($id);
|
|
|
|
|
2018-07-12 18:28:20 -07:00
|
|
|
$this->authorize('update', $set);
|
|
|
|
|
2020-04-01 01:25:31 -07:00
|
|
|
if ($request->filled('field_id')) {
|
|
|
|
foreach ($set->fields as $field) {
|
|
|
|
if ($field->id == $request->input('field_id')) {
|
|
|
|
return redirect()->route("fieldsets.show", [$id])->withInput()->withErrors(['field_id' => trans('admin/custom_fields/message.field.already_added')]);
|
|
|
|
}
|
2016-12-15 19:17:07 -08:00
|
|
|
}
|
2020-04-01 01:25:31 -07:00
|
|
|
|
2022-05-01 13:16:56 -07:00
|
|
|
$results = $set->fields()->attach($request->input('field_id'), ["required" => ($request->input('required') == "on"),"order" => (int)$request->input('order', 1)]);
|
2020-04-01 01:25:31 -07:00
|
|
|
|
|
|
|
return redirect()->route("fieldsets.show", [$id])->with("success", trans('admin/custom_fields/message.field.create.assoc_success'));
|
2016-12-15 19:17:07 -08:00
|
|
|
}
|
2020-04-01 01:25:31 -07:00
|
|
|
return redirect()->route("fieldsets.show", [$id])->with("error", 'No field selected.');
|
2016-12-15 19:17:07 -08:00
|
|
|
|
|
|
|
|
|
|
|
}
|
2020-04-24 00:39:32 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the field in a fieldset to required
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v5.0]
|
|
|
|
*/
|
|
|
|
public function makeFieldRequired($fieldset_id, $field_id)
|
|
|
|
{
|
|
|
|
|
|
|
|
$this->authorize('update', CustomFieldset::class);
|
|
|
|
$field = CustomField::findOrFail($field_id);
|
|
|
|
$fieldset = CustomFieldset::findOrFail($fieldset_id);
|
|
|
|
$fields[$field->id] = ['required' => 1];
|
|
|
|
$fieldset->fields()->syncWithoutDetaching($fields);
|
|
|
|
|
|
|
|
return redirect()->route('fieldsets.show', ['fieldset' => $fieldset_id])
|
|
|
|
->with("success", trans('Field successfully set to required'));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the field in a fieldset to optional
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v5.0]
|
|
|
|
*/
|
|
|
|
public function makeFieldOptional($fieldset_id, $field_id)
|
|
|
|
{
|
|
|
|
$this->authorize('update', CustomFieldset::class);
|
|
|
|
$field = CustomField::findOrFail($field_id);
|
|
|
|
$fieldset = CustomFieldset::findOrFail($fieldset_id);
|
|
|
|
$fields[$field->id] = ['required' => 0];
|
|
|
|
$fieldset->fields()->syncWithoutDetaching($fields);
|
|
|
|
|
|
|
|
return redirect()->route('fieldsets.show', ['fieldset' => $fieldset_id])
|
|
|
|
->with("success", trans('Field successfully set to optional'));
|
|
|
|
|
|
|
|
}
|
2016-12-15 19:17:07 -08:00
|
|
|
}
|