Copie propre du code final de Folliow (projet d'équipe DWWM)

This commit is contained in:
Yasser Boulayoune 2026-03-06 18:00:00 +01:00
commit eed64cfeb6
67 changed files with 17897 additions and 0 deletions

0
models/.gitkeep Normal file
View file

View file

@ -0,0 +1,30 @@
<?php
namespace Models;
use \PDO;
/**
* Traitement des requêtes pour le status de l'utilisateur
* @author : Laura
*/
class AuthorisationModel extends MotherModel{
public function __construct(){
parent::__construct();
}
/**
* fonction de récupération des infos d'authorisation
* @return array
*/
public function findAllAuthorisation():array{
$strRq = "SELECT *
FROM authorisation";
return $this->_db->query($strRq)->fetchAll();
}
}

93
models/CategoryModel.php Normal file
View file

@ -0,0 +1,93 @@
<?php
namespace Models;
use \PDO;
/**
* Traitement des requêtes pour les catégories
* @author : Laura
*/
class CategoryModel extends MotherModel{
/**
* Fonction de récupération des catégories
* @param int $intLimit
* @return array
*/
public function findAllCategory(int $intLimit=0):array{
$strRq = "SELECT category.*
FROM category";
if ($intLimit > 0){
$strRq .= " LIMIT :limit";
}
$rqPrep = $this->_db->prepare($strRq);
if ($intLimit > 0){
$rqPrep->bindValue(":limit", $intLimit, PDO::PARAM_INT);
}
$rqPrep->execute();
return $rqPrep->fetchAll();
}
/**
* fonction d'insertion d'une nouvelle catégorie dans la bdd
* @param object $objCategory l'objet catégorie
* @return bool Est-ce que la requête s'est bien passée
*/
public function insertCategory(object $objCategory):bool{
$strRq = "INSERT INTO category (category_name)
VALUES (:name)";
$rqPrep = $this->_db->prepare($strRq);
$rqPrep->bindValue(":name", $objCategory->getName(), PDO::PARAM_STR);
return $rqPrep->execute();
}
/**
* fonction de suppression d'une catégorie dans la bdd
* @param object $objCategory l'objet catégorie
* @return bool Est-ce que la requête s'est bien passée
*/
public function deleteCategory(object $objCategory):bool{
$strRq = "DELETE FROM category
WHERE category_id= :id";
$rqPrep = $this->_db->prepare($strRq);
$rqPrep->bindValue(":id", $objCategory->getId(), PDO::PARAM_INT);
return $rqPrep->execute();
}
/**
* fonction de modification d'une catégorie dans la bdd
* @param object $objCategory l'objet catégorie
* @return bool Est-ce que la requête s'est bien passée
*/
public function editCategory(object $objCategory):bool{
$strRq = "UPDATE category
SET category_name = :name
WHERE category_id = :id";
$rqPrep = $this->_db->prepare($strRq);
$rqPrep->bindValue(":id", $objCategory->getId(), PDO::PARAM_INT);
$rqPrep->bindValue(":name", $objCategory->getName(), PDO::PARAM_STR);
return $rqPrep->execute();
}
}

39
models/ImageModel.php Normal file
View file

@ -0,0 +1,39 @@
<?php
namespace Models;
use \PDO;
/**
* Traitement de la requête pour les images
* @author Laura
*/
class ImageModel extends MotherModel{
/**
* Fonction de récupération des images
* @param int $intLimit
* @return array
*/
public function findAllImage(int $intLimit=0):array{
$strRq = "SELECT image.*
FROM image";
if ($intLimit > 0){
$strRq .= " LIMIT :limit";
}
$rqPrep = $this->_db->prepare($strRq);
if ($intLimit > 0){
$rqPrep->bindValue(":limit", $intLimit, PDO::PARAM_INT);
}
$rqPrep->execute();
return $rqPrep->fetchAll();
}
}

24
models/MotherModel.php Normal file
View file

@ -0,0 +1,24 @@
<?php
namespace Models;
use \PDO;
class MotherModel {
protected $_db;
public function __construct(){
try{
$this->_db = new PDO(
"mysql:host=".$_ENV['DB_HOSTNAME'].";dbname=".$_ENV['DB_DATABASE'],
$_ENV['DB_USERNAME'],
$_ENV['DB_PASSWORD'],
array(PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC)
);
$this->_db->exec("SET CHARACTER SET utf8");
$this->_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException$e) {
echo "Échec : " . $e->getMessage();
}
}
}

320
models/ProjectModel.php Normal file
View file

@ -0,0 +1,320 @@
<?php
namespace Models;
use \PDO;
/**
* Traitement des requêtes pour les projets
* @author Laura & Guillaume
*/
class ProjectModel extends MotherModel{
/**
* 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
WHERE 1=1";
if ($strKeywords != '') {
$strRq .= " AND (project_title LIKE :keywords OR project_content LIKE :keywords)";
}
if ($intAuthor > 0){
$strRq .= " AND project_user_id = :author";
}
if ($intCategory > 0){
$strRq .= " AND project_category = :category";
}
if ($boolOlderThan6Months === true) {
$strRq .= " AND project_creation_date <= DATE_SUB(NOW(), INTERVAL 6 MONTH)";
}
if ($intPeriod == 0){
if ($strDate != ''){
$strRq .= " AND project_creation_date = :date_exacte";
}
} else {
if ($strStartDate != '' && $strEndDate != ''){
$strRq .= " AND project_creation_date BETWEEN :date_debut AND :date_fin";
} else {
if ($strStartDate != ''){
$strRq .= " AND project_creation_date >= :date_debut";
} else if ($strEndDate != ''){
$strRq .= " AND project_creation_date <= :date_fin";
}
}
}
$strRq .= " ORDER BY project_creation_date DESC";
if ($intLimit > 0){
$strRq .= " LIMIT :limit";
}
$rqPrep = $this->_db->prepare($strRq);
if ($strKeywords != '') {
$rqPrep->bindValue(':keywords', '%' . $strKeywords . '%', PDO::PARAM_STR);
}
if ($intAuthor > 0){
$rqPrep->bindValue(':author', $intAuthor, PDO::PARAM_INT);
}
if ($intCategory > 0){
$rqPrep->bindValue(':category', $intCategory, PDO::PARAM_INT);
}
if ($intPeriod == 0){
if ($strDate != ''){
$rqPrep->bindValue(':date_exacte', $strDate, PDO::PARAM_STR);
}
} else {
if ($strStartDate != '' && $strEndDate != ''){
$rqPrep->bindValue(':date_debut', $strStartDate, PDO::PARAM_STR);
$rqPrep->bindValue(':date_fin', $strEndDate, PDO::PARAM_STR);
} else {
if ($strStartDate != ''){
$rqPrep->bindValue(':date_debut', $strStartDate, PDO::PARAM_STR);
} else if ($strEndDate != ''){
$rqPrep->bindValue(':date_fin', $strEndDate, PDO::PARAM_STR);
}
}
}
if ($intLimit > 0){
$rqPrep->bindValue(':limit', $intLimit, PDO::PARAM_INT);
}
$rqPrep->execute();
return $rqPrep->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);
$boolOk = $rqPrep->execute();
if ($boolOk) {
$objProject->setId($this->_db->lastInsertId());
}
return $boolOk;
}
/**
* Fonction de recherche d'un seul projet
* @param int $intId
* @return array|bool
*/
public function findOne(int $intId) :array|bool{
$strRq = "SELECT project.*,
users.user_pseudo AS 'project_creatorname',
users.user_image,
users.user_mail,
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(),
project_status = 'supprimé'
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);
$rqPrep->bindValue(":status", "en_attente", PDO::PARAM_STR);
$rqPrep->bindValue(":project", $projectId, PDO::PARAM_INT);
return $rqPrep->execute();
}
}

262
models/UserModel.php Normal file
View file

@ -0,0 +1,262 @@
<?php
namespace Models;
use \PDO;
/**
* Traitement des requêtes pour les utilisateurs
* @author : Yasser, Guillaume & Besnik
*/
class UserModel extends MotherModel{
public function __construct(){
parent::__construct();
}
/**
* Fonction de recherche des utilisateurs et leur niveau d'autorisation
* @return array
*/
public function findAllUsers():array{
$strRq = "SELECT user_id, user_firstname, user_name, user_image, user_status, authorisation_name, user_pseudo
FROM users INNER JOIN authorisation ON authorisation.authorisation_id = users.user_status
WHERE user_deleted_at IS NULL";
return $this->_db->query($strRq)->fetchAll();
}
/**
* Fonction de vérification des utilisateurs
* @param string $strMail
* @param string $strPwd
* @return array|bool
*/
public function verifUser(string $strMail, string $strPwd):array|bool{
$strRq = "SELECT user_id, user_name, user_firstname, user_password, user_image, user_status, authorisation_name, user_pseudo
FROM users INNER JOIN authorisation ON authorisation.authorisation_id = users.user_status
WHERE user_mail = :mail";
$rqPrep = $this->_db->prepare($strRq);
$rqPrep->bindValue(":mail", $strMail, PDO::PARAM_STR);
$rqPrep->execute();
$arrUser = $rqPrep->fetch();
if ($arrUser === false) {
return false;
}
if (empty($arrUser['user_password'])) {
return false;
}
if (!password_verify($strPwd, $arrUser['user_password'])) {
return false;
}
unset($arrUser['user_password']);
return $arrUser;
}
/**
* Fonction d'insertion d'un utilisateur en BDD
* @param object $objUser L'objet utilisateur
* @return bool Est-ce que la requête s'est bien passée
*/
public function insert(object $objUser):bool{
$strRq = "INSERT INTO users (user_name, user_firstname, user_pseudo, user_mail, user_password, user_phone, user_work, user_location, user_description)
VALUES (:name, :firstname, :pseudo,:mail, :pwd, :phone, :work, :location,:description)";
$rqPrep = $this->_db->prepare($strRq);
$rqPrep->bindValue(":name", $objUser->getName(), PDO::PARAM_STR);
$rqPrep->bindValue(":firstname", $objUser->getFirstname(), PDO::PARAM_STR);
$rqPrep->bindValue(":pseudo", $objUser->getPseudo(), PDO::PARAM_STR);
$rqPrep->bindValue(":mail", $objUser->getMail(), PDO::PARAM_STR);
$rqPrep->bindValue(":pwd", $objUser->getPwdHash(), PDO::PARAM_STR);
$rqPrep->bindValue(':phone', $objUser->getPhone() ?? "", PDO::PARAM_STR);
$rqPrep->bindValue(':work', $objUser->getWork() ?? "", PDO::PARAM_STR);
$rqPrep->bindValue(':location', $objUser->getLocation() ?? "", PDO::PARAM_STR);
$rqPrep->bindValue(':description', $objUser->getDescription() ?? "", PDO::PARAM_STR);
return $rqPrep->execute();
}
/**
* Méthode pour sauvegarder l'utilisateur lorsqu'il veut être souvenue
* @param int $userId l'id de l'utilisateur a se rappeler
* @param string $token Token hashé unique lié à l'utilisateur à se rappeler
* @return bool Est-ce que la requête s'est bien passée
*/
public function remember(int $userId, string $token):bool{
$strRq = "INSERT INTO tokens (token_user_id, token_hash, token_created_at, token_expire_at) VALUES (:id, :token, NOW(), :exp)";
$rqPrep = $this->_db->prepare($strRq);
$rqPrep->bindValue(":id", $userId, PDO::PARAM_INT);
$rqPrep->bindValue(":token", $token, PDO::PARAM_STR);
$rqPrep->bindValue(":exp",
date('Y-m-d H:i:s', time() + (15*24*60*60))
, PDO::PARAM_STR);
return $rqPrep->execute();
}
/**
* Méthode pour récupperer l'utilisateur par rapport au cookie enregistrer
* @param string $hash Token hashé unique lié à l'utilisateur
* @return bool Est-ce que la requête s'est bien passée
*/
public function getTokenUser(string $hash){
$strRq = $this->_db->prepare("SELECT token_user_id FROM tokens WHERE token_hash = :hash AND token_expire_at > NOW()");
$strRq->execute(['hash' => $hash]);
return $strRq->fetch();
}
/**
* Méthode pour supprimer le token lors de la déconnexion
* @param string $hash Token hashé unique lié à l'utilisateur
* @return bool Est-ce que la requête s'est bien passée
*/
public function deleteToken(string $hash){
$strRq = $this->_db->prepare("DELETE FROM tokens WHERE token_hash = :hash");
return $strRq->execute(['hash' => $hash]);
}
/**
* Méthode pour mettre a jour l'utilisateur
* @param object $objUser L'objet user a mettre à jour
* @return bool Est-ce que la requête s'est bien passée
*/
public function update(object $objUser):bool{
$strRq = "UPDATE users SET
user_name = :name,
user_firstname = :firstname,
user_pseudo = :pseudo,
user_mail = :mail,
user_phone = :phone,
user_work = :work,
user_location = :location,
user_description = :description,
user_image = :image
WHERE user_id = :id";
$rqPrep = $this->_db->prepare($strRq);
$rqPrep->bindValue(":id", $objUser->getId(), PDO::PARAM_INT);
$rqPrep->bindValue(":name", $objUser->getName(), PDO::PARAM_STR);
$rqPrep->bindValue(":firstname", $objUser->getFirstname(), PDO::PARAM_STR);
$rqPrep->bindValue(":pseudo", $objUser->getPseudo(), PDO::PARAM_STR);
$rqPrep->bindValue(":mail", $objUser->getMail(), PDO::PARAM_STR);
$rqPrep->bindValue(':phone', $objUser->getPhone() ?? "", PDO::PARAM_STR);
$rqPrep->bindValue(':work', $objUser->getWork() ?? "", PDO::PARAM_STR);
$rqPrep->bindValue(':location', $objUser->getLocation() ?? "", PDO::PARAM_STR);
$rqPrep->bindValue(':description', $objUser->getDescription() ?? "", PDO::PARAM_STR);
$rqPrep->bindValue(':image', $objUser->getImage() ?? "", PDO::PARAM_STR);
return $rqPrep->execute();
}
/**
* Fonction de vérification de mail
* @param string $mail
* @return bool Est-ce que la requête s'est bien passée
*/
public function mailExists(string $mail): bool{
$rq = $this->_db->prepare("SELECT 1 FROM users WHERE user_mail = :mail LIMIT 1");
$rq->bindValue(":mail", $mail);
$rq->execute();
return (bool)$rq->fetchColumn();
}
/**
* Fonction de changement de status d'un utilisateur
* @param object $objUser L'objet utilisateur
* @return bool Est-ce que la requête s'est bien passée
*/
public function editStatus(object $objUser):bool{
$strRq = "UPDATE users
SET user_status = :status
WHERE user_id = :id";
$rqPrep = $this->_db->prepare($strRq);
$rqPrep->bindValue(":id", $objUser->getId(), PDO::PARAM_INT);
$rqPrep->bindValue(":status", $objUser->getStatus(), PDO::PARAM_INT);
return $rqPrep->execute();
}
/**
* Fonction permettant de supprimer un utilisateur avec une date de suppression
* @param int $intId L'identifiant de l'utilisateur
* @return bool Est-ce que la requête s'est bien passée
*/
public function delete_soft(int $intId):bool{
$strRq = "UPDATE users
SET user_deleted_at = NOW()
WHERE user_id = :id";
$rqPrep = $this->_db->prepare($strRq);
$rqPrep->bindValue(":id", $intId, PDO::PARAM_INT);
return $rqPrep->execute();
}
/**
* Récupère les informations d'un utilisateur par son ID
* @param int $intId L'identifiant de l'utilisateur
* @return array Tableau associatif (ou false si pas trouvé)
*/
public function findUserById(int $intId): array|bool {
$strRq = "SELECT user_id,user_status ,user_image ,user_name, user_firstname, user_pseudo, user_mail, user_phone, user_work, user_location, user_description, authorisation_name
FROM users INNER JOIN authorisation ON authorisation.authorisation_id = users.user_status
WHERE user_id = :id";
$prep = $this->_db->prepare($strRq);
$prep->bindValue(':id', $intId, PDO::PARAM_INT);
$prep->execute();
return $prep->fetch();
}
/**
* Récupère les informations d'un utilisateur par son ID
* @param string $strPseudo Pseudo de l'utilisateur
* @return array Tableau associatif (ou false si pas trouvé)
*/
public function findUserByPseudo(string $strPseudo): array|bool {
$strRq = "SELECT user_id,user_image, user_status ,user_name, user_firstname, user_pseudo, user_mail, user_phone, user_work, user_location, user_description, authorisation_name
FROM users INNER JOIN authorisation ON authorisation.authorisation_id = users.user_status
WHERE user_pseudo = :pseudo";
$prep = $this->_db->prepare($strRq);
$prep->bindValue(':pseudo', $strPseudo, PDO::PARAM_STR);
$prep->execute();
return $prep->fetch();
}
/**
* Verifie sur le pseudo entré n'est pas déjà utilisé
* @param string $pseudo Pseudo a verifié
* @return bool Le pseudo existe ou pas
*/
public function pseudoExists(string $pseudo): bool{
$rq = $this->_db->prepare("SELECT 1 FROM users WHERE user_pseudo = :pseudo LIMIT 1");
$rq->bindValue(":pseudo", $pseudo, PDO::PARAM_STR);
$rq->execute();
return (bool)$rq->fetchColumn();
}
}