diff --git a/Controllers/RecipeController.php b/Controllers/RecipeController.php index 66b3eaf..efb2d5f 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); } @@ -97,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); } diff --git a/Models/IngredientModel.php b/Models/IngredientModel.php index fe5252c..7c791c8 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); @@ -39,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(); @@ -50,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(); diff --git a/Views/recipe/edit.php b/Views/recipe/edit.php index 8cb218d..c5cd745 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]);