Fixed #3416 - bulk delete asset models

This commit is contained in:
snipe 2017-12-12 09:03:37 -08:00
parent 694c6f3ec6
commit 88b64034e8
6 changed files with 184 additions and 13 deletions

View file

@ -393,20 +393,39 @@ class AssetModelsController extends Controller
$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 @@ class AssetModelsController extends Controller
$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 @@ class AssetModelsController extends Controller
}
/**
* 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)
{
$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'));
}
}

View file

@ -4,6 +4,9 @@ return array(
'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. <a href="/hardware/models/:model_id/restore">Click here to restore it</a>.',
'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',

View file

@ -33,4 +33,10 @@ return array(
'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.'
),
);

View file

@ -0,0 +1,92 @@
@extends('layouts/default')
{{-- Page title --}}
@section('title')
{{ trans('admin/models/general.bulk_delete') }}
@parent
@stop
@section('header_right')
<a href="{{ URL::previous() }}" class="btn btn-primary pull-right">
{{ trans('general.back') }}</a>
@stop
{{-- Page content --}}
@section('content')
<div class="row">
<!-- left column -->
<div class="col-md-8 col-md-offset-2">
<p>{{ trans('admin/models/general.bulk_delete_help') }}</p>
<form class="form-horizontal" method="post" action="{{ route('models.bulkdelete.store') }}" autocomplete="off" role="form">
{{csrf_field()}}
<div class="box box-default">
<div class="box-header with-border">
<h3 class="box-title" style="color: red">{{ trans('admin/models/general.bulk_delete_warn', ['model_count' => $valid_count]) }}</h3>
</div>
<div class="box-body">
<table class="table table-striped table-condensed">
<thead>
<tr>
<td class="col-md-1">
<label>
<input type="checkbox" class="all minimal" checked="checked">
</label>
</td>
<td class="col-md-1"><i class="fa fa-barcode"></i></td>
<td class="col-md-10">Name</td>
</tr>
</thead>
<tbody>
@foreach ($models as $model)
<tr{!! (($model->assets_count > 0 ) ? ' class="danger"' : '') !!}>
<td>
<input type="checkbox" name="ids[]" class="minimal{{ (($model->assets_count == 0) ? '' : ' disabled') }}" value="{{ $model->id }}" {!! (($model->assets_count == 0) ? ' checked="checked"' : ' disabled') !!}>
</td>
<td>{{ $model->assets_count }}</td>
<td>{{ $model->name }}</td>
</tr>
@endforeach
</tbody>
</table>
</div><!-- /.box-body -->
<div class="box-footer text-right">
<a class="btn btn-link pull-left" href="{{ URL::previous() }}" method="post" enctype="multipart/form-data">{{ trans('button.cancel') }}</a>
<button type="submit" class="btn btn-success" id="submit-button"><i class="fa fa-check icon-white"></i> {{ trans('general.delete') }}</button>
</div><!-- /.box-footer -->
</div><!-- /.box -->
</form>
</div> <!-- .col-md-12-->
</div><!--.row-->
@stop
@section('moar_scripts')
<script>
// Check-all / Uncheck all
$(function () {
var checkAll = $('input.all');
var checkboxes = $('input.minimal');
checkAll.on('ifChecked ifUnchecked', function(event) {
if (event.type == 'ifChecked') {
checkboxes.iCheck('check');
} else {
checkboxes.iCheck('uncheck');
}
});
checkboxes.on('ifChanged', function(event){
if(checkboxes.filter(':checked').length == checkboxes.length) {
checkAll.prop('checked', 'checked');
} else {
checkAll.removeProp('checked');
}
checkAll.iCheck('update');
});
});
</script>
@stop

View file

@ -45,6 +45,7 @@
<div id="toolbar">
<select name="bulk_actions" class="form-control select2" style="width: 300px;">
<option value="edit">Bulk Edit</option>
<option value="delete">Bulk Delete</option>
</select>
<button class="btn btn-primary" id="bulkEdit" disabled>Go</button>
</div>

View file

@ -10,6 +10,7 @@ Route::group([ 'prefix' => 'models', 'middleware' => ['auth'] ], function () {
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', [