Select Git revision
Recette.php 6.89 KiB
<?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";
}
// Dernier ajout important
$queryString .= " GROUP BY m_recette.id";
// Having Count pour les ingredients
if(!empty($ingredients)){
$queryString .= " HAVING COUNT(m_recette.id)=".count($ingredients);
}
// 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($ingredients)){
$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);
}