diff --git a/controllers/user_controller.php b/controllers/user_controller.php index ccba625..d981df7 100644 --- a/controllers/user_controller.php +++ b/controllers/user_controller.php @@ -151,38 +151,163 @@ class UserCtrl extends MotherCtrl { */ public function user(){ - $intId = isset($_GET['id']) ? (int)$_GET['id'] : 0; + /**$intId = isset($_GET['id']) ? (int)$_GET['id'] : 0; - if ($intId <= 0) { - header("Location:index.php"); - exit; - } + if ($intId <= 0) { + header("Location: index.php"); + exit; + } + + //affichage info utilisateur + $objUserModel = new UserModel; + $arrUserData = $objUserModel->findUserById($intId); + + if ($arrUserData === false) { + header("Location: index.php"); + exit; + }*/ + + $strPseudo = $_GET['pseudo']??''; - //affichage info utilisateur $objUserModel = new UserModel; - $arrUserData = $objUserModel->findUserById($intId); + $arrUserData = $objUserModel->findUserByPseudo($strPseudo); - if ($arrUserData === false) { - header("Location:index.php"); - exit; - } - $objUser = new User; - $objUser->hydrate($arrUserData); + if ($arrUserData === false) { + header("Location: index.php"); + exit; + } - //affichage projet de l'utilisateur - $objProjectModel = new ProjectModel; - $arrProjects = $objProjectModel->findAll(0,'',$intId); + $objUser = new User; + $objUser->hydrate($arrUserData); - $arrProjectToDisplay = array(); - foreach($arrProjects as $projectData) { - $objProject = new Project(); - $objProject->hydrate($projectData); - $arrProjectToDisplay[] = $objProject; - } + //affichage projet de l'utilisateur + $objProjectModel = new ProjectModel; + $arrProjects = $objProjectModel->findAll(0,'',$objUser->getId()); - $this->_arrData['user'] = $objUser; - $this->_arrData['arrProjectToDisplay'] = $arrProjectToDisplay; + $arrProjectToDisplay = array(); + foreach($arrProjects as $projectData) { + $objProject = new Project(); + $objProject->hydrate($projectData); + $arrProjectToDisplay[] = $objProject; + } + + $this->_arrData['user'] = $objUser; + $this->_arrData['arrProjectToDisplay'] = $arrProjectToDisplay; $this->_display("user"); } + + public function edit(){ + if(!isset($_SESSION['user'])){ + header("Location: index.php"); + exit; + } + + $objUserModel = new UserModel; + $arrError = []; + $objUser = new User; + $arrUserData = $objUserModel->findUserById($_SESSION['user']['user_id']); + $objUser->hydrate($arrUserData); + if (!empty($_POST)) { + if ($objUserModel->mailExists($_POST['user_mail']) && ($_POST['user_mail'] != $objUser->getMail())) { + + $arrError['user_mail'] = "Ce mail est déjà associé"; + } else { + if ($objUserModel->pseudoExists($_POST['user_pseudo']) && ($_POST['user_pseudo'] != $objUser->getPseudo())){ + $arrError['user_pseudo'] = "Ce pseudo est déjà utiliser"; + }else{ + $objUser->hydrate($_POST); + $objUser->setId($_SESSION['user']['user_id']); + + // Vérification de l'image + $arrTypeAllowed = array('image/jpeg', 'image/png', 'image/webp'); + $boolImageOk = true; + + if ($_FILES['image']['error'] != 4) { + if (!in_array($_FILES['image']['type'], $arrTypeAllowed)) { + $arrError['image'] = "Le type de fichier n'est pas autorisé"; + } else { + switch ($_FILES['image']['error']) { + case 0: + $strImageName = uniqid() . ".webp"; + $strOldImg = $objUser->getImage(); + $objUser->setImage($strImageName); + break; + case 1: + case 2: + $arrError['image'] = "Le fichier est trop volumineux"; + break; + case 3: + $arrError['image'] = "Le fichier a été partiellement téléchargé"; + break; + case 6: + $arrError['image'] = "Le répertoire temporaire est manquant"; + break; + default: + $arrError['image'] = "Erreur sur l'image"; + break; + } + } + } + + // Traitement de l'image si pas d'erreur + if (count($arrError) == 0 && isset($strImageName)) { + $strDest = $_ENV['IMG_USER_PATH'] . $strImageName; + $strSource = $_FILES['image']['tmp_name']; + list($intWidth, $intHeight) = getimagesize($strSource); + + $intDestWidth = 200; $intDestHeight = 200; + $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); + } + + $objDest = imagecreatetruecolor($intDestWidth, $intDestHeight); + switch ($_FILES['image']['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); + $boolImageOk = imagewebp($objDest, $strDest); + imagedestroy($objDest); + imagedestroy($objSource); + } + + + $boolInsert = $objUserModel->update($objUser); + + if ($boolInsert === true) { + if (isset($strOldImg) && !empty($strOldImg) && isset($strImageName)) { + $strOldFile = $_ENV['IMG_USER_PATH'] . $strOldImg; + if (file_exists($strOldFile)) unlink($strOldFile); + } + $arrNewInfo = $objUserModel->findUserByPseudo($objUser->getPseudo()); + $_SESSION['user'] = $arrNewInfo; + $_SESSION['success'] = "Compte modifier avec succès"; + header("Location:?ctrl=user&action=user&pseudo=".$objUser->getPseudo()); + exit; + } else { + $arrError['global'] = "Erreur lors de l'update"; + } + } + } + } + + $this->_arrData["arrError"] = $arrError; + $this->_arrData['objUser'] = $objUser; + $this->_display("useredit"); + + } } diff --git a/models/mother_model.php b/models/mother_model.php index 59eea4f..c3f088c 100644 --- a/models/mother_model.php +++ b/models/mother_model.php @@ -26,7 +26,7 @@ Pour passer sur le serveur de YASS: *"mysql:host=boulayoune.com;dbname=projet_folliow", // Serveur et BDD "projet_user", //Nom d'utilisateur de la base de données - "F0lliowRules!",// Mot de passe de la base de données + "F0lliowRules!",// Mot de passe de la base de données Site pour BDD: https://phpmyadmin.boulayoune.com/index.php?route=/sql&pos=0&db=projet_folliow&table=project Pour passer en local: diff --git a/models/project_model.php b/models/project_model.php index 4cf0d90..8d2a9a2 100644 --- a/models/project_model.php +++ b/models/project_model.php @@ -20,7 +20,7 @@ string $strEndDate='', int $intCategory=0, bool $bool6Months=false):array{ $strRq = "SELECT project.*, - CONCAT(user_firstname, ' ', user_name) AS 'project_creatorname', + user_pseudo AS 'project_creatorname', user_image FROM project INNER JOIN users ON user_id = project_user_id"; diff --git a/models/user_model.php b/models/user_model.php index baa46dd..f17b9a3 100644 --- a/models/user_model.php +++ b/models/user_model.php @@ -18,7 +18,7 @@ * @return array */ public function findAllUsers():array{ - $strRq = "SELECT user_id, user_firstname, user_name, user_image, user_status, authorisation_name + $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(); @@ -32,7 +32,7 @@ */ 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 + $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 = '".$strMail."'"; @@ -52,7 +52,7 @@ */ 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) + $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); @@ -70,6 +70,37 @@ return $rqPrep->execute(); } + 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 @@ -125,7 +156,9 @@ */ public function findUserById(int $intId): array|bool { - $strRq = "SELECT * FROM users WHERE user_id = :id"; + $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); @@ -133,4 +166,26 @@ return $prep->fetch(); } + + 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(); + } + + 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 $rq->fetchColumn(); + } } diff --git a/uploads/profiles/699b019dc6db1.webp b/uploads/profiles/699b019dc6db1.webp new file mode 100644 index 0000000..db8dfc7 Binary files /dev/null and b/uploads/profiles/699b019dc6db1.webp differ diff --git a/uploads/profiles/699b020302a4e.webp b/uploads/profiles/699b020302a4e.webp new file mode 100644 index 0000000..db8dfc7 Binary files /dev/null and b/uploads/profiles/699b020302a4e.webp differ diff --git a/uploads/profiles/699b028f54007.webp b/uploads/profiles/699b028f54007.webp new file mode 100644 index 0000000..527efea Binary files /dev/null and b/uploads/profiles/699b028f54007.webp differ diff --git a/uploads/profiles/thomas.jpg b/uploads/profiles/thomas.jpg deleted file mode 100644 index 051578c..0000000 Binary files a/uploads/profiles/thomas.jpg and /dev/null differ diff --git a/views/_partial/header.tpl b/views/_partial/header.tpl index f64fd9c..6a495b4 100644 --- a/views/_partial/header.tpl +++ b/views/_partial/header.tpl @@ -65,15 +65,15 @@ {* Utilisateur connecté *}