From 88b64034e8cb815d9c2cdcd13f80a245d7b6ab4a Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 12 Dec 2017 09:03:37 -0800 Subject: [PATCH] Fixed #3416 - bulk delete asset models --- .../Controllers/AssetModelsController.php | 94 ++++++++++++++++--- resources/lang/en/admin/models/general.php | 3 + resources/lang/en/admin/models/message.php | 6 ++ resources/views/models/bulk-delete.blade.php | 92 ++++++++++++++++++ resources/views/models/index.blade.php | 1 + routes/web/models.php | 1 + 6 files changed, 184 insertions(+), 13 deletions(-) create mode 100644 resources/views/models/bulk-delete.blade.php diff --git a/app/Http/Controllers/AssetModelsController.php b/app/Http/Controllers/AssetModelsController.php index 3fe707c34436..ca43b0c925c9 100755 --- a/app/Http/Controllers/AssetModelsController.php +++ b/app/Http/Controllers/AssetModelsController.php @@ -393,20 +393,39 @@ public function postBulkEdit(Request $request) $models_raw_array = Input::get('ids'); - if (is_array($models_raw_array)) { - $models = AssetModel::whereIn('id', $models_raw_array)->get(); - $nochange = ['NC' => 'No Change']; - $fieldset_list = $nochange + Helper::customFieldsetList(); - $depreciation_list = $nochange + Helper::depreciationList(); - $category_list = $nochange + Helper::categoryList('asset'); - $manufacturer_list = $nochange + Helper::manufacturerList(); + // Make sure some IDs have been selected + if ((is_array($models_raw_array)) && (count($models_raw_array) > 0)) { + + + $models = AssetModel::whereIn('id', $models_raw_array)->withCount('assets')->orderBy('assets_count', 'ASC')->get(); + + // If deleting.... + if ($request->input('bulk_actions')=='delete') { + $valid_count = 0; + foreach ($models as $model) { + if ($model->assets_count == 0) { + $valid_count++; + } + } + 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(); + $category_list = $nochange + Helper::categoryList('asset'); + $manufacturer_list = $nochange + Helper::manufacturerList(); + + + return view('models/bulk-edit', compact('models')) + ->with('manufacturer_list', $manufacturer_list) + ->with('category_list', $category_list) + ->with('fieldset_list', $fieldset_list) + ->with('depreciation_list', $depreciation_list); + } - - return view('models/bulk-edit', compact('models')) - ->with('manufacturer_list', $manufacturer_list) - ->with('category_list', $category_list) - ->with('fieldset_list', $fieldset_list) - ->with('depreciation_list', $depreciation_list); } return redirect()->route('models.index') @@ -429,6 +448,7 @@ public function postBulkEditSave(Request $request) $models_raw_array = Input::get('ids'); $update_array = array(); + if (($request->has('manufacturer_id') && ($request->input('manufacturer_id')!='NC'))) { $update_array['manufacturer_id'] = $request->input('manufacturer_id'); } @@ -455,4 +475,52 @@ public function postBulkEditSave(Request $request) } + /** + * Validate and delete the given Asset Models. An Asset Model + * cannot be deleted if there are associated assets. + * + * @author [A. Gianotto] [] + * @since [v1.0] + * @param int $modelId + * @return Redirect + */ + public function postBulkDelete(Request $request) + { + $models_raw_array = Input::get('ids'); + + if ((is_array($models_raw_array)) && (count($models_raw_array) > 0)) { + + $models = AssetModel::whereIn('id', $models_raw_array)->withCount('assets')->get(); + + $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') + ->with('success', trans('admin/models/message.bulkdelete.success',['success_count'=> $del_count] )); + } + + 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')); + + } + } diff --git a/resources/lang/en/admin/models/general.php b/resources/lang/en/admin/models/general.php index e0da09f780c9..7fc92ca6ae76 100644 --- a/resources/lang/en/admin/models/general.php +++ b/resources/lang/en/admin/models/general.php @@ -4,6 +4,9 @@ 'about_models_title' => 'About Asset Models', 'about_models_text' => 'Asset Models are a way to group identical assets. "MBP 2013", "IPhone 6s", etc.', 'deleted' => 'This model has been deleted. Click here to restore it.', + 'bulk_delete' => 'Bulk Delete Asset Models', + 'bulk_delete_help' => 'Use the checkboxes below to confirm the deletion of the selected asset models. Asset models that have assets associated with them cannot be deleted until the assets are associated with a different model.', + 'bulk_delete_warn' => 'You are about to delete :model_count asset models.', 'restore' => 'Restore Model', 'requestable' => 'Users may request this model', 'show_mac_address' => 'Show MAC address field in assets in this model', diff --git a/resources/lang/en/admin/models/message.php b/resources/lang/en/admin/models/message.php index 5b1b9a1a1b68..e3b29d5b4bb6 100644 --- a/resources/lang/en/admin/models/message.php +++ b/resources/lang/en/admin/models/message.php @@ -33,4 +33,10 @@ 'success' => 'Models updated.' ), + 'bulkdelete' => array( + 'error' => 'No models were selected, so nothing was deleted.', + 'success' => ':success_count model(s) deleted!', + 'success_partial' => ':success_count model(s) were deleted, however :fail_count were unable to be deleted because they still have assets associated with them.' + ), + ); diff --git a/resources/views/models/bulk-delete.blade.php b/resources/views/models/bulk-delete.blade.php new file mode 100644 index 000000000000..5a21db83e36d --- /dev/null +++ b/resources/views/models/bulk-delete.blade.php @@ -0,0 +1,92 @@ +@extends('layouts/default') + +{{-- Page title --}} +@section('title') + {{ trans('admin/models/general.bulk_delete') }} + @parent +@stop + +@section('header_right') + + {{ trans('general.back') }} +@stop + +{{-- Page content --}} +@section('content') +
+ +
+

{{ trans('admin/models/general.bulk_delete_help') }}

+
+ {{csrf_field()}} +
+
+

{{ trans('admin/models/general.bulk_delete_warn', ['model_count' => $valid_count]) }}

+
+ +
+ + + + + + + + + + @foreach ($models as $model) + assets_count > 0 ) ? ' class="danger"' : '') !!}> + + + + + + @endforeach + +
+ + + Name
+ assets_count == 0) ? ' checked="checked"' : ' disabled') !!}> + {{ $model->assets_count }}{{ $model->name }}
+
+ + +
+
+
+
+@stop +@section('moar_scripts') + +@stop diff --git a/resources/views/models/index.blade.php b/resources/views/models/index.blade.php index b7ab4caf2b82..c1092650ca24 100755 --- a/resources/views/models/index.blade.php +++ b/resources/views/models/index.blade.php @@ -45,6 +45,7 @@
diff --git a/routes/web/models.php b/routes/web/models.php index e629307b3c13..25437051fe5c 100644 --- a/routes/web/models.php +++ b/routes/web/models.php @@ -10,6 +10,7 @@ Route::get('{modelId}/custom_fields', ['as' => 'custom_fields/model','uses' => 'AssetModelsController@getCustomFields']); Route::post('bulkedit', ['as' => 'models.bulkedit.index','uses' => 'AssetModelsController@postBulkEdit']); Route::post('bulksave', ['as' => 'models.bulkedit.store','uses' => 'AssetModelsController@postBulkEditSave']); + Route::post('bulkdelete', ['as' => 'models.bulkdelete.store','uses' => 'AssetModelsController@postBulkDelete']); }); Route::resource('models', 'AssetModelsController', [