snipe-it/app/Http/Controllers/Kits/PredefinedKitsController.php

469 lines
15 KiB
PHP
Raw Normal View History

2018-10-19 07:30:25 -07:00
<?php
2018-10-31 06:06:38 -07:00
namespace App\Http\Controllers\Kits;
2018-10-19 07:30:25 -07:00
2018-10-31 06:06:38 -07:00
use App\Http\Controllers\Controller;
use App\Http\Requests\ImageUploadRequest;
use App\Models\PredefinedKit;
use App\Models\PredefinedLicence;
use App\Models\PredefinedModel;
2018-10-31 06:06:38 -07:00
use Illuminate\Http\Request;
2018-10-19 07:30:25 -07:00
/**
* This controller handles all access kits management:
* list, add/remove/change
*
2019-02-23 11:44:03 -08:00
* @author [D. Minaev] [<dmitriy.minaev.v@gmail.com>]
2018-10-19 07:30:25 -07:00
*/
class PredefinedKitsController extends Controller
{
2019-02-23 11:44:03 -08:00
/**
* @author [D. Minaev] [<dmitriy.minaev.v@gmail.com>]
* @return \Illuminate\Contracts\View\View
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
2018-10-19 07:30:25 -07:00
public function index()
{
2019-02-23 11:44:03 -08:00
$this->authorize('index', PredefinedKit::class);
2018-10-19 07:30:25 -07:00
return view('kits/index');
}
/**
2019-02-23 11:44:03 -08:00
* Returns a form view to create a new kit.
*
2019-02-23 11:44:03 -08:00
* @author [D. Minaev] [<dmitriy.minaev.v@gmail.com>]
* @throws \Illuminate\Auth\Access\AuthorizationException
2018-10-19 07:30:25 -07:00
* @return mixed
*/
public function create()
{
2018-11-13 09:33:32 -08:00
$this->authorize('create', PredefinedKit::class);
2018-10-31 06:06:38 -07:00
return view('kits/create')->with('item', new PredefinedKit);
2018-10-19 07:30:25 -07:00
}
/**
2019-02-23 11:44:03 -08:00
* Validate and process the new Predefined Kit data.
*
* @author [D. Minaev] [<dmitriy.minaev.v@gmail.com>]
* @return Redirect
*/
2018-10-19 07:30:25 -07:00
public function store(ImageUploadRequest $request)
{
2018-11-13 09:33:32 -08:00
$this->authorize('create', PredefinedKit::class);
2018-10-19 07:30:25 -07:00
// Create a new Predefined Kit
$kit = new PredefinedKit;
2019-02-23 11:44:03 -08:00
$kit->name = $request->input('name');
2018-10-19 07:30:25 -07:00
if (! $kit->save()) {
2018-10-19 07:30:25 -07:00
return redirect()->back()->withInput()->withErrors($kit->getErrors());
}
2019-02-23 11:44:03 -08:00
$success = $kit->save();
if (! $success) {
2018-10-19 07:30:25 -07:00
return redirect()->back()->withInput()->withErrors($kit->getErrors());
}
return redirect()->route('kits.index')->with('success', trans('admin/kits/general.kit_created'));
2018-10-19 07:30:25 -07:00
}
/**
2019-02-23 11:44:03 -08:00
* Returns a view containing the Predefined Kit edit form.
*
* @author [D. Minaev] [<dmitriy.minaev.v@gmail.com>]
* @since [v1.0]
* @param int $kit_id
* @return View
*/
public function edit($kit_id = null)
2018-10-19 07:30:25 -07:00
{
$this->authorize('update', PredefinedKit::class);
2018-10-31 06:06:38 -07:00
if ($kit = PredefinedKit::find($kit_id)) {
2018-10-19 07:30:25 -07:00
return view('kits/edit')
2019-02-23 11:44:03 -08:00
->with('item', $kit)
->with('models', $kit->models)
->with('licenses', $kit->licenses);
2018-10-19 07:30:25 -07:00
}
return redirect()->route('kits.index')->with('error', trans('admin/kits/general.kit_none'));
2018-10-19 07:30:25 -07:00
}
/**
2019-02-23 11:44:03 -08:00
* Validates and processes form data from the edit
* Predefined Kit form based on the kit ID passed.
*
* @author [D. Minaev] [<dmitriy.minaev.v@gmail.com>]
* @since [v1.0]
* @param int $kit_id
* @return Redirect
*/
public function update(ImageUploadRequest $request, $kit_id = null)
2018-10-19 07:30:25 -07:00
{
$this->authorize('update', PredefinedKit::class);
// Check if the kit exists
2018-10-31 06:06:38 -07:00
if (is_null($kit = PredefinedKit::find($kit_id))) {
2018-10-19 07:30:25 -07:00
// Redirect to the kits management page
return redirect()->route('kits.index')->with('error', trans('admin/kits/general.kit_none'));
2018-10-19 07:30:25 -07:00
}
2019-02-23 11:44:03 -08:00
$kit->name = $request->input('name');
2018-10-31 06:06:38 -07:00
if ($kit->save()) {
return redirect()->route('kits.index')->with('success', trans('admin/kits/general.kit_updated'));
2018-10-19 07:30:25 -07:00
}
2018-10-19 07:30:25 -07:00
return redirect()->back()->withInput()->withErrors($kit->getErrors());
}
/**
2019-02-23 11:44:03 -08:00
* Validate and delete the given Predefined Kit.
* Also delete all contained helping items
*
* @author [D. Minaev] [<dmitriy.minaev.v@gmail.com>]
* @since [v1.0]
* @param int $kit_id
* @return Redirect
*/
2018-10-31 06:06:38 -07:00
public function destroy($kit_id)
2018-10-19 07:30:25 -07:00
{
$this->authorize('delete', PredefinedKit::class);
// Check if the kit exists
2018-10-31 06:06:38 -07:00
if (is_null($kit = PredefinedKit::find($kit_id))) {
return redirect()->route('kits.index')->with('error', trans('admin/kits/general.kit_not_found'));
2018-10-19 07:30:25 -07:00
}
// Delete childs
$kit->models()->detach();
$kit->licenses()->detach();
$kit->consumables()->detach();
$kit->accessories()->detach();
2018-10-19 07:30:25 -07:00
// Delete the kit
$kit->delete();
// Redirect to the kit management page
return redirect()->route('kits.index')->with('success', trans('admin/kits/general.kit_deleted'));
2018-10-19 07:30:25 -07:00
}
/**
2019-02-23 11:44:03 -08:00
* Get the kit information to present to the kit view page
*
* @author [D. Minaev] [<dmitriy.minaev.v@gmail.com>]
* @since [v1.0]
* @param int $modelId
* @return View
*/
public function show($kit_id = null)
2018-10-19 07:30:25 -07:00
{
2019-02-23 11:44:03 -08:00
return $this->edit($kit_id);
2018-10-31 06:06:38 -07:00
}
/**
2019-02-23 11:44:03 -08:00
* Returns a view containing the Predefined Kit edit form.
*
* @author [D. Minaev] [<dmitriy.minaev.v@gmail.com>]
* @param int $kit_id
* @return View
*/
2018-10-31 06:06:38 -07:00
public function editModel($kit_id, $model_id)
2019-02-23 11:44:03 -08:00
{
2018-10-31 06:06:38 -07:00
$this->authorize('update', PredefinedKit::class);
2019-02-23 11:44:03 -08:00
if (($kit = PredefinedKit::find($kit_id))
&& ($model = $kit->models()->find($model_id))) {
2018-10-31 06:06:38 -07:00
return view('kits/model-edit', [
'kit' => $kit,
'model' => $model,
'item' => $model->pivot,
2018-10-31 06:06:38 -07:00
]);
}
return redirect()->route('kits.index')->with('error', trans('admin/kits/general.kit_none'));
2018-10-31 06:06:38 -07:00
}
/**
2019-02-23 11:44:03 -08:00
* Get the kit information to present to the kit view page
*
* @author [D. Minaev] [<dmitriy.minaev.v@gmail.com>]
* @param int $modelId
* @return View
*/
public function updateModel(Request $request, $kit_id, $model_id)
{
2018-10-31 06:06:38 -07:00
$this->authorize('update', PredefinedKit::class);
if (is_null($kit = PredefinedKit::find($kit_id))) {
// Redirect to the kits management page
return redirect()->route('kits.index')->with('error', trans('admin/kits/general.kit_none'));
2018-10-31 06:06:38 -07:00
}
$validator = \Validator::make($request->all(), $kit->makeModelRules($model_id));
2018-11-13 09:33:32 -08:00
2018-10-31 06:06:38 -07:00
if ($validator->fails()) {
return redirect()->back()->withInput()->withErrors($validator);
}
2018-11-13 09:33:32 -08:00
2018-10-31 06:06:38 -07:00
$pivot = $kit->models()->wherePivot('id', $request->input('pivot_id'))->first()->pivot;
2018-11-13 09:33:32 -08:00
2018-10-31 06:06:38 -07:00
$pivot->model_id = $request->input('model_id');
$pivot->quantity = $request->input('quantity');
$pivot->save();
2019-02-23 11:44:03 -08:00
return redirect()->route('kits.edit', $kit_id)->with('success', trans('admin/kits/general.kit_model_updated'));
2018-10-31 06:06:38 -07:00
}
/**
2019-02-23 11:44:03 -08:00
* Remove the model from set
*
* @author [D. Minaev] [<dmitriy.minaev.v@gmail.com>]
* @param int $modelId
* @return View
*/
public function detachModel($kit_id, $model_id)
{
2018-10-31 06:06:38 -07:00
$this->authorize('update', PredefinedKit::class);
if (is_null($kit = PredefinedKit::find($kit_id))) {
// Redirect to the kits management page
return redirect()->route('kits.index')->with('error', trans('admin/kits/general.kit_none'));
2018-10-31 06:06:38 -07:00
}
// Delete childs
$kit->models()->detach($model_id);
2018-10-31 06:06:38 -07:00
// Redirect to the kit management page
return redirect()->route('kits.edit', $kit_id)->with('success', trans('admin/kits/general.kit_model_detached'));
2018-10-19 07:30:25 -07:00
}
/**
2019-02-23 11:44:03 -08:00
* Returns a view containing attached license edit form.
*
* @author [D. Minaev] [<dmitriy.minaev.v@gmail.com>]
* @param int $kit_id
* @param int $license_id
* @return View
*/
public function editLicense($kit_id, $license_id)
2019-02-23 11:44:03 -08:00
{
$this->authorize('update', PredefinedKit::class);
if (! ($kit = PredefinedKit::find($kit_id))) {
return redirect()->route('kits.index')->with('error', trans('admin/kits/general.kit_none'));
}
if (! ($license = $kit->licenses()->find($license_id))) {
return redirect()->route('kits.index')->with('error', trans('admin/kits/general.license_none'));
}
2019-02-23 11:44:03 -08:00
return view('kits/license-edit', [
'kit' => $kit,
'license' => $license,
'item' => $license->pivot,
]);
}
/**
2019-02-23 11:44:03 -08:00
* Update attached licese
*
* @author [D. Minaev] [<dmitriy.minaev.v@gmail.com>]
* @param int $kit_id
* @param int $license_id
* @return View
*/
public function updateLicense(Request $request, $kit_id, $license_id)
{
$this->authorize('update', PredefinedKit::class);
if (is_null($kit = PredefinedKit::find($kit_id))) {
// Redirect to the kits management page
return redirect()->route('kits.index')->with('error', trans('admin/kits/general.kit_none'));
}
$validator = \Validator::make($request->all(), $kit->makeLicenseRules($license_id));
if ($validator->fails()) {
return redirect()->back()->withInput()->withErrors($validator);
}
$pivot = $kit->licenses()->wherePivot('id', $request->input('pivot_id'))->first()->pivot;
$pivot->license_id = $request->input('license_id');
$pivot->quantity = $request->input('quantity');
$pivot->save();
2019-02-23 11:44:03 -08:00
return redirect()->route('kits.edit', $kit_id)->with('success', trans('admin/kits/general.license_updated'));
}
/**
2019-02-23 11:44:03 -08:00
* Remove the license from set
*
2019-02-23 11:44:03 -08:00
* @author [D. Minaev] [<dmitriy.minaev.v@gmail.com>]
* @param int $kit_id
* @param int $license_id
* @return View
*/
public function detachLicense($kit_id, $license_id)
{
$this->authorize('update', PredefinedKit::class);
if (is_null($kit = PredefinedKit::find($kit_id))) {
// Redirect to the kits management page
return redirect()->route('kits.index')->with('error', trans('admin/kits/general.kit_none'));
}
// Delete childs
$kit->licenses()->detach($license_id);
// Redirect to the kit management page
return redirect()->route('kits.edit', $kit_id)->with('success', trans('admin/kits/general.license_detached'));
}
/**
2019-02-23 11:44:03 -08:00
* Returns a view containing attached accessory edit form.
*
* @author [D. Minaev] [<dmitriy.minaev.v@gmail.com>]
* @param int $kit_id
* @param int $accessoryId
* @return View
*/
public function editAccessory($kit_id, $accessory_id)
2019-02-23 11:44:03 -08:00
{
$this->authorize('update', PredefinedKit::class);
if (! ($kit = PredefinedKit::find($kit_id))) {
return redirect()->route('kits.index')->with('error', trans('admin/kits/general.kit_none'));
}
if (! ($accessory = $kit->accessories()->find($accessory_id))) {
return redirect()->route('kits.index')->with('error', trans('admin/kits/general.accessory_none'));
}
2019-02-23 11:44:03 -08:00
return view('kits/accessory-edit', [
'kit' => $kit,
'accessory' => $accessory,
'item' => $accessory->pivot,
]);
}
/**
2019-02-23 11:44:03 -08:00
* Update attached accessory
*
2019-02-23 11:44:03 -08:00
* @author [D. Minaev] [<dmitriy.minaev.v@gmail.com>]
* @param int $kit_id
* @param int $accessory_id
* @return View
*/
public function updateAccessory(Request $request, $kit_id, $accessory_id)
{
$this->authorize('update', PredefinedKit::class);
if (is_null($kit = PredefinedKit::find($kit_id))) {
// Redirect to the kits management page
return redirect()->route('kits.index')->with('error', trans('admin/kits/general.kit_none'));
}
$validator = \Validator::make($request->all(), $kit->makeAccessoryRules($accessory_id));
if ($validator->fails()) {
return redirect()->back()->withInput()->withErrors($validator);
}
$pivot = $kit->accessories()->wherePivot('id', $request->input('pivot_id'))->first()->pivot;
$pivot->accessory_id = $request->input('accessory_id');
$pivot->quantity = $request->input('quantity');
$pivot->save();
2019-02-23 11:44:03 -08:00
return redirect()->route('kits.edit', $kit_id)->with('success', trans('admin/kits/general.accessory_updated'));
}
/**
2019-02-23 11:44:03 -08:00
* Remove the accessory from set
*
* @author [D. Minaev] [<dmitriy.minaev.v@gmail.com>]
* @param int $accessory_id
* @return View
*/
public function detachAccessory($kit_id, $accessory_id)
{
$this->authorize('update', PredefinedKit::class);
if (is_null($kit = PredefinedKit::find($kit_id))) {
// Redirect to the kits management page
return redirect()->route('kits.index')->with('error', trans('admin/kits/general.kit_none'));
}
// Delete childs
$kit->accessories()->detach($accessory_id);
// Redirect to the kit management page
return redirect()->route('kits.edit', $kit_id)->with('success', trans('admin/kits/general.accessory_detached'));
}
/**
2019-02-23 11:44:03 -08:00
* Returns a view containing attached consumable edit form.
*
* @author [D. Minaev] [<dmitriy.minaev.v@gmail.com>]
* @param int $kit_id
* @param int $consumable_id
* @return View
*/
public function editConsumable($kit_id, $consumable_id)
2019-02-23 11:44:03 -08:00
{
$this->authorize('update', PredefinedKit::class);
if (! ($kit = PredefinedKit::find($kit_id))) {
return redirect()->route('kits.index')->with('error', trans('admin/kits/general.kit_none'));
}
if (! ($consumable = $kit->consumables()->find($consumable_id))) {
return redirect()->route('kits.index')->with('error', trans('admin/kits/general.consumable_none'));
}
2019-02-23 11:44:03 -08:00
return view('kits/consumable-edit', [
'kit' => $kit,
'consumable' => $consumable,
'item' => $consumable->pivot,
]);
}
/**
2019-02-23 11:44:03 -08:00
* Update attached consumable
*
* @author [D. Minaev] [<dmitriy.minaev.v@gmail.com>]
* @param int $kit_id
* @param int $consumableId
* @return View
*/
public function updateConsumable(Request $request, $kit_id, $consumable_id)
{
$this->authorize('update', PredefinedKit::class);
if (is_null($kit = PredefinedKit::find($kit_id))) {
// Redirect to the kits management page
return redirect()->route('kits.index')->with('error', trans('admin/kits/general.kit_none'));
}
$validator = \Validator::make($request->all(), $kit->makeConsumableRules($consumable_id));
if ($validator->fails()) {
return redirect()->back()->withInput()->withErrors($validator);
}
$pivot = $kit->consumables()->wherePivot('id', $request->input('pivot_id'))->first()->pivot;
$pivot->consumable_id = $request->input('consumable_id');
$pivot->quantity = $request->input('quantity');
$pivot->save();
2019-02-23 11:44:03 -08:00
return redirect()->route('kits.edit', $kit_id)->with('success', trans('admin/kits/general.consumable_updated'));
}
/**
2019-02-23 11:44:03 -08:00
* Remove the consumable from set
*
* @author [D. Minaev] [<dmitriy.minaev.v@gmail.com>]
* @param int $consumable_id
* @return View
*/
public function detachConsumable($kit_id, $consumable_id)
{
$this->authorize('update', PredefinedKit::class);
if (is_null($kit = PredefinedKit::find($kit_id))) {
// Redirect to the kits management page
return redirect()->route('kits.index')->with('error', trans('admin/kits/general.kit_none'));
}
// Delete childs
$kit->consumables()->detach($consumable_id);
// Redirect to the kit management page
return redirect()->route('kits.edit', $kit_id)->with('success', trans('admin/kits/general.consumable_detached'));
}
2018-10-19 07:30:25 -07:00
}