From 5cbcac28b10ac388133bb38c739c0029e73932a6 Mon Sep 17 00:00:00 2001 From: Godfrey M Date: Wed, 14 Feb 2024 16:53:38 -0800 Subject: [PATCH 01/11] adds custom fields as an example in label settings --- app/Http/Controllers/LabelsController.php | 21 +++++++++++++++++++++ resources/views/settings/labels.blade.php | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/LabelsController.php b/app/Http/Controllers/LabelsController.php index bb08d2cd45..b518be878a 100755 --- a/app/Http/Controllers/LabelsController.php +++ b/app/Http/Controllers/LabelsController.php @@ -6,6 +6,7 @@ use App\Models\Asset; use App\Models\AssetModel; use App\Models\Category; use App\Models\Company; +use App\Models\CustomFieldset; use App\Models\Labels\Label; use App\Models\Location; use App\Models\Manufacturer; @@ -26,6 +27,21 @@ class LabelsController extends Controller */ public function show(string $labelName) { + $data = explode(';', Setting::getSettings()->label2_fields); + $fields = str_replace('=', ' ', $data); + + $custom_fields= []; + + foreach ($fields as $field) { + + $parts = explode('_', $field, 2); + + $key = trim($parts[0]); + $value = trim($parts[1]); + + $custom_fields["_".$value] = $key ; + } +// dd($custom_fields); $labelName = str_replace('/', '\\', $labelName); $template = Label::find($labelName); @@ -65,6 +81,11 @@ class LabelsController extends Controller $exampleAsset->model->category->id = 999999; $exampleAsset->model->category->name = trans('admin/labels/table.example_category'); + + foreach($custom_fields as $key => $value){ + $exampleAsset->{$key} = "{{$value}}" ; + } + $settings = Setting::getSettings(); if (request()->has('settings')) { $overrides = request()->get('settings'); diff --git a/resources/views/settings/labels.blade.php b/resources/views/settings/labels.blade.php index a6edd29154..cfa7ba1785 100644 --- a/resources/views/settings/labels.blade.php +++ b/resources/views/settings/labels.blade.php @@ -1,4 +1,4 @@ -@extends('layouts/default') + @extends('layouts/default') {{-- Page title --}} @section('title') From 250b0a7afb3d926ad8291124694324944a761720 Mon Sep 17 00:00:00 2001 From: Godfrey M Date: Thu, 15 Feb 2024 11:50:41 -0800 Subject: [PATCH 02/11] gives a better example with custom fields in label preview --- app/Http/Controllers/LabelsController.php | 91 +++++++++++------------ resources/views/settings/labels.blade.php | 2 +- 2 files changed, 44 insertions(+), 49 deletions(-) diff --git a/app/Http/Controllers/LabelsController.php b/app/Http/Controllers/LabelsController.php index b518be878a..7324a9f2a8 100755 --- a/app/Http/Controllers/LabelsController.php +++ b/app/Http/Controllers/LabelsController.php @@ -6,7 +6,6 @@ use App\Models\Asset; use App\Models\AssetModel; use App\Models\Category; use App\Models\Company; -use App\Models\CustomFieldset; use App\Models\Labels\Label; use App\Models\Location; use App\Models\Manufacturer; @@ -27,63 +26,59 @@ class LabelsController extends Controller */ public function show(string $labelName) { + $setting = Setting::getSettings(); + //This part allows for the custom selection label preview $data = explode(';', Setting::getSettings()->label2_fields); - $fields = str_replace('=', ' ', $data); + $data = array_map(function($element) { + $a = explode('=', $element); + return [$a[0] => $a[1]]; + }, $data); - $custom_fields= []; - - foreach ($fields as $field) { - - $parts = explode('_', $field, 2); - - $key = trim($parts[0]); - $value = trim($parts[1]); - - $custom_fields["_".$value] = $key ; - } -// dd($custom_fields); $labelName = str_replace('/', '\\', $labelName); $template = Label::find($labelName); - $exampleAsset = new Asset(); + if($setting->label2_enable && ($setting->label2_template != 'DefaultLabel')) { + $exampleAsset = new Asset(); - $exampleAsset->id = 999999; - $exampleAsset->name = 'JEN-867-5309'; - $exampleAsset->asset_tag = '100001'; - $exampleAsset->serial = 'SN9876543210'; - $exampleAsset->asset_eol_date = '2025-01-01'; - $exampleAsset->order_number = '12345'; - $exampleAsset->purchase_date = '2023-01-01'; - $exampleAsset->status_id = 1; + $exampleAsset->id = 999999; + $exampleAsset->name = 'JEN-867-5309'; + $exampleAsset->asset_tag = '100001'; + $exampleAsset->serial = 'SN9876543210'; + $exampleAsset->asset_eol_date = '2025-01-01'; + $exampleAsset->order_number = '12345'; + $exampleAsset->purchase_date = '2023-01-01'; + $exampleAsset->status_id = 1; - $exampleAsset->company = new Company([ - 'name' => trans('admin/labels/table.example_company'), - 'phone' => '1-555-555-5555', - 'email' => 'company@example.com', - ]); + $exampleAsset->company = new Company([ + 'name' => trans('admin/labels/table.example_company'), + 'phone' => '1-555-555-5555', + 'email' => 'company@example.com', + ]); - $exampleAsset->setRelation('assignedTo', new User(['first_name' => 'Luke', 'last_name' => 'Skywalker'])); - $exampleAsset->defaultLoc = new Location(['name' => trans('admin/labels/table.example_defaultloc'), 'phone' => '1-555-555-5555']); - $exampleAsset->location = new Location(['name' => trans('admin/labels/table.example_location'), 'phone' => '1-555-555-5555']); + $exampleAsset->setRelation('assignedTo', new User(['first_name' => 'Luke', 'last_name' => 'Skywalker'])); + $exampleAsset->defaultLoc = new Location(['name' => trans('admin/labels/table.example_defaultloc'), 'phone' => '1-555-555-5555']); + $exampleAsset->location = new Location(['name' => trans('admin/labels/table.example_location'), 'phone' => '1-555-555-5555']); - $exampleAsset->model = new AssetModel(); - $exampleAsset->model->id = 999999; - $exampleAsset->model->name = trans('admin/labels/table.example_model'); - $exampleAsset->model->model_number = 'MDL5678'; - $exampleAsset->model->manufacturer = new Manufacturer(); - $exampleAsset->model->manufacturer->id = 999999; - $exampleAsset->model->manufacturer->name = trans('admin/labels/table.example_manufacturer'); - $exampleAsset->model->manufacturer->support_email = 'support@test.com'; - $exampleAsset->model->manufacturer->support_phone = '1-555-555-5555'; - $exampleAsset->model->manufacturer->support_url = 'https://example.com'; - $exampleAsset->supplier = new Supplier(['name' => trans('admin/labels/table.example_company')]); - $exampleAsset->model->category = new Category(); - $exampleAsset->model->category->id = 999999; - $exampleAsset->model->category->name = trans('admin/labels/table.example_category'); + $exampleAsset->model = new AssetModel(); + $exampleAsset->model->id = 999999; + $exampleAsset->model->name = trans('admin/labels/table.example_model'); + $exampleAsset->model->model_number = 'MDL5678'; + $exampleAsset->model->manufacturer = new Manufacturer(); + $exampleAsset->model->manufacturer->id = 999999; + $exampleAsset->model->manufacturer->name = trans('admin/labels/table.example_manufacturer'); + $exampleAsset->model->manufacturer->support_email = 'support@test.com'; + $exampleAsset->model->manufacturer->support_phone = '1-555-555-5555'; + $exampleAsset->model->manufacturer->support_url = 'https://example.com'; + $exampleAsset->supplier = new Supplier(['name' => trans('admin/labels/table.example_company')]); + $exampleAsset->model->category = new Category(); + $exampleAsset->model->category->id = 999999; + $exampleAsset->model->category->name = trans('admin/labels/table.example_category'); + } - - foreach($custom_fields as $key => $value){ - $exampleAsset->{$key} = "{{$value}}" ; + foreach($data as $innerArray){ + foreach($innerArray as $key => $value) { + $exampleAsset->{$value} = "{{$key}}"; + } } $settings = Setting::getSettings(); diff --git a/resources/views/settings/labels.blade.php b/resources/views/settings/labels.blade.php index cfa7ba1785..a6edd29154 100644 --- a/resources/views/settings/labels.blade.php +++ b/resources/views/settings/labels.blade.php @@ -1,4 +1,4 @@ - @extends('layouts/default') +@extends('layouts/default') {{-- Page title --}} @section('title') From 414bc10c40ad1fb3e1f09209e75a16efc45686db Mon Sep 17 00:00:00 2001 From: Godfrey M Date: Thu, 15 Feb 2024 12:02:08 -0800 Subject: [PATCH 03/11] removes the double for loop with a better solution --- app/Http/Controllers/LabelsController.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/app/Http/Controllers/LabelsController.php b/app/Http/Controllers/LabelsController.php index 7324a9f2a8..379836758b 100755 --- a/app/Http/Controllers/LabelsController.php +++ b/app/Http/Controllers/LabelsController.php @@ -27,7 +27,7 @@ class LabelsController extends Controller public function show(string $labelName) { $setting = Setting::getSettings(); - //This part allows for the custom selection label preview + //This part allows for the custom field selection to be visible in the label preview $data = explode(';', Setting::getSettings()->label2_fields); $data = array_map(function($element) { $a = explode('=', $element); @@ -74,12 +74,15 @@ class LabelsController extends Controller $exampleAsset->model->category->id = 999999; $exampleAsset->model->category->name = trans('admin/labels/table.example_category'); } - - foreach($data as $innerArray){ - foreach($innerArray as $key => $value) { - $exampleAsset->{$value} = "{{$key}}"; - } + //turns a multidimensional array in an associative array for no double for looping + $result = array(); + foreach ($data as $entry) { + $result[key($entry)] = reset($entry); } + foreach($result as $key => $value) { + $exampleAsset->{$value} = "{{$key}}"; + } + $settings = Setting::getSettings(); if (request()->has('settings')) { From 70f26f33a58cfcb8ec88c6e627f73622fe373d85 Mon Sep 17 00:00:00 2001 From: Godfrey M Date: Thu, 15 Feb 2024 12:34:09 -0800 Subject: [PATCH 04/11] does a collect and map instead of a for loop for the array solution --- app/Http/Controllers/LabelsController.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/app/Http/Controllers/LabelsController.php b/app/Http/Controllers/LabelsController.php index 379836758b..f68e38a42b 100755 --- a/app/Http/Controllers/LabelsController.php +++ b/app/Http/Controllers/LabelsController.php @@ -74,16 +74,16 @@ class LabelsController extends Controller $exampleAsset->model->category->id = 999999; $exampleAsset->model->category->name = trans('admin/labels/table.example_category'); } - //turns a multidimensional array in an associative array for no double for looping - $result = array(); - foreach ($data as $entry) { - $result[key($entry)] = reset($entry); - } - foreach($result as $key => $value) { + + //turns a multidimensional array in an associative array + $field_selections = collect($data)->mapWithKeys(function ($item) { + return $item; + })->toArray(); + + foreach($field_selections as $key => $value) { $exampleAsset->{$value} = "{{$key}}"; } - $settings = Setting::getSettings(); if (request()->has('settings')) { $overrides = request()->get('settings'); From 529310c93a5bb27c6eb4be08330055c9bd3ed502 Mon Sep 17 00:00:00 2001 From: Godfrey M Date: Thu, 15 Feb 2024 12:41:31 -0800 Subject: [PATCH 05/11] fixed typo in comment --- app/Http/Controllers/LabelsController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Http/Controllers/LabelsController.php b/app/Http/Controllers/LabelsController.php index f68e38a42b..3a8ff4faf5 100755 --- a/app/Http/Controllers/LabelsController.php +++ b/app/Http/Controllers/LabelsController.php @@ -75,7 +75,7 @@ class LabelsController extends Controller $exampleAsset->model->category->name = trans('admin/labels/table.example_category'); } - //turns a multidimensional array in an associative array + //turns a multidimensional array into an associative array $field_selections = collect($data)->mapWithKeys(function ($item) { return $item; })->toArray(); From 83474d6e5980953d44974a323a697601af2c005b Mon Sep 17 00:00:00 2001 From: Godfrey M Date: Thu, 15 Feb 2024 12:59:47 -0800 Subject: [PATCH 06/11] moves the array manipulation up the page where it should be --- app/Http/Controllers/LabelsController.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/app/Http/Controllers/LabelsController.php b/app/Http/Controllers/LabelsController.php index 3a8ff4faf5..6363ee671a 100755 --- a/app/Http/Controllers/LabelsController.php +++ b/app/Http/Controllers/LabelsController.php @@ -27,13 +27,18 @@ class LabelsController extends Controller public function show(string $labelName) { $setting = Setting::getSettings(); - //This part allows for the custom field selection to be visible in the label preview + //grabs the field selection selected and turns it into a multidimensional array $data = explode(';', Setting::getSettings()->label2_fields); $data = array_map(function($element) { $a = explode('=', $element); return [$a[0] => $a[1]]; }, $data); + //turns a multidimensional array into an associative array + $field_selections = collect($data)->mapWithKeys(function ($item) { + return $item; + })->toArray(); + $labelName = str_replace('/', '\\', $labelName); $template = Label::find($labelName); @@ -75,10 +80,7 @@ class LabelsController extends Controller $exampleAsset->model->category->name = trans('admin/labels/table.example_category'); } - //turns a multidimensional array into an associative array - $field_selections = collect($data)->mapWithKeys(function ($item) { - return $item; - })->toArray(); + foreach($field_selections as $key => $value) { $exampleAsset->{$value} = "{{$key}}"; From dc77c01fd3de8744f59f03c9744457e8f9eed457 Mon Sep 17 00:00:00 2001 From: Godfrey M Date: Thu, 15 Feb 2024 13:00:38 -0800 Subject: [PATCH 07/11] removes spacing --- app/Http/Controllers/LabelsController.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/Http/Controllers/LabelsController.php b/app/Http/Controllers/LabelsController.php index 6363ee671a..6537da077d 100755 --- a/app/Http/Controllers/LabelsController.php +++ b/app/Http/Controllers/LabelsController.php @@ -27,6 +27,7 @@ class LabelsController extends Controller public function show(string $labelName) { $setting = Setting::getSettings(); + //grabs the field selection selected and turns it into a multidimensional array $data = explode(';', Setting::getSettings()->label2_fields); $data = array_map(function($element) { @@ -80,8 +81,6 @@ class LabelsController extends Controller $exampleAsset->model->category->name = trans('admin/labels/table.example_category'); } - - foreach($field_selections as $key => $value) { $exampleAsset->{$value} = "{{$key}}"; } From 1b80c8938ad76492601f9452b153f159f42e493c Mon Sep 17 00:00:00 2001 From: Godfrey M Date: Thu, 15 Feb 2024 13:04:00 -0800 Subject: [PATCH 08/11] removed unnecessary if statement --- app/Http/Controllers/LabelsController.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/Http/Controllers/LabelsController.php b/app/Http/Controllers/LabelsController.php index 6537da077d..80ace3084a 100755 --- a/app/Http/Controllers/LabelsController.php +++ b/app/Http/Controllers/LabelsController.php @@ -43,7 +43,6 @@ class LabelsController extends Controller $labelName = str_replace('/', '\\', $labelName); $template = Label::find($labelName); - if($setting->label2_enable && ($setting->label2_template != 'DefaultLabel')) { $exampleAsset = new Asset(); $exampleAsset->id = 999999; @@ -79,7 +78,6 @@ class LabelsController extends Controller $exampleAsset->model->category = new Category(); $exampleAsset->model->category->id = 999999; $exampleAsset->model->category->name = trans('admin/labels/table.example_category'); - } foreach($field_selections as $key => $value) { $exampleAsset->{$value} = "{{$key}}"; From 69317fb40367d8d74347bf44f05235613a787ac4 Mon Sep 17 00:00:00 2001 From: Godfrey M Date: Thu, 15 Feb 2024 13:26:47 -0800 Subject: [PATCH 09/11] Marcus is a beast and condensed everything into 4 lines --- app/Http/Controllers/LabelsController.php | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/app/Http/Controllers/LabelsController.php b/app/Http/Controllers/LabelsController.php index 80ace3084a..7ea6843d4f 100755 --- a/app/Http/Controllers/LabelsController.php +++ b/app/Http/Controllers/LabelsController.php @@ -26,20 +26,6 @@ class LabelsController extends Controller */ public function show(string $labelName) { - $setting = Setting::getSettings(); - - //grabs the field selection selected and turns it into a multidimensional array - $data = explode(';', Setting::getSettings()->label2_fields); - $data = array_map(function($element) { - $a = explode('=', $element); - return [$a[0] => $a[1]]; - }, $data); - - //turns a multidimensional array into an associative array - $field_selections = collect($data)->mapWithKeys(function ($item) { - return $item; - })->toArray(); - $labelName = str_replace('/', '\\', $labelName); $template = Label::find($labelName); @@ -79,9 +65,12 @@ class LabelsController extends Controller $exampleAsset->model->category->id = 999999; $exampleAsset->model->category->name = trans('admin/labels/table.example_category'); - foreach($field_selections as $key => $value) { - $exampleAsset->{$value} = "{{$key}}"; - } + collect(explode(';', Setting::getSettings()->label2_fields)) + ->each(function ($item) use ($exampleAsset){ + $pair = explode('=', $item); + + $exampleAsset->{$pair[1]} = "{{$pair[0]}}"; + }); $settings = Setting::getSettings(); if (request()->has('settings')) { From bee016e0be2bf033eeeca0e16ff8fc512fea1988 Mon Sep 17 00:00:00 2001 From: Godfrey M Date: Wed, 21 Feb 2024 09:31:01 -0800 Subject: [PATCH 10/11] keeps example values in the example preview, fixes indent --- app/Http/Controllers/LabelsController.php | 71 ++++++++++++----------- 1 file changed, 38 insertions(+), 33 deletions(-) diff --git a/app/Http/Controllers/LabelsController.php b/app/Http/Controllers/LabelsController.php index 7ea6843d4f..07d14dbbc7 100755 --- a/app/Http/Controllers/LabelsController.php +++ b/app/Http/Controllers/LabelsController.php @@ -6,6 +6,7 @@ use App\Models\Asset; use App\Models\AssetModel; use App\Models\Category; use App\Models\Company; +use App\Models\CustomField; use App\Models\Labels\Label; use App\Models\Location; use App\Models\Manufacturer; @@ -29,47 +30,51 @@ class LabelsController extends Controller $labelName = str_replace('/', '\\', $labelName); $template = Label::find($labelName); - $exampleAsset = new Asset(); + $exampleAsset = new Asset(); - $exampleAsset->id = 999999; - $exampleAsset->name = 'JEN-867-5309'; - $exampleAsset->asset_tag = '100001'; - $exampleAsset->serial = 'SN9876543210'; - $exampleAsset->asset_eol_date = '2025-01-01'; - $exampleAsset->order_number = '12345'; - $exampleAsset->purchase_date = '2023-01-01'; - $exampleAsset->status_id = 1; + $exampleAsset->id = 999999; + $exampleAsset->name = 'JEN-867-5309'; + $exampleAsset->asset_tag = '100001'; + $exampleAsset->serial = 'SN9876543210'; + $exampleAsset->asset_eol_date = '2025-01-01'; + $exampleAsset->order_number = '12345'; + $exampleAsset->purchase_date = '2023-01-01'; + $exampleAsset->status_id = 1; - $exampleAsset->company = new Company([ - 'name' => trans('admin/labels/table.example_company'), - 'phone' => '1-555-555-5555', - 'email' => 'company@example.com', - ]); + $exampleAsset->company = new Company([ + 'name' => trans('admin/labels/table.example_company'), + 'phone' => '1-555-555-5555', + 'email' => 'company@example.com', + ]); - $exampleAsset->setRelation('assignedTo', new User(['first_name' => 'Luke', 'last_name' => 'Skywalker'])); - $exampleAsset->defaultLoc = new Location(['name' => trans('admin/labels/table.example_defaultloc'), 'phone' => '1-555-555-5555']); - $exampleAsset->location = new Location(['name' => trans('admin/labels/table.example_location'), 'phone' => '1-555-555-5555']); + $exampleAsset->setRelation('assignedTo', new User(['first_name' => 'Luke', 'last_name' => 'Skywalker'])); + $exampleAsset->defaultLoc = new Location(['name' => trans('admin/labels/table.example_defaultloc'), 'phone' => '1-555-555-5555']); + $exampleAsset->location = new Location(['name' => trans('admin/labels/table.example_location'), 'phone' => '1-555-555-5555']); - $exampleAsset->model = new AssetModel(); - $exampleAsset->model->id = 999999; - $exampleAsset->model->name = trans('admin/labels/table.example_model'); - $exampleAsset->model->model_number = 'MDL5678'; - $exampleAsset->model->manufacturer = new Manufacturer(); - $exampleAsset->model->manufacturer->id = 999999; - $exampleAsset->model->manufacturer->name = trans('admin/labels/table.example_manufacturer'); - $exampleAsset->model->manufacturer->support_email = 'support@test.com'; - $exampleAsset->model->manufacturer->support_phone = '1-555-555-5555'; - $exampleAsset->model->manufacturer->support_url = 'https://example.com'; - $exampleAsset->supplier = new Supplier(['name' => trans('admin/labels/table.example_company')]); - $exampleAsset->model->category = new Category(); - $exampleAsset->model->category->id = 999999; - $exampleAsset->model->category->name = trans('admin/labels/table.example_category'); + $exampleAsset->model = new AssetModel(); + $exampleAsset->model->id = 999999; + $exampleAsset->model->name = trans('admin/labels/table.example_model'); + $exampleAsset->model->model_number = 'MDL5678'; + $exampleAsset->model->manufacturer = new Manufacturer(); + $exampleAsset->model->manufacturer->id = 999999; + $exampleAsset->model->manufacturer->name = trans('admin/labels/table.example_manufacturer'); + $exampleAsset->model->manufacturer->support_email = 'support@test.com'; + $exampleAsset->model->manufacturer->support_phone = '1-555-555-5555'; + $exampleAsset->model->manufacturer->support_url = 'https://example.com'; + $exampleAsset->supplier = new Supplier(['name' => trans('admin/labels/table.example_company')]); + $exampleAsset->model->category = new Category(); + $exampleAsset->model->category->id = 999999; + $exampleAsset->model->category->name = trans('admin/labels/table.example_category'); + + $customFieldColumns = CustomField::all()->pluck('db_column'); collect(explode(';', Setting::getSettings()->label2_fields)) - ->each(function ($item) use ($exampleAsset){ + ->each(function ($item) use ($customFieldColumns, $exampleAsset) { $pair = explode('=', $item); - $exampleAsset->{$pair[1]} = "{{$pair[0]}}"; + if ($customFieldColumns->contains($pair[1])) { + $exampleAsset->{$pair[1]} = "{{$pair[0]}}"; + } }); $settings = Setting::getSettings(); From 14c86d447bb60881dc85a1ce81680ae18a76b108 Mon Sep 17 00:00:00 2001 From: Godfrey M Date: Wed, 21 Feb 2024 11:38:03 -0800 Subject: [PATCH 11/11] added filter to collectifunction --- app/Http/Controllers/LabelsController.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/Http/Controllers/LabelsController.php b/app/Http/Controllers/LabelsController.php index 07d14dbbc7..4fe04dc1c5 100755 --- a/app/Http/Controllers/LabelsController.php +++ b/app/Http/Controllers/LabelsController.php @@ -69,6 +69,7 @@ class LabelsController extends Controller $customFieldColumns = CustomField::all()->pluck('db_column'); collect(explode(';', Setting::getSettings()->label2_fields)) + ->filter() ->each(function ($item) use ($customFieldColumns, $exampleAsset) { $pair = explode('=', $item);