From c8891236e6a5d3bb6b6dad64caa49036216459a9 Mon Sep 17 00:00:00 2001 From: Bernd Bestel Date: Tue, 5 Sep 2023 21:07:38 +0200 Subject: [PATCH] Optimized meal plan `weekRecipe` handling (fixes #2333) --- changelog/74_UNRELEASED_xxxx-xx-xx.md | 2 +- controllers/RecipesController.php | 13 ++++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/changelog/74_UNRELEASED_xxxx-xx-xx.md b/changelog/74_UNRELEASED_xxxx-xx-xx.md index 1fd2527a..184c1d57 100644 --- a/changelog/74_UNRELEASED_xxxx-xx-xx.md +++ b/changelog/74_UNRELEASED_xxxx-xx-xx.md @@ -22,7 +22,7 @@ ### Meal plan -- xxx +- Fixed that the meal plan page was broken after deleting _all_ (of all weeks) meal plan items ### Chores diff --git a/controllers/RecipesController.php b/controllers/RecipesController.php index 3b33dc07..ed7b1ed6 100644 --- a/controllers/RecipesController.php +++ b/controllers/RecipesController.php @@ -57,17 +57,24 @@ class RecipesController extends BaseController ]; } + $weekRecipe = $this->getDatabase()->recipes()->where("type = 'mealplan-week' AND name = LTRIM(STRFTIME('%Y-%W', DATE('$start')), '0')")->fetch(); + $weekRecipeId = 0; + if ($weekRecipe != null) + { + $weekRecipeId = $weekRecipe->id; + } + return $this->renderPage($response, 'mealplan', [ 'fullcalendarEventSources' => $events, 'recipes' => $recipes, - 'internalRecipes' => $this->getDatabase()->recipes()->where("id IN (SELECT recipe_id FROM meal_plan_internal_recipe_relation WHERE $mealPlanWhereTimespan)")->fetchAll(), - 'recipesResolved' => $this->getRecipesService()->GetRecipesResolved("recipe_id IN (SELECT recipe_id FROM meal_plan_internal_recipe_relation WHERE $mealPlanWhereTimespan)"), + 'internalRecipes' => $this->getDatabase()->recipes()->where("id IN (SELECT recipe_id FROM meal_plan_internal_recipe_relation WHERE $mealPlanWhereTimespan) OR id = $weekRecipeId")->fetchAll(), + 'recipesResolved' => $this->getRecipesService()->GetRecipesResolved("recipe_id IN (SELECT recipe_id FROM meal_plan_internal_recipe_relation WHERE $mealPlanWhereTimespan) OR recipe_id = $weekRecipeId"), 'products' => $this->getDatabase()->products()->orderBy('name', 'COLLATE NOCASE'), 'quantityUnits' => $this->getDatabase()->quantity_units()->orderBy('name', 'COLLATE NOCASE'), 'quantityUnitConversionsResolved' => $this->getDatabase()->cache__quantity_unit_conversions_resolved(), 'mealplanSections' => $this->getDatabase()->meal_plan_sections()->orderBy('sort_number'), 'usedMealplanSections' => $this->getDatabase()->meal_plan_sections()->where("id IN (SELECT section_id FROM meal_plan WHERE $mealPlanWhereTimespan)")->orderBy('sort_number'), - 'weekRecipe' => $this->getDatabase()->recipes()->where("type = 'mealplan-week' AND name = LTRIM(STRFTIME('%Y-%W', DATE('$start')), '0')")->fetch() + 'weekRecipe' => $weekRecipe ]); }