Fixed #3416 - bulk delete asset models

This commit is contained in:
snipe 2017-12-12 09:10:05 -08:00
parent 1d82f80e73
commit 608bb1b5b1
9 changed files with 196 additions and 21 deletions

View file

@ -16,7 +16,8 @@ class RecryptFromMcrypt extends Command
* *
* @var string * @var string
*/ */
protected $signature = 'snipeit:legacy-recrypt'; protected $signature = 'snipeit:legacy-recrypt
{--force : Force a re-crypt of encrypted data from MCRYPT.}';
/** /**
* The console command description. * The console command description.
@ -81,7 +82,9 @@ class RecryptFromMcrypt extends Command
$this->error('================================!!!! WARNING !!!!================================'); $this->error('================================!!!! WARNING !!!!================================');
$this->comment("This tool will attempt to decrypt your old Snipe-IT (mcrypt, now deprecated) encrypted data and re-encrypt it using OpenSSL. \n\nYou should only continue if you have backed up any and all old APP_KEYs and have backed up your data."); $this->comment("This tool will attempt to decrypt your old Snipe-IT (mcrypt, now deprecated) encrypted data and re-encrypt it using OpenSSL. \n\nYou should only continue if you have backed up any and all old APP_KEYs and have backed up your data.");
if ($this->confirm("Are you SURE you wish to continue?")) { $force = ($this->option('force')) ? true : false;
if ($force || ($this->confirm("Are you SURE you wish to continue?"))) {
$backup_file = 'backups/env-backups/'.'app_key-'.date('Y-m-d-gis'); $backup_file = 'backups/env-backups/'.'app_key-'.date('Y-m-d-gis');

View file

@ -393,8 +393,25 @@ class AssetModelsController extends Controller
$models_raw_array = Input::get('ids'); $models_raw_array = Input::get('ids');
if (is_array($models_raw_array)) { // Make sure some IDs have been selected
$models = AssetModel::whereIn('id', $models_raw_array)->get(); 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']; $nochange = ['NC' => 'No Change'];
$fieldset_list = $nochange + Helper::customFieldsetList(); $fieldset_list = $nochange + Helper::customFieldsetList();
$depreciation_list = $nochange + Helper::depreciationList(); $depreciation_list = $nochange + Helper::depreciationList();
@ -409,6 +426,8 @@ class AssetModelsController extends Controller
->with('depreciation_list', $depreciation_list); ->with('depreciation_list', $depreciation_list);
} }
}
return redirect()->route('models.index') return redirect()->route('models.index')
->with('error', 'You must select at least one model to edit.'); ->with('error', 'You must select at least one model to edit.');
@ -429,6 +448,7 @@ class AssetModelsController extends Controller
$models_raw_array = Input::get('ids'); $models_raw_array = Input::get('ids');
$update_array = array(); $update_array = array();
if (($request->has('manufacturer_id') && ($request->input('manufacturer_id')!='NC'))) { if (($request->has('manufacturer_id') && ($request->input('manufacturer_id')!='NC'))) {
$update_array['manufacturer_id'] = $request->input('manufacturer_id'); $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_title' => 'About Asset Models',
'about_models_text' => 'Asset Models are a way to group identical assets. "MBP 2013", "IPhone 6s", etc.', '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>.', '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', 'restore' => 'Restore Model',
'requestable' => 'Users may request this model', 'requestable' => 'Users may request this model',
'show_mac_address' => 'Show MAC address field in assets in 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.' '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"> <div id="toolbar">
<select name="bulk_actions" class="form-control select2" style="width: 300px;"> <select name="bulk_actions" class="form-control select2" style="width: 300px;">
<option value="edit">Bulk Edit</option> <option value="edit">Bulk Edit</option>
<option value="delete">Bulk Delete</option>
</select> </select>
<button class="btn btn-primary" id="bulkEdit" disabled>Go</button> <button class="btn btn-primary" id="bulkEdit" disabled>Go</button>
</div> </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::get('{modelId}/custom_fields', ['as' => 'custom_fields/model','uses' => 'AssetModelsController@getCustomFields']);
Route::post('bulkedit', ['as' => 'models.bulkedit.index','uses' => 'AssetModelsController@postBulkEdit']); Route::post('bulkedit', ['as' => 'models.bulkedit.index','uses' => 'AssetModelsController@postBulkEdit']);
Route::post('bulksave', ['as' => 'models.bulkedit.store','uses' => 'AssetModelsController@postBulkEditSave']); Route::post('bulksave', ['as' => 'models.bulkedit.store','uses' => 'AssetModelsController@postBulkEditSave']);
Route::post('bulkdelete', ['as' => 'models.bulkdelete.store','uses' => 'AssetModelsController@postBulkDelete']);
}); });
Route::resource('models', 'AssetModelsController', [ Route::resource('models', 'AssetModelsController', [

0
snipeit.sh Executable file → Normal file
View file

View file

@ -164,3 +164,4 @@ echo "your upgraded Snipe-IT.\n";
echo "--------------------------------------------------------\n\n"; echo "--------------------------------------------------------\n\n";