From c0a0246e5b0f2a13c556dc5983df66b80a511db3 Mon Sep 17 00:00:00 2001 From: SIMAILA Djalim Date: Thu, 26 Jan 2023 23:39:40 +0100 Subject: [PATCH] fixed particularities --- Controllers/RecipeController.php | 44 ++++++++++++++++++++++++++++++-- Models/ParticularityModel.php | 18 +++++++------ Views/recipe/edit.php | 13 +++++++--- 3 files changed, 61 insertions(+), 14 deletions(-) diff --git a/Controllers/RecipeController.php b/Controllers/RecipeController.php index 48ba988..231bb55 100644 --- a/Controllers/RecipeController.php +++ b/Controllers/RecipeController.php @@ -8,12 +8,10 @@ final class RecipeController if(count($A_urlParams)!=1){ throw new HTTPSpecialCaseException(404); } - $O_recipe = RecipeModel::getByID($A_urlParams[0]); if ($O_recipe === null) { throw new HTTPSpecialCaseException(404); } - View::show("recipe/view", array( "ADMIN" => Session::is_admin(), "USER_ID" => Session::is_login() ? $_SESSION["ID"] : null, @@ -93,6 +91,27 @@ final class RecipeController $A_ingredientNames = Utils::getOrDie($A_postParams, "recipeIngredientNames"); $A_ingredientQuantities = Utils::getOrDie($A_postParams, "recipeIngredientQuantities"); + + + // handle particularities + if(isset($A_postParams["recipeVegan"])){ + $O_part = new ParticularityModel($O_recipe->I_ID, "végan"); + $O_part->insert(); + } + if(isset($A_postParams["recipeVegetarian"])){ + $O_part = new ParticularityModel($O_recipe->I_ID, "végétarien"); + $O_part->insert(); + } + if(isset($A_postParams["recipeLactoseFree"])){ + $O_part = new ParticularityModel($O_recipe->I_ID, "sans lactose"); + $O_part->insert(); + } + if(isset($A_postParams["recipeGlutenFree"])){ + $O_part = new ParticularityModel($O_recipe->I_ID, "sans gluten"); + $O_part->insert(); + } + + $A_ingredients = array(); for($i=0; $iupdate(); + + ParticularityModel::removeByRecipe($O_recipe->I_ID); + + // handle particularities + if(isset($A_postParams["part_Vegan"])){ + $O_part = new ParticularityModel($O_recipe->I_ID, "végan"); + $O_part->insert(); + } + if(isset($A_postParams["part_Vegeta"])){ + $O_part = new ParticularityModel($O_recipe->I_ID, "végétarien"); + $O_part->insert(); + } + if(isset($A_postParams["part_LactoseFree"])){ + $O_part = new ParticularityModel($O_recipe->I_ID, "sans lactose"); + $O_part->insert(); + } + if(isset($A_postParams["part_GlutenFree"])){ + $O_part = new ParticularityModel($O_recipe->I_ID, "sans gluten"); + $O_part->insert(); + } + // update img if necessary $fp = Utils::tryProcessImg("recipeImage"); diff --git a/Models/ParticularityModel.php b/Models/ParticularityModel.php index acc1e00..21c5c58 100644 --- a/Models/ParticularityModel.php +++ b/Models/ParticularityModel.php @@ -21,15 +21,10 @@ final class ParticularityModel public function insert(){ $O_model = Model::get(); - $stmt = $O_model->prepare("SELECT 1 FROM PARTICULARITY WHERE :name=name"); + $stmt = $O_model->prepare("SELECT ID FROM PARTICULARITY WHERE :name=name"); $stmt->bindParam("name", $this->S_NAME); $stmt->execute(); - if($stmt->rowCount() === 0){ - $stmt = $O_model->prepare("INSERT INTO PARTICULARITY (NAME) VALUES(:name)"); - $stmt->bindParam("name", $this->S_NAME); - $stmt->execute(); - $this->I_PARTICULARITY_ID = Model::get()->lastInsertId(); - } + $this->I_PARTICULARITY_ID = $stmt->fetch()["ID"]; $stmt = $O_model->prepare("INSERT INTO RECIPE_PARTICULARITY VALUES(:recipe_id, :particularity_id)"); $stmt->bindParam("recipe_id", $this->I_RECIPE_ID); $stmt->bindParam("particularity_id", $this->I_PARTICULARITY_ID); @@ -67,11 +62,18 @@ final class ParticularityModel $stmt->bindParam("id", $this->I_PARTICULARITY_ID); $stmt->execute(); $stmt = $O_model->prepare("DELETE FROM RECIPE_PARTICULARITY WHERE PARTICULARITY_ID=:id"); - $stmt->execute(); $stmt->bindParam("id", $this->I_PARTICULARITY_ID); $stmt->execute(); } + + public static function removeByRecipe($I_recipe_id){ + $O_model = Model::get(); + $stmt = $O_model->prepare("DELETE FROM RECIPE_PARTICULARITY WHERE RECIPE_ID=:id"); + $stmt->bindParam("id",$I_recipe_id); + $stmt->execute(); + } + public static function searchByRecipe($I_recipe_id) { $O_model = Model::get(); diff --git a/Views/recipe/edit.php b/Views/recipe/edit.php index 6e2dccf..05f18fe 100644 --- a/Views/recipe/edit.php +++ b/Views/recipe/edit.php @@ -15,7 +15,10 @@ if ($O_recipe === null) { $S_descr = $O_recipe->S_DESCR; $A_instructions = $O_recipe->getSplitInstructions(); $S_difficultyName = $O_recipe->getDifficulty()->S_NAME; - $A_parts = array(); // TODO + $A_parts = array(); + foreach(ParticularityModel::searchByRecipe($O_recipe->I_ID) as $O_part){ + array_push($A_parts, $O_part->S_NAME); + } $A_ingredients = $O_recipe->getIngredients(); } ?> @@ -51,11 +54,13 @@ if ($O_recipe === null) { Particularités du plat : - > + > - > + > + + > - > + >