2016-03-25 01:18:05 -07:00
|
|
|
<?php
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2019-03-13 20:12:03 -07:00
|
|
|
use App\Helpers\Helper;
|
|
|
|
use App\Http\Requests\ImageUploadRequest;
|
2024-07-04 12:50:11 -07:00
|
|
|
use App\Http\Requests\StoreAssetModelRequest;
|
2023-11-22 10:02:47 -08:00
|
|
|
use App\Models\Actionlog;
|
2016-03-25 01:18:05 -07:00
|
|
|
use App\Models\AssetModel;
|
2024-02-14 00:12:31 -08:00
|
|
|
use App\Models\CustomField;
|
2023-11-22 10:02:47 -08:00
|
|
|
use App\Models\User;
|
2018-07-24 19:35:26 -07:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2023-07-13 16:36:14 -07:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2022-06-22 19:06:07 -07:00
|
|
|
use Illuminate\Support\Facades\Validator;
|
2024-05-29 04:38:15 -07:00
|
|
|
use Illuminate\Http\Request;
|
2024-05-29 04:53:51 -07:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2024-05-29 04:38:15 -07:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2024-07-04 12:50:11 -07:00
|
|
|
use \Illuminate\Contracts\View\View;
|
|
|
|
use \Illuminate\Http\RedirectResponse;
|
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
/**
|
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]
|
|
|
|
*/
|
2024-07-04 12:50:11 -07:00
|
|
|
public function index() : View
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2018-01-20 04:39:31 -08:00
|
|
|
$this->authorize('index', AssetModel::class);
|
2021-06-10 13:15:52 -07:00
|
|
|
|
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]
|
|
|
|
*/
|
2024-07-04 12:50:11 -07:00
|
|
|
public function create() : View
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2018-01-20 04:39:31 -08:00
|
|
|
$this->authorize('create', AssetModel::class);
|
2021-06-10 13:15:52 -07:00
|
|
|
|
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
|
|
|
|
*/
|
2024-07-04 12:50:11 -07:00
|
|
|
public function store(ImageUploadRequest $request) : RedirectResponse
|
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
|
|
|
$model = new AssetModel;
|
|
|
|
|
2016-12-26 15:17:46 -08:00
|
|
|
$model->eol = $request->input('eol');
|
|
|
|
$model->depreciation_id = $request->input('depreciation_id');
|
2021-06-10 13:15:52 -07:00
|
|
|
$model->name = $request->input('name');
|
|
|
|
$model->model_number = $request->input('model_number');
|
2023-08-21 12:07:57 -07:00
|
|
|
$model->min_amt = $request->input('min_amt');
|
2021-06-10 13:15:52 -07:00
|
|
|
$model->manufacturer_id = $request->input('manufacturer_id');
|
|
|
|
$model->category_id = $request->input('category_id');
|
|
|
|
$model->notes = $request->input('notes');
|
|
|
|
$model->user_id = Auth::id();
|
2024-06-17 03:57:30 -07:00
|
|
|
$model->requestable = $request->has('requestable');
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2023-03-24 06:11:24 -07:00
|
|
|
if ($request->input('fieldset_id') != '') {
|
2023-12-12 22:27:50 -08:00
|
|
|
$model->fieldset_id = $request->input('fieldset_id');
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
2020-08-26 11:59:30 -07:00
|
|
|
$model = $request->handleImages($model);
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
if ($model->save()) {
|
2018-04-23 21:16:55 -07:00
|
|
|
if ($this->shouldAddDefaultValues($request->input())) {
|
2022-06-22 19:06:07 -07:00
|
|
|
if (!$this->assignCustomFieldsDefaultValues($model, $request->input('default_values'))){
|
2022-06-22 20:17:05 -07:00
|
|
|
return redirect()->back()->withInput()->with('error', trans('admin/custom_fields/message.fieldset_default_value.error'));
|
2022-06-22 19:06:07 -07:00
|
|
|
}
|
2018-04-23 21:16:55 -07:00
|
|
|
}
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
return redirect()->route('models.index')->with('success', trans('admin/models/message.create.success'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2021-06-10 13:15:52 -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
|
|
|
|
*/
|
2024-07-04 12:50:11 -07:00
|
|
|
public function edit($modelId = null) : View | RedirectResponse
|
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';
|
2024-07-04 12:50:11 -07:00
|
|
|
return view('models/edit', compact('item', 'category_type'))->with('depreciation_list', Helper::depreciationList());
|
2021-06-10 13:15:52 -07:00
|
|
|
|
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
|
2024-05-29 10:01:40 -07:00
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
2018-07-24 19:35:26 -07:00
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
2024-07-04 13:14:58 -07:00
|
|
|
public function update(StoreAssetModelRequest $request, $modelId) : RedirectResponse
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2018-03-07 18:22:49 -08:00
|
|
|
$this->authorize('update', AssetModel::class);
|
2024-07-04 12:50:11 -07:00
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
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.does_not_exist'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
2020-08-26 02:29:37 -07:00
|
|
|
$model = $request->handleImages($model);
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
$model->depreciation_id = $request->input('depreciation_id');
|
|
|
|
$model->eol = $request->input('eol');
|
|
|
|
$model->name = $request->input('name');
|
|
|
|
$model->model_number = $request->input('model_number');
|
2023-08-21 12:07:57 -07:00
|
|
|
$model->min_amt = $request->input('min_amt');
|
2021-06-10 13:15:52 -07:00
|
|
|
$model->manufacturer_id = $request->input('manufacturer_id');
|
|
|
|
$model->category_id = $request->input('category_id');
|
|
|
|
$model->notes = $request->input('notes');
|
|
|
|
$model->requestable = $request->input('requestable', '0');
|
2020-08-26 02:29:37 -07:00
|
|
|
|
2018-04-23 21:16:55 -07:00
|
|
|
$this->removeCustomFieldsDefaultValues($model);
|
|
|
|
|
2023-12-12 22:27:50 -08:00
|
|
|
$model->fieldset_id = $request->input('fieldset_id');
|
2018-04-23 21:16:55 -07:00
|
|
|
|
2023-12-12 22:27:50 -08:00
|
|
|
if ($this->shouldAddDefaultValues($request->input())) {
|
|
|
|
if (!$this->assignCustomFieldsDefaultValues($model, $request->input('default_values'))){
|
|
|
|
return redirect()->back()->withInput()->with('error', trans('admin/custom_fields/message.fieldset_default_value.error'));
|
2018-04-23 21:16:55 -07:00
|
|
|
}
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2023-12-12 22:27:50 -08:00
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
if ($model->save()) {
|
2023-07-13 16:36:14 -07:00
|
|
|
if ($model->wasChanged('eol')) {
|
2023-10-22 08:56:38 -07:00
|
|
|
if ($model->eol > 0) {
|
|
|
|
$newEol = $model->eol;
|
|
|
|
$model->assets()->whereNotNull('purchase_date')->where('eol_explicit', false)
|
|
|
|
->update(['asset_eol_date' => DB::raw('DATE_ADD(purchase_date, INTERVAL ' . $newEol . ' MONTH)')]);
|
|
|
|
} elseif ($model->eol == 0) {
|
|
|
|
$model->assets()->whereNotNull('purchase_date')->where('eol_explicit', false)
|
|
|
|
->update(['asset_eol_date' => DB::raw('null')]);
|
|
|
|
}
|
2023-07-13 16:36:14 -07:00
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
return redirect()->route('models.index')->with('success', trans('admin/models/message.update.success'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2021-06-10 13:15:52 -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
|
|
|
|
*/
|
2024-07-04 12:50:11 -07:00
|
|
|
public function destroy($modelId) : RedirectResponse
|
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))) {
|
2023-11-22 10:02:47 -08:00
|
|
|
return redirect()->route('models.index')->with('error', trans('admin/models/message.does_not_exist'));
|
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) {
|
2021-06-10 13:15:52 -07:00
|
|
|
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) {
|
2024-05-29 04:38:15 -07:00
|
|
|
Log::info($e);
|
2017-10-28 05:46:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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]
|
2023-11-22 10:02:47 -08:00
|
|
|
* @param int $id
|
2018-07-24 19:35:26 -07:00
|
|
|
*/
|
2024-07-04 12:50:11 -07:00
|
|
|
public function getRestore($id) : RedirectResponse
|
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
|
|
|
|
2023-11-22 10:02:47 -08:00
|
|
|
if ($model = AssetModel::withTrashed()->find($id)) {
|
|
|
|
|
|
|
|
if ($model->deleted_at == '') {
|
|
|
|
return redirect()->back()->with('error', trans('general.not_deleted', ['item_type' => trans('general.asset_model')]));
|
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2023-11-22 10:02:47 -08:00
|
|
|
if ($model->restore()) {
|
|
|
|
$logaction = new Actionlog();
|
|
|
|
$logaction->item_type = User::class;
|
|
|
|
$logaction->item_id = $model->id;
|
|
|
|
$logaction->created_at = date('Y-m-d H:i:s');
|
2024-07-04 12:50:11 -07:00
|
|
|
$logaction->user_id = auth()->id();
|
2023-11-22 12:08:41 -08:00
|
|
|
$logaction->logaction('restore');
|
2023-11-22 10:02:47 -08:00
|
|
|
|
|
|
|
|
|
|
|
// Redirect them to the deleted page if there are more, otherwise the section index
|
|
|
|
$deleted_models = AssetModel::onlyTrashed()->count();
|
|
|
|
if ($deleted_models > 0) {
|
|
|
|
return redirect()->back()->with('success', trans('admin/models/message.restore.success'));
|
|
|
|
}
|
|
|
|
return redirect()->route('models.index')->with('success', trans('admin/models/message.restore.success'));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check validation
|
|
|
|
return redirect()->back()->with('error', trans('general.could_not_restore', ['item_type' => trans('general.asset_model'), 'error' => $model->getErrors()->first()]));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2023-11-22 10:02:47 -08:00
|
|
|
|
|
|
|
return redirect()->back()->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
|
|
|
* Get the model information to present to the model view page
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v1.0]
|
|
|
|
* @param int $modelId
|
|
|
|
*/
|
2024-07-04 12:50:11 -07:00
|
|
|
public function show($modelId = null) : View | RedirectResponse
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2018-01-20 04:39:31 -08:00
|
|
|
$this->authorize('view', AssetModel::class);
|
2024-06-18 02:27:11 -07:00
|
|
|
$model = AssetModel::withTrashed()->find($modelId);
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-04-07 17:26:56 -07:00
|
|
|
return redirect()->route('models.index')->with('error', trans('admin/models/message.does_not_exist'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-06-10 13:15:52 -07:00
|
|
|
* Get the clone page to clone a model
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v1.0]
|
|
|
|
* @param int $modelId
|
|
|
|
*/
|
2024-07-04 12:50:11 -07:00
|
|
|
public function getClone($modelId = null) : View | RedirectResponse
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2021-12-09 05:42:18 -08:00
|
|
|
$this->authorize('create', AssetModel::class);
|
2016-03-25 01:18:05 -07:00
|
|
|
// 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)
|
2023-08-28 15:23:26 -07:00
|
|
|
->with('model_id', $model_to_clone->id)
|
2018-07-24 19:35:26 -07:00
|
|
|
->with('clone_model', $model_to_clone);
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2021-06-10 13:15:52 -07:00
|
|
|
* Get the custom fields form
|
|
|
|
*
|
|
|
|
* @author [B. Wetherington] [<uberbrady@gmail.com>]
|
|
|
|
* @since [v2.0]
|
|
|
|
* @param int $modelId
|
|
|
|
*/
|
2024-07-04 12:50:11 -07:00
|
|
|
public function getCustomFields($modelId) : View
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2021-06-10 13:15:52 -07:00
|
|
|
return view('models.custom_fields_form')->with('model', AssetModel::find($modelId));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-06-08 17:48:48 -07:00
|
|
|
/**
|
|
|
|
* Returns a view that allows the user to bulk edit model attrbutes
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v1.7]
|
|
|
|
*/
|
2024-07-04 12:50:11 -07:00
|
|
|
public function postBulkEdit(Request $request) : View | RedirectResponse
|
2017-06-08 17:48:48 -07:00
|
|
|
{
|
2019-12-11 11:09:54 -08:00
|
|
|
$models_raw_array = $request->input('ids');
|
2017-10-18 08:45:05 -07:00
|
|
|
|
2017-12-12 09:10:05 -08:00
|
|
|
// Make sure some IDs have been selected
|
|
|
|
if ((is_array($models_raw_array)) && (count($models_raw_array) > 0)) {
|
2019-05-22 00:52:51 -07:00
|
|
|
$models = AssetModel::whereIn('id', $models_raw_array)->withCount('assets as assets_count')->orderBy('assets_count', 'ASC')->get();
|
2017-12-12 09:10:05 -08:00
|
|
|
|
|
|
|
// If deleting....
|
2021-06-10 13:15:52 -07:00
|
|
|
if ($request->input('bulk_actions') == 'delete') {
|
2017-12-12 09:10:05 -08:00
|
|
|
$valid_count = 0;
|
|
|
|
foreach ($models as $model) {
|
|
|
|
if ($model->assets_count == 0) {
|
|
|
|
$valid_count++;
|
|
|
|
}
|
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2017-12-12 09:10:05 -08:00
|
|
|
return view('models/bulk-delete', compact('models'))->with('valid_count', $valid_count);
|
|
|
|
|
|
|
|
// Otherwise display the bulk edit screen
|
|
|
|
} else {
|
|
|
|
$nochange = ['NC' => 'No Change'];
|
|
|
|
$fieldset_list = $nochange + Helper::customFieldsetList();
|
|
|
|
$depreciation_list = $nochange + Helper::depreciationList();
|
|
|
|
|
|
|
|
return view('models/bulk-edit', compact('models'))
|
|
|
|
->with('fieldset_list', $fieldset_list)
|
|
|
|
->with('depreciation_list', $depreciation_list);
|
|
|
|
}
|
2017-10-18 08:15:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->route('models.index')
|
|
|
|
->with('error', 'You must select at least one model to edit.');
|
2017-06-08 17:48:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a view that allows the user to bulk edit model attrbutes
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v1.7]
|
|
|
|
*/
|
2024-07-04 12:50:11 -07:00
|
|
|
public function postBulkEditSave(Request $request) : RedirectResponse
|
2017-06-08 17:48:48 -07:00
|
|
|
{
|
2019-12-11 11:09:54 -08:00
|
|
|
$models_raw_array = $request->input('ids');
|
2021-06-10 13:15:52 -07:00
|
|
|
$update_array = [];
|
2017-06-08 17:48:48 -07:00
|
|
|
|
2017-12-12 09:10:05 -08:00
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
if (($request->filled('manufacturer_id') && ($request->input('manufacturer_id') != 'NC'))) {
|
2017-06-08 17:48:48 -07:00
|
|
|
$update_array['manufacturer_id'] = $request->input('manufacturer_id');
|
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
if (($request->filled('category_id') && ($request->input('category_id') != 'NC'))) {
|
2017-06-08 17:48:48 -07:00
|
|
|
$update_array['category_id'] = $request->input('category_id');
|
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
if ($request->input('fieldset_id') != 'NC') {
|
2017-06-08 17:48:48 -07:00
|
|
|
$update_array['fieldset_id'] = $request->input('fieldset_id');
|
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
if ($request->input('depreciation_id') != 'NC') {
|
2017-06-08 17:48:48 -07:00
|
|
|
$update_array['depreciation_id'] = $request->input('depreciation_id');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (count($update_array) > 0) {
|
|
|
|
AssetModel::whereIn('id', $models_raw_array)->update($update_array);
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2017-06-08 17:48:48 -07:00
|
|
|
return redirect()->route('models.index')
|
|
|
|
->with('success', trans('admin/models/message.bulkedit.success'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->route('models.index')
|
|
|
|
->with('warning', trans('admin/models/message.bulkedit.error'));
|
|
|
|
}
|
|
|
|
|
2017-12-12 09:10:05 -08:00
|
|
|
/**
|
|
|
|
* Validate and delete the given Asset Models. An Asset Model
|
|
|
|
* cannot be deleted if there are associated assets.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v1.0]
|
|
|
|
* @param int $modelId
|
|
|
|
*/
|
2024-07-04 12:50:11 -07:00
|
|
|
public function postBulkDelete(Request $request) : RedirectResponse
|
2017-12-12 09:10:05 -08:00
|
|
|
{
|
2019-12-11 11:09:54 -08:00
|
|
|
$models_raw_array = $request->input('ids');
|
2017-12-12 09:10:05 -08:00
|
|
|
|
|
|
|
if ((is_array($models_raw_array)) && (count($models_raw_array) > 0)) {
|
2019-05-22 00:52:51 -07:00
|
|
|
$models = AssetModel::whereIn('id', $models_raw_array)->withCount('assets as assets_count')->get();
|
2017-12-12 09:10:05 -08:00
|
|
|
|
|
|
|
$del_error_count = 0;
|
|
|
|
$del_count = 0;
|
|
|
|
|
|
|
|
foreach ($models as $model) {
|
|
|
|
|
|
|
|
if ($model->assets_count > 0) {
|
|
|
|
$del_error_count++;
|
|
|
|
} else {
|
|
|
|
$model->delete();
|
|
|
|
$del_count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ($del_error_count == 0) {
|
|
|
|
return redirect()->route('models.index')
|
2021-06-10 13:15:52 -07:00
|
|
|
->with('success', trans('admin/models/message.bulkdelete.success', ['success_count'=> $del_count]));
|
2017-12-12 09:10:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->route('models.index')
|
|
|
|
->with('warning', trans('admin/models/message.bulkdelete.success_partial', ['fail_count'=>$del_error_count, 'success_count'=> $del_count]));
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->route('models.index')
|
|
|
|
->with('error', trans('admin/models/message.bulkdelete.error'));
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
2024-07-04 12:50:11 -07:00
|
|
|
private function shouldAddDefaultValues(array $input) : bool
|
2018-04-23 21:16:55 -07:00
|
|
|
{
|
2021-06-10 13:15:52 -07:00
|
|
|
return ! empty($input['add_default_values'])
|
|
|
|
&& ! empty($input['default_values'])
|
2023-03-24 06:11:24 -07:00
|
|
|
&& ! empty($input['fieldset_id']);
|
2018-04-23 21:16:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds default values to a model (as long as they are truthy)
|
|
|
|
*
|
|
|
|
* @param AssetModel $model
|
|
|
|
* @param array $defaultValues
|
|
|
|
*/
|
2024-02-14 00:12:31 -08:00
|
|
|
private function assignCustomFieldsDefaultValues(AssetModel $model, array $defaultValues): bool
|
2018-04-23 21:16:55 -07:00
|
|
|
{
|
2022-06-22 19:06:07 -07:00
|
|
|
$data = array();
|
|
|
|
foreach ($defaultValues as $customFieldId => $defaultValue) {
|
2024-02-14 00:12:31 -08:00
|
|
|
$customField = CustomField::find($customFieldId);
|
2022-06-22 19:06:07 -07:00
|
|
|
|
|
|
|
$data[$customField->db_column] = $defaultValue;
|
|
|
|
}
|
|
|
|
|
2022-08-24 14:18:11 -07:00
|
|
|
$fieldsets = $model->fieldset->validation_rules();
|
|
|
|
$rules = array();
|
|
|
|
|
|
|
|
foreach ($fieldsets as $fieldset => $validation){
|
|
|
|
// If the field is marked as required, eliminate the rule so it doesn't interfere with the default values
|
|
|
|
// (we are at model level, the rule still applies when creating a new asset using this model)
|
|
|
|
$index = array_search('required', $validation);
|
|
|
|
if ($index !== false){
|
2022-08-25 16:16:50 -07:00
|
|
|
$validation[$index] = 'nullable';
|
2022-08-24 14:18:11 -07:00
|
|
|
}
|
|
|
|
$rules[$fieldset] = $validation;
|
|
|
|
}
|
2022-06-22 19:06:07 -07:00
|
|
|
|
|
|
|
$validator = Validator::make($data, $rules);
|
|
|
|
|
|
|
|
if($validator->fails()){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-04-23 21:16:55 -07:00
|
|
|
foreach ($defaultValues as $customFieldId => $defaultValue) {
|
2022-02-09 08:49:10 -08:00
|
|
|
if(is_array($defaultValue)){
|
2022-02-09 11:41:33 -08:00
|
|
|
$model->defaultValues()->attach($customFieldId, ['default_value' => implode(', ', $defaultValue)]);
|
2022-02-09 11:12:42 -08:00
|
|
|
}elseif ($defaultValue) {
|
2018-04-23 21:16:55 -07:00
|
|
|
$model->defaultValues()->attach($customFieldId, ['default_value' => $defaultValue]);
|
|
|
|
}
|
|
|
|
}
|
2022-06-22 19:06:07 -07:00
|
|
|
return true;
|
2018-04-23 21:16:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes all default values
|
|
|
|
*
|
|
|
|
*/
|
2024-07-04 12:50:11 -07:00
|
|
|
private function removeCustomFieldsDefaultValues(AssetModel $model) : void
|
2018-04-23 21:16:55 -07:00
|
|
|
{
|
|
|
|
$model->defaultValues()->detach();
|
|
|
|
}
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|