Skip to content
Snippets Groups Projects
Commit 19d10f39 authored by Sofiane Lasri's avatar Sofiane Lasri
Browse files

ajout de la classe Recette

parent 57d2e172
No related branches found
No related tags found
No related merge requests found
<?php
class Recette{
// Recettes
public static function getRecettes($search=""){
// C'est gitcopilot qui écrit 75% de cette fonction
// On va récupérer les recettes selon la recherche
// Si $search est une liste, on va chercher selon son contenu
// $search["categoryId"], $search["name"], ["ingredientId"], ["difficulte"], ["time"], ["auteurId"]
if(is_array($search) && !empty($search)){
// https://stackoverflow.com/a/18603279
$categoryId = $search["categoryId"] ?? "";
$name = $search["name"] ?? "";
$ingredients = $search["ingredients"] ?? "";
$difficulte = $search["difficulte"] ?? "";
$tempsPreparation = $search["tempsPreparation"] ?? "";
$auteurId = $search["auteurId"] ?? "";
// On construit la requête
$queryString = "SELECT * FROM m_recette INNER JOIN m_recetteIngredient ON m_recette.id=m_recetteIngredient.recetteId WHERE 1=1";
if(!empty($categoryId)){
$queryString .= " AND categoryId=:categoryId";
}
if(!empty($name)){
$queryString .= " AND nom LIKE :name";
}
if(!empty($ingredients)){
$ingredientsIn = implode(',', $ingredients);
$queryString .= " AND ingredientId IN (:ingredients)";
}
if(!empty($difficulte)){
$queryString .= " AND difficulte=:difficulte";
}
if(!empty($tempsPreparation)){
$queryString .= " AND tempsPreparation=:tempsPreparation";
}
if(!empty($auteurId)){
$queryString .= " AND auteurId=:auteurId";
}
// On la prépare
$query = Connexion::pdo()->prepare($queryString." ORDER BY nom");
// On rempli les paramètres
if(!empty($categoryId)){
$query->bindParam(':categoryId', $categoryId);
}
if(!empty($name)){
$name = "%".$name."%";
$query->bindParam(':name', $name);
}
if(!empty($ingredientId)){
$query->bindParam(':ingredients', $ingredientsIn);
}
if(!empty($difficulte)){
$query->bindParam(':difficulte', $difficulte);
}
if(!empty($tempsPreparation)){
$query->bindParam(':tempsPreparation', $tempsPreparation);
}
if(!empty($auteurId)){
$query->bindParam(':auteurId', $auteurId);
}
// On exécute
$query->execute();
// Et on retourne le résultat
return $query->fetchAll(PDO::FETCH_ASSOC);
}else{
// Si $search n'est pas un array, on va chercher toutes les recettes
$query = Connexion::pdo()->prepare("SELECT * FROM m_recette ORDER BY nom");
$query->execute();
return $query->fetchAll(PDO::FETCH_ASSOC);
}
}
public static function getRecette($recetteId){
$query = Connexion::pdo()->prepare("SELECT * FROM m_recette WHERE id=?");
$query->execute([$recetteId]);
return $query->fetch(PDO::FETCH_ASSOC);
}
// Récupréation diverses infos relatives aux recettes
public static function getCategories(){
$query = Connexion::pdo()->prepare("SELECT * FROM m_categorie ORDER BY nom");
$query->execute();
return $query->fetchAll(PDO::FETCH_ASSOC);
}
public static function getUstensiles(){
$query = Connexion::pdo()->prepare("SELECT * FROM m_ustensile ORDER BY nom");
$query->execute();
return $query->fetchAll(PDO::FETCH_ASSOC);
}
public static function getIngredients($recetteId=""){
if(empty($recetteId)){
$query = Connexion::pdo()->prepare("SELECT * FROM m_ingredient ORDER BY nom");
$query->execute();
return $query->fetchAll(PDO::FETCH_ASSOC);
}else{
$query = Connexion::pdo()->prepare("SELECT * FROM m_ingredient WHERE id IN (SELECT ingredientId FROM m_recetteIngredient WHERE recetteId=:recetteId) ORDER BY nom");
$query->bindParam(':recetteId', $recetteId);
$query->execute();
return $query->fetchAll(PDO::FETCH_ASSOC);
}
}
// Envoyer une recette
public static function sendRecette($recetteTitle, $recetteContent, $recetteDescription, $recetteCategory, $recetteIngredients, $recettePreparation, $recetteUstensiles, $recetteHeaderPic, $recetteDifficulte){
$recetteTitle = utf8_encode(htmlspecialchars($recetteTitle));
$recetteContent = utf8_encode(htmlspecialchars($recetteContent));
$recetteDescription = utf8_encode(htmlspecialchars($recetteDescription));
$quantite = 1; // Je suis un boulet j'ai oublié ça xD
// On insert la recette
$query = Connexion::pdo()->prepare("INSERT INTO m_recette (id, categoryId, auteurId, nom, description, contenu, image, tempsPreparation, difficulte, datePost, dateModif) VALUES (NULL, :categoryId, :auteurId, :nom, :description, :contenu, :image, :tempsPreparation, :difficulte, NOW(), NOW())");
$query->bindParam(':categoryId', $recetteCategory);
$query->bindParam(':auteurId', $_SESSION["userId"]);
$query->bindParam(':nom', $recetteTitle);
$query->bindParam(':description', $recetteDescription);
$query->bindParam(':contenu', $recetteContent);
$query->bindParam(':image', $recetteHeaderPic);
$query->bindParam(':tempsPreparation', $recettePreparation);
$query->bindParam(':difficulte', $recetteDifficulte);
$query->execute();
// Puis on répertorie les ingrédients
$recetteId = Connexion::pdo()->lastInsertId();
foreach($recetteIngredients as $ingredient){
$query = Connexion::pdo()->prepare("INSERT INTO m_recetteIngredient (recetteId, ingredientId, quantite) VALUES (:recetteId, :ingredientId, :quantite)");
$query->bindParam(':recetteId', $recetteId);
$query->bindParam(':ingredientId', $ingredient);
$query->bindParam(':quantite', $quantite);
$query->execute();
}
// Et on termine par les ustensiles
foreach($recetteUstensiles as $ustensile){
$query = Connexion::pdo()->prepare("INSERT INTO m_recetteUstensile (recetteId, ustensileId) VALUES (:recetteId, :ustensileId)");
$query->bindParam(':recetteId', $recetteId);
$query->bindParam(':ustensileId', $ustensile);
$query->execute();
}
// On retourne l'id de la recette
return $recetteId;
}
}
\ No newline at end of file
......@@ -101,7 +101,7 @@ if(isset($_GET["checkUsernameEmail"]) && !empty($_GET["checkUsernameEmail"])){
}elseif(isset($_GET["closeTopBarInfos"])){
// Cette fonction va fermer la barre d'information en haut de la page
setcookie("topBarInfos", "false", time()+getWebsiteSetting("cookieDuration"));
}elseif(isset($_GET["sendRecette"])){
}elseif(isset($_GET["Recette::sendRecette"])){
$return = null;
if(empty($_POST)){
// Ici on a pas reçu de données, nous ne sommes pas censsé arriver ici
......
......@@ -206,84 +206,6 @@ function registerUser($username, $password, $email){
return $return;
}
// Recettes
function getRecettes($search=""){
// C'est gitcopilot qui écrit 75% de cette fonction
// On va récupérer les recettes selon la recherche
// Si $search est une liste, on va chercher selon son contenu
// $search["categoryId"], $search["name"], ["ingredientId"], ["difficulte"], ["time"], ["auteurId"]
if(is_array($search) && !empty($search)){
// https://stackoverflow.com/a/18603279
$categoryId = $search["categoryId"] ?? "";
$name = $search["name"] ?? "";
$ingredients = $search["ingredients"] ?? "";
$difficulte = $search["difficulte"] ?? "";
$tempsPreparation = $search["tempsPreparation"] ?? "";
$auteurId = $search["auteurId"] ?? "";
// On construit la requête
$queryString = "SELECT * FROM m_recette INNER JOIN m_recetteIngredient ON m_recette.id=m_recetteIngredient.recetteId WHERE 1=1";
if(!empty($categoryId)){
$queryString .= " AND categoryId=:categoryId";
}
if(!empty($name)){
$queryString .= " AND nom LIKE :name";
}
if(!empty($ingredients)){
$ingredientsIn = implode(',', $ingredients);
$queryString .= " AND ingredientId IN (:ingredients)";
}
if(!empty($difficulte)){
$queryString .= " AND difficulte=:difficulte";
}
if(!empty($tempsPreparation)){
$queryString .= " AND tempsPreparation=:tempsPreparation";
}
if(!empty($auteurId)){
$queryString .= " AND auteurId=:auteurId";
}
// On la prépare
$query = Connexion::pdo()->prepare($queryString." ORDER BY nom");
// On rempli les paramètres
if(!empty($categoryId)){
$query->bindParam(':categoryId', $categoryId);
}
if(!empty($name)){
$name = "%".$name."%";
$query->bindParam(':name', $name);
}
if(!empty($ingredientId)){
$query->bindParam(':ingredients', $ingredientsIn);
}
if(!empty($difficulte)){
$query->bindParam(':difficulte', $difficulte);
}
if(!empty($tempsPreparation)){
$query->bindParam(':tempsPreparation', $tempsPreparation);
}
if(!empty($auteurId)){
$query->bindParam(':auteurId', $auteurId);
}
// On exécute
$query->execute();
// Et on retourne le résultat
return $query->fetchAll(PDO::FETCH_ASSOC);
}else{
// Si $search n'est pas un array, on va chercher toutes les recettes
$query = Connexion::pdo()->prepare("SELECT * FROM m_recette ORDER BY nom");
$query->execute();
return $query->fetchAll(PDO::FETCH_ASSOC);
}
}
function getRecette($recetteId){
$query = Connexion::pdo()->prepare("SELECT * FROM m_recette WHERE id=?");
$query->execute([$recetteId]);
return $query->fetch(PDO::FETCH_ASSOC);
}
// Vérifie les permissions
function verifyUserPermission($userId, $permission){
// $permission peut être un tableau ou un string
......@@ -416,66 +338,3 @@ function isConnected(){
}
}
// Récupréation diverses infos relatives aux recettes
function getCategories(){
$query = Connexion::pdo()->prepare("SELECT * FROM m_categorie ORDER BY nom");
$query->execute();
return $query->fetchAll(PDO::FETCH_ASSOC);
}
function getUstensiles(){
$query = Connexion::pdo()->prepare("SELECT * FROM m_ustensile ORDER BY nom");
$query->execute();
return $query->fetchAll(PDO::FETCH_ASSOC);
}
function getIngredients($recetteId=""){
if(empty($recetteId)){
$query = Connexion::pdo()->prepare("SELECT * FROM m_ingredient ORDER BY nom");
$query->execute();
return $query->fetchAll(PDO::FETCH_ASSOC);
}else{
$query = Connexion::pdo()->prepare("SELECT * FROM m_ingredient WHERE id IN (SELECT ingredientId FROM m_recetteIngredient WHERE recetteId=:recetteId) ORDER BY nom");
$query->bindParam(':recetteId', $recetteId);
$query->execute();
return $query->fetchAll(PDO::FETCH_ASSOC);
}
}
// Envoyer une recette
function sendRecette($recetteTitle, $recetteContent, $recetteDescription, $recetteCategory, $recetteIngredients, $recettePreparation, $recetteUstensiles, $recetteHeaderPic, $recetteDifficulte){
$recetteTitle = utf8_encode(htmlspecialchars($recetteTitle));
$recetteContent = utf8_encode(htmlspecialchars($recetteContent));
$recetteDescription = utf8_encode(htmlspecialchars($recetteDescription));
$quantite = 1; // Je suis un boulet j'ai oublié ça xD
// On insert la recette
$query = Connexion::pdo()->prepare("INSERT INTO m_recette (id, categoryId, auteurId, nom, description, contenu, image, tempsPreparation, difficulte, datePost, dateModif) VALUES (NULL, :categoryId, :auteurId, :nom, :description, :contenu, :image, :tempsPreparation, :difficulte, NOW(), NOW())");
$query->bindParam(':categoryId', $recetteCategory);
$query->bindParam(':auteurId', $_SESSION["userId"]);
$query->bindParam(':nom', $recetteTitle);
$query->bindParam(':description', $recetteDescription);
$query->bindParam(':contenu', $recetteContent);
$query->bindParam(':image', $recetteHeaderPic);
$query->bindParam(':tempsPreparation', $recettePreparation);
$query->bindParam(':difficulte', $recetteDifficulte);
$query->execute();
// Puis on répertorie les ingrédients
$recetteId = Connexion::pdo()->lastInsertId();
foreach($recetteIngredients as $ingredient){
$query = Connexion::pdo()->prepare("INSERT INTO m_recetteIngredient (recetteId, ingredientId, quantite) VALUES (:recetteId, :ingredientId, :quantite)");
$query->bindParam(':recetteId', $recetteId);
$query->bindParam(':ingredientId', $ingredient);
$query->bindParam(':quantite', $quantite);
$query->execute();
}
// Et on termine par les ustensiles
foreach($recetteUstensiles as $ustensile){
$query = Connexion::pdo()->prepare("INSERT INTO m_recetteUstensile (recetteId, ustensileId) VALUES (:recetteId, :ustensileId)");
$query->bindParam(':recetteId', $recetteId);
$query->bindParam(':ustensileId', $ustensile);
$query->execute();
}
// On retourne l'id de la recette
return $recetteId;
}
\ No newline at end of file
......@@ -49,7 +49,7 @@
<div class="form-group">
<select multiple class="form-control" id="recetteIngredients">
<?php
$ingredients = getIngredients();
$ingredients = Recette::getIngredients();
foreach($ingredients as $ingredient) {
echo '<option value="'.$ingredient['id'].'">'.$ingredient['nom'].' - '.$ingredient['calories'].' cal</option>';
}
......@@ -66,7 +66,7 @@
<div class="form-group">
<select multiple class="form-control" id="recetteUstensiles">
<?php
$ustensiles = getUstensiles();
$ustensiles = Recette::getUstensiles();
foreach($ustensiles as $ustensile) {
echo '<option value="'.$ustensile['id'].'">'.$ustensile['nom'].'</option>';
}
......@@ -83,7 +83,7 @@
<div class="form-group">
<select class="form-control" id="recetteCategory">
<?php
$categories = getCategories();
$categories = Recette::getCategories();
foreach($categories as $category) {
echo '<option value="'.$category['id'].'">'.$category['nom'].'</option>';
}
......@@ -266,7 +266,7 @@
} else {
// On peut publier
$.ajax({
url: '<?=getWebsiteSetting("websiteUrl")?><?=genPageLink("backTasks")?>?sendRecette',
url: '<?=getWebsiteSetting("websiteUrl")?><?=genPageLink("backTasks")?>?Recette::sendRecette',
type: 'POST',
data: {
action: "publishRecette",
......
<?php
if(isset($_GET['recetteId']) && !empty($_GET['recetteId'])){
$recetteId=$_GET['recetteId'];
$recette=getRecette($recetteId);
$recette=Recette::getRecette($recetteId);
}
?>
<!DOCTYPE html>
......@@ -90,7 +90,7 @@ if(isset($_GET['recetteId']) && !empty($_GET['recetteId'])){
<div class="ingredients">
<h4>Ingredients</h4>
<?php
$ingredient=getIngredients($recetteId);
$ingredient=Recette::Recette::getIngredients($recetteId);
$i=0 ;
foreach($ingredient as $valeur){
$i++;
......
......@@ -49,7 +49,7 @@
if(isset($_GET["name"]) AND !empty($_GET["name"])){
$search["name"]=($_GET["name"]);
}
$recettes = getRecettes($search);
$recettes = Recette::getRecettes($search);
foreach($recettes as $recette){
$array["userId"] = $recette["auteurId"];
$utilisateur = getUtilisateur($array);
......
......@@ -53,7 +53,7 @@
<div class="row pb-3">
<?php
$search["difficulte"] = 1;
$recettes = getRecettes($search);
$recettes = Recette::getRecettes($search);
foreach($recettes as $recette){
$array["userId"] = $recette["auteurId"];
$utilisateur = getUtilisateur($array);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment