From d3b5345f7993442ea07114de65870537bf2c97c0 Mon Sep 17 00:00:00 2001 From: Thomas Rubini <74205383+ThomasRubini@users.noreply.github.com> Date: Thu, 26 Jan 2023 18:52:43 +0100 Subject: [PATCH 1/6] fix keys sent to backend --- Views/recipe/edit.php | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/Views/recipe/edit.php b/Views/recipe/edit.php index 8cb218d..f92d448 100644 --- a/Views/recipe/edit.php +++ b/Views/recipe/edit.php @@ -77,9 +77,9 @@ if ($O_recipe === null) { foreach($A_ingredients as $O_ingredient) { echo '
  • - + - +
  • '; $i++; } @@ -89,9 +89,9 @@ if ($O_recipe === null) { } else { echo '
  • - + - +
  • '; @@ -112,16 +112,17 @@ if ($O_recipe === null) { if (count($A_steps) === 0) { echo '
  • - +
  • '; $numberOfInstructions = 1; } else { + $i = 1; foreach($A_steps as $S_step) { echo '
  • - +
  • '; $i++; } @@ -157,7 +158,6 @@ buttonIngredientPlus.addEventListener('click', () => { if(nextArray[e].tagName == "LABEL") { nextArray[e].setAttribute("for", "recipeIngredient"+numberOfIngredients); } else { - nextArray[e].setAttribute("name", "recipeIngredient"+numberOfIngredients); nextArray[e].setAttribute("id", "recipeIngredient"+numberOfIngredients); nextArray[e].value = ""; } @@ -191,11 +191,10 @@ buttonInstructionPlus.addEventListener('click', () => { for(let e in nextArray) { console.log(nextArray[e]); if(nextArray[e].tagName == "LABEL") { - nextArray[e].setAttribute("for", "recipeIngredient"+numberOfInstructions); + nextArray[e].setAttribute("for", "recipeInstruction"+numberOfInstructions); nextArray[e].textContent = "Étape "+numberOfInstructions+"\u00A0:"; } else { - nextArray[e].setAttribute("name", "recipeIngredient"+numberOfInstructions); - nextArray[e].setAttribute("id", "recipeIngredient"+numberOfInstructions); + nextArray[e].setAttribute("id", "recipeInstruction"+numberOfInstructions); nextArray[e].value = ""; } next.appendChild(nextArray[e]); From 4e4126586e01ca7424a63861d5767dbdeeae70bf Mon Sep 17 00:00:00 2001 From: Thomas Rubini <74205383+ThomasRubini@users.noreply.github.com> Date: Thu, 26 Jan 2023 18:53:05 +0100 Subject: [PATCH 2/6] make IngredientModel::insert fetch ID --- Models/IngredientModel.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Models/IngredientModel.php b/Models/IngredientModel.php index fe5252c..4d29852 100644 --- a/Models/IngredientModel.php +++ b/Models/IngredientModel.php @@ -23,7 +23,7 @@ final class IngredientModel public function insert(){ $O_model = Model::get(); - $stmt = $O_model->prepare("SELECT 1 FROM INGREDIENT WHERE :name=name"); + $stmt = $O_model->prepare("SELECT ID FROM INGREDIENT WHERE :name=name"); $stmt->bindParam("name", $this->S_NAME); $stmt->execute(); if($stmt->rowCount() === 0){ @@ -31,6 +31,8 @@ final class IngredientModel $stmt->bindParam("name", $this->S_NAME); $stmt->execute(); $this->I_INGREDIENT_ID = Model::get()->lastInsertId(); + } else { + $this->I_INGREDIENT_ID = $stmt->fetch()["ID"]; } $stmt = $O_model->prepare("INSERT INTO RECIPE_INGREDIENT VALUES(:recipe_id, :ingredient_id, :quantity)"); $stmt->bindParam("recipe_id", $this->I_RECIPE_ID); From 44c785aa016bda8399160c7dbec518de210d15c2 Mon Sep 17 00:00:00 2001 From: Thomas Rubini <74205383+ThomasRubini@users.noreply.github.com> Date: Thu, 26 Jan 2023 18:53:11 +0100 Subject: [PATCH 3/6] add ingredients on recipe/create --- Controllers/RecipeController.php | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/Controllers/RecipeController.php b/Controllers/RecipeController.php index 66b3eaf..49f1777 100644 --- a/Controllers/RecipeController.php +++ b/Controllers/RecipeController.php @@ -62,9 +62,16 @@ final class RecipeController $O_recipe->S_NAME = Utils::getOrDie($A_postParams, "recipeName"); $O_recipe->I_TIME = Utils::intOrDie(Utils::getOrDie($A_postParams, "recipeTime")); $O_recipe->S_DESCR = Utils::getOrDie($A_postParams, "recipeDescription"); - $O_recipe->S_RECIPE = null; // TODO $O_recipe->I_DIFFICULTY_ID = $O_difficulty->I_ID; $O_recipe->I_AUTHOR_ID = $_SESSION["ID"]; + + $S_recipe = ""; + $i = 0; + foreach(Utils::getOrDie($A_postParams, "recipeInstructions") as $S_instr) { + $S_recipe.= "\n\n".$S_instr; + $i++; + } + $O_recipe->S_RECIPE = substr($S_recipe, 2); } public function createAction(Array $A_urlParams = null, Array $A_postParams = null) @@ -75,6 +82,20 @@ final class RecipeController self::fillRecipeFromPostParams($O_recipe, $A_postParams); $O_recipe->insert(); + $A_ingredientNames = Utils::getOrDie($A_postParams, "recipeIngredientNames"); + $A_ingredientQuantities = Utils::getOrDie($A_postParams, "recipeIngredientQuantities"); + + $A_ingredients = array(); + for($i=0; $iI_ID, + $A_ingredientNames[$i], + $A_ingredientQuantities[$i] + ); + $O_ingr->insert(); + array_push($A_ingredients, $O_ingr); + } + header("Location: /recipe/view/".$O_recipe->I_ID); } From b195da0e46f87ea5b8073a25a99bf313ba020d47 Mon Sep 17 00:00:00 2001 From: Thomas Rubini <74205383+ThomasRubini@users.noreply.github.com> Date: Thu, 26 Jan 2023 19:08:01 +0100 Subject: [PATCH 4/6] fix typo --- Views/recipe/edit.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Views/recipe/edit.php b/Views/recipe/edit.php index f92d448..c5cd745 100644 --- a/Views/recipe/edit.php +++ b/Views/recipe/edit.php @@ -77,7 +77,7 @@ if ($O_recipe === null) { foreach($A_ingredients as $O_ingredient) { echo '
  • - +
  • '; From 7ef799668a9f8c1ef8b9bcaff94541990a164ac2 Mon Sep 17 00:00:00 2001 From: Thomas Rubini <74205383+ThomasRubini@users.noreply.github.com> Date: Thu, 26 Jan 2023 19:20:09 +0100 Subject: [PATCH 5/6] add IngredientModel::update() --- Models/IngredientModel.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Models/IngredientModel.php b/Models/IngredientModel.php index 4d29852..7c791c8 100644 --- a/Models/IngredientModel.php +++ b/Models/IngredientModel.php @@ -41,6 +41,15 @@ final class IngredientModel $stmt->execute(); } + public function update(){ + $O_model = Model::get(); + $stmt = $O_model->prepare("UPDATE RECIPE_INGREDIENT SET QUANTITY=:quantity + WHERE RECIPE_ID=:recipe_id AND INGREDIENT_ID=:ingredient_id"); + $stmt->bindParam("quantity", $this->S_QUANTITY); + $stmt->bindParam("recipe_id", $this->I_RECIPE_ID); + $stmt->bindParam("ingredient_id", $this->I_INGREDIENT_ID); + $stmt->execute(); + } public function delete(){ $O_model = Model::get(); @@ -52,6 +61,7 @@ final class IngredientModel $stmt->bindParam("id", $this->I_INGREDIENT_ID); $stmt->execute(); } + public static function getByRecipeAndName($I_recipe_id, $S_name){ $S_name = strtolower($S_name); $O_model = Model::get(); 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 6/6] 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); }