From 81b8c111ca59ad376b981cc1e5567e623181e064 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 11 Apr 2024 15:00:14 -0700 Subject: [PATCH 1/5] Allow multiple fields to be displayed in one row --- app/View/Label.php | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/app/View/Label.php b/app/View/Label.php index f47ad6acd..f43263aef 100644 --- a/app/View/Label.php +++ b/app/View/Label.php @@ -142,7 +142,30 @@ class Label implements View // Remove Duplicates $toAdd = $field ->filter(fn($o) => !$myFields->contains('dataSource', $o['dataSource'])) - ->first(); + ->reduce(function ($carry, $item) { + // On the first iteration we simply return the item. + // If there is only one item to be processed for the row + // then this effectively skips everything below this if block. + if (is_null($carry)){ + return $item; + } + + // At this point we are dealing with a row with multiple items being displayed. + + + // The end result of this will be in this format: + // {labelOne} {valueOne} | {labelTwo} {valueTwo} | {labelThree} {valueThree} + $carry['value'] = implode(' | ', [ + implode(' ', [$carry['label'], $carry['value']]), + implode(' ', [$item['label'], $item['value']]), + ]); + + // We'll set the label to an empty string since we + // injected the label into the value field above. + $carry['label'] = ''; + + return $carry; + }); return $toAdd ? $myFields->push($toAdd) : $myFields; }, new Collection()); From 2b137d76fa7e3ffc5007ffe0c083debb4c26d7fc Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 11 Apr 2024 16:38:22 -0700 Subject: [PATCH 2/5] Trim string to avoid leading whitespace if label is empty --- app/View/Label.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/View/Label.php b/app/View/Label.php index f43263aef..b1b121bd4 100644 --- a/app/View/Label.php +++ b/app/View/Label.php @@ -155,10 +155,10 @@ class Label implements View // The end result of this will be in this format: // {labelOne} {valueOne} | {labelTwo} {valueTwo} | {labelThree} {valueThree} - $carry['value'] = implode(' | ', [ + $carry['value'] = trim(implode(' | ', [ implode(' ', [$carry['label'], $carry['value']]), implode(' ', [$item['label'], $item['value']]), - ]); + ])); // We'll set the label to an empty string since we // injected the label into the value field above. From da03cfdbe5837c3a558802e59f1648a089872b67 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 11 Apr 2024 16:39:29 -0700 Subject: [PATCH 3/5] Formatting --- app/View/Label.php | 1 - 1 file changed, 1 deletion(-) diff --git a/app/View/Label.php b/app/View/Label.php index b1b121bd4..3c49f04b6 100644 --- a/app/View/Label.php +++ b/app/View/Label.php @@ -152,7 +152,6 @@ class Label implements View // At this point we are dealing with a row with multiple items being displayed. - // The end result of this will be in this format: // {labelOne} {valueOne} | {labelTwo} {valueTwo} | {labelThree} {valueThree} $carry['value'] = trim(implode(' | ', [ From c3a71cc18285b7266a69fd90e2b293a0ec49f5fc Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 11 Apr 2024 16:44:13 -0700 Subject: [PATCH 4/5] Improve variable names and add comment --- app/View/Label.php | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/app/View/Label.php b/app/View/Label.php index 3c49f04b6..bb587d6c0 100644 --- a/app/View/Label.php +++ b/app/View/Label.php @@ -142,28 +142,29 @@ class Label implements View // Remove Duplicates $toAdd = $field ->filter(fn($o) => !$myFields->contains('dataSource', $o['dataSource'])) - ->reduce(function ($carry, $item) { + ->reduce(function ($previous, $current) { // On the first iteration we simply return the item. // If there is only one item to be processed for the row // then this effectively skips everything below this if block. - if (is_null($carry)){ - return $item; + if (is_null($previous)) { + return $current; } // At this point we are dealing with a row with multiple items being displayed. + // We need to combine the label and value of the current item with the previous item. // The end result of this will be in this format: // {labelOne} {valueOne} | {labelTwo} {valueTwo} | {labelThree} {valueThree} - $carry['value'] = trim(implode(' | ', [ - implode(' ', [$carry['label'], $carry['value']]), - implode(' ', [$item['label'], $item['value']]), + $previous['value'] = trim(implode(' | ', [ + implode(' ', [$previous['label'], $previous['value']]), + implode(' ', [$current['label'], $current['value']]), ])); // We'll set the label to an empty string since we // injected the label into the value field above. - $carry['label'] = ''; + $previous['label'] = ''; - return $carry; + return $previous; }); return $toAdd ? $myFields->push($toAdd) : $myFields; From a4e959818ae84b41fcf1fc097771e625b52e1232 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 11 Apr 2024 17:23:28 -0700 Subject: [PATCH 5/5] Add comment --- app/View/Label.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/View/Label.php b/app/View/Label.php index bb587d6c0..3ec3a4099 100644 --- a/app/View/Label.php +++ b/app/View/Label.php @@ -142,6 +142,8 @@ class Label implements View // Remove Duplicates $toAdd = $field ->filter(fn($o) => !$myFields->contains('dataSource', $o['dataSource'])) + // For fields that have multiple options, we need to combine them + // into a single field so all values are displayed. ->reduce(function ($previous, $current) { // On the first iteration we simply return the item. // If there is only one item to be processed for the row