You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
WF-Website/src/Model/UserModel.php

176 lines
5.4 KiB

<?php
namespace Model;
use Entity\User;
use Entity\UserEntity;
use Gateway\UserGateway;
use Gateway\Gateway;
class UserModel extends Model
{
public function insertUser(string $username,string $email,string $passwd) : bool{
/*global $rep,$image;*/
return $this->gateway->insertUser( $username, $email, $passwd, false, 1);
}
public function deleteUser(string $id) : bool{
return $this->gateway->delete($id);
}
// ===================== Get FUNCTION =====================
public function getNumberOfUsers() : int
{
return $this->gateway->getNumberOfUsers()[0]['count'] ?? 0;
}
// public function getFavoriteUser(string $id) : array{
// $res[0] = array();
// $data = $this->gateway->getFavorite($id);
// foreach ($data as $favoris) {
// $res[0][] = new Quote();
// }
// }
public function getDataUser(int $id) : ?UserEntity {
$res = $this->gateway->findDataUser($id);
if ($res)
return new UserEntity(
$res[0]['id_user'],
$res[0]['username'],
$res[0]['password'],
$res[0]['email'],
$res[0]['img'],
$res[0]['creation']
);
return null;
}
public function getUsername(string $username) : ?UserEntity
{
$res = $this->gateway->findUsername($username);
if ($res)
return new UserEntity(
$res[0]['id_user'],
$res[0]['username'],
$res[0]['password'],
$res[0]['email'],
$res[0]['imgpath'],
$res[0]['creation']
);
return null;
}
public function getEmail(string $email) : ?UserEntity
{
$res = $this->gateway->findEmail($email);
if ($res)
return new UserEntity(
$res[0]['id_user'],
$res[0]['username'],
$res[0]['password'],
$res[0]['email'],
$res[0]['img'],
$res[0]['creation']
);
return null;
}
public function getIdByUsername(string $username){
$res = $this->gateway->getIdUser($username);
return $res[0]['id_user'];
}
public function getEmailWithUser(string $user){
$res = $this->gateway->emailWithUser($user);
return $res[0]['email'];
}
// ===================== Bool FUNCTION =====================
public function IsExisteUsername(string $username):bool{
return $this->gateway->IsExisteUsername($username);
}
public function IsExisteEmail(string $email):bool{
return $this->gateway->IsExisteEmail($email);
}
public function isPassWd(string $username, string $passWd): bool {
$hash = $this->gateway->getPasswordHash($username);
return $hash !== null && password_verify($passWd, $hash);
}
public function isFavorite(?string $username, int $idq): bool {
if($_SESSION["user"] == NULL){
return false;
}
else{
$res = $this->gateway->inFavorite($_SESSION["user"],$idq);
return $res;
}
}
// ===================== Set FUNCTION =====================
public function setUsername(string $username, string $newUsername): string {
if ($this->IsExisteUsername($newUsername)) {// Vérifier si le nouveau nom d'utilisateur existe déjà
return $username;// Retourne l'ancien nom d'utilisateur sans modification
}
$res = $this->gateway->updateUsername($username, $newUsername);// Sinon, mettre à jour le nom d'utilisateur
// Retourner le nouveau nom d'utilisateur après modification
if (!empty($res) && isset($res[0]['username'])) {
return $res[0]['username'];
}
return $username; // En cas d'échec, retourne l'ancien nom d'utilisateur
}
public function setEmail(string $username, string $newEmail){
if ($this->IsExisteEmail($newEmail)) {
return $username;
}
$res = $this->gateway->updateEmail($username,$newEmail);
if (!empty($res) && isset($res[0]['email'])) {
return $res[0]['email'];
}
return $username;// En cas d'échec, retourne l'ancien email
}
public function setImage(string $username,string $newImage){
$res = $this->gateway->updateImg($username,$newImage);
$src[] = $res[0]['img'];
return $src;
}
public function setPassWd(string $username, string $newPassWd):void{
$res = $this->gateway->updatePasswd($username,$newPassWd);
}
public function addFavorite(string $username, int $id){
$this->gateway->addFavorite($username,$id);
}
public function supFavorite(string $username, int $id){
$this->gateway->supFavorite($username,$id);
}
}
?>