From 11b47b308b5df6b4cc97e507c10b9330381fae6d Mon Sep 17 00:00:00 2001 From: spencerrlongg Date: Mon, 2 Jun 2025 18:39:08 -0500 Subject: [PATCH 1/4] front end done, sloppy --- .../custom_fields_form_bulk_edit.blade.php | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/resources/views/models/custom_fields_form_bulk_edit.blade.php b/resources/views/models/custom_fields_form_bulk_edit.blade.php index cea495ccd2..12bf14b136 100644 --- a/resources/views/models/custom_fields_form_bulk_edit.blade.php +++ b/resources/views/models/custom_fields_form_bulk_edit.blade.php @@ -30,6 +30,12 @@ :selected="old($field->db_column_name(),(isset($item) ? Helper::gracefulDecrypt($field, $item->{$field->db_column_name()}) : $field->defaultValue($model->id)))" class="format form-control" /> +
+ +
@elseif ($field->element=='textarea') @if($field->is_unique) @@ -37,7 +43,7 @@ @endif @if(!$field->is_unique) - @endif + @endif @elseif ($field->element=='checkbox') @foreach ($field->formatFieldValuesAsArray() as $key => $value) @@ -45,7 +51,6 @@ {$field->db_column_name()}))) ? ' checked="checked"' : '') : (old($field->db_column_name()) != '' ? ' checked="checked"' : (in_array($key, array_map('trim', explode(',', $field->defaultValue($model->id)))) ? ' checked="checked"' : '')) }}> {{ $value }} - @endforeach @elseif ($field->element=='radio') @foreach ($field->formatFieldValuesAsArray() as $value) @@ -115,8 +120,14 @@ @endif - +
+ +
- @endforeach + + @endforeach @endif @endforeach From 03725c8e0cd92e2f09ff56d3d6e9dfa6352a1c80 Mon Sep 17 00:00:00 2001 From: spencerrlongg Date: Tue, 3 Jun 2025 17:21:07 -0500 Subject: [PATCH 2/4] custom field null and filtering --- .../Assets/BulkAssetsController.php | 21 +++++++++++++++++++ .../custom_fields_form_bulk_edit.blade.php | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/Assets/BulkAssetsController.php b/app/Http/Controllers/Assets/BulkAssetsController.php index 6fc25224f9..443cd2b8fa 100644 --- a/app/Http/Controllers/Assets/BulkAssetsController.php +++ b/app/Http/Controllers/Assets/BulkAssetsController.php @@ -201,6 +201,7 @@ class BulkAssetsController extends Controller */ public function update(Request $request) : RedirectResponse { + //dd($request->all()); $this->authorize('update', Asset::class); $has_errors = 0; $error_array = array(); @@ -213,6 +214,20 @@ class BulkAssetsController extends Controller } $custom_field_columns = CustomField::all()->pluck('db_column')->toArray(); + // find input attirubtes that start with 'null_' + $temp_custom_fields_to_null = array_filter($request->all(), function ($key) { + // filter out all keys that start with 'null_' + return (strpos($key, 'null_') === 0); + }, ARRAY_FILTER_USE_KEY);; + // remove 'null_' from the keys + $custom_fields_to_null = []; + foreach ($temp_custom_fields_to_null as $key => $value) { + $custom_fields_to_null[str_replace('null_', '', $key)] = $value; + } + + + + if (! $request->filled('ids') || count($request->input('ids')) == 0) { @@ -252,6 +267,7 @@ class BulkAssetsController extends Controller || ($request->filled('null_next_audit_date')) || ($request->filled('null_asset_eol_date')) || ($request->anyFilled($custom_field_columns)) + || ($request->anyFilled(array_keys($custom_fields_to_null))) ) { // Let's loop through those assets and build an update array @@ -278,6 +294,10 @@ class BulkAssetsController extends Controller foreach ($custom_field_columns as $key => $custom_field_column) { $this->conditionallyAddItem($custom_field_column); } + foreach ($custom_fields_to_null as $key => $custom_field_to_null) { + + $this->conditionallyAddItem($key); + } if (!($asset->eol_explicit)) { if ($request->filled('model_id')) { @@ -423,6 +443,7 @@ class BulkAssetsController extends Controller } /** + * * Start all the custom fields shenanigans */ diff --git a/resources/views/models/custom_fields_form_bulk_edit.blade.php b/resources/views/models/custom_fields_form_bulk_edit.blade.php index 12bf14b136..09f06e575b 100644 --- a/resources/views/models/custom_fields_form_bulk_edit.blade.php +++ b/resources/views/models/custom_fields_form_bulk_edit.blade.php @@ -122,7 +122,7 @@
From 0fb1639915f8e0e190d6b0f3ff9c7941aa2756ee Mon Sep 17 00:00:00 2001 From: spencerrlongg Date: Tue, 3 Jun 2025 18:06:41 -0500 Subject: [PATCH 3/4] this works! --- .../Assets/BulkAssetsController.php | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/app/Http/Controllers/Assets/BulkAssetsController.php b/app/Http/Controllers/Assets/BulkAssetsController.php index 443cd2b8fa..f70d40e5d8 100644 --- a/app/Http/Controllers/Assets/BulkAssetsController.php +++ b/app/Http/Controllers/Assets/BulkAssetsController.php @@ -214,15 +214,16 @@ class BulkAssetsController extends Controller } $custom_field_columns = CustomField::all()->pluck('db_column')->toArray(); - // find input attirubtes that start with 'null_' - $temp_custom_fields_to_null = array_filter($request->all(), function ($key) { + + // find custom field input attributes that start with 'null_' + $null_custom_fields_inputs = array_filter($request->all(), function ($key) { // filter out all keys that start with 'null_' return (strpos($key, 'null_') === 0); }, ARRAY_FILTER_USE_KEY);; - // remove 'null_' from the keys + // remove 'null' from the keys $custom_fields_to_null = []; - foreach ($temp_custom_fields_to_null as $key => $value) { - $custom_fields_to_null[str_replace('null_', '', $key)] = $value; + foreach ($null_custom_fields_inputs as $key => $value) { + $custom_fields_to_null[str_replace('null', '', $key)] = $value; } @@ -267,7 +268,7 @@ class BulkAssetsController extends Controller || ($request->filled('null_next_audit_date')) || ($request->filled('null_asset_eol_date')) || ($request->anyFilled($custom_field_columns)) - || ($request->anyFilled(array_keys($custom_fields_to_null))) + || ($request->anyFilled(array_keys($null_custom_fields_inputs))) ) { // Let's loop through those assets and build an update array @@ -295,7 +296,7 @@ class BulkAssetsController extends Controller $this->conditionallyAddItem($custom_field_column); } foreach ($custom_fields_to_null as $key => $custom_field_to_null) { - + //dd($key); $this->conditionallyAddItem($key); } @@ -451,6 +452,15 @@ class BulkAssetsController extends Controller if ($asset->model->fieldset) { foreach ($asset->model->fieldset->fields as $field) { + // null custom fields + if ($custom_fields_to_null) { + foreach ($custom_fields_to_null as $key => $custom_field_to_null) { + if ($field->db_column == $key) { + $this->update_array[$field->db_column] = null; + } + } + } + if ((array_key_exists($field->db_column, $this->update_array)) && ($field->field_encrypted == '1')) { if (Gate::allows('admin')) { $decrypted_old = Helper::gracefulDecrypt($field, $asset->{$field->db_column}); From 12d5e4f7d27c524a07843d1acdbc44f4fa287665 Mon Sep 17 00:00:00 2001 From: spencerrlongg Date: Tue, 3 Jun 2025 19:07:02 -0500 Subject: [PATCH 4/4] cleanup and test --- .../Assets/BulkAssetsController.php | 2 - .../Feature/Assets/Ui/BulkEditAssetsTest.php | 41 +++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/Assets/BulkAssetsController.php b/app/Http/Controllers/Assets/BulkAssetsController.php index f70d40e5d8..88ba51bf78 100644 --- a/app/Http/Controllers/Assets/BulkAssetsController.php +++ b/app/Http/Controllers/Assets/BulkAssetsController.php @@ -201,7 +201,6 @@ class BulkAssetsController extends Controller */ public function update(Request $request) : RedirectResponse { - //dd($request->all()); $this->authorize('update', Asset::class); $has_errors = 0; $error_array = array(); @@ -296,7 +295,6 @@ class BulkAssetsController extends Controller $this->conditionallyAddItem($custom_field_column); } foreach ($custom_fields_to_null as $key => $custom_field_to_null) { - //dd($key); $this->conditionallyAddItem($key); } diff --git a/tests/Feature/Assets/Ui/BulkEditAssetsTest.php b/tests/Feature/Assets/Ui/BulkEditAssetsTest.php index 44e9052482..58f7a11598 100644 --- a/tests/Feature/Assets/Ui/BulkEditAssetsTest.php +++ b/tests/Feature/Assets/Ui/BulkEditAssetsTest.php @@ -197,6 +197,47 @@ class BulkEditAssetsTest extends TestCase }); } + public function testBulkEditAssetsNullsCustomFieldsIfSelected() + { + $this->markIncompleteIfMySQL('Custom Fields tests do not work on MySQL'); + + CustomField::factory()->ram()->create(); + CustomField::factory()->cpu()->create(); + CustomField::factory()->phone()->create(); + + // when getting the custom field directly from the factory the field has not been fully created yet + // so we have to do a query afterwards to get the actual model :shrug: + + $ram = CustomField::where('name', 'RAM')->first(); + $cpu = CustomField::where('name', 'CPU')->first(); + $phone = CustomField::where('name', 'Phone Number')->first(); + + $assets = Asset::factory()->count(10)->hasMultipleCustomFields([$ram, $cpu])->create([ + $ram->db_column => 8, + $cpu->db_column => '2.1', + ]); + + $id_array = $assets->pluck('id')->toArray(); + + $this->actingAs(User::factory()->editAssets()->create())->post(route('hardware/bulksave'), [ + 'ids' => $id_array, + $ram->db_column => 16, + $cpu->db_column => '4.1', + 'null'.$phone->db_column => '8304997586', + ])->assertStatus(302); + + $this->actingAs(User::factory()->editAssets()->create())->post(route('hardware/bulksave'), [ + 'ids' => $id_array, + null.$phone->db_column => 1, + ]); + + Asset::findMany($id_array)->each(function (Asset $asset) use ($ram, $cpu, $phone) { + $this->assertEquals(16, $asset->{$ram->db_column}); + $this->assertEquals('4.1', $asset->{$cpu->db_column}); + $this->assertEquals(null, $asset->{$phone->db_column}); + }); + } + public function testBulkEditAssetsAcceptsAndUpdatesEncryptedCustomFields() { $this->markIncompleteIfMySQL('Custom Fields tests do not work on MySQL');