diff --git a/Controllers/CategoryController.php b/Controllers/CategoryController.php index a6af7d7..edfef23 100644 --- a/Controllers/CategoryController.php +++ b/Controllers/CategoryController.php @@ -5,13 +5,60 @@ final class CategoryController public function defaultAction(Array $A_urlParams = null, Array $A_postParams = null) { - $A_recipes = RecipeModel::getRandomRecipes(3); + $A_vegeta = ParticularityModel::getByName("végétarien")->getRecipes(); + $A_vegan = ParticularityModel::getByName("végan")->getRecipes(); + $A_gluten = ParticularityModel::getByName("sans gluten")->getRecipes(); + $A_lactose = ParticularityModel::getByName("sans lactose")->getRecipes(); + $A_recipes = RecipeModel::getUncategorizedRecipes(); - // TODO actually fill out by particularity instead $A_array_categories = array( - "Végan" => $A_recipes, - "Sans gluten" => $A_recipes, - "Sans lactose" => $A_recipes + "Végan" => $A_vegan, + "Végétarien" => $A_vegeta, + "Sans gluten" => $A_gluten, + "Sans lactose" => $A_lactose, + "Non Catégorisé" => $A_recipes + ); + + View::show("category/view", $A_array_categories); + } + + public function lactoseLessAction(Array $A_urlParams = null, Array $A_postParams = null) + { + $A_lactose = ParticularityModel::getByName("sans lactose")->getRecipes(); + + $A_array_categories = array( + "Sans lactose" => $A_lactose + ); + + View::show("category/view", $A_array_categories); + } + public function glutenLessAction(Array $A_urlParams = null, Array $A_postParams = null) + { + $A_gluten = ParticularityModel::getByName("sans gluten")->getRecipes(); + + $A_array_categories = array( + "Sans gluten" => $A_gluten + ); + + View::show("category/view", $A_array_categories); + } + public function veganAction(Array $A_urlParams = null, Array $A_postParams = null) + { + $A_vegan = ParticularityModel::getByName("végan")->getRecipes(); + + $A_array_categories = array( + "Végan" => $A_vegan + ); + + View::show("category/view", $A_array_categories); + } + + public function vegetarianAction(Array $A_urlParams = null, Array $A_postParams = null) + { + $A_vegeta = ParticularityModel::getByName("végétarien")->getRecipes(); + + $A_array_categories = array( + "Végétarien" => $A_vegeta ); View::show("category/view", $A_array_categories); diff --git a/Controllers/ManageUserController.php b/Controllers/ManageUserController.php index 38c6b6c..1f22b29 100644 --- a/Controllers/ManageUserController.php +++ b/Controllers/ManageUserController.php @@ -52,13 +52,18 @@ final class ManageUserController if (isset($A_postParams["enable"])) { $O_user->B_DISABLED = 0; $O_user->update(); - }else if (isset($A_postParams["disable"])) { + } else if (isset($A_postParams["disable"])) { $O_user->B_DISABLED = 1; $O_user->update(); - }else if (isset($A_postParams["delete"])) { - $O_user->delete(); - } - + } else if (isset($A_postParams["op"])) { + $O_user->B_ADMIN = 1; + $O_user->update(); + } else if (isset($A_postParams["deop"])) { + $O_user->B_ADMIN = 0; + $O_user->update(); + } else if (isset($A_postParams["delete"])) { + $O_user->delete(); + } header("Location: ".$_SERVER['HTTP_REFERER']); } diff --git a/Controllers/RecipeController.php b/Controllers/RecipeController.php index 948b733..9cfbd50 100644 --- a/Controllers/RecipeController.php +++ b/Controllers/RecipeController.php @@ -8,15 +8,13 @@ final class RecipeController if(count($A_urlParams)!=1){ throw new HTTPSpecialCaseException(404); } - - //TODO MAKE THE VIEW USE THE NEW DATA FORMAT $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, "RECIPE" => $O_recipe )); } @@ -51,7 +49,7 @@ final class RecipeController View::show("recipe/edit", array("POST_URI" => "/recipe/create", "RECIPE" => null)); } - private static function fillRecipeFromPostParams($O_recipe, Array $A_postParams) + private static function fillBasicRecipeAttributes($O_recipe, Array $A_postParams) { $O_difficulty = DifficultyModel::getByName(Utils::getOrDie($A_postParams, "recipeDifficulty")); if($O_difficulty === null){ @@ -61,9 +59,35 @@ 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_instructions = ""; + $i = 0; + foreach(Utils::getOrDie($A_postParams, "recipeInstructions") as $S_instr) { + $S_instructions.= "\n\n".$S_instr; + $i++; + } + $O_recipe->S_INSTRUCTIONS = substr($S_instructions, 2); + } + + private function handleParticularities($O_recipe, $A_postParams){ + // 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(); + } } public function createAction(Array $A_urlParams = null, Array $A_postParams = null) @@ -71,9 +95,36 @@ final class RecipeController Session::login_or_die(); $O_recipe = RecipeModel::createEmpty(); - self::fillRecipeFromPostParams($O_recipe, $A_postParams); + + // fill basic recipe attribtues + self::fillBasicRecipeAttributes($O_recipe, $A_postParams); + $O_recipe->I_AUTHOR_ID = $_SESSION["ID"]; $O_recipe->insert(); + // update img if necessary + $fp = Utils::tryProcessImg("recipeImage"); + if($fp !== null) { + $O_recipe->updateImg($fp); + } + + // handle ingredients + $A_ingredientNames = Utils::getOrDie($A_postParams, "recipeIngredientNames"); + $A_ingredientQuantities = Utils::getOrDie($A_postParams, "recipeIngredientQuantities"); + + // handle particularities + self::handleParticularities($O_recipe, $A_postParams); + + $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); } @@ -93,17 +144,73 @@ final class RecipeController } } - self::fillRecipeFromPostParams($O_recipe, $A_postParams); + // fill basic recipe attribtues + self::fillBasicRecipeAttributes($O_recipe, $A_postParams); $O_recipe->update(); + ParticularityModel::removeByRecipe($O_recipe->I_ID); + + // handle particularities + self::handleParticularities($O_recipe, $A_postParams); + + // update img if necessary + $fp = Utils::tryProcessImg("recipeImage"); + if($fp !== null) { + $O_recipe->updateImg($fp); + } + + // handle ingredients + $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]); + } + } + + // delete all remaining ingredients, they have been deleted by the user + foreach($A_ingrsInDB as $O_ingr){ + $O_ingr->delete(); + } + header("Location: /recipe/view/".$O_recipe->I_ID); } public function deleteAction(Array $A_urlParams = null, Array $A_postParams = null) { + if(count($A_urlParams)!=1){ + throw new HTTPSpecialCaseException(404); + } + Session::login_or_die(); - $O_recipe = RecipeModel::getByID(Utils::intOrDie(Utils::getOrDie($A_postParams, "recipe_id"))); + $O_recipe = RecipeModel::getByID(Utils::intOrDie($A_urlParams[0])); if ($O_recipe->I_AUTHOR_ID !== $_SESSION["ID"]) { if(!Session::is_admin()){ @@ -112,6 +219,8 @@ final class RecipeController } $O_recipe->delete(); + + header("Location: /"); } public function searchAction(Array $A_urlParams = null, Array $A_postParams = null, Array $A_getParams = null) @@ -131,7 +240,6 @@ final class RecipeController private function searchQueryView(Array $A_urlParams = null, Array $A_postParams = null, Array $A_getParams = null) { - //TODO change this when the function will return object array $A_results = RecipeModel::searchRecipesByName($A_getParams["query"]); View::show("recipe/search", array( diff --git a/Controllers/UserController.php b/Controllers/UserController.php index e6742d0..ec1126e 100644 --- a/Controllers/UserController.php +++ b/Controllers/UserController.php @@ -7,6 +7,10 @@ ini_set("session.cookie_lifetime", $__SESSION_TIMEOUT); final class UserController { + private static function currentDate(){ + return date("Y-m-d H:i:s"); + } + public function loginAction(Array $A_urlParams = null, Array $A_postParams = null, Array $A_getParams = null) { if (Session::is_login()) { @@ -27,7 +31,7 @@ final class UserController } private function redirectToPreviousPage(Array $A_postParams = null){ - if (isset($A_postParams["return_uri"])) { + if (isset($A_postParams["return_uri"]) && !empty($A_postParams["return_uri"])) { header("Location: ".$A_postParams["return_uri"]); } else { header("Location: /"); @@ -53,6 +57,9 @@ final class UserController return header("Location: /user/login"); } + $O_user->S_LAST_SEEN = self::currentDate(); + $O_user->update(); + Session::set_login($O_user->I_ID); self::redirectToPreviousPage($A_postParams); @@ -81,7 +88,7 @@ final class UserController $S_password_hash = password_hash($S_password, PASSWORD_DEFAULT); - $O_user = new UserModel($S_email, $S_username, $S_password_hash, null, date("Y-m-d"), 0, 0); + $O_user = UserModel::createFull($S_email, $S_username, $S_password_hash, self::currentDate(), self::currentDate(), 0, 0); $O_user->insert(); Session::set_login($O_user->I_ID); @@ -96,8 +103,6 @@ final class UserController header("Location: /"); } - // Kept for compatibility purposes - // TODO do a redirect route once implemented public function viewAction(Array $A_urlParams = null, Array $A_postParams = null) { return self::defaultAction($A_urlParams, $A_postParams); @@ -113,7 +118,6 @@ final class UserController $O_user = UserModel::getByID($_SESSION["ID"]); - //TODO Convert User into array return View::show("user/edit", array("USER" => $O_user)); } @@ -123,30 +127,23 @@ final class UserController $O_user = UserModel::getByID($_SESSION["ID"]); - if (isset($_FILES["profilPicture"])) { - - if ($_FILES['profilPicture']['error'] === UPLOAD_ERR_OK) { - $info = getimagesize($_FILES['profilPicture']['tmp_name']); - if ($info !== false && ($info[2] === IMAGETYPE_JPEG || $info[2] !== IMAGETYPE_PNG)) { - $fp = fopen($_FILES['profilPicture']['tmp_name'], 'rb'); - $O_user->updateProfilePic($fp); - } - } - + $fp = Utils::tryProcessImg("profilPicture"); + if($fp !== null) { + $O_user->updateProfilePic($fp); } - if (isset($_POST["email"])) { + + if (isset($_POST["email"]) && !empty($S_email)) { $S_email = $_POST["email"]; - if (!empty($S_email) && filter_var($S_email, FILTER_VALIDATE_EMAIL)) { + if (filter_var($S_email, FILTER_VALIDATE_EMAIL)) { $O_user->S_EMAIL = $_POST["email"]; $O_user->update(); + } else { + throw new HTTPSpecialCaseException(400, "Invalid email"); } } - if (isset($_POST["username"])) { - $S_username = $_POST["username"]; - if (!empty($S_username)) { - $O_user->S_USERNAME = $_POST["username"]; - $O_user->update(); - } + if (isset($_POST["username"]) && !empty($S_email)) { + $O_user->S_USERNAME = $_POST["username"]; + $O_user->update(); } header("Location: /user"); @@ -191,7 +188,7 @@ final class UserController $O_user = UserModel::getByID($A_urlParams[0]); if (isset($O_user)) { - $S_pfp = $O_user->getProfilePic(); + $S_pfp = $O_user->queryProfilePic(); if($S_pfp !== null) { header("Content-Type: image"); echo $S_pfp; diff --git a/Kernel/Utils.php b/Kernel/Utils.php index af510f0..d88914d 100644 --- a/Kernel/Utils.php +++ b/Kernel/Utils.php @@ -17,5 +17,24 @@ final class Utils if (is_numeric($data)) return (int) $data; else throw new HTTPSpecialCaseException(400, "Not an int"); } - + + public static function tryProcessImg($filename) { + if (isset($_FILES[$filename])) { + $file = $_FILES[$filename]; + if(!empty($file["name"])) { + if ($file['error'] === UPLOAD_ERR_OK) { + $info = getimagesize($file['tmp_name']); + if ($info !== false && ($info[2] === IMAGETYPE_JPEG || $info[2] === IMAGETYPE_PNG)) { + $fp = fopen($file['tmp_name'], 'rb'); + return $fp; + } else { + throw new HTTPSpecialCaseException(400, "Image submitted is not jpeg/png"); + } + } else { + throw new HTTPSpecialCaseException(400, "Image upload error"); + } + } + } + return null; + } } diff --git a/Models/ApprModel.php b/Models/ApprModel.php index 7756c09..fd5e0de 100644 --- a/Models/ApprModel.php +++ b/Models/ApprModel.php @@ -18,7 +18,7 @@ final class ApprModel { $this->I_AUTHOR_ID = $I_AUTHOR_ID; $this->I_RECIPE_ID = $I_RECIPE_ID; } - private function createFromRow($A_row,$I_id){ + private static function createFromRow($A_row,$I_id){ $O_appr = new ApprModel($A_row["COMMENT"], $A_row["NOTE"], $A_row["DATE"], $A_row["AUTHOR_ID"], $A_row["RECIPE_ID"]); $O_appr->I_ID = $I_id; return $O_appr; @@ -53,6 +53,15 @@ final class ApprModel { $stmt->bindParam("id", $this->I_ID); $stmt->execute(); } + + public function getAuthorOrAnon(){ + $O_author = self::getAuthor(); + if ($O_author === null) { + return UserModel::getAnonUser(); + } else { + return $O_author; + } + } public function getAuthor(){ if($this->O_AUTHOR === null){ diff --git a/Models/IngredientModel.php b/Models/IngredientModel.php index fe5252c..a7b03d2 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,17 +41,23 @@ 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(); - $stmt = $O_model->prepare("DELETE FROM INGREDIENT WHERE ID=:id"); - $stmt->bindParam("id", $this->I_INGREDIENT_ID); - $stmt->execute(); $stmt = $O_model->prepare("DELETE FROM RECIPE_INGREDIENT WHERE INGREDIENT_ID=:id"); - $stmt->execute(); $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/Models/ParticularityModel.php b/Models/ParticularityModel.php index a94e07c..21c5c58 100644 --- a/Models/ParticularityModel.php +++ b/Models/ParticularityModel.php @@ -21,21 +21,40 @@ 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); $stmt->execute(); } + public static function getByName($S_name){ + $O_model = Model::get(); + $stmt = $O_model->prepare("SELECT * FROM PARTICULARITY WHERE NAME=:name"); + $stmt->bindParam("name", $S_name); + $stmt->execute(); + + $row = $stmt->fetch(); + if ($row === false) return null; + + $O_part = new ParticularityModel($row["NAME"],null); + $O_part->I_PARTICULARITY_ID = $row["ID"]; + return $O_part; + } + public function getRecipes(){ + $O_model = Model::get(); + $stmt = $O_model->prepare("SELECT RECIPE_ID FROM RECIPE_PARTICULARITY WHERE PARTICULARITY_ID=:id"); + $stmt->bindParam("id", $this->I_PARTICULARITY_ID); + $stmt->execute(); + $A_recipes = array(); + foreach($stmt->fetchAll() as $row){ + array_push($A_recipes, RecipeModel::getByID($row["RECIPE_ID"])); + } + return $A_recipes; + } public function delete(){ $O_model = Model::get(); @@ -43,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/Models/RecipeModel.php b/Models/RecipeModel.php index c4fad86..496ed90 100644 --- a/Models/RecipeModel.php +++ b/Models/RecipeModel.php @@ -6,7 +6,7 @@ final class RecipeModel public $S_NAME = null; public $I_TIME = null; public $S_DESCR = null; - public $S_RECIPE = null; + public $S_INSTRUCTIONS = null; public $I_DIFFICULTY_ID = null; public $I_AUTHOR_ID = null; @@ -22,13 +22,13 @@ final class RecipeModel return new RecipeModel(); } - public static function createFull($S_NAME, $I_TIME, $S_DESCR, $S_RECIPE, $I_DIFFICULTY_ID, $I_AUTHOR_ID) + public static function createFull($S_NAME, $I_TIME, $S_DESCR, $S_INSTRUCTIONS, $I_DIFFICULTY_ID, $I_AUTHOR_ID) { $O_recipe = new RecipeModel(); $O_recipe->S_NAME = $S_NAME; $O_recipe->I_TIME = $I_TIME; $O_recipe->S_DESCR = $S_DESCR; - $O_recipe->S_RECIPE = $S_RECIPE; + $O_recipe->S_INSTRUCTIONS = $S_INSTRUCTIONS; $O_recipe->I_DIFFICULTY_ID = $I_DIFFICULTY_ID; $O_recipe->I_AUTHOR_ID = $I_AUTHOR_ID; return $O_recipe; @@ -37,29 +37,39 @@ final class RecipeModel public function insert() { $O_model = Model::get(); - $stmt = $O_model->prepare("INSERT INTO RECIPE (NAME, TIME, DESCR, RECIPE ,DIFFICULTY_ID, AUTHOR_ID) VALUES(:name, :time, :descr, :recipe, :difficulty_id, :author_id)"); + $stmt = $O_model->prepare("INSERT INTO RECIPE (NAME, TIME, DESCR, INSTRUCTIONS ,DIFFICULTY_ID, AUTHOR_ID) VALUES(:name, :time, :descr, :instructions, :difficulty_id, :author_id)"); $stmt->bindParam("name", $this->S_NAME); $stmt->bindParam("time", $this->I_TIME); $stmt->bindParam("descr", $this->S_DESCR); - $stmt->bindParam("recipe", $this->S_RECIPE); + $stmt->bindParam("instructions", $this->S_INSTRUCTIONS); $stmt->bindParam("difficulty_id", $this->I_DIFFICULTY_ID); $stmt->bindParam("author_id", $this->I_AUTHOR_ID); $stmt->execute(); $this->I_ID = Model::get()->lastInsertId(); } + public function update() { $O_model = Model::get(); - $stmt = $O_model->prepare("UPDATE RECIPE SET NAME=:name, TIME=:time, DESCR=:descr, RECIPE=:recipe, DIFFICULTY_ID=:difficulty_id, AUTHOR_ID=:author_id WHERE ID=:id"); + $stmt = $O_model->prepare("UPDATE RECIPE SET NAME=:name, TIME=:time, DESCR=:descr, INSTRUCTIONS=:instructions, DIFFICULTY_ID=:difficulty_id, AUTHOR_ID=:author_id WHERE ID=:id"); $stmt->bindParam("id", $this->I_ID); $stmt->bindParam("name", $this->S_NAME); $stmt->bindParam("time", $this->I_TIME); $stmt->bindParam("descr", $this->S_DESCR); - $stmt->bindParam("recipe", $this->S_RECIPE); + $stmt->bindParam("instructions", $this->S_INSTRUCTIONS); $stmt->bindParam("difficulty_id", $this->I_DIFFICULTY_ID); $stmt->bindParam("author_id", $this->I_AUTHOR_ID); $stmt->execute(); } + + public function updateImg($img_fp){ + $O_model = Model::get(); + $stmt = $O_model->prepare("UPDATE RECIPE SET IMG=:img WHERE ID=:id"); + $stmt->bindParam("id", $this->I_ID); + $stmt->bindParam("img", $img_fp, PDO::PARAM_LOB); + $stmt->execute(); + } + public function delete(){ $O_model = Model::get(); $stmt = $O_model->prepare("DELETE FROM RECIPE WHERE ID=:id"); @@ -68,7 +78,7 @@ final class RecipeModel } private static function createFromRow($A_row, $I_ID){ - $O_recipe = RecipeModel::createFull($A_row["NAME"], $A_row["TIME"], $A_row["DESCR"], $A_row["RECIPE"], $A_row["DIFFICULTY_ID"], $A_row["AUTHOR_ID"]); + $O_recipe = RecipeModel::createFull($A_row["NAME"], $A_row["TIME"], $A_row["DESCR"], $A_row["INSTRUCTIONS"], $A_row["DIFFICULTY_ID"], $A_row["AUTHOR_ID"]); $O_recipe->I_ID = $I_ID; return $O_recipe; } @@ -119,6 +129,15 @@ final class RecipeModel return round($avg*2)/2; } + + public function getAuthorOrAnon(){ + $O_author = self::getAuthor(); + if ($O_author === null) { + return UserModel::getAnonUser(); + } else { + return $O_author; + } + } public function getAuthor(){ if($this->O_AUTHOR === null){ @@ -147,7 +166,10 @@ final class RecipeModel return $this->A_APPRS; } - //TODO: return array object + public function getSplitInstructions(){ + return explode("\n\n", str_replace("\r", "", $this->S_INSTRUCTIONS)); + } + public static function searchRecipesByName($S_query) { @@ -171,7 +193,7 @@ final class RecipeModel from CTE JOIN RECIPE WHERE CTE.NAME is not null - AND INSTR(RECIPE.NAME, CTE.NAME) > 0 + AND RECIPE.NAME LIKE CONCAT('%', CTE.NAME, '%') > 0 GROUP BY RECIPE.ID ORDER BY count(RECIPE.ID) LIMIT 10; @@ -188,6 +210,19 @@ final class RecipeModel return $A_recipes; } + public static function getUncategorizedRecipes(){ + $O_model = Model::get(); + $stmt = $O_model->prepare("SELECT * FROM RECIPE WHERE ID NOT IN (SELECT RECIPE_ID FROM RECIPE_PARTICULARITY)"); + $stmt->execute(); + + $A_recipes = array(); + foreach($stmt->fetchAll() as $row){ + array_push($A_recipes, self::createFromRow($row, $row["ID"])); + } + + return $A_recipes; + } + public static function getRandomRecipes($I_n) { $O_model = Model::get(); diff --git a/Models/UserModel.php b/Models/UserModel.php index f71051e..28abcc9 100644 --- a/Models/UserModel.php +++ b/Models/UserModel.php @@ -2,6 +2,8 @@ final class UserModel extends UserSessionModel { + private static $O_ANONUSER = null; + public $I_ID = null; public $S_EMAIL = null; public $S_USERNAME= null; @@ -11,22 +13,43 @@ final class UserModel extends UserSessionModel public $B_ADMIN = 0; public $B_DISABLED = 0; - public function __construct($S_EMAIL, $S_USERNAME,$S_PASSWORD_HASH,$S_LAST_SEEN,$S_FIRST_SEEN,$B_ADMIN,$B_DISABLED) + private function __construct(){} + + public static function createFull($S_EMAIL, $S_USERNAME,$S_PASSWORD_HASH,$S_LAST_SEEN,$S_FIRST_SEEN,$B_ADMIN,$B_DISABLED) { - $this->S_EMAIL = $S_EMAIL; - $this->S_USERNAME = $S_USERNAME; - $this->S_PASSWORD_HASH = $S_PASSWORD_HASH; - $this->S_LAST_SEEN = $S_LAST_SEEN; - $this->S_FIRST_SEEN = $S_FIRST_SEEN; - $this->B_ADMIN = $B_ADMIN; - $this->B_DISABLED = $B_DISABLED; + $O_user = new UserModel(); + $O_user->S_EMAIL = $S_EMAIL; + $O_user->S_USERNAME = $S_USERNAME; + $O_user->S_PASSWORD_HASH = $S_PASSWORD_HASH; + $O_user->S_LAST_SEEN = $S_LAST_SEEN; + $O_user->S_FIRST_SEEN = $S_FIRST_SEEN; + $O_user->B_ADMIN = $B_ADMIN; + $O_user->B_DISABLED = $B_DISABLED; + + return $O_user; } + + public static function createEmpty(){ + $O_user = new UserModel(); + } + + public static function getAnonUser(){ + if(self::$O_ANONUSER === null) { + self::$O_ANONUSER = new UserModel(); + self::$O_ANONUSER->I_ID = 0; + self::$O_ANONUSER->S_EMAIL = "anonymous_user@example.fr"; + self::$O_ANONUSER->S_USERNAME = "Anonymous user"; + } + return self::$O_ANONUSER; + } + public function insert(){ $O_model = Model::get(); - $stmt = $O_model->prepare("INSERT INTO USER (EMAIL, USERNAME, PASS_HASH, FIRST_SEEN) VALUES(:email, :username, :password_hash, :first_seen)"); + $stmt = $O_model->prepare("INSERT INTO USER (EMAIL, USERNAME, PASS_HASH, FIRST_SEEN, LAST_SEEN) VALUES(:email, :username, :password_hash, :first_seen, :last_seen)"); $stmt->bindParam("email", $this->S_EMAIL); $stmt->bindParam("username", $this->S_USERNAME); $stmt->bindParam("password_hash", $this->S_PASSWORD_HASH); + $stmt->bindParam("last_seen", $this->S_LAST_SEEN); $stmt->bindParam("first_seen", $this->S_FIRST_SEEN); $stmt->execute(); $this->I_ID = Model::get()->lastInsertId(); @@ -67,7 +90,7 @@ final class UserModel extends UserSessionModel } private static function createFromRow($A_row, $I_ID){ - $O_user = new UserModel($A_row["EMAIL"],$A_row["USERNAME"],$A_row["PASS_HASH"],$A_row["LAST_SEEN"],$A_row["FIRST_SEEN"],$A_row["ADMIN"],$A_row["DISABLED"]); + $O_user = UserModel::createFull($A_row["EMAIL"],$A_row["USERNAME"],$A_row["PASS_HASH"],$A_row["LAST_SEEN"],$A_row["FIRST_SEEN"],$A_row["ADMIN"],$A_row["DISABLED"]); $O_user->I_ID = $I_ID; return $O_user; } diff --git a/Modules/Session/Session.php b/Modules/Session/Session.php index bf49959..f659897 100644 --- a/Modules/Session/Session.php +++ b/Modules/Session/Session.php @@ -60,7 +60,16 @@ final class Session public static function login_or_die() { if (!self::is_login()) { - header("Location: /user/login?return_uri=".$_SERVER["REQUEST_URI"]); + $S_uri = null; + + // special case: user probably got there from the account button + if (str_starts_with($_SERVER["REQUEST_URI"], "/user/") && isset($_SERVER["HTTP_REFERER"])) { + $S_uri = $_SERVER['HTTP_REFERER']; + } else { + $S_uri = $_SERVER["REQUEST_URI"]; + } + + header("Location: /user/login?return_uri=".$S_uri); throw new HTTPSpecialCaseException(403); } } diff --git a/Views/appreciations/view_all.php b/Views/appreciations/view_all.php index e77ecc6..0e75b83 100644 --- a/Views/appreciations/view_all.php +++ b/Views/appreciations/view_all.php @@ -1,29 +1,33 @@ -
-

Appréciations:

+
+

Appréciations:

- - - - - 0 - 1 - 2 - 3 - 4 - 5 + + +
+ + 0 + 1 + 2 + 3 + 4 + 5 +
getApprs() as $O_appr){ + View::show("appreciations/view_single", array( + "ADMIN" => $A_view["ADMIN"], + "USER_ID" => $A_view["USER_ID"], + "APPR" => $O_appr + )); } ?>
diff --git a/Views/appreciations/view_single.php b/Views/appreciations/view_single.php index 9705cdc..3a119ce 100644 --- a/Views/appreciations/view_single.php +++ b/Views/appreciations/view_single.php @@ -1,10 +1,21 @@ + +
- " alt="profile picture"> -

-

-

- Supprimer l'appréciation" : "" ?> + profile picture +

getAuthorOrAnon()->S_USERNAME ?>

+

Le S_DATE ?>

+

I_NOTE ?>/5

+ I_AUTHOR_ID && $O_appr->I_AUTHOR_ID !== null) + ); + if ($B_can_delete) { ?> + Supprimer l'appréciation +
-

+

S_COMMENT ?>

diff --git a/Views/category/view.php b/Views/category/view.php index b8ca07f..a796b92 100644 --- a/Views/category/view.php +++ b/Views/category/view.php @@ -1,12 +1,10 @@ "type_de_cuisson", - "Temps de préparation" => "temps_de_preparation", - "Difficulté" => "difficulte", "Végan" => "vegan", - "Sans gluten" => "sans_gluten", - "Sans lactose" => "sans_lactose" -); + "Végétarien" => "vegetarian", + "Sans gluten" => "glutenLess", + "Sans lactose" => "lactoseLess", + "Non Catégorisé" => "uncategorized"); ?>
diff --git a/Views/common/category_list.php b/Views/common/category_list.php index dcbf492..a7e376e 100644 --- a/Views/common/category_list.php +++ b/Views/common/category_list.php @@ -1,11 +1,10 @@ "type_de_cuisson", - "Temps de préparation" => "temps_de_preparation", - "Difficulté" => "difficulte", "Végan" => "vegan", - "Sans gluten" => "sans_gluten", - "Sans lactose" => "sans_lactose"); + "Végetérien" => "vegetarian", + "Sans gluten" => "glutenLess", + "Sans lactose" => "lactoseLess", + "Non Catégorisé" => "uncategorized"); ?>