Merge pull request #136 from ThomasRubini/recipe_ingredients

This commit is contained in:
Thomas Rubini 2023-01-26 19:21:03 +01:00 committed by GitHub
commit 7e63369481
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 78 additions and 12 deletions

View File

@ -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; $i<count($A_ingredientNames); $i++) {
$O_ingr = new IngredientModel(
$O_recipe->I_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; $i<count($A_ingredientNames); $i++) {
$O_ingr = null;
// search ingredient in DB's list
foreach($A_ingrsInDB as $O_ingr_loop) {
if($O_ingr_loop->S_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);
}

View File

@ -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();

View File

@ -77,9 +77,9 @@ if ($O_recipe === null) {
foreach($A_ingredients as $O_ingredient) {
echo '<li>
<label for="recipeIngredient'.$i.'">Ingrédient&nbsp;:</label>
<input type="text" name="recipeIngredient'.$i.'" id="recipeIngredient'.$i.'" placeholder="Farine" value="'.$O_ingredient->S_NAME.'">
<input type="text" name="recipeIngredientNames[]" id="recipeIngredient'.$i.'" placeholder="Farine" value="'.$O_ingredient->S_NAME.'">
<label for="recipeQuantity'.$i.'">Quantité&nbsp;:</label>
<input type="text" name="recipeQuantity'.$i.'" id="recipeIngredient'.$i.'" placeholder="500g" value="'.$O_ingredient->S_QUANTITY.'">
<input type="text" name="recipeIngredientQuantities[]" id="recipeIngredient'.$i.'" placeholder="500g" value="'.$O_ingredient->S_QUANTITY.'">
</li>';
$i++;
}
@ -89,9 +89,9 @@ if ($O_recipe === null) {
} else {
echo '<li>
<label for="recipeIngredient1">Ingrédient&nbsp;:</label>
<input type="text" name="recipeIngredient1" id="recipeIngredient1" placeholder="Farine">
<input type="text" name="recipeIngredientNames[]" id="recipeIngredient1" placeholder="Farine">
<label for="recipeQuantity1">Quantité&nbsp;:</label>
<input type="text" name="recipeQuantity1" id="recipeIngredient1" placeholder="500g">
<input type="text" name="recipeIngredientQuantities[]" id="recipeIngredient1" placeholder="500g">
</li>
</ul>
<button type="button" disabled="disabled" id="recipeButtonIngrdientLess">-</button>';
@ -112,16 +112,17 @@ if ($O_recipe === null) {
if (count($A_steps) === 0) {
echo '<li>
<label for="recipeInstruction1">Étape 1&nbsp;:</label>
<input type="text" name="recipeInstruction1" id="recipeInstruction1" placeholder="Battre les oeufs et la farine dans un grand saladier.">
<input type="text" name="recipeInstructions[]" id="recipeInstruction1" placeholder="Battre les oeufs et la farine dans un grand saladier.">
</li>
</ol>
<button type="button" disabled="disabled" id="recipeButtonInstructionLess">-</button>';
$numberOfInstructions = 1;
} else {
$i = 1;
foreach($A_steps as $S_step) {
echo '<li>
<label for="recipeInstruction'.$i.'">Étape '.$i.'&nbsp;:</label>
<input type="text" name="recipeInstruction'.$i.'" id="recipeInstruction'.$i.'" placeholder="Battre les oeufs et la farine dans un grand saladier." value="'.$S_step.'">
<input type="text" name="recipeInstructions[]" id="recipeInstruction'.$i.'" placeholder="Battre les oeufs et la farine dans un grand saladier." value="'.$S_step.'">
</li>';
$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]);