2016-03-25 01:18:05 -07:00
|
|
|
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2019-03-13 20:12:03 -07:00
|
|
|
use App\Helpers\Helper;
|
|
|
|
use App\Http\Requests\ImageUploadRequest;
|
|
|
|
use App\Models\AssetModel;
|
2018-07-24 19:35:26 -07:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use Illuminate\Support\Facades\Input;
|
|
|
|
use Illuminate\Support\Facades\View;
|
2016-03-25 01:18:05 -07:00
|
|
|
use Redirect;
|
|
|
|
|
|
|
|
/**
|
2016-04-07 13:21:09 -07:00
|
|
|
* This class controls all actions related to asset models for
|
|
|
|
* the Snipe-IT Asset Management application.
|
|
|
|
*
|
|
|
|
* @version v1.0
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
2016-03-25 01:18:05 -07:00
|
|
|
*/
|
|
|
|
class AssetModelsController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
2018-07-24 19:35:26 -07:00
|
|
|
* Returns a view that invokes the ajax tables which actually contains
|
|
|
|
* the content for the accessories listing, which is generated in getDatatable.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v1.0]
|
|
|
|
* @return View
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
2016-12-15 06:11:03 -08:00
|
|
|
public function index()
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2018-01-20 04:39:31 -08:00
|
|
|
$this->authorize('index', AssetModel::class);
|
2017-06-09 16:44:03 -07:00
|
|
|
return view('models/index');
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-24 19:35:26 -07:00
|
|
|
* Returns a view containing the asset model creation form.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v1.0]
|
|
|
|
* @return View
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
2016-12-15 06:11:03 -08:00
|
|
|
public function create()
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2018-01-20 04:39:31 -08:00
|
|
|
$this->authorize('create', AssetModel::class);
|
2018-07-24 19:35:26 -07:00
|
|
|
return view('models/edit')->with('category_type', 'asset')
|
|
|
|
->with('depreciation_list', Helper::depreciationList())
|
|
|
|
->with('item', new AssetModel);
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2018-07-24 19:35:26 -07:00
|
|
|
* Validate and process the new Asset Model data.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v1.0]
|
|
|
|
* @param ImageUploadRequest $request
|
|
|
|
* @return Redirect
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
2017-10-06 16:56:43 -07:00
|
|
|
public function store(ImageUploadRequest $request)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
|
|
|
|
2018-01-20 04:39:31 -08:00
|
|
|
$this->authorize('create', AssetModel::class);
|
2016-03-25 01:18:05 -07:00
|
|
|
// Create a new asset model
|
|
|
|
$model = new AssetModel;
|
|
|
|
|
2016-09-15 19:58:27 -07:00
|
|
|
// Save the model data
|
2016-12-26 15:17:46 -08:00
|
|
|
$model->eol = $request->input('eol');
|
|
|
|
$model->depreciation_id = $request->input('depreciation_id');
|
2016-12-19 22:00:50 -08:00
|
|
|
$model->name = $request->input('name');
|
|
|
|
$model->model_number = $request->input('model_number');
|
|
|
|
$model->manufacturer_id = $request->input('manufacturer_id');
|
|
|
|
$model->category_id = $request->input('category_id');
|
|
|
|
$model->notes = $request->input('notes');
|
2017-09-25 11:53:10 -07:00
|
|
|
$model->user_id = Auth::id();
|
2016-09-15 19:58:27 -07:00
|
|
|
$model->requestable = Input::has('requestable');
|
|
|
|
|
2016-12-15 15:56:52 -08:00
|
|
|
if ($request->input('custom_fieldset')!='') {
|
|
|
|
$model->fieldset_id = e($request->input('custom_fieldset'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
2018-09-29 21:33:52 -07:00
|
|
|
$model = $request->handleImages($model);
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
// Was it created?
|
|
|
|
if ($model->save()) {
|
2018-04-23 21:16:55 -07:00
|
|
|
if ($this->shouldAddDefaultValues($request->input())) {
|
|
|
|
$this->assignCustomFieldsDefaultValues($model, $request->input('default_values'));
|
|
|
|
}
|
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
// Redirect to the new model page
|
2016-12-15 06:11:03 -08:00
|
|
|
return redirect()->route("models.index")->with('success', trans('admin/models/message.create.success'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2016-12-19 22:00:50 -08:00
|
|
|
return redirect()->back()->withInput()->withErrors($model->getErrors());
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-24 19:35:26 -07:00
|
|
|
* Returns a view containing the asset model edit form.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v1.0]
|
|
|
|
* @param int $modelId
|
|
|
|
* @return View
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
2016-12-15 06:11:03 -08:00
|
|
|
public function edit($modelId = null)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2018-03-07 18:22:49 -08:00
|
|
|
$this->authorize('update', AssetModel::class);
|
2017-10-28 11:17:52 -07:00
|
|
|
if ($item = AssetModel::find($modelId)) {
|
|
|
|
$category_type = 'asset';
|
|
|
|
$view = View::make('models/edit', compact('item','category_type'));
|
|
|
|
$view->with('depreciation_list', Helper::depreciationList());
|
|
|
|
return $view;
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
2017-10-28 11:17:52 -07:00
|
|
|
return redirect()->route('models.index')->with('error', trans('admin/models/message.does_not_exist'));
|
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2018-07-24 19:35:26 -07:00
|
|
|
* Validates and processes form data from the edit
|
|
|
|
* Asset Model form based on the model ID passed.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v1.0]
|
|
|
|
* @param ImageUploadRequest $request
|
|
|
|
* @param int $modelId
|
|
|
|
* @return Redirect
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
2017-10-06 17:03:51 -07:00
|
|
|
public function update(ImageUploadRequest $request, $modelId = null)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2018-03-07 18:22:49 -08:00
|
|
|
$this->authorize('update', AssetModel::class);
|
2016-03-25 01:18:05 -07:00
|
|
|
// Check if the model exists
|
|
|
|
if (is_null($model = AssetModel::find($modelId))) {
|
|
|
|
// Redirect to the models management page
|
2016-12-19 11:04:28 -08:00
|
|
|
return redirect()->route('models.index')->with('error', trans('admin/models/message.does_not_exist'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
2017-11-07 11:28:13 -08:00
|
|
|
$model->depreciation_id = $request->input('depreciation_id');
|
|
|
|
$model->eol = $request->input('eol');
|
2016-12-19 22:00:50 -08:00
|
|
|
$model->name = $request->input('name');
|
|
|
|
$model->model_number = $request->input('model_number');
|
|
|
|
$model->manufacturer_id = $request->input('manufacturer_id');
|
|
|
|
$model->category_id = $request->input('category_id');
|
|
|
|
$model->notes = $request->input('notes');
|
2017-11-07 11:28:13 -08:00
|
|
|
$model->requestable = $request->input('requestable', '0');
|
2016-09-15 19:58:27 -07:00
|
|
|
|
2018-04-23 21:16:55 -07:00
|
|
|
$this->removeCustomFieldsDefaultValues($model);
|
|
|
|
|
2016-12-15 15:56:52 -08:00
|
|
|
if ($request->input('custom_fieldset')=='') {
|
2016-03-25 01:18:05 -07:00
|
|
|
$model->fieldset_id = null;
|
|
|
|
} else {
|
2016-12-19 22:00:50 -08:00
|
|
|
$model->fieldset_id = $request->input('custom_fieldset');
|
2018-04-23 21:16:55 -07:00
|
|
|
|
|
|
|
if ($this->shouldAddDefaultValues($request->input())) {
|
|
|
|
$this->assignCustomFieldsDefaultValues($model, $request->input('default_values'));
|
|
|
|
}
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
2018-09-29 21:33:52 -07:00
|
|
|
$model = $request->handleImages($model);
|
2017-11-07 11:28:13 -08:00
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
if ($model->save()) {
|
2016-12-15 06:11:03 -08:00
|
|
|
return redirect()->route("models.index")->with('success', trans('admin/models/message.update.success'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2016-12-19 22:00:50 -08:00
|
|
|
return redirect()->back()->withInput()->withErrors($model->getErrors());
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-24 19:35:26 -07:00
|
|
|
* Validate and delete the given Asset Model. An Asset Model
|
|
|
|
* cannot be deleted if there are associated assets.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v1.0]
|
|
|
|
* @param int $modelId
|
|
|
|
* @return Redirect
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
2016-12-15 06:11:03 -08:00
|
|
|
public function destroy($modelId)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2018-01-20 04:39:31 -08:00
|
|
|
$this->authorize('delete', AssetModel::class);
|
2016-03-25 01:18:05 -07:00
|
|
|
// Check if the model exists
|
|
|
|
if (is_null($model = AssetModel::find($modelId))) {
|
2016-12-19 11:04:28 -08:00
|
|
|
return redirect()->route('models.index')->with('error', trans('admin/models/message.not_found'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
2016-12-19 22:00:50 -08:00
|
|
|
if ($model->assets()->count() > 0) {
|
2016-03-25 01:18:05 -07:00
|
|
|
// Throw an error that this model is associated with assets
|
2016-12-15 06:11:03 -08:00
|
|
|
return redirect()->route('models.index')->with('error', trans('admin/models/message.assoc_users'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2017-10-28 05:46:43 -07:00
|
|
|
|
|
|
|
if ($model->image) {
|
|
|
|
try {
|
2018-09-29 21:33:52 -07:00
|
|
|
Storage::disk('public')->delete('models/'.$model->image);
|
2017-10-28 05:46:43 -07:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
\Log::error($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-19 22:00:50 -08:00
|
|
|
// Delete the model
|
|
|
|
$model->delete();
|
|
|
|
|
|
|
|
// Redirect to the models management page
|
|
|
|
return redirect()->route('models.index')->with('success', trans('admin/models/message.delete.success'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2018-07-24 19:35:26 -07:00
|
|
|
* Restore a given Asset Model (mark as un-deleted)
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v1.0]
|
|
|
|
* @param int $modelId
|
|
|
|
* @return Redirect
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
2016-03-25 01:18:05 -07:00
|
|
|
public function getRestore($modelId = null)
|
|
|
|
{
|
2018-01-20 04:39:31 -08:00
|
|
|
$this->authorize('create', AssetModel::class);
|
2016-03-25 01:18:05 -07:00
|
|
|
// Get user information
|
|
|
|
$model = AssetModel::withTrashed()->find($modelId);
|
|
|
|
|
|
|
|
if (isset($model->id)) {
|
|
|
|
$model->restore();
|
2018-07-24 19:35:26 -07:00
|
|
|
return redirect()->route('models.index')->with('success', trans('admin/models/message.restore.success'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2016-12-19 22:00:50 -08:00
|
|
|
return redirect()->back()->with('error', trans('admin/models/message.not_found'));
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2018-07-24 19:35:26 -07:00
|
|
|
* Get the model information to present to the model view page
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v1.0]
|
|
|
|
* @param int $modelId
|
|
|
|
* @return View
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
2016-12-15 06:11:03 -08:00
|
|
|
public function show($modelId = null)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2018-01-20 04:39:31 -08:00
|
|
|
$this->authorize('view', AssetModel::class);
|
2016-03-25 01:18:05 -07:00
|
|
|
$model = AssetModel::withTrashed()->find($modelId);
|
|
|
|
|
|
|
|
if (isset($model->id)) {
|
2017-06-09 16:44:03 -07:00
|
|
|
return view('models/view', compact('model'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2016-12-19 22:00:50 -08:00
|
|
|
// Redirect to the user management page
|
2018-07-24 19:35:26 -07:00
|
|
|
return redirect()->route('models.index')->with('error', trans('admin/models/message.does_not_exist'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the clone page to clone a model
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v1.0]
|
|
|
|
* @param int $modelId
|
|
|
|
* @return View
|
|
|
|
*/
|
|
|
|
public function getClone($modelId = null)
|
|
|
|
{
|
|
|
|
// Check if the model exists
|
|
|
|
if (is_null($model_to_clone = AssetModel::find($modelId))) {
|
2016-12-19 11:04:28 -08:00
|
|
|
return redirect()->route('models.index')->with('error', trans('admin/models/message.does_not_exist'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
$model = clone $model_to_clone;
|
|
|
|
$model->id = null;
|
|
|
|
|
|
|
|
// Show the page
|
2018-07-24 19:35:26 -07:00
|
|
|
return view('models/edit')
|
|
|
|
->with('depreciation_list', Helper::depreciationList())
|
|
|
|
->with('item', $model)
|
|
|
|
->with('clone_model', $model_to_clone);
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the custom fields form
|
|
|
|
*
|
|
|
|
* @author [B. Wetherington] [<uberbrady@gmail.com>]
|
|
|
|
* @since [v2.0]
|
|
|
|
* @param int $modelId
|
|
|
|
* @return View
|
|
|
|
*/
|
|
|
|
public function getCustomFields($modelId)
|
|
|
|
{
|
2018-07-24 19:35:26 -07:00
|
|
|
return view("models.custom_fields_form")->with("model", AssetModel::find($modelId));
|
2017-06-08 17:48:48 -07:00
|
|
|
}
|
|
|
|
|
2017-12-12 09:10:05 -08:00
|
|
|
|
2018-04-23 21:16:55 -07:00
|
|
|
/**
|
|
|
|
* Returns true if a fieldset is set, 'add default values' is ticked and if
|
|
|
|
* any default values were entered into the form.
|
|
|
|
*
|
|
|
|
* @param array $input
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
private function shouldAddDefaultValues(array $input)
|
|
|
|
{
|
|
|
|
return !empty($input['add_default_values'])
|
|
|
|
&& !empty($input['default_values'])
|
|
|
|
&& !empty($input['custom_fieldset']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds default values to a model (as long as they are truthy)
|
|
|
|
*
|
|
|
|
* @param AssetModel $model
|
|
|
|
* @param array $defaultValues
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function assignCustomFieldsDefaultValues(AssetModel $model, array $defaultValues)
|
|
|
|
{
|
|
|
|
foreach ($defaultValues as $customFieldId => $defaultValue) {
|
|
|
|
if ($defaultValue) {
|
|
|
|
$model->defaultValues()->attach($customFieldId, ['default_value' => $defaultValue]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes all default values
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function removeCustomFieldsDefaultValues(AssetModel $model)
|
|
|
|
{
|
|
|
|
$model->defaultValues()->detach();
|
|
|
|
}
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|