projet_php/models/project_model.php
2026-02-10 08:42:18 +01:00

143 lines
No EOL
4.1 KiB
PHP

<?php
require_once('mother_model.php');
/**
* Traitement des requêtes pour les projets
* @author : Laura
*/
class ProjectModel extends Connect{
public function findAll(int $intLimit=0, string $strKeywords='', int $intAuthor=0,
int $intPeriod=0, string $strDate='', string $strStartDate='',
string $strEndDate='', int $intCategory=0):array{
// Ecrire la requête
$strRq = "SELECT project.*,
CONCAT(user_firstname, ' ', user_name) AS 'project_creatorname',
user_image
FROM project
INNER JOIN users ON user_id = project_user";
$strWhere = " WHERE ";
// Recherche par mot clé
if ($strKeywords != '') {
$strRq .= " WHERE (project_title LIKE '%".$strKeywords."%'
OR project_content LIKE '%".$strKeywords."%') ";
//$boolWhere = true;
$strWhere = " AND ";
}
// Recherche par auteur
if ($intAuthor > 0){
$strRq .= $strWhere." user_id = ".$intAuthor;
$strWhere = " AND ";
}
// Recherche par catégorie
if ($intCategory > 0){
$strRq .= $strWhere." project_category = ".$intCategory;
$strWhere = " AND ";
}
// Recherche par dates
if ($intPeriod == 0){
// Par date exacte
if ($strDate != ''){
$strRq .= $strWhere." project_creation_date = '".$strDate."'";
}
}else{
// Par période de dates
if ($strStartDate != '' && $strEndDate != ''){
$strRq .= $strWhere." project_creation_date BETWEEN '".$strStartDate."' AND '".$strEndDate."'";
}else{
if ($strStartDate != ''){
// A partir de
$strRq .= $strWhere." project_creation_date >= '".$strStartDate."'";
}else if ($strEndDate != ''){
// Avant le
$strRq .= $strWhere." project_creation_date <= '".$strEndDate."'";
}
}
}
$strRq .= " ORDER BY project_creation_date DESC";
if ($intLimit > 0){
$strRq .= " LIMIT ".$intLimit;
}
// Lancer la requête et récupérer les résultats
return $this->_db->query($strRq)->fetchAll();
}
//Fonction d'insertion d'information dans la BDD (Repris de la partie BLOG vu en cours..)
public function insert(object $objProject):bool{
//Construire la requête
$strRq = "INSERT INTO project (project_title, project_description, project_thumbnail, project_content, project_status, project_creation_date)
VALUES (:title, :description, :thumbnail, :content, :status, DATE(NOW()))";
// Préparer la requête
$rqPrep = $this->_db->prepare($strRq);
// Donne les informations
$rqPrep->bindValue(":title", $objProject->getTitle(), PDO::PARAM_STR);
$rqPrep->bindValue(":description", $objProject->getDescription(), PDO::PARAM_STR);
$rqPrep->bindValue(":thumbnail", $objProject->getThumbnail(), PDO::PARAM_STR);
$rqPrep->bindValue(":content", $objProject->getContent(), PDO::PARAM_STR);
$rqPrep->bindValue(":status", $objProject->getStatus(), PDO::PARAM_STR);
//Executer la requête
//var_dump($strRq);die;
//return $db->exec($strRq);
return $rqPrep->execute();
}
public function findOne(int $intId) {
$strRq = "SELECT project.*,
CONCAT(users.user_firstname, ' ', users.user_name) AS 'project_creatorname',
users.user_image,
category.category_name
FROM project
INNER JOIN users ON users.user_id = project.project_user
LEFT JOIN category ON category.category_id = project.project_category
WHERE project.project_id = :id";
$rqPrep = $this->_db->prepare($strRq);
$rqPrep->bindValue(":id", $intId, PDO::PARAM_INT);
$rqPrep->execute();
return $rqPrep->fetch();
}
public function accept(int $id){
//SQL pour changer le status en accept
$strRq = "UPDATE project
SET project_status= 'publié'
WHERE project_id =".$id;
//retourne la commande
return $this->_db->query($strRq);
}
public function refuse(int $id){
$strRq = "UPDATE project
SET project_status= 'refusé'
WHERE project_id =".$id;
return $this->_db->query($strRq);
}
public function delete(int $id){
$strRq = "DELETE FROM project
WHERE project_id =".$id;
return $this->_db->query($strRq);
}
}