From 25bd38ece75a77a9ceec64dbe0f4aff2d595d94c Mon Sep 17 00:00:00 2001 From: Thomas Rubini <74205383+ThomasRubini@users.noreply.github.com> Date: Thu, 26 Jan 2023 19:20:33 +0100 Subject: [PATCH] update recipe ingredients --- Controllers/RecipeController.php | 34 ++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/Controllers/RecipeController.php b/Controllers/RecipeController.php index 49f1777..efb2d5f 100644 --- a/Controllers/RecipeController.php +++ b/Controllers/RecipeController.php @@ -118,6 +118,40 @@ final class RecipeController self::fillRecipeFromPostParams($O_recipe, $A_postParams); $O_recipe->update(); + $A_ingredientNames = Utils::getOrDie($A_postParams, "recipeIngredientNames"); + $A_ingredientQuantities = Utils::getOrDie($A_postParams, "recipeIngredientQuantities"); + + $A_ingrsInDB = IngredientModel::searchByRecipe($O_recipe->I_ID); + + for($i=0; $iS_NAME === $A_ingredientNames[$i]) { + $O_ingr = $O_ingr_loop; + break; + } + } + + if($O_ingr === null) { + // if not present, create if and insert it + $O_ingr = new IngredientModel( + $O_recipe->I_ID, + $A_ingredientNames[$i], + $A_ingredientQuantities[$i] + ); + $O_ingr->insert(); + } else { + $O_ingr->S_QUANTITY = $A_ingredientQuantities[$i]; + $O_ingr->update(); + // if already present, update it and remove it from $A_ingrsInDB + + $ingr_key = array_search($O_ingr, $A_ingrsInDB, true); + unset($A_ingrsInDB[$ingr_key]); + } + } + header("Location: /recipe/view/".$O_recipe->I_ID); }