projet_php/controllers/project_controller.php
laura.chevillet 0918b215d1 minou
2026-02-25 15:52:43 +01:00

398 lines
No EOL
13 KiB
PHP

<?php
require("./models/project_model.php");
require("./entities/project_entity.php");
require("./models/category_model.php");
require("./entities/category_entity.php");
require("./models/image_model.php");
require("./entities/image_entity.php");
require("./models/user_model.php");
require("./entities/user_entity.php");
require("mother_controller.php");
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;
/**
* Le controler des Project
* @author Yasser, Laura, Besnik & Guillaume
*/
class ProjectCtrl extends MotherCtrl{
/**
* Fonction d'affichage de la page d'acceuil
*/
public function home(){
$intCategory = 0;
if (!empty($_GET['filter_cat'])) {
$intCategory = (int) $_GET['filter_cat'];
}
$boolOld = false;
if (!empty($_GET['filter_old']) && $_GET['filter_old'] == 'true') {
$boolOld = true;
}
$objProjectModel = new ProjectModel;
$arrProject = $objProjectModel->findAll(0,'',0,0,'','','',$intCategory,$boolOld);
$arrProjectToDisplay = array();
foreach($arrProject as $arrDetProject){
$objProject = new Project;
$objProject->hydrate($arrDetProject);
$arrProjectToDisplay[] = $objProject;
}
$this->_arrData['arrProjectToDisplay'] = $arrProjectToDisplay;
$this->_display("home");
}
/**
* Fonction d'affichage de la barre de recherche
*/
public function search(){
//Récupérer les informations du formulaire
$strKeywords = $_POST['keywords']??'';
$intAuthor = $_POST['author']??0;
$intPeriod = $_POST['period']??0;
$strDate = $_POST['date']??'';
$strStartDate = $_POST['startdate']??'';
$strEndDate = $_POST['enddate']??'';
$intCategory = $_POST['category']??0;
// Récupération des projets
$objProjectModel = new ProjectModel;
$arrProject = $objProjectModel->findAll(intAuthor:$intAuthor, intPeriod:$intPeriod, strDate:$strDate,
strKeywords:$strKeywords, strStartDate:$strStartDate, strEndDate:$strEndDate, intCategory:$intCategory);
$arrProjectToDisplay = array();
foreach($arrProject as $arrDetProject){
$objProject = new Project;
$objProject->hydrate($arrDetProject);
$arrProjectToDisplay[] = $objProject;
}
// Récupération des utilisateurs
$objUserModel = new UserModel;
$arrUser = $objUserModel->findAllUsers();
// Récupération des catégories
$objCategoryModel = new CategoryModel;
$arrCategory = $objCategoryModel->findAllCategory();
$this->_arrData['arrProjectToDisplay'] = $arrProjectToDisplay;
$this->_arrData['arrCategory'] = $arrCategory;
$this->_arrData['arrProject'] = $arrProject;
$this->_arrData['arrUser'] = $arrUser;
$this->_display("search");
}
/**
* Fonction d'affichage de la page projet
*/
public function addedit_project() {
if (!isset($_SESSION['user'])){
header("Location:index.php?ctrl=error&action=error_403");
exit;
}
$objProject = new Project;
$objProjectModel = new ProjectModel;
$objCategoryModel = new CategoryModel;
if (isset($_GET['id'])){
$arrProject = $objProjectModel->findOne($_GET['id']);
$objProject->hydrate($arrProject); // BDD
}
$arrError = [];
if (count($_POST) > 0) {
$objProject->hydrate($_POST);
if ($objProject->getTitle() == ""){
$arrError['title'] = "Le titre est obligatoire";
}
if ($objProject->getDescription() == ""){
$arrError['description'] = "La description est obligatoire";
}
if ($objProject->getContent() == ""){
$arrError['content'] = "Le contenu est obligatoire";
}
// Vérification de l'image
$arrTypeAllowed = array('image/jpeg', 'image/png', 'image/webp');
if ($_FILES['thumbnail']['error'] != 4){
if (!in_array($_FILES['thumbnail']['type'], $arrTypeAllowed)){
$arrError['thumbnail'] = "Le type de fichier n'est pas autorisé";
}else{
switch ($_FILES['thumbnail']['error']){
case 0 :
$strImageName = uniqid().".webp";
$strOldImg = $objProject->getThumbnail();
$objProject->setThumbnail($strImageName);
break;
case 1 :
case 2 :
$arrError['thumbnail'] = "Le fichier est trop volumineux";
break;
case 3 :
$arrError['thumbnail'] = "Le fichier a été partiellement téléchargé";
break;
case 6 :
$arrError['thumbnail'] = "Le répertoire temporaire est manquant";
break;
default :
$arrError['thumbnail'] = "Erreur sur l'image";
break;
}
}
}else{
// Est-ce que le fichier existe ?
if (is_null($objProject->getThumbnail())){
$arrError['thumbnail'] = "L'image est obligatoire";
}
}
// SI pas d'erreur : on traite l'image depuis la bdd
if (count($arrError) == 0){
$boolImageOk = true;
// Redimensionnement de l'image
if (isset($strImageName)){
$strDest = $_ENV['IMG_PATH'].$strImageName;
$strSource = $_FILES['thumbnail']['tmp_name'];
list($intWidth, $intHeight) = getimagesize($strSource);
$intDestWidth = 200; $intDestHeight = 250;
$fltDestRatio = $intDestWidth / $intDestHeight;
$fltSourceRatio = $intWidth / $intHeight;
if ($fltSourceRatio > $fltDestRatio) {
$intCropHeight = $intHeight;
$intCropWidth = (int)round($intHeight * $fltDestRatio);
$intCropX = (int)(($intWidth - $intCropWidth) / 2);
$intCropY = 0;
} else {
$intCropWidth = $intWidth;
$intCropHeight = (int)round($intWidth / $fltDestRatio);
$intCropX = 0;
$intCropY = (int)(($intHeight - $intCropHeight) / 2);
}
// Condition en fonction de l'extension de l'image
$objDest = imagecreatetruecolor($intDestWidth, $intDestHeight);
switch ($_FILES['thumbnail']['type']) {
case 'image/jpeg' :
$objSource = imagecreatefromjpeg($strSource);
break;
case 'image/png' :
$objSource = imagecreatefrompng($strSource);
break;
case 'image/webp' :
$objSource = imagecreatefromwebp($strSource);
break;
}
imagecopyresampled($objDest, $objSource, 0, 0, $intCropX, $intCropY, $intDestWidth, $intDestHeight, $intCropWidth, $intCropHeight);
// Sauvegarde du fichier
$boolImageOk = imagewebp($objDest, $strDest);
imagedestroy($objDest);
imagedestroy($objSource);
}
// SI image ok, on balance tout dans la bdd
if ($boolImageOk){
if (!isset($_GET['id'])){
$objProject->setUser_id($_SESSION['user']['user_id']);
$boolOk = $objProjectModel->insert($objProject);
} else {
$boolOk = $objProjectModel->updateProject($objProject);
}
if ($boolOk){
// Suppression de l'ancienne image
if(isset($strOldImg) && !empty($strOldImg) && isset($strImageName)){
$strOldFile = $_ENV['IMG_PATH'].$strOldImg;
if (file_exists($strOldFile)) unlink($strOldFile);
}
$_SESSION['success'] = (!isset($_GET['id'])) ? "Le projet a bien été créé" : "Le projet a bien été modifié";
header("Location:index.php");
exit;
} else {
$arrError[] = "Erreur lors de l'enregistrement en base de données";
}
} else {
$arrError['thumbnail'] = "Erreur dans le traitement de l'image";
}
}
}
// Données pour la vue
$this->_arrData['arrCategory'] = $objCategoryModel->findAllCategory();
$this->_arrData['objProject'] = $objProject;
$this->_arrData['arrError'] = $arrError;
$this->_display('addedit_project');
}
/**
* Fonction d'affichage d'un projet
*/
public function display() {
$intId = $_GET['id'] ?? null;
if ($intId) {
$objProjectModel = new ProjectModel();
$arrProject = $objProjectModel->findOne((int)$intId);
if ($arrProject) {
$objProject = new Project();
$objProject->hydrate($arrProject);
$this->_arrData["objProject"] = $objProject;
$this->_display("project_display");
} else {
header("Location: index.php?ctrl=project&action=home");
exit;
}
} else {
header("Location: index.php?ctrl=project&action=home");
exit;
}
}
/**
* Fonction d'envoi d'email
*/
public function sendEmail(){
if (count($_POST) > 0) {
$projectId = (int)($_POST['project_id'] ?? 0);
$toEmail = trim($_POST['to_email'] ?? '');
if ($projectId <= 0 || !filter_var($toEmail, FILTER_VALIDATE_EMAIL)) {
header("Location: index.php?ctrl=project&action=display&id=".$projectId."&mail=fail");
exit;
}
$objProjectModel = new ProjectModel();
$arrProject = $objProjectModel->findOne($projectId);
if (!$arrProject) {
header("Location: index.php?ctrl=project&action=home");
exit;
}
$objProject = new Project();
$objProject->hydrate($arrProject);
$objMail = new PHPMailer(); // Nouvel objet Mail
$objMail->IsSMTP();
$objMail->Mailer = "smtp";
$objMail->CharSet = PHPMailer::CHARSET_UTF8;
$objMail->SMTPDebug = 0;
$objMail->SMTPAuth = TRUE;
$objMail->SMTPSecure = "tls";
$objMail->Port = 587;
$objMail->Host = "smtp.gmail.com";
$objMail->Username = "projet.folliow@gmail.com";
$objMail->Password = "dqnw mqbu cwvg enbp";
$objMail->IsHTML(true);
$objMail->setFrom('projet.folliow@gmail.com', 'Projet Folliow');
// Destinataire
$objMail->addAddress($toEmail);
// Mail
$objMail->Subject = "Projet : " . $objProject->getTitle();
$url = "http://localhost/projet_php/public/index.php?ctrl=project&action=display&id=" . $projectId;
$objMail->Body =
"<h3>" . $objProject->getTitle() . "</h3>" .
"<p>" . $objProject->getDescription() . "</p>" .
"<p><a href='" . $url . "'>Voir le projet</a></p>";
// Envoi + redirection
if ($objMail->Send()) {
header("Location: index.php?ctrl=project&action=display&id=".$projectId."&mail=ok");
} else {
// Pour debug si besoin: echo $objMail->ErrorInfo; exit;
header("Location: index.php?ctrl=project&action=display&id=".$projectId."&mail=fail");
}
exit;
}
header("Location: index.php?ctrl=project&action=home");
exit;
}
/**
* Fonction d'acceptation d'un projet
*/
public function accept(){
//Récupéré l'id dans l'url
$intId = $_GET['id'];
//Je créer un nouveau model pour exec la commande SQL
$objProjectModel = new ProjectModel;
$objProjectModel->accept($intId);
//Redirection vers la page
header("Location: index.php");
exit;
}
public function refuse(){
//Récupéré l'id dans l'url
$intId = $_GET['id'];
//Je créer un nouveau model pour exec la commande SQL
$objProjectModel = new ProjectModel;
$objProjectModel->refuse($intId);
//Redirection vers la page
header("Location: index.php");
exit;
}
/**
* Fonction de suppression d'un projet
*/
public function delete(){
//Récupéré l'id dans l'url
$intId = $_GET['id'];
//Je créer un nouveau model pour exec la commande SQL
$objProjectModel = new ProjectModel;
$objProjectModel->delete($intId);
//Redirection vers la page
header("Location: index.php");
exit;
}
}