make edit and new actions work
This commit is contained in:
parent
6f66a44e69
commit
8ad62d8620
@ -40,7 +40,7 @@ final class RecipeController
|
||||
throw new HTTPSpecialCaseException(400, "You are not the owner of this recipe");
|
||||
}
|
||||
}
|
||||
//TODO MAKE THE VIEW USE THE NEW DATA FORMAT
|
||||
|
||||
View::show("recipe/edit", array("POST_URI" => "/recipe/update", "RECIPE" => $O_recipe));
|
||||
}
|
||||
|
||||
@ -48,25 +48,49 @@ final class RecipeController
|
||||
{
|
||||
Session::login_or_die();
|
||||
|
||||
View::show("recipe/edit", array("POST_URI" => "/recipe/create", "RECIPE" => array()));
|
||||
View::show("recipe/edit", array("POST_URI" => "/recipe/create", "RECIPE" => null));
|
||||
}
|
||||
|
||||
private static function fillRecipeFromPostParams($O_recipe, Array $A_postParams)
|
||||
{
|
||||
$O_difficulty = DifficultyModel::getByName(Utils::getOrDie($A_postParams, "recipeDifficulty"));
|
||||
if($O_difficulty === null){
|
||||
throw new HTTPSpecialCaseException(400, "Invalid difficulty");
|
||||
}
|
||||
|
||||
$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"];
|
||||
}
|
||||
|
||||
public function createAction(Array $A_urlParams = null, Array $A_postParams = null)
|
||||
{
|
||||
Session::login_or_die();
|
||||
|
||||
$O_difficulty = DifficultyModel::getByName(Utils::getOrDie($A_postParams, "recipeDifficulty"));
|
||||
if($O_difficulty === null){
|
||||
throw new HTTPSpecialCaseException(400, "Invalid difficulty");
|
||||
$O_recipe = RecipeModel::createEmpty();
|
||||
self::fillRecipeFromPostParams($O_recipe, $A_postParams);
|
||||
$O_recipe->insert();
|
||||
|
||||
header("Location: /recipe/view/".$O_recipe->I_ID);
|
||||
}
|
||||
|
||||
public function updateAction(Array $A_urlParams = null, Array $A_postParams = null)
|
||||
{
|
||||
Session::login_or_die();
|
||||
|
||||
$O_recipe = RecipeModel::getByID(Utils::getOrDie($A_postParams, "recipeID"));
|
||||
|
||||
if ($O_recipe->I_AUTHOR_ID !== $_SESSION["ID"]) {
|
||||
if(!Session::is_admin()){
|
||||
throw new HTTPSpecialCaseException(400, "You are not the owner of this recipe");
|
||||
}
|
||||
}
|
||||
|
||||
$O_recipe = new RecipeModel(
|
||||
Utils::getOrDie($A_postParams, "recipeName"),
|
||||
Utils::getOrDie($A_postParams, "recipeTime"),
|
||||
Utils::getOrDie($A_postParams, "recipeDescription"),
|
||||
null, $O_difficulty->I_ID, $_SESSION["ID"]
|
||||
);
|
||||
$O_recipe->insert();
|
||||
self::fillRecipeFromPostParams($O_recipe, $A_postParams);
|
||||
$O_recipe->update();
|
||||
|
||||
header("Location: /recipe/view/".$O_recipe->I_ID);
|
||||
}
|
||||
|
@ -1,15 +1,22 @@
|
||||
<?php
|
||||
$O_recipe = $A_view["RECIPE"];
|
||||
|
||||
$A_recipe = $A_view["RECIPE"];
|
||||
|
||||
function getOrEmpty($A_Dict, $S_keyName) {
|
||||
if (isset($A_Dict[$S_keyName])) {
|
||||
return $A_Dict[$S_keyName];
|
||||
} else {
|
||||
if($S_keyName == "TYPE" || $S_keyName == "INGREDIENTS")
|
||||
return array();
|
||||
return "";
|
||||
}
|
||||
if ($O_recipe === null) {
|
||||
$S_name = null;
|
||||
$I_time = null;
|
||||
$S_descr = null;
|
||||
$S_recipe = null;
|
||||
$S_difficultyName = null;
|
||||
$A_parts = array();
|
||||
$A_ingredients = array();
|
||||
} else {
|
||||
$S_name = $O_recipe->S_NAME;
|
||||
$I_time = $O_recipe->I_TIME;
|
||||
$S_descr = $O_recipe->S_DESCR;
|
||||
$S_recipe = $O_recipe->S_RECIPE;
|
||||
$S_difficultyName = $O_recipe->getDifficulty()->S_NAME;
|
||||
$A_parts = array(); // TODO
|
||||
$A_ingredients = $O_recipe->getIngredients();
|
||||
}
|
||||
?>
|
||||
|
||||
@ -21,36 +28,36 @@ function getOrEmpty($A_Dict, $S_keyName) {
|
||||
<input type="file" name="recipeImage" id="recipeImage">
|
||||
|
||||
<label for="recipeName">Nom de la recette :</label>
|
||||
<input type="text" name="recipeName" id="recipeName" placeholder="Nom du plat" value="<?= getOrEmpty($A_recipe, "NAME") ?>" required>
|
||||
<input type="text" name="recipeName" id="recipeName" placeholder="Nom du plat" value="<?= $S_name ?>" required>
|
||||
</br>
|
||||
<label for="recipeDescription">Description de la recette</label>
|
||||
</br>
|
||||
<textarea name="recipeDescription" id="recipeDescription"><?= getOrEmpty($A_recipe, "DESC") ?></textarea>
|
||||
<textarea name="recipeDescription" id="recipeDescription"><?= $S_descr ?></textarea>
|
||||
|
||||
<section>
|
||||
<h1>Informations alimentaires</h1>
|
||||
|
||||
<label for="recipeFifficulte">Niveau de difficulé :</label>
|
||||
<select name="recipeDifficulty" id="recipeDifficulte" required>
|
||||
<option value="tresFacile" <?= getOrEmpty($A_recipe, "DIFFICULTY_NAME")=="Très facile"? 'selected="selected"' : "" ?> >Très facile</option>
|
||||
<option value="facile" <?= getOrEmpty($A_recipe, "DIFFICULTY_NAME")=="Facile"? 'selected="selected"' : "" ?>>Facile</option>
|
||||
<option value="moyen" <?= getOrEmpty($A_recipe, "DIFFICULTY_NAME")=="Moyen"? 'selected="selected"' : "" ?>>Moyen</option>
|
||||
<option value="difficile" <?= getOrEmpty($A_recipe, "DIFFICULTY_NAME")=="Difficle"? 'selected="selected"' : "" ?>>Difficile</option>
|
||||
<option value="tresFacile" <?= $S_difficultyName==="Très facile"? 'selected="selected"' : "" ?> >Très facile</option>
|
||||
<option value="facile" <?= $S_difficultyName==="Facile"? 'selected="selected"' : "" ?>>Facile</option>
|
||||
<option value="moyen" <?= $S_difficultyName==="Moyen"? 'selected="selected"' : "" ?>>Moyen</option>
|
||||
<option value="difficile" <?= $S_difficultyName==="Difficle"? 'selected="selected"' : "" ?>>Difficile</option>
|
||||
</select>
|
||||
|
||||
|
||||
<legend>Particularités du plat :</legend>
|
||||
<input type="checkbox" name="part_Vegan" id="recipeVegan" <?= in_array("Végan", getOrEmpty($A_recipe, "TYPE"))? "checked":"" ?> >
|
||||
<input type="checkbox" name="part_Vegan" id="recipeVegan" <?= in_array("Végan", $A_parts)? "checked":"" ?> >
|
||||
<label for="recipeVegan">Végan</label>
|
||||
<input type="checkbox" name="part_LactoseFree" id="recipeLactoseFree" <?= in_array("Sans lactose", getOrEmpty($A_recipe, "TYPE"))? "checked":"" ?> >
|
||||
<input type="checkbox" name="part_LactoseFree" id="recipeLactoseFree" <?= in_array("Sans lactose", $A_parts)? "checked":"" ?> >
|
||||
<label for="recipeLactoseFree">Sans lactose</label>
|
||||
<input type="checkbox" name="part_GlutenFree" id="recipeGlutenFree" <?= in_array("Sans gluten", getOrEmpty($A_recipe, "TYPE"))? "checked":"" ?> >
|
||||
<input type="checkbox" name="part_GlutenFree" id="recipeGlutenFree" <?= in_array("Sans gluten", $A_parts)? "checked":"" ?> >
|
||||
<label for="recipeGlutenFree">Sans gluten</label>
|
||||
|
||||
</br>
|
||||
|
||||
<label for="recipeTime">Temps de préparation :</label>
|
||||
<input type="number" name="recipeTime" id="recipeTime" min="5" max="1500" step="5" placeholder="Temps de préparation" value="<?= getOrEmpty($A_recipe, "TIME") ?>" required>
|
||||
<input type="number" name="recipeTime" id="recipeTime" min="5" max="1500" step="5" placeholder="Temps de préparation" value="<?= $I_time ?>" required>
|
||||
<label for="recipeTime">minutes</label>
|
||||
|
||||
</section>
|
||||
@ -62,15 +69,14 @@ function getOrEmpty($A_Dict, $S_keyName) {
|
||||
|
||||
<ul class="recipeIngredients">
|
||||
<?php
|
||||
$ingredients = getOrEmpty($A_recipe, "INGREDIENTS");
|
||||
if(sizeof($ingredients) > 0) {
|
||||
if(sizeof($A_ingredients) > 0) {
|
||||
$i = 1;
|
||||
foreach($ingredients as $ingredient) {
|
||||
foreach($A_ingredients as $O_ingredient) {
|
||||
echo '<li>
|
||||
<label for="recipeIngredient'.$i.'">Ingrédient :</label>
|
||||
<input type="text" name="recipeIngredient'.$i.'" id="recipeIngredient'.$i.'" placeholder="Farine" value="'.$ingredient["NAME"].'">
|
||||
<input type="text" name="recipeIngredient'.$i.'" id="recipeIngredient'.$i.'" placeholder="Farine" value="'.$O_ingredient["NAME"].'">
|
||||
<label for="recipeQuantity'.$i.'">Quantité :</label>
|
||||
<input type="text" name="recipeQuantity'.$i.'" id="recipeIngredient'.$i.'" placeholder="500g" value="'.$ingredient["QUANTITY"].'">
|
||||
<input type="text" name="recipeQuantity'.$i.'" id="recipeIngredient'.$i.'" placeholder="500g" value="'.$O_ingredient["QUANTITY"].'">
|
||||
</li>';
|
||||
$i++;
|
||||
}
|
||||
@ -100,9 +106,8 @@ function getOrEmpty($A_Dict, $S_keyName) {
|
||||
|
||||
<ol class="recipeInstructions">
|
||||
<?php
|
||||
$preparation = getOrEmpty($A_recipe, "RECIPE");
|
||||
if(!empty($preparation)) {
|
||||
$steps = explode("\n", $preparation);
|
||||
if(!empty($S_recipe)) {
|
||||
$steps = explode("\n", $S_recipe);
|
||||
$i = 1;
|
||||
foreach($steps as $step) {
|
||||
echo '<li>
|
||||
|
Loading…
Reference in New Issue
Block a user