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;
|
2016-03-25 01:18:05 -07:00
|
|
|
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;
|
2022-06-22 19:06:07 -07:00
|
|
|
use Illuminate\Support\Facades\Validator;
|
2016-03-25 01:18:05 -07:00
|
|
|
use Redirect;
|
2019-12-10 20:39:29 -08:00
|
|
|
use Request;
|
2019-12-10 22:03:15 -08:00
|
|
|
use Storage;
|
2016-03-25 01:18:05 -07:00
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
|
|
|
|
|
|
/**
|
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);
|
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]
|
|
|
|
* @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);
|
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
|
|
|
|
* @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');
|
2021-06-10 13:15:52 -07: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');
|
|
|
|
$model->user_id = Auth::id();
|
|
|
|
$model->requestable = Request::has('requestable');
|
|
|
|
|
|
|
|
if ($request->input('custom_fieldset') != '') {
|
2016-12-15 15:56:52 -08:00
|
|
|
$model->fieldset_id = e($request->input('custom_fieldset'));
|
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
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
// Was it created?
|
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
|
|
|
}
|
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
// Redirect to the new model page
|
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
|
|
|
|
* @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';
|
2021-06-10 13:15:52 -07:00
|
|
|
$view = View::make('models/edit', compact('item', 'category_type'));
|
2017-10-28 11:17:52 -07:00
|
|
|
$view->with('depreciation_list', Helper::depreciationList());
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2017-10-28 11:17:52 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
|
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');
|
|
|
|
$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);
|
|
|
|
|
2021-06-10 13:15:52 -07: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())) {
|
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
|
|
|
}
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($model->save()) {
|
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
|
|
|
|
* @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) {
|
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) {
|
2019-03-13 12:22:12 -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]
|
|
|
|
* @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();
|
2021-06-10 13:15:52 -07:00
|
|
|
|
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
|
|
|
}
|
2021-06-10 13:15:52 -07: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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
* @return View
|
|
|
|
*/
|
2016-03-25 01:18:05 -07:00
|
|
|
public function getClone($modelId = null)
|
|
|
|
{
|
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)
|
|
|
|
->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
|
|
|
|
* @return View
|
|
|
|
*/
|
2016-03-25 01:18:05 -07:00
|
|
|
public function getCustomFields($modelId)
|
|
|
|
{
|
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]
|
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
*/
|
|
|
|
public function postBulkEdit(Request $request)
|
|
|
|
{
|
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]
|
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
*/
|
|
|
|
public function postBulkEditSave(Request $request)
|
|
|
|
{
|
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
|
|
|
|
* @return Redirect
|
|
|
|
*/
|
|
|
|
public function postBulkDelete(Request $request)
|
|
|
|
{
|
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) {
|
|
|
|
\Log::debug($model->id);
|
|
|
|
|
|
|
|
if ($model->assets_count > 0) {
|
|
|
|
$del_error_count++;
|
|
|
|
} else {
|
|
|
|
$model->delete();
|
|
|
|
$del_count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
\Log::debug($del_count);
|
|
|
|
\Log::debug($del_error_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
|
2021-06-10 13:15:52 -07:00
|
|
|
* @return bool
|
2018-04-23 21:16:55 -07:00
|
|
|
*/
|
|
|
|
private function shouldAddDefaultValues(array $input)
|
|
|
|
{
|
2021-06-10 13:15:52 -07:00
|
|
|
return ! empty($input['add_default_values'])
|
|
|
|
&& ! empty($input['default_values'])
|
|
|
|
&& ! empty($input['custom_fieldset']);
|
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
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function assignCustomFieldsDefaultValues(AssetModel $model, array $defaultValues)
|
|
|
|
{
|
2022-06-22 19:06:07 -07:00
|
|
|
$data = array();
|
|
|
|
foreach ($defaultValues as $customFieldId => $defaultValue) {
|
|
|
|
$customField = \App\Models\CustomField::find($customFieldId);
|
|
|
|
|
|
|
|
$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){
|
|
|
|
unset($validation[$index]);
|
|
|
|
}
|
|
|
|
$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
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function removeCustomFieldsDefaultValues(AssetModel $model)
|
|
|
|
{
|
|
|
|
$model->defaultValues()->detach();
|
|
|
|
}
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|