From e1837069da97f1dfcbbd0007c2eda0ddd1ee3d2e Mon Sep 17 00:00:00 2001 From: snipe Date: Wed, 9 Dec 2015 20:35:24 -0800 Subject: [PATCH] Allow bulk model update for #1462, fixes #1442 --- app/config/version.php | 4 +- app/controllers/admin/AssetsController.php | 123 ++++++++++++++---- app/lang/en/admin/hardware/form.php | 4 +- app/lang/en/admin/hardware/message.php | 2 +- app/routes.php | 5 + .../backend/hardware/bulk-delete.blade.php | 71 ++++++++++ app/views/backend/hardware/bulk.blade.php | 12 ++ app/views/backend/hardware/index.blade.php | 1 + 8 files changed, 190 insertions(+), 32 deletions(-) create mode 100644 app/views/backend/hardware/bulk-delete.blade.php diff --git a/app/config/version.php b/app/config/version.php index f9bfc38354..680fefbc2b 100644 --- a/app/config/version.php +++ b/app/config/version.php @@ -1,5 +1,5 @@ 'v2.0.6-97', - 'hash_version' => 'v2.0.6-97-g72f3b45', + 'app_version' => 'v2.0.6-99', + 'hash_version' => 'v2.0.6-99-g25338c0', ); \ No newline at end of file diff --git a/app/controllers/admin/AssetsController.php b/app/controllers/admin/AssetsController.php index db26b0986c..153f113300 100755 --- a/app/controllers/admin/AssetsController.php +++ b/app/controllers/admin/AssetsController.php @@ -33,6 +33,7 @@ use Artisan; use Symfony\Component\Console\Output\BufferedOutput; use CustomField; use Symfony\Component\HttpFoundation\JsonResponse; +use Models; class AssetsController extends AdminController @@ -1086,43 +1087,56 @@ class AssetsController extends AdminController public function postBulkEdit($assets = null) { - if (!Company::isCurrentUserAuthorized()) { - return Redirect::to('hardware')->with('error', Lang::get('general.insufficient_permissions')); - } - else if (!Input::has('edit_asset')) { - return Redirect::back()->with('error', 'No assets selected'); - } else { - $asset_raw_array = Input::get('edit_asset'); - foreach ($asset_raw_array as $asset_id => $value) { - $asset_ids[] = $asset_id; + if (!Company::isCurrentUserAuthorized()) { + return Redirect::to('hardware')->with('error', Lang::get('general.insufficient_permissions')); - } + } elseif (!Input::has('edit_asset')) { + return Redirect::back()->with('error', 'No assets selected'); - } + } else { + $asset_raw_array = Input::get('edit_asset'); + foreach ($asset_raw_array as $asset_id => $value) { + $asset_ids[] = $asset_id; + + } + + } if (Input::has('bulk_actions')) { // Create labels if (Input::get('bulk_actions')=='labels') { - $assets = Asset::find($asset_ids); - $assetcount = count($assets); - $count = 0; + $settings = Setting::getSettings(); + if ($settings->qr_code=='1') { - $settings = Setting::getSettings(); - return View::make('backend/hardware/labels')->with('assets',$assets)->with('settings',$settings)->with('count',$count); + $assets = Asset::find($asset_ids); + $assetcount = count($assets); + $count = 0; + return View::make('backend/hardware/labels')->with('assets',$assets)->with('settings',$settings)->with('count',$count); + + } else { + // QR codes are not enabled + return Redirect::to("hardware")->with('error','Barcodes are not enabled in Admin > Settings'); + } + + } elseif (Input::get('bulk_actions')=='delete') { + + + $assets = Asset::with('assigneduser','assetloc')->find($asset_ids); + return View::make('backend/hardware/bulk-delete')->with('assets',$assets); // Bulk edit } elseif (Input::get('bulk_actions')=='edit') { $assets = Input::get('edit_asset'); - $supplier_list = array('' => '') + Supplier::orderBy('name', 'asc')->lists('name', 'id'); - $statuslabel_list = array('' => '') + Statuslabel::lists('name', 'id'); - $location_list = array('' => '') + Location::lists('name', 'id'); + $statuslabel_list = array('' => '') + Statuslabel::lists('name', 'id'); + $location_list = array('' => '') + Location::lists('name', 'id'); + $models_list = array('' => '') + Model::lists('name', 'id'); - return View::make('backend/hardware/bulk')->with('assets',$assets)->with('supplier_list',$supplier_list)->with('statuslabel_list',$statuslabel_list)->with('location_list',$location_list); + return View::make('backend/hardware/bulk')->with('assets',$assets)->with('supplier_list',$supplier_list)->with('statuslabel_list',$statuslabel_list)->with('location_list',$location_list)->with('models_list',$models_list); } @@ -1145,14 +1159,14 @@ class AssetsController extends AdminController public function postBulkSave($assets = null) { - if (!Company::isCurrentUserAuthorized()) { - return Redirect::to('hardware')->with('error', Lang::get('general.insufficient_permissions')); - } - else if (Input::has('bulk_edit')) { + if (!Company::isCurrentUserAuthorized()) { + return Redirect::to('hardware')->with('error', Lang::get('general.insufficient_permissions')); + + } elseif (Input::has('bulk_edit')) { $assets = Input::get('bulk_edit'); - if ( (Input::has('purchase_date')) || (Input::has('purchase_cost')) || (Input::has('supplier_id')) || (Input::has('order_number')) || (Input::has('warranty_months')) || (Input::has('rtd_location_id')) || (Input::has('requestable')) || (Input::has('status_id')) ) { + if ( (Input::has('purchase_date')) || (Input::has('purchase_cost')) || (Input::has('supplier_id')) || (Input::has('order_number')) || (Input::has('warranty_months')) || (Input::has('rtd_location_id')) || (Input::has('requestable')) || (Input::has('status_id')) || (Input::has('model_id')) ) { foreach ($assets as $key => $value) { @@ -1170,6 +1184,10 @@ class AssetsController extends AdminController $update_array['supplier_id'] = e(Input::get('supplier_id')); } + if (Input::has('model_id')) { + $update_array['model_id'] = e(Input::get('model_id')); + } + if (Input::has('order_number')) { $update_array['order_number'] = e(Input::get('order_number')); } @@ -1186,11 +1204,11 @@ class AssetsController extends AdminController $update_array['status_id'] = e(Input::get('status_id')); } - if (Input::get('requestable')=='1') { + if (Input::get('requestable')=='1') { $update_array['requestable'] = 1; } else { - $update_array['requestable'] = 0; - } + $update_array['requestable'] = 0; + } if (DB::table('assets') @@ -1227,6 +1245,55 @@ class AssetsController extends AdminController } + /** + * Save bulk edits + * + * @return View + **/ + public function postBulkDelete($assets = null) + { + + if (!Company::isCurrentUserAuthorized()) { + return Redirect::to('hardware')->with('error', Lang::get('general.insufficient_permissions')); + } elseif (Input::has('bulk_edit')) { + //$assets = Input::get('bulk_edit'); + $assets = Asset::find(Input::get('bulk_edit')); + //print_r($assets); + + + foreach ($assets as $asset) { + //echo '
  • '.$asset; + $update_array['deleted_at'] = date('Y-m-d h:i:s'); + $update_array['assigned_to'] = NULL; + + if (DB::table('assets') + ->where('id', $asset->id) + ->update($update_array)) { + + $logaction = new Actionlog(); + $logaction->asset_id = $asset->id; + $logaction->asset_type = 'hardware'; + $logaction->created_at = date("Y-m-d H:i:s"); + $logaction->user_id = Sentry::getUser()->id; + $log = $logaction->logaction('deleted'); + + } + + } // endforeach + return Redirect::to("hardware")->with('success', Lang::get('admin/hardware/message.delete.success')); + + // no values given, nothing to update + } else { + return Redirect::to("hardware")->with('info',Lang::get('admin/hardware/message.delete.nothing_updated')); + + } + + // Something weird happened here - default to hardware + return Redirect::to("hardware"); + + } + + public function getDatatable($status = null) { diff --git a/app/lang/en/admin/hardware/form.php b/app/lang/en/admin/hardware/form.php index 5cadad3641..e4bf1e3f39 100755 --- a/app/lang/en/admin/hardware/form.php +++ b/app/lang/en/admin/hardware/form.php @@ -1,7 +1,9 @@ 'Confrm Bulk Delete Assets', + 'bulk_delete_help' => 'Review the assets for bulk deletion below. Once deleted, these assets can be restored, but they will no longer be associated with any users they are currently assigned to.', + 'bulk_delete_warn' => 'You are about to delete :asset_count assets.', 'bulk_update' => 'Bulk Update Assets', 'bulk_update_help' => 'This form allows you to update multiple assets at once. Only fill in the fields you need to change. Any fields left blank will remain unchanged. ', 'bulk_update_warn' => 'You are about to edit the properties of :asset_count assets.', diff --git a/app/lang/en/admin/hardware/message.php b/app/lang/en/admin/hardware/message.php index 5bd3798513..403369846d 100755 --- a/app/lang/en/admin/hardware/message.php +++ b/app/lang/en/admin/hardware/message.php @@ -52,7 +52,7 @@ return array( 'checkin' => array( 'error' => 'Asset was not checked in, please try again', 'success' => 'Asset checked in successfully.', - 'user_does_not_exist' => 'That user is invalid. Please try again.' + 'user_does_not_exist' => 'That user is invalid.. Please try again.' ), 'requests' => array( diff --git a/app/routes.php b/app/routes.php index d4cc5c31bd..4f39ccec58 100755 --- a/app/routes.php +++ b/app/routes.php @@ -199,6 +199,11 @@ 'as' => 'hardware/bulkedit', 'uses' => 'AssetsController@postBulkEdit' ] ); + Route::post( 'bulkdelete', + [ + 'as' => 'hardware/bulkdelete', + 'uses' => 'AssetsController@postBulkDelete' + ] ); Route::post( 'bulksave', [ 'as' => 'hardware/bulksave', diff --git a/app/views/backend/hardware/bulk-delete.blade.php b/app/views/backend/hardware/bulk-delete.blade.php new file mode 100644 index 0000000000..2b8a9429fa --- /dev/null +++ b/app/views/backend/hardware/bulk-delete.blade.php @@ -0,0 +1,71 @@ +@extends('backend/layouts/default') + +{{-- Page title --}} +@section('title') + @lang('admin/hardware/form.bulk_delete') :: + +@parent +@stop + +{{-- Page content --}} + +@section('content') + +
    +
    + @lang('general.back') +

    + + @lang('admin/hardware/form.bulk_delete') +

    +
    +
    + +
    + +
    +

    @lang('admin/hardware/form.bulk_delete_help')

    +

    @lang('admin/hardware/form.bulk_delete_warn', ['asset_count' => count($assets)])

    + + +
    + + + + + + + + + + + + + @foreach ($assets as $asset) + + + + + + + + @endforeach + + + +
    IDNameLocationAssigned To
    {{{ $asset->id }}}{{{ $asset->showAssetName() }}} + @if ($asset->assetloc) + {{{ $asset->assetloc->name }}} + @endif + + @if ($asset->assigneduser) + {{{ $asset->assigneduser->fullName() }}} ({{{ $asset->assigneduser->username }}}) + @endif +
    + + + +
    +
    +
    +@stop diff --git a/app/views/backend/hardware/bulk.blade.php b/app/views/backend/hardware/bulk.blade.php index c19017e298..a66c25e8cb 100755 --- a/app/views/backend/hardware/bulk.blade.php +++ b/app/views/backend/hardware/bulk.blade.php @@ -56,6 +56,18 @@ + +
    + +
    + {{ Form::select('model_id', $models_list , Input::old('model_id'), array('class'=>'select2', 'style'=>'width:350px')) }} + {{ $errors->first('model_id', '
    :message') }} +
    +
    + +
    diff --git a/app/views/backend/hardware/index.blade.php b/app/views/backend/hardware/index.blade.php index 87fb1324e9..da923156ee 100755 --- a/app/views/backend/hardware/index.blade.php +++ b/app/views/backend/hardware/index.blade.php @@ -100,6 +100,7 @@