From 73022ac167b4151022f42c0294784ef63edc636c Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Tue, 2 Mar 2021 19:57:06 -0800 Subject: [PATCH] Add option to group or not group journal entries --- app/Http/Controllers/JournalEntryController.php | 13 +++++++++++-- resources/views/journal-entries/create.blade.php | 8 ++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/JournalEntryController.php b/app/Http/Controllers/JournalEntryController.php index a6c19cc..1f4046b 100644 --- a/app/Http/Controllers/JournalEntryController.php +++ b/app/Http/Controllers/JournalEntryController.php @@ -131,16 +131,25 @@ class JournalEntryController extends Controller 'ingredients.id.*' => 'required_with:ingredients.amount.*|nullable', 'ingredients.type' => ['required', 'array', new ArrayNotEmpty], 'ingredients.type.*' => ['required_with:ingredients.id.*', 'nullable', new UsesIngredientTrait()], + 'group_entries' => ['nullable', 'boolean'], ]); $ingredients = ArrayFormat::flipTwoDimensionalKeys($input['ingredients']); /** @var \App\Models\JournalEntry[] $entries */ $entries = []; - // TODO: Improve efficiency? Potential for lots of queries here... + $entry_key = 0; + // TODO: Improve efficiency. Potential for lots of queries here... foreach ($ingredients as $ingredient) { + // Set entry key (combined date and meal or individual entries). + if (isset($input['group_entries']) && (bool) $input['group_entries']) { + $entry_key = "{$ingredient['date']}{$ingredient['meal']}"; + } + else { + $entry_key++; + } + // Prepare entry values. - $entry_key = "{$ingredient['date']}{$ingredient['meal']}"; $entries[$entry_key] = $entries[$entry_key] ?? JournalEntry::make([ 'date' => $ingredient['date'], 'meal' => $ingredient['meal'], diff --git a/resources/views/journal-entries/create.blade.php b/resources/views/journal-entries/create.blade.php index 13cfc65..3905718 100644 --- a/resources/views/journal-entries/create.blade.php +++ b/resources/views/journal-entries/create.blade.php @@ -26,8 +26,12 @@ -
- Add entries +
+
+ + +
+ Add entries