301 lines
No EOL
9.3 KiB
PHP
301 lines
No EOL
9.3 KiB
PHP
<?php
|
|
require_once('mother_model.php');
|
|
|
|
|
|
/**
|
|
* Traitement des requêtes pour les projets
|
|
* @author : Laura
|
|
*/
|
|
|
|
class ProjectModel extends Connect{
|
|
|
|
|
|
/**
|
|
* Fonction de recherche des projets
|
|
* @param type string, int et bool
|
|
* @return array
|
|
*/
|
|
public function findAll(int $intLimit=0, string $strKeywords='', int $intAuthor=0,
|
|
int $intPeriod=0, string $strDate='', string $strStartDate='',
|
|
string $strEndDate='', int $intCategory=0, bool $boolOlderThan6Months=false): array {
|
|
|
|
|
|
$strRq = "SELECT project.*,
|
|
user_pseudo AS 'project_creatorname',
|
|
user_image
|
|
FROM project
|
|
INNER JOIN users ON user_id = project_user_id";
|
|
|
|
$strRq .= " WHERE project_deleted_at IS NULL";
|
|
|
|
$strAnd = " AND ";
|
|
|
|
// Recherche par mot clé avec quote pour éviter bug du '
|
|
if ($strKeywords != '') {
|
|
|
|
$strSafeKeywords = $this->_db->quote("%" . $strKeywords . "%");
|
|
|
|
$strRq .= $strAnd. " (project_title LIKE ".$strSafeKeywords."
|
|
OR project_content LIKE ".$strSafeKeywords.") ";
|
|
|
|
}
|
|
|
|
// Recherche par auteur
|
|
if ($intAuthor > 0){
|
|
$strRq .= $strAnd." user_id = ".$intAuthor;
|
|
|
|
}
|
|
|
|
// Recherche par catégorie
|
|
if ($intCategory > 0){
|
|
$strRq .= $strAnd." project_category = ".$intCategory;
|
|
}
|
|
|
|
//recherche par ancienneté
|
|
if ($boolOlderThan6Months === true) {
|
|
$strRq .= $strAnd . " project_creation_date <= DATE_SUB(NOW(), INTERVAL 6 MONTH) ";
|
|
}
|
|
|
|
// Recherche par dates
|
|
if ($intPeriod == 0){
|
|
if ($strDate != ''){
|
|
$strRq .= $strAnd." project_creation_date = '".$strDate."'";
|
|
}
|
|
}else{
|
|
if ($strStartDate != '' && $strEndDate != ''){
|
|
$strRq .= $strAnd." project_creation_date BETWEEN '".$strStartDate."' AND '".$strEndDate."'";
|
|
}else{
|
|
if ($strStartDate != ''){
|
|
$strRq .= $strAnd." project_creation_date >= '".$strStartDate."'";
|
|
}else if ($strEndDate != ''){
|
|
$strRq .= $strAnd." project_creation_date <= '".$strEndDate."'";
|
|
}
|
|
}
|
|
}
|
|
|
|
$strRq .= " ORDER BY project_creation_date DESC";
|
|
|
|
if ($intLimit > 0){
|
|
$strRq .= " LIMIT ".$intLimit;
|
|
}
|
|
|
|
return $this->_db->query($strRq)->fetchAll();
|
|
}
|
|
|
|
|
|
/**
|
|
* Fonction d'insertion d'un nouveau projet dans la bdd
|
|
* @param object $objProject l'objet projet
|
|
* @return bool Est-ce que la requête s'est bien passée
|
|
*/
|
|
public function insert(object $objProject):bool{
|
|
|
|
$strRq = "INSERT INTO project (project_title, project_description, project_thumbnail, project_content, project_status, project_creation_date, project_user_id, project_category)
|
|
VALUES (:title, :description, :thumbnail, :content, :status, DATE(NOW()), :project_user_id, :project_category)";
|
|
|
|
$rqPrep = $this->_db->prepare($strRq);
|
|
|
|
$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);
|
|
$rqPrep->bindValue(":project_user_id", $objProject->getUser_id(), PDO::PARAM_STR);
|
|
$rqPrep->bindValue(":project_category", $objProject->getCategory(), PDO::PARAM_STR);
|
|
|
|
// On met une variable boolOk pour récupérer l'id du projet
|
|
$boolOk = $rqPrep->execute();
|
|
|
|
// Si boolOk est remplis
|
|
if ($boolOk) {
|
|
// On récupère l'ID auto-incrémenté et on l'injecte dans l'objet
|
|
$objProject->setId($this->_db->lastInsertId());
|
|
}
|
|
|
|
return $boolOk;
|
|
}
|
|
|
|
/**
|
|
* Fonction de recherche d'un seul projet
|
|
* @param int $intId
|
|
* @return array
|
|
*/
|
|
public function findOne(int $intId) :array{
|
|
$strRq = "SELECT project.*,
|
|
users.user_pseudo AS 'project_creatorname',
|
|
users.user_image,
|
|
category.category_name
|
|
FROM project
|
|
INNER JOIN users ON users.user_id = project.project_user_id
|
|
LEFT JOIN category ON category.category_id = project.project_category
|
|
WHERE project.project_id = :id
|
|
AND project.project_deleted_at IS NULL";
|
|
|
|
$rqPrep = $this->_db->prepare($strRq);
|
|
$rqPrep->bindValue(":id", $intId, PDO::PARAM_INT);
|
|
$rqPrep->execute();
|
|
|
|
return $rqPrep->fetch();
|
|
}
|
|
|
|
/**
|
|
* Fonction de changement de status (accepter) d'un projet en BDD
|
|
* @param int $id l'id du projet
|
|
* @return bool Est-ce que la requête s'est bien passée
|
|
*/
|
|
public function accept(int $id){
|
|
|
|
$strRq = "UPDATE project
|
|
SET project_status = 'publié',
|
|
project_edit_date = NOW()
|
|
WHERE project_id =".$id;
|
|
|
|
return $this->_db->query($strRq);
|
|
}
|
|
|
|
/**
|
|
* Fonction de changement de status (refusé) d'un projet en BDD
|
|
* @param int $id l'id du projet
|
|
* @return bool Est-ce que la requête s'est bien passée
|
|
*/
|
|
public function refuse(int $id){
|
|
|
|
$strRq = "UPDATE project
|
|
SET project_status = 'refusé',
|
|
project_edit_date = NOW()
|
|
WHERE project_id =".$id;
|
|
|
|
return $this->_db->query($strRq);
|
|
}
|
|
|
|
/**
|
|
* Fonction de suppression d'un projet en BDD
|
|
* @param int $id l'id du projet
|
|
* @return bool Est-ce que la requête s'est bien passée
|
|
*/
|
|
public function delete_soft_project(int $intId): bool {
|
|
$strRq = "UPDATE project
|
|
SET project_deleted_at = NOW(),
|
|
project_edit_date = NOW()
|
|
WHERE project_id = :id";
|
|
|
|
$rqPrep = $this->_db->prepare($strRq);
|
|
$rqPrep->bindValue(":id", $intId, PDO::PARAM_INT);
|
|
|
|
return $rqPrep->execute();
|
|
}
|
|
|
|
/**
|
|
* Fonction de mise à jour d'un projet en BDD
|
|
* @param object $objProject L'objet utilisateur
|
|
* @return bool Est-ce que la requête s'est bien passée
|
|
*/
|
|
public function updateProject(object $objProject):bool{
|
|
|
|
$strRq = "UPDATE project
|
|
SET project_title = :title,
|
|
project_description = :description,
|
|
project_content = :content,
|
|
project_thumbnail = :thumbnail,
|
|
project_status = 'en_attente',
|
|
project_edit_date = NOW()
|
|
WHERE project_id = :id";
|
|
|
|
$rqPrep = $this->_db->prepare($strRq);
|
|
|
|
$rqPrep->bindValue(":title", $objProject->getTitle(), PDO::PARAM_STR);
|
|
$rqPrep->bindValue(":description", $objProject->getDescription(), PDO::PARAM_STR);
|
|
$rqPrep->bindValue(":content", $objProject->getContent(), PDO::PARAM_STR);
|
|
$rqPrep->bindValue(":thumbnail", $objProject->getThumbnail(), PDO::PARAM_STR);
|
|
$rqPrep->bindValue(":id", $objProject->getId(), PDO::PARAM_INT);
|
|
|
|
return $rqPrep->execute();
|
|
}
|
|
|
|
/**
|
|
* Fonction de récupération d'image d'un projet en BDD
|
|
* @param int $objProject L'Id du projet choisit
|
|
* @return array Un tableau avec les informations de la bdd
|
|
*/
|
|
public function getImagesByProjectId(int $projectId): array {
|
|
$strRq = "SELECT image_id, image_name, image_alt, image_status
|
|
FROM image
|
|
WHERE image_project = :id";
|
|
|
|
$rqPrep = $this->_db->prepare($strRq);
|
|
$rqPrep->bindValue(":id", $projectId, PDO::PARAM_INT);
|
|
$rqPrep->execute();
|
|
|
|
return $rqPrep->fetchAll(PDO::FETCH_ASSOC);
|
|
}
|
|
|
|
/**
|
|
* Fonction de récupération d'image d'un projet en BDD
|
|
* @param int $id L'Id de l'image choisit
|
|
* @return array Un tableau avec les informations de la bdd
|
|
*/
|
|
public function deleteImage(int $id): bool {
|
|
$strRq = "DELETE FROM image WHERE image_id = :id";
|
|
$rqPrep = $this->_db->prepare($strRq);
|
|
$rqPrep->bindValue(':id', $id, PDO::PARAM_INT);
|
|
return $rqPrep->execute();
|
|
}
|
|
|
|
/**
|
|
* Fonction de modifications de status de l'image d'un projet en BDD
|
|
* @param int $id L'Id de l'image choisit, string $status le status choisit
|
|
* @return array Un tableau avec les informations de la bdd
|
|
*/
|
|
public function updateImageStatus(int $id, string $status): bool {
|
|
$strRq = "UPDATE image
|
|
SET image_status = :status
|
|
WHERE image_id = :id";
|
|
$rqPrep = $this->_db->prepare($strRq);
|
|
$rqPrep->bindValue(':status', $status, PDO::PARAM_STR);
|
|
$rqPrep->bindValue(':id', $id, PDO::PARAM_INT);
|
|
return $rqPrep->execute();
|
|
}
|
|
|
|
/**
|
|
* Fonction de récupération d'image d'un projet en BDD
|
|
* @param int $id L'Id de l'image choisit
|
|
* @return array Un tableau avec les informations de la bdd
|
|
*/
|
|
public function findImage(int $id): array|bool {
|
|
$strRq = "SELECT * FROM image
|
|
WHERE image_id = :id";
|
|
$rqPrep = $this->_db->prepare($strRq);
|
|
$rqPrep->bindValue(':id', $id, PDO::PARAM_INT);
|
|
$rqPrep->execute();
|
|
return $rqPrep->fetch(PDO::FETCH_ASSOC);
|
|
}
|
|
|
|
|
|
/**
|
|
* Ajoute une image liée à un projet dans la table 'image'
|
|
* @param string $fileName Nom du fichier image
|
|
* @param int $projectId ID du projet parent
|
|
* @param string $alt Texte alternatif
|
|
* @return bool
|
|
*/
|
|
public function addImageInProject(string $fileName, int $projectId, string $alt = "Image projet"): bool {
|
|
$strRq = "INSERT INTO image (
|
|
image_name,
|
|
image_alt,
|
|
image_status,
|
|
image_project
|
|
)
|
|
VALUES (:name, :alt, :status, :project)";
|
|
|
|
$rqPrep = $this->_db->prepare($strRq);
|
|
|
|
$rqPrep->bindValue(":name", $fileName, PDO::PARAM_STR);
|
|
$rqPrep->bindValue(":alt", $alt, PDO::PARAM_STR);
|
|
// On met le statut par défaut en "en_attente" pour la modération
|
|
$rqPrep->bindValue(":status", "en_attente", PDO::PARAM_STR);
|
|
$rqPrep->bindValue(":project", $projectId, PDO::PARAM_INT);
|
|
|
|
return $rqPrep->execute();
|
|
}
|
|
|
|
} |