From c997ae44c2750e2e22faa98d84e78e7b9e7349a8 Mon Sep 17 00:00:00 2001 From: snipe Date: Mon, 20 Nov 2023 22:56:59 +0000 Subject: [PATCH 01/15] FAFO troubleshooting Signed-off-by: snipe --- .../Assets/BulkAssetsController.php | 110 ++++++++++-------- resources/views/hardware/bulk.blade.php | 7 +- 2 files changed, 63 insertions(+), 54 deletions(-) diff --git a/app/Http/Controllers/Assets/BulkAssetsController.php b/app/Http/Controllers/Assets/BulkAssetsController.php index 45ca5bab7c..df7c72ed09 100644 --- a/app/Http/Controllers/Assets/BulkAssetsController.php +++ b/app/Http/Controllers/Assets/BulkAssetsController.php @@ -23,6 +23,13 @@ class BulkAssetsController extends Controller /** * Display the bulk edit page. * + * This method is super weird because it's kinda of like a controller within a controller. + * It's main function is to determine what the bulk action in, and then return a view with + * the information that view needs, be it bulk delete, bulk edit, restore, etc. + * + * This is something that made sense at the time, but sort of doesn't make sense now. A JS front-end to determine form + * action would make a lot more sense here and make things a lot more clear. + * * @author [A. Gianotto] [] * @return View * @internal param int $assetId @@ -32,7 +39,9 @@ class BulkAssetsController extends Controller public function edit(Request $request) { $this->authorize('view', Asset::class); - + + \Log::debug('Bulk action was triggered: '.$request->input('bulk_actions')); + if (! $request->filled('ids')) { return redirect()->back()->with('error', trans('admin/hardware/message.update.no_assets_selected')); } @@ -41,60 +50,45 @@ class BulkAssetsController extends Controller $bulk_back_url = request()->headers->get('referer'); session(['bulk_back_url' => $bulk_back_url]); - $asset_ids = array_values(array_unique($request->input('ids'))); - - //custom fields logic - $asset_custom_field = Asset::with(['model.fieldset.fields', 'model'])->whereIn('id', $asset_ids)->whereHas('model', function ($query) { - return $query->where('fieldset_id', '!=', null); - })->get(); - - $models = $asset_custom_field->unique('model_id'); - $modelNames = []; - foreach($models as $model) { - $modelNames[] = $model->model->name; - } + $asset_ids = $request->input('ids'); + $assets = Asset::with('assignedTo', 'location', 'model')->find($asset_ids); if ($request->filled('bulk_actions')) { + + switch ($request->input('bulk_actions')) { case 'labels': $this->authorize('view', Asset::class); - $assets_found = Asset::find($asset_ids); - - if ($assets_found->isEmpty()){ - return redirect()->back(); - } return (new Label) - ->with('assets', $assets_found) + ->with('assets', $assets) ->with('settings', Setting::getSettings()) ->with('bulkedit', true) ->with('count', 0); case 'delete': $this->authorize('delete', Asset::class); - $assets = Asset::with('assignedTo', 'location')->find($asset_ids); - $assets->each(function ($asset) { - $this->authorize('delete', $asset); + $assets->each(function ($assets) { + $this->authorize('delete', $assets); }); return view('hardware/bulk-delete')->with('assets', $assets); case 'restore': + \Log::debug('We seem to be bulk restoring'); $this->authorize('update', Asset::class); - $assets = Asset::withTrashed()->find($asset_ids); $assets->each(function ($asset) { $this->authorize('delete', $asset); }); - return view('hardware/bulk-restore')->with('assets', $assets); case 'edit': $this->authorize('update', Asset::class); + \Log::debug('We seem to be bulk editing'); + return view('hardware/bulk') - ->with('assets', $asset_ids) - ->with('statuslabel_list', Helper::statusLabelList()) - ->with('models', $models->pluck(['model'])) - ->with('modelNames', $modelNames); + ->with('assets', $assets) + ->with('statuslabel_list', Helper::statusLabelList()); } } @@ -115,27 +109,35 @@ class BulkAssetsController extends Controller $has_errors = 0; $error_array = array(); + + print_r($assets, true); // Get the back url from the session and then destroy the session $bulk_back_url = route('hardware.index'); + if ($request->session()->has('bulk_back_url')) { $bulk_back_url = $request->session()->pull('bulk_back_url'); } $custom_field_columns = CustomField::all()->pluck('db_column')->toArray(); + + \Log::debug('Custom fields columns: '); + \Log::debug(print_r($custom_field_columns, true)); - if (Session::exists('ids')) { - $assets = Session::get('ids'); - } elseif (! $request->filled('ids') || count($request->input('ids')) <= 0) { + if (! $request->filled('ids') || count($request->input('ids')) == 0) { return redirect($bulk_back_url)->with('error', trans('admin/hardware/message.update.no_assets_selected')); } - - $assets = array_keys($request->input('ids')); - - if ($request->anyFilled($custom_field_columns)) { + + // $assetsIds = array_keys($request->input('ids')); + $assets = Asset::find($request->input('ids')); + + // We should tighten checks here - burpsuite could punch through this I'd pull the custom fields for new and old here + if ($request->anyFilled($custom_field_columns)) { $custom_fields_present = true; } else { $custom_fields_present = false; } + + // If ANY of these are filled, prepare to update the values on the assets if (($request->filled('purchase_date')) || ($request->filled('expected_checkin')) || ($request->filled('purchase_cost')) @@ -154,7 +156,8 @@ class BulkAssetsController extends Controller || ($request->anyFilled($custom_field_columns)) ) { - foreach ($assets as $assetId) { + // Let's loop through those assets and build an update array + foreach ($assets as $asset) { $this->update_array = []; @@ -186,7 +189,6 @@ class BulkAssetsController extends Controller $this->update_array['purchase_cost'] = $request->input('purchase_cost'); } - if ($request->filled('company_id')) { $this->update_array['company_id'] = $request->input('company_id'); if ($request->input('company_id') == 'clear') { @@ -195,20 +197,23 @@ class BulkAssetsController extends Controller } if ($request->filled('rtd_location_id')) { + if (($request->filled('update_real_loc')) && (($request->input('update_real_loc')) == '0')) { $this->update_array['rtd_location_id'] = $request->input('rtd_location_id'); } + if (($request->filled('update_real_loc')) && (($request->input('update_real_loc')) == '1')) { $this->update_array['location_id'] = $request->input('rtd_location_id'); $this->update_array['rtd_location_id'] = $request->input('rtd_location_id'); } + if (($request->filled('update_real_loc')) && (($request->input('update_real_loc')) == '2')) { $this->update_array['location_id'] = $request->input('rtd_location_id'); } + } $changed = []; - $asset = Asset::find($assetId); foreach ($this->update_array as $key => $value) { if ($this->update_array[$key] != $asset->{$key}) { @@ -216,20 +221,25 @@ class BulkAssetsController extends Controller $changed[$key]['new'] = $this->update_array[$key]; } } - + + \Log::debug(print_r($changed, true)); + + $existing_assetmodel = $asset->model; + $updated_model = $request->input('model_id'); + + // Use the rules of the new model fieldsets if the model changed + if (($request->filled('model_id')) && ($request->filled('model_id')!=$existing_assetmodel->id)) { + \Log::debug('Old and new models are different - change from '.$asset->model->id.' to model '.$request->input('model_id')); + $this->update_array['model_id'] = $request->input('model_id'); + $updated_model = \App\Models\AssetModel::find($request->input('model_id')); + } + + + /** Start all the custom fields shenanigans */ if ($custom_fields_present) { - $model = $asset->model()->first(); - - // Use the rules of the new model fieldsets if the model changed - if ($request->filled('model_id')) { - $this->update_array['model_id'] = $request->input('model_id'); - $model = \App\Models\AssetModel::find($request->input('model_id')); - } - - // Make sure this model is valid - $assetCustomFields = ($model) ? $model->fieldset : null; + $assetCustomFields = ($updated_model) ? $updated_model->fieldset : null; if ($assetCustomFields && $assetCustomFields->fields) { @@ -260,7 +270,6 @@ class BulkAssetsController extends Controller */ } else { - if ((array_key_exists($field->db_column, $this->update_array)) && ($asset->{$field->db_column} != $this->update_array[$field->db_column])) { // Check if this is an array, and if so, flatten it @@ -278,6 +287,7 @@ class BulkAssetsController extends Controller + \Log::debug(print_r($this->update_array, true)); // Check if it passes validation, and then try to save if (!$asset->update($this->update_array)) { diff --git a/resources/views/hardware/bulk.blade.php b/resources/views/hardware/bulk.blade.php index a7e52dfa56..78db17afc1 100755 --- a/resources/views/hardware/bulk.blade.php +++ b/resources/views/hardware/bulk.blade.php @@ -19,11 +19,10 @@

{{ trans('admin/hardware/form.bulk_update_help') }}

+ +
{{ trans_choice('admin/hardware/form.bulk_update_warn', count($assets), ['asset_count' => count($assets)]) }} - @if (count($models) > 0) - {{ trans_choice('admin/hardware/form.bulk_update_with_custom_field', count($models), ['asset_model_count' => count($models)]) }} - @endif
@@ -189,7 +188,7 @@ - @include("models/custom_fields_form_bulk_edit",["models" => $models]) + @foreach ($assets as $key => $value) From 5574218966389542a0f0870a75505c1a1fc3b3bd Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 21 Nov 2023 00:14:14 +0000 Subject: [PATCH 02/15] More FAFO - cleanup needed, but model_id works now Signed-off-by: snipe --- .../Assets/BulkAssetsController.php | 57 +++++++++++++------ 1 file changed, 41 insertions(+), 16 deletions(-) diff --git a/app/Http/Controllers/Assets/BulkAssetsController.php b/app/Http/Controllers/Assets/BulkAssetsController.php index df7c72ed09..5111b623cf 100644 --- a/app/Http/Controllers/Assets/BulkAssetsController.php +++ b/app/Http/Controllers/Assets/BulkAssetsController.php @@ -87,7 +87,7 @@ class BulkAssetsController extends Controller \Log::debug('We seem to be bulk editing'); return view('hardware/bulk') - ->with('assets', $assets) + ->with('assets', $asset_ids) ->with('statuslabel_list', Helper::statusLabelList()); } } @@ -109,8 +109,6 @@ class BulkAssetsController extends Controller $has_errors = 0; $error_array = array(); - - print_r($assets, true); // Get the back url from the session and then destroy the session $bulk_back_url = route('hardware.index'); @@ -120,15 +118,24 @@ class BulkAssetsController extends Controller $custom_field_columns = CustomField::all()->pluck('db_column')->toArray(); - \Log::debug('Custom fields columns: '); - \Log::debug(print_r($custom_field_columns, true)); +// \Log::debug('ALL Custom fields columns - these may or may not apply: '); +// \Log::debug(print_r($custom_field_columns, true)); if (! $request->filled('ids') || count($request->input('ids')) == 0) { return redirect($bulk_back_url)->with('error', trans('admin/hardware/message.update.no_assets_selected')); } // $assetsIds = array_keys($request->input('ids')); - $assets = Asset::find($request->input('ids')); + \Log::debug('Request IDs:'); + \Log::debug(print_r(array_keys($request->input('ids')), true)); + + $assets = Asset::whereIn('id', array_keys($request->input('ids')))->get(); + + \Log::debug('Affected asset list: '); + foreach ($assets as $log_asset) { + \Log::debug(' - Asset affected: '.$log_asset->asset_tag); + } + // We should tighten checks here - burpsuite could punch through this I'd pull the custom fields for new and old here if ($request->anyFilled($custom_field_columns)) { @@ -161,18 +168,22 @@ class BulkAssetsController extends Controller $this->update_array = []; + // Leave out model_id and sttaus here because we do math on that later. We have to d some extra + // validation and checks on those two $this->conditionallyAddItem('purchase_date') ->conditionallyAddItem('expected_checkin') ->conditionallyAddItem('order_number') ->conditionallyAddItem('requestable') - ->conditionallyAddItem('status_id') ->conditionallyAddItem('supplier_id') ->conditionallyAddItem('warranty_months') ->conditionallyAddItem('next_audit_date'); foreach ($custom_field_columns as $key => $custom_field_column) { $this->conditionallyAddItem($custom_field_column); - } + } + /** + * Blank out fields that were requested to be blanked out via checkbox + */ if ($request->input('null_purchase_date')=='1') { $this->update_array['purchase_date'] = null; } @@ -213,6 +224,27 @@ class BulkAssetsController extends Controller } + $existing_assetmodel = $asset->model; + + + // Use the new model_id and add it to the existing $this + if (($request->filled('model_id')) && ($request->input('model_id')!= $existing_assetmodel->id)) { + \Log::debug('Old and new models are different - change from '.$asset->model->id.' to model '.$request->input('model_id')); + $this->update_array['model_id'] = $request->input('model_id'); + $updated_model = \App\Models\AssetModel::find($request->input('model_id')); + } + + $existing_status = $asset->statuslabel; + + // Use the new status_id and add it to the existing $this + if (($request->filled('status_id')) && ($request->input('status_id')!= $existing_status->id)) { + \Log::debug('Old and new models are different - change from '.$asset->statuslabel->id.' to model '.$request->input('status_id')); + $this->update_array['status_id'] = $request->input('status_id'); + $updated_status = \App\Models\Statuslabel::find($request->input('status_id')); + } + + + // Anything that happens past this WILL NOT BE logged in the edit log $changed = []; foreach ($this->update_array as $key => $value) { @@ -222,17 +254,10 @@ class BulkAssetsController extends Controller } } + \Log::debug('What changed?'); \Log::debug(print_r($changed, true)); - $existing_assetmodel = $asset->model; - $updated_model = $request->input('model_id'); - // Use the rules of the new model fieldsets if the model changed - if (($request->filled('model_id')) && ($request->filled('model_id')!=$existing_assetmodel->id)) { - \Log::debug('Old and new models are different - change from '.$asset->model->id.' to model '.$request->input('model_id')); - $this->update_array['model_id'] = $request->input('model_id'); - $updated_model = \App\Models\AssetModel::find($request->input('model_id')); - } /** Start all the custom fields shenanigans */ From f9d5c451bca303675e205afad763b3e4e3ea5f91 Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 21 Nov 2023 10:28:33 +0000 Subject: [PATCH 03/15] Committing this for now - not done Signed-off-by: snipe --- .../Assets/BulkAssetsController.php | 30 +++++-------------- 1 file changed, 7 insertions(+), 23 deletions(-) diff --git a/app/Http/Controllers/Assets/BulkAssetsController.php b/app/Http/Controllers/Assets/BulkAssetsController.php index 5111b623cf..b02b2e265a 100644 --- a/app/Http/Controllers/Assets/BulkAssetsController.php +++ b/app/Http/Controllers/Assets/BulkAssetsController.php @@ -7,6 +7,8 @@ use App\Helpers\Helper; use App\Http\Controllers\CheckInOutRequest; use App\Http\Controllers\Controller; use App\Models\Asset; +use App\Models\AssetModel; +use App\Models\Statuslabel; use App\Models\Setting; use App\View\Label; use Illuminate\Http\Request; @@ -174,6 +176,8 @@ class BulkAssetsController extends Controller ->conditionallyAddItem('expected_checkin') ->conditionallyAddItem('order_number') ->conditionallyAddItem('requestable') + ->conditionallyAddItem('model_id') + ->conditionallyAddItem('status_id') ->conditionallyAddItem('supplier_id') ->conditionallyAddItem('warranty_months') ->conditionallyAddItem('next_audit_date'); @@ -224,25 +228,6 @@ class BulkAssetsController extends Controller } - $existing_assetmodel = $asset->model; - - - // Use the new model_id and add it to the existing $this - if (($request->filled('model_id')) && ($request->input('model_id')!= $existing_assetmodel->id)) { - \Log::debug('Old and new models are different - change from '.$asset->model->id.' to model '.$request->input('model_id')); - $this->update_array['model_id'] = $request->input('model_id'); - $updated_model = \App\Models\AssetModel::find($request->input('model_id')); - } - - $existing_status = $asset->statuslabel; - - // Use the new status_id and add it to the existing $this - if (($request->filled('status_id')) && ($request->input('status_id')!= $existing_status->id)) { - \Log::debug('Old and new models are different - change from '.$asset->statuslabel->id.' to model '.$request->input('status_id')); - $this->update_array['status_id'] = $request->input('status_id'); - $updated_status = \App\Models\Statuslabel::find($request->input('status_id')); - } - // Anything that happens past this WILL NOT BE logged in the edit log $changed = []; @@ -256,15 +241,14 @@ class BulkAssetsController extends Controller \Log::debug('What changed?'); \Log::debug(print_r($changed, true)); - - - + /** Start all the custom fields shenanigans */ if ($custom_fields_present) { // Make sure this model is valid - $assetCustomFields = ($updated_model) ? $updated_model->fieldset : null; + // ALISON - FIX THIS BEFORE PUSHING + $assetCustomFields = ($request->input('model_id')) ? $request->input('model_id')->fieldset : null; if ($assetCustomFields && $assetCustomFields->fields) { From 4723cfd4ba7bbfb2c2d29207b94d49a7c2cd854f Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 28 Nov 2023 13:32:47 +0000 Subject: [PATCH 04/15] More refactoring Signed-off-by: snipe --- .../Assets/BulkAssetsController.php | 166 ++++++++++++------ app/Models/Asset.php | 29 ++- resources/views/hardware/bulk.blade.php | 10 +- 3 files changed, 143 insertions(+), 62 deletions(-) diff --git a/app/Http/Controllers/Assets/BulkAssetsController.php b/app/Http/Controllers/Assets/BulkAssetsController.php index b02b2e265a..6eca157b71 100644 --- a/app/Http/Controllers/Assets/BulkAssetsController.php +++ b/app/Http/Controllers/Assets/BulkAssetsController.php @@ -120,8 +120,8 @@ class BulkAssetsController extends Controller $custom_field_columns = CustomField::all()->pluck('db_column')->toArray(); -// \Log::debug('ALL Custom fields columns - these may or may not apply: '); -// \Log::debug(print_r($custom_field_columns, true)); + \Log::debug('ALL Custom fields columns - these may or may not apply: '); + \Log::debug(print_r($custom_field_columns, true)); if (! $request->filled('ids') || count($request->input('ids')) == 0) { return redirect($bulk_back_url)->with('error', trans('admin/hardware/message.update.no_assets_selected')); @@ -139,14 +139,14 @@ class BulkAssetsController extends Controller } - // We should tighten checks here - burpsuite could punch through this I'd pull the custom fields for new and old here - if ($request->anyFilled($custom_field_columns)) { - $custom_fields_present = true; - } else { - $custom_fields_present = false; - } + /** + * If ANY of these are filled, prepare to update the values on the assets. + * + * Additional checks will be needed for some of them to make sure the values + * make sense (for example, changing the status ID to something incompatible with + * its checkout status. + */ - // If ANY of these are filled, prepare to update the values on the assets if (($request->filled('purchase_date')) || ($request->filled('expected_checkin')) || ($request->filled('purchase_cost')) @@ -170,14 +170,17 @@ class BulkAssetsController extends Controller $this->update_array = []; - // Leave out model_id and sttaus here because we do math on that later. We have to d some extra - // validation and checks on those two + /** + * Leave out model_id and status here because we do math on that later. We have to do some extra + * validation and checks on those two. + * + * It's tempting to make these match the request check above, but some of these values require + * extra work to make sure the data makes sense. + */ $this->conditionallyAddItem('purchase_date') ->conditionallyAddItem('expected_checkin') ->conditionallyAddItem('order_number') ->conditionallyAddItem('requestable') - ->conditionallyAddItem('model_id') - ->conditionallyAddItem('status_id') ->conditionallyAddItem('supplier_id') ->conditionallyAddItem('warranty_months') ->conditionallyAddItem('next_audit_date'); @@ -211,6 +214,53 @@ class BulkAssetsController extends Controller } } + /** + * We're trying to change the model ID - we need to do some extra checks here to make sure + * the custom field values work for the custom fieldset rules around this asset. Uniqueness + * and requiredness across the fieldset is particularly important, since those are + * fieldset-specific attributes. + */ + if ($request->filled('model_id')) { + \Log::debug('Change the model ID!'); + $asset->model_id = Model::find($request->input('model_id'))->id; + \Log::debug('New model ID is:'.$asset->model_id); + } + + /** + * We're trying to change the status ID - we need to do some extra checks here to + * make sure the status label type is one that makes sense for the state of the asset, + * for example, we shouldn't be able to make an asset archived if it's currently assigned + * to someone/something. + */ + if ($request->filled('status_id')) { + \Log::debug('Change the status ID!'); + $updated_status = Statuslabel::find($request->input('status_id')); + \Log::debug('New status ID is:'.$updated_status->id); + \Log::debug('Status label type is: '.$updated_status->getStatuslabelType()); + + // We cannot assign a non-deployable status type if the asset is already assigned. + // This could probably be added to a form request. + // If the asset isn't assigned, we don't care what the status is. + // Otherwise we need to make sure the status type is still a deployable one. + if ( + ($asset->assigned_to == '') + || ($updated_status->deployable == '1') && ($asset->assetstatus->deployable == '1') + ) { + $asset->status_id = $updated_status->id; + } + + } + + /** + * We're changing the location ID - figure out which location we should apply + * this change to: + * + * 0 - RTD location only + * 1 - location ID and RTD location ID + * 2 - location ID only + * + * Note: this is kinda dumb and we should just use human-readable values IMHO. - snipe + */ if ($request->filled('rtd_location_id')) { if (($request->filled('update_real_loc')) && (($request->input('update_real_loc')) == '0')) { @@ -229,70 +279,74 @@ class BulkAssetsController extends Controller } - // Anything that happens past this WILL NOT BE logged in the edit log + /** + * ------------------------------------------------------------------------------ + * ANYTHING that happens past this foreach + * WILL NOT BE logged in the edit log_meta data + * ------------------------------------------------------------------------------ + */ $changed = []; foreach ($this->update_array as $key => $value) { + if ($this->update_array[$key] != $asset->{$key}) { $changed[$key]['old'] = $asset->{$key}; $changed[$key]['new'] = $this->update_array[$key]; } + } \Log::debug('What changed?'); \Log::debug(print_r($changed, true)); - /** Start all the custom fields shenanigans */ - if ($custom_fields_present) { + /** + * Start all the custom fields shenanigans + */ - // Make sure this model is valid - // ALISON - FIX THIS BEFORE PUSHING - $assetCustomFields = ($request->input('model_id')) ? $request->input('model_id')->fieldset : null; + // Does the model have a fieldset? + if ($asset->model->fieldset) { + foreach ($asset->model->fieldset->fields as $field) { - if ($assetCustomFields && $assetCustomFields->fields) { - - foreach ($assetCustomFields->fields as $field) { - - if ((array_key_exists($field->db_column, $this->update_array)) && ($field->field_encrypted=='1')) { - $decrypted_old = Helper::gracefulDecrypt($field, $asset->{$field->db_column}); - - /* - * Check if the decrypted existing value is different from one we just submitted - * and if not, pull it out of the object since it shouldn't really be updating at all. - * If we don't do this, it will try to re-encrypt it, and the same value encrypted two - * different times will have different values, so it will *look* like it was updated - * but it wasn't. - */ - if ($decrypted_old != $this->update_array[$field->db_column]) { - $asset->{$field->db_column} = \Crypt::encrypt($this->update_array[$field->db_column]); - } else { - /* - * Remove the encrypted custom field from the update_array, since nothing changed - */ - unset($this->update_array[$field->db_column]); - unset($asset->{$field->db_column}); - } + if ((array_key_exists($field->db_column, $this->update_array)) && ($field->field_encrypted == '1')) { + $decrypted_old = Helper::gracefulDecrypt($field, $asset->{$field->db_column}); + /* + * Check if the decrypted existing value is different from one we just submitted + * and if not, pull it out of the object since it shouldn't really be updating at all. + * If we don't do this, it will try to re-encrypt it, and the same value encrypted two + * different times will have different values, so it will *look* like it was updated + * but it wasn't. + */ + if ($decrypted_old != $this->update_array[$field->db_column]) { + $asset->{$field->db_column} = \Crypt::encrypt($this->update_array[$field->db_column]); + } else { /* - * These custom fields aren't encrypted, just carry on as usual + * Remove the encrypted custom field from the update_array, since nothing changed */ + unset($this->update_array[$field->db_column]); + unset($asset->{$field->db_column}); + } + + /* + * These custom fields aren't encrypted, just carry on as usual + */ + } else { + + if ((array_key_exists($field->db_column, $this->update_array)) && ($asset->{$field->db_column} != $this->update_array[$field->db_column])) { + + // Check if this is an array, and if so, flatten it + if (is_array($this->update_array[$field->db_column])) { + $asset->{$field->db_column} = implode(', ', $this->update_array[$field->db_column]); } else { - - if ((array_key_exists($field->db_column, $this->update_array)) && ($asset->{$field->db_column} != $this->update_array[$field->db_column])) { - - // Check if this is an array, and if so, flatten it - if (is_array($this->update_array[$field->db_column])) { - $asset->{$field->db_column} = implode(', ', $this->update_array[$field->db_column]); - } else { - $asset->{$field->db_column} = $this->update_array[$field->db_column]; - } - } + $asset->{$field->db_column} = $this->update_array[$field->db_column]; } + } + } + + } // endforeach + } - } // endforeach - } // end custom field check - } // end custom fields handler diff --git a/app/Models/Asset.php b/app/Models/Asset.php index 4e5a25974e..7ef526d891 100644 --- a/app/Models/Asset.php +++ b/app/Models/Asset.php @@ -266,7 +266,7 @@ class Asset extends Depreciable /** * Determines if an asset is available for checkout. - * This checks to see if the it's checked out to an invalid (deleted) user + * This checks to see if it's checked out to an invalid (deleted) user * OR if the assigned_to and deleted_at fields on the asset are empty AND * that the status is deployable * @@ -292,6 +292,31 @@ class Asset extends Depreciable } + /** + * Determines if an asset is available for checkout. + * This checks to see if it's checked out to an invalid (deleted) user + * OR if the assigned_to and deleted_at fields on the asset are empty AND + * that the status is deployable + * + * @author [A. Gianotto] [] + * @since [v3.0] + * @return bool + */ + public function isDeployableMeta() + { + + // The asset status is not archived and is deployable + if (($this->assetstatus) && ($this->assetstatus->archived == '0') && (! $this->deleted_at) + && ($this->assetstatus->deployable == '1')) + { + return true; + + } + + return false; + } + + /** * Checks the asset out to the target * @@ -753,7 +778,7 @@ class Asset extends Depreciable } /** - * Establishes the asset -> status relationship + * Establishes the asset -> license seats relationship * * @author [A. Gianotto] [] * @since [v4.0] diff --git a/resources/views/hardware/bulk.blade.php b/resources/views/hardware/bulk.blade.php index 78db17afc1..0b0c6876e4 100755 --- a/resources/views/hardware/bulk.blade.php +++ b/resources/views/hardware/bulk.blade.php @@ -21,15 +21,16 @@ -
- {{ trans_choice('admin/hardware/form.bulk_update_warn', count($assets), ['asset_count' => count($assets)]) }} -
- {{ csrf_field() }}
+ +
+ {{ trans_choice('admin/hardware/form.bulk_update_warn', count($assets), ['asset_count' => count($assets)]) }} +
+
@@ -75,6 +76,7 @@
{{ Form::select('status_id', $statuslabel_list , old('status_id'), array('class'=>'select2', 'style'=>'width:100%', 'aria-label'=>'status_id')) }} +

If assets are already assigned, they cannot be changed to a non-deployable status type and this value change will be skipped.

{!! $errors->first('status_id', '') !!}
From 9d786d9386df584d4aab96573cad895b2d4ac069 Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 28 Nov 2023 13:43:33 +0000 Subject: [PATCH 05/15] Removed unused method Signed-off-by: snipe --- app/Models/Asset.php | 25 ------------------------- 1 file changed, 25 deletions(-) diff --git a/app/Models/Asset.php b/app/Models/Asset.php index 7ef526d891..f23ff52f64 100644 --- a/app/Models/Asset.php +++ b/app/Models/Asset.php @@ -292,31 +292,6 @@ class Asset extends Depreciable } - /** - * Determines if an asset is available for checkout. - * This checks to see if it's checked out to an invalid (deleted) user - * OR if the assigned_to and deleted_at fields on the asset are empty AND - * that the status is deployable - * - * @author [A. Gianotto] [] - * @since [v3.0] - * @return bool - */ - public function isDeployableMeta() - { - - // The asset status is not archived and is deployable - if (($this->assetstatus) && ($this->assetstatus->archived == '0') && (! $this->deleted_at) - && ($this->assetstatus->deployable == '1')) - { - return true; - - } - - return false; - } - - /** * Checks the asset out to the target * From fa1176ce141dfe581396c69dcbb104ebd07cccbc Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 28 Nov 2023 15:14:12 +0000 Subject: [PATCH 06/15] Added translation Signed-off-by: snipe --- resources/lang/en/general.php | 1 + 1 file changed, 1 insertion(+) diff --git a/resources/lang/en/general.php b/resources/lang/en/general.php index a568e00436..1f2f4322cc 100644 --- a/resources/lang/en/general.php +++ b/resources/lang/en/general.php @@ -491,5 +491,6 @@ return [ 'upload_error' => 'Error uploading file. Please check that there are no empty rows and that no column names are duplicated.', 'copy_to_clipboard' => 'Copy to Clipboard', 'copied' => 'Copied!', + 'status_compatibility' => 'If assets are already assigned, they cannot be changed to a non-deployable status type and this value change will be skipped.', ]; From c91713e20a5aac52520d94f0564da2e6dfc8419e Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 28 Nov 2023 15:14:55 +0000 Subject: [PATCH 07/15] Removed debugging Signed-off-by: snipe --- .../Assets/BulkAssetsController.php | 29 ++----------------- 1 file changed, 2 insertions(+), 27 deletions(-) diff --git a/app/Http/Controllers/Assets/BulkAssetsController.php b/app/Http/Controllers/Assets/BulkAssetsController.php index 6eca157b71..1151337ba7 100644 --- a/app/Http/Controllers/Assets/BulkAssetsController.php +++ b/app/Http/Controllers/Assets/BulkAssetsController.php @@ -41,9 +41,7 @@ class BulkAssetsController extends Controller public function edit(Request $request) { $this->authorize('view', Asset::class); - - \Log::debug('Bulk action was triggered: '.$request->input('bulk_actions')); - + if (! $request->filled('ids')) { return redirect()->back()->with('error', trans('admin/hardware/message.update.no_assets_selected')); } @@ -77,7 +75,6 @@ class BulkAssetsController extends Controller return view('hardware/bulk-delete')->with('assets', $assets); case 'restore': - \Log::debug('We seem to be bulk restoring'); $this->authorize('update', Asset::class); $assets->each(function ($asset) { $this->authorize('delete', $asset); @@ -86,7 +83,6 @@ class BulkAssetsController extends Controller case 'edit': $this->authorize('update', Asset::class); - \Log::debug('We seem to be bulk editing'); return view('hardware/bulk') ->with('assets', $asset_ids) @@ -120,23 +116,14 @@ class BulkAssetsController extends Controller $custom_field_columns = CustomField::all()->pluck('db_column')->toArray(); - \Log::debug('ALL Custom fields columns - these may or may not apply: '); - \Log::debug(print_r($custom_field_columns, true)); if (! $request->filled('ids') || count($request->input('ids')) == 0) { return redirect($bulk_back_url)->with('error', trans('admin/hardware/message.update.no_assets_selected')); } - // $assetsIds = array_keys($request->input('ids')); - \Log::debug('Request IDs:'); - \Log::debug(print_r(array_keys($request->input('ids')), true)); $assets = Asset::whereIn('id', array_keys($request->input('ids')))->get(); - \Log::debug('Affected asset list: '); - foreach ($assets as $log_asset) { - \Log::debug(' - Asset affected: '.$log_asset->asset_tag); - } /** @@ -221,9 +208,7 @@ class BulkAssetsController extends Controller * fieldset-specific attributes. */ if ($request->filled('model_id')) { - \Log::debug('Change the model ID!'); - $asset->model_id = Model::find($request->input('model_id'))->id; - \Log::debug('New model ID is:'.$asset->model_id); + $asset->model_id = AssetModel::find($request->input('model_id'))->id; } /** @@ -233,10 +218,7 @@ class BulkAssetsController extends Controller * to someone/something. */ if ($request->filled('status_id')) { - \Log::debug('Change the status ID!'); $updated_status = Statuslabel::find($request->input('status_id')); - \Log::debug('New status ID is:'.$updated_status->id); - \Log::debug('Status label type is: '.$updated_status->getStatuslabelType()); // We cannot assign a non-deployable status type if the asset is already assigned. // This could probably be added to a form request. @@ -296,10 +278,6 @@ class BulkAssetsController extends Controller } - \Log::debug('What changed?'); - \Log::debug(print_r($changed, true)); - - /** * Start all the custom fields shenanigans */ @@ -348,9 +326,6 @@ class BulkAssetsController extends Controller } - - - \Log::debug(print_r($this->update_array, true)); // Check if it passes validation, and then try to save if (!$asset->update($this->update_array)) { From 187d3abeb4d5619fe8585d57decf238a1947fe86 Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 28 Nov 2023 15:15:07 +0000 Subject: [PATCH 08/15] Use translation string Signed-off-by: snipe --- resources/views/hardware/bulk.blade.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/resources/views/hardware/bulk.blade.php b/resources/views/hardware/bulk.blade.php index 0b0c6876e4..fa4680e2e8 100755 --- a/resources/views/hardware/bulk.blade.php +++ b/resources/views/hardware/bulk.blade.php @@ -29,6 +29,10 @@
{{ trans_choice('admin/hardware/form.bulk_update_warn', count($assets), ['asset_count' => count($assets)]) }} + + @if (count($models) > 0) + {{ trans_choice('admin/hardware/form.bulk_update_with_custom_field', count($models), ['asset_model_count' => count($models)]) }} + @endif
@@ -76,7 +80,7 @@
{{ Form::select('status_id', $statuslabel_list , old('status_id'), array('class'=>'select2', 'style'=>'width:100%', 'aria-label'=>'status_id')) }} -

If assets are already assigned, they cannot be changed to a non-deployable status type and this value change will be skipped.

+

{{ trans('general.status_compatibility') }}

{!! $errors->first('status_id', '') !!}
@@ -190,8 +194,8 @@
+ @include("models/custom_fields_form_bulk_edit",["models" => $models]) - @foreach ($assets as $key => $value) @endforeach From 6ae03a204bc407dac2033692094124cfa8882c39 Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 28 Nov 2023 15:21:14 +0000 Subject: [PATCH 09/15] Added the model info back in Signed-off-by: snipe --- .../Controllers/Assets/BulkAssetsController.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/Assets/BulkAssetsController.php b/app/Http/Controllers/Assets/BulkAssetsController.php index 1151337ba7..60f7b874ae 100644 --- a/app/Http/Controllers/Assets/BulkAssetsController.php +++ b/app/Http/Controllers/Assets/BulkAssetsController.php @@ -41,7 +41,7 @@ class BulkAssetsController extends Controller public function edit(Request $request) { $this->authorize('view', Asset::class); - + if (! $request->filled('ids')) { return redirect()->back()->with('error', trans('admin/hardware/message.update.no_assets_selected')); } @@ -53,6 +53,17 @@ class BulkAssetsController extends Controller $asset_ids = $request->input('ids'); $assets = Asset::with('assignedTo', 'location', 'model')->find($asset_ids); + //custom fields logic + $asset_custom_field = Asset::with(['model.fieldset.fields', 'model'])->whereIn('id', $asset_ids)->whereHas('model', function ($query) { + return $query->where('fieldset_id', '!=', null); + })->get(); + + $models = $asset_custom_field->unique('model_id'); + $modelNames = []; + foreach($models as $model) { + $modelNames[] = $model->model->name; + } + if ($request->filled('bulk_actions')) { @@ -86,6 +97,8 @@ class BulkAssetsController extends Controller return view('hardware/bulk') ->with('assets', $asset_ids) + ->with('models', $models->pluck(['model'])) + ->with('modelNames', $modelNames) ->with('statuslabel_list', Helper::statusLabelList()); } } From 2c6b957fbe565fa8b116571b5f792227a6db3b4a Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 28 Nov 2023 15:28:37 +0000 Subject: [PATCH 10/15] Removed unused query Signed-off-by: snipe --- app/Http/Controllers/Assets/BulkAssetsController.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/app/Http/Controllers/Assets/BulkAssetsController.php b/app/Http/Controllers/Assets/BulkAssetsController.php index 60f7b874ae..ded29bc735 100644 --- a/app/Http/Controllers/Assets/BulkAssetsController.php +++ b/app/Http/Controllers/Assets/BulkAssetsController.php @@ -53,12 +53,7 @@ class BulkAssetsController extends Controller $asset_ids = $request->input('ids'); $assets = Asset::with('assignedTo', 'location', 'model')->find($asset_ids); - //custom fields logic - $asset_custom_field = Asset::with(['model.fieldset.fields', 'model'])->whereIn('id', $asset_ids)->whereHas('model', function ($query) { - return $query->where('fieldset_id', '!=', null); - })->get(); - - $models = $asset_custom_field->unique('model_id'); + $models = $assets->unique('model_id'); $modelNames = []; foreach($models as $model) { $modelNames[] = $model->model->name; From 3b9f06962793529b1ffde1beeb33bd66d51cbe89 Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 28 Nov 2023 15:54:42 +0000 Subject: [PATCH 11/15] Switched back to old version Signed-off-by: snipe --- app/Http/Controllers/Assets/BulkAssetsController.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/Assets/BulkAssetsController.php b/app/Http/Controllers/Assets/BulkAssetsController.php index ded29bc735..89ed27e8be 100644 --- a/app/Http/Controllers/Assets/BulkAssetsController.php +++ b/app/Http/Controllers/Assets/BulkAssetsController.php @@ -42,6 +42,9 @@ class BulkAssetsController extends Controller { $this->authorize('view', Asset::class); + /** + * No asset IDs were passed + */ if (! $request->filled('ids')) { return redirect()->back()->with('error', trans('admin/hardware/message.update.no_assets_selected')); } @@ -50,6 +53,7 @@ class BulkAssetsController extends Controller $bulk_back_url = request()->headers->get('referer'); session(['bulk_back_url' => $bulk_back_url]); + $asset_ids = $request->input('ids'); $assets = Asset::with('assignedTo', 'location', 'model')->find($asset_ids); @@ -92,9 +96,9 @@ class BulkAssetsController extends Controller return view('hardware/bulk') ->with('assets', $asset_ids) + ->with('statuslabel_list', Helper::statusLabelList()) ->with('models', $models->pluck(['model'])) - ->with('modelNames', $modelNames) - ->with('statuslabel_list', Helper::statusLabelList()); + ->with('modelNames', $modelNames); } } From 2d4a14d4bbbeb28a2ef3280ed9a54d2dfe69844a Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 28 Nov 2023 15:55:41 +0000 Subject: [PATCH 12/15] Fixed from rebase :( Signed-off-by: snipe --- app/Http/Controllers/Assets/BulkAssetsController.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/Http/Controllers/Assets/BulkAssetsController.php b/app/Http/Controllers/Assets/BulkAssetsController.php index 89ed27e8be..dd8b26a193 100644 --- a/app/Http/Controllers/Assets/BulkAssetsController.php +++ b/app/Http/Controllers/Assets/BulkAssetsController.php @@ -86,6 +86,7 @@ class BulkAssetsController extends Controller case 'restore': $this->authorize('update', Asset::class); + $assets = Asset::withTrashed()->find($asset_ids); $assets->each(function ($asset) { $this->authorize('delete', $asset); }); From 2247be77d83b316a1d954898c2a642a30b369232 Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 28 Nov 2023 15:56:20 +0000 Subject: [PATCH 13/15] Fixed translation Signed-off-by: snipe --- resources/views/hardware/bulk-restore.blade.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/views/hardware/bulk-restore.blade.php b/resources/views/hardware/bulk-restore.blade.php index caabe9fab8..e4bfb6e4dd 100644 --- a/resources/views/hardware/bulk-restore.blade.php +++ b/resources/views/hardware/bulk-restore.blade.php @@ -30,7 +30,7 @@ {{ trans('admin/hardware/table.id') }} - {{ trans('admin/hardware/table.name') }} + {{ trans('admin/hardware/form.name') }} {{ trans('admin/hardware/table.location')}} From 601ebcc1e6f83c92ec525eec58099ed441b0ebff Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 28 Nov 2023 16:00:42 +0000 Subject: [PATCH 14/15] Removed table header contraint Signed-off-by: snipe --- resources/views/hardware/view.blade.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/resources/views/hardware/view.blade.php b/resources/views/hardware/view.blade.php index 5a043ffc40..badb4b1e6b 100755 --- a/resources/views/hardware/view.blade.php +++ b/resources/views/hardware/view.blade.php @@ -1206,15 +1206,15 @@ {{ trans('admin/hardware/table.icon') }} - {{ trans('general.date') }} - {{ trans('general.admin') }} - {{ trans('general.action') }} - {{ trans('general.item') }} - {{ trans('general.target') }} - {{ trans('general.notes') }} - {{ trans('general.signature') }} - {{ trans('general.download') }} - {{ trans('admin/hardware/table.changed')}} + {{ trans('general.date') }} + {{ trans('general.admin') }} + {{ trans('general.action') }} + {{ trans('general.item') }} + {{ trans('general.target') }} + {{ trans('general.notes') }} + {{ trans('general.signature') }} + {{ trans('general.download') }} + {{ trans('admin/hardware/table.changed')}} From 899890b22422993320bd415c23c6e89a3cf56ff1 Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 28 Nov 2023 16:25:35 +0000 Subject: [PATCH 15/15] Use update array nomenclature Signed-off-by: snipe --- app/Http/Controllers/Assets/BulkAssetsController.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/Assets/BulkAssetsController.php b/app/Http/Controllers/Assets/BulkAssetsController.php index dd8b26a193..d0c66d4b11 100644 --- a/app/Http/Controllers/Assets/BulkAssetsController.php +++ b/app/Http/Controllers/Assets/BulkAssetsController.php @@ -221,7 +221,7 @@ class BulkAssetsController extends Controller * fieldset-specific attributes. */ if ($request->filled('model_id')) { - $asset->model_id = AssetModel::find($request->input('model_id'))->id; + $this->update_array['model_id'] = AssetModel::find($request->input('model_id'))->id; } /** @@ -241,7 +241,7 @@ class BulkAssetsController extends Controller ($asset->assigned_to == '') || ($updated_status->deployable == '1') && ($asset->assetstatus->deployable == '1') ) { - $asset->status_id = $updated_status->id; + $this->update_array['status_id'] = $updated_status->id; } }