@ -28,6 +28,8 @@ class UserControler {
private UserModel $uMod;
private QuoteModel $qMod;
private QuestionModel $mdl;
private CharacterModel $caMod;
private SourceModel $srcMod;
public function __construct() {
global $co;
@ -35,12 +37,22 @@ class UserControler {
$this->uMod = new UserModel(new UserGateway($co));
$this->qMod = new QuoteModel(new QuoteGateway($co));
$this -> mdl = new QuestionModel(new QuestionGateway($co));
$this -> caMod = new CharacterModel(new CharacterGateway($co));
$this->srcMod = new SourceModel(new SourceGateway($co));
}
public function profil() {
global $vues;
$p = $this->uMod->getUsername($_SESSION["user"]);
// Pour les messages d'erreur
$error_message = null;
if (isset($_SESSION['error_message'])) {
$error_message = $_SESSION['error_message'];
// Supprimer le message d'erreur après l'avoir lu
unset($_SESSION['error_message']);
}
require_once $vues['profil'];
}
@ -112,6 +124,7 @@ class UserControler {
if ($_SESSION['no_question'] >= $total_questions) {
$_SESSION['no_question'] = 0;
$this->endQuiz($id_quiz, $score);
$_SESSION['score'] = 0;
}
else header("Location: ".$racine."/quiz/$id_quiz"); ///~kekentin/WF/WF-Website
@ -156,7 +169,6 @@ class UserControler {
$answer = explode('-', $answerd)[0];
$id = (int) explode('-', $answerd)[1];
}
$res = $this->mdl->getQuestion($id);
return $answer == $res->getCanswer();
}
@ -206,6 +218,9 @@ class UserControler {
header("Location:". $racine ."/quote/$id");
}
// ===================== UPDATE DATA USER FUNCTION =====================
public function changedata() : void{
global $vues, $racine;
if ($_POST)
@ -213,39 +228,88 @@ class UserControler {
$newImage = $_POST['image'] ?? null;
$newPseudo = $_POST['pseudo'] ?? null;
$newEmail = $_POST['email'] ?? null;
$oldPasswd = $_POST['oldPasswd']?? null;
$newMdpFirst = $_POST['passwdFirst'] ?? null;
$newMdpSecond = $_POST['passwdSecond'] ?? null;
//Modif le pseudo
if($newPseudo){
$user = $this-> uMod->setUsername($_SESSION['user'], $newPseudo);
if($user == $newPseudo){ //Si le pseudo existe déjà
$_SESSION['user'] = $newPseudo;
}
}
else if($newEmail){
$user = $this-> uMod->setEmail($_SESSION['user'], $newEmail);
if($newPseudo){//Modif le pseudo
$this->updatePseudo($newPseudo);
}
else if($newEmail){//Modif l'email
$this->updateEmail($newEmail);
}
else if($newMdpFirst & & $newMdpSecond){ //Modif le mot de passe
$this->updatePassWd($oldPasswd, $newMdpFirst,$newMdpSecond);
}
else if($newImage){//Modif l'image
$this->updateImg();
}
}
header("Location: ". $racine."/profil");
else if($newMdpFirst & & $newMdpSecond){
}
if($newMdpFirst == $newMdpSecond){
$option = ['cost' => 12];
$newPassWd = password_hash($newMdpFirst, PASSWORD_BCRYPT, $option );
$user = $this-> uMod->setPassWd($_SESSION['user'], $newPassWd);
}
}
public function updatePseudo(string $newPseudo){
$user = $this-> uMod->setUsername($_SESSION['user'], $newPseudo );
if($user == $newPseudo){
$_SESSION['user'] = $newPseudo;
else if($newImage){
$user = $this->uMod->setImage($_SESSION['user']);
}
else{ // pseudo invalide
$_SESSION['error_message'] = $newPseudo . " n'est pas valide ou non libre";
header("Location: ". $racine."/profil");
}
}
public function updateEmail(string $newEmail){
$user = $this-> uMod->setEmail($_SESSION['user'], $newEmail);
if($user == $_SESSION['user']){ // si email incorrect, renvoie le nom de l'utilisateur de la session
$_SESSION['error_message'] = "L'email n'est pas valide";
header("Location: ". $racine."/profil");
}
}
public function updatePassWd(string $oldPasswd, string $newMdpFirst, string $newMdpSecond){
if(!$oldPasswd){
$_SESSION['error_message'] = "Veuillez taper votre ancien mot de passe";
header("Location: ". $racine."/profil");
}
else if(!$this->uMod->isPassWd($_SESSION['user'], $oldPasswd)){
$_SESSION['error_message'] = "Votre ancien mot de passe est incorrect";
header("Location: ". $racine."/profil");
}
else{
if($newMdpFirst == $newMdpSecond){
$option = ['cost' => 12];
$newPassWd = password_hash($newMdpFirst, PASSWORD_BCRYPT, $option);
$user = $this-> uMod->setPassWd($_SESSION['user'], $newPassWd);
}
else{
$_SESSION['error_message'] = "Les mots de passe ne correspondent pas";
header("Location: ". $racine."/profil");
}
}
header("Location: ". $racine."/profil");
}
public function updateImg(){
$user = $this->uMod->setImage($_SESSION['user']);
}
// ===================== SUBMIT FUNCTION =====================
/**
* @throws SyntaxError
* @throws RuntimeError
@ -253,6 +317,8 @@ class UserControler {
*/
public function submit() : void{
global $vues;
$p = $this->caMod->getAllPerso();
$s = $this->srcMod->getAllSources();
require_once $vues['submitQuote'];
}
@ -267,7 +333,7 @@ class UserControler {
{
$content = $_POST['content'] ?? null;
$character = $_POST['character'] ?? null;
$source = $_POST['sou rce '] ?? null;
$source = $_POST['src'] ?? null;
//$img = $_POST['img'] ?? null;
$errors = [null, null];
@ -275,12 +341,12 @@ class UserControler {
$gw = new CharacterGateway($co);
$mdl = new CharacterModel($gw);
$character = $mdl -> getCharacterByName ($character);
$character = $mdl -> getCharacterById ($character);
$gw = new SourceGateway($co);
$mdl = new SourceModel($gw);
$source = $mdl -> getSourceByTitle ($source);
$source = $mdl -> getSourceById ($source);
if (!$character)
$errors[0] = "Personnage inexistant";
@ -289,16 +355,16 @@ class UserControler {
if ($errors[0] || $errors[1])
{
global $twig;
echo $twig->render("submitQuote.html.twig", ['errors' => $errors] );
$this->submit( );
exit();
}
$gw = new QuoteGateway($co);
$mdl = new QuoteModel($gw);
$mdl -> insert4User($content, '/imgPath', 'fr', $this -> getIdOfUser(), $source->getIdSource(), $character->getIdCharacter());
// $mdl -> insert4User($content, '/imgPath', 'fr', $this -> getIdOfUser(), $source->getIdSource(), $character->getIdCharacter());
return [$content, $_POST['character'], $_POST['sou rce ']];
return [$content, $_POST['character'], $_POST['src']];
}
return null;
}
@ -308,14 +374,41 @@ class UserControler {
* @throws RuntimeError
* @throws LoaderError
*/
public function validsubmit() : void{
$recap = $this -> toSubmit();
public function validsubmit() : void
{
if($_POST['src'] == 'other' & & $_POST['character'] == 'other'){
global $vues;
$src = true;
$char = true;
require_once $vues['create'];
exit();
}
elseif($_POST['src'] == 'other'){
global $vues;
$src = true;
$char = false;
require_once $vues['create'];
exit();
}
elseif($_POST['character'] == 'other'){
global $vues;
$src = false;
$char = true;
require_once $vues['create'];
exit();
}
if($_POST)
$recap = $this -> toSubmit();
if ($recap)
{
$this -> recapSubmitQuote($recap);
}
}
// ===================== SUBMIT QUOTE FUNCTION =====================
/**
* @throws RuntimeError
* @throws SyntaxError
@ -337,4 +430,70 @@ class UserControler {
}
return null;
}
public function add(){
global $vues;
var_dump($_POST);
$error = [];
if($_POST['req'] == "both"){
$src = true;
$char = true;
}
elseif($_POST['req'] == "src"){
$src = true;
$char = false;
$type = array("Movie","Serie","VideoGame","Anime");
if(Verification::verifNotNull($_POST["titre"])){
$_POST["titre"] = Verification::verifChar($_POST["titre"]);
if($this->srcMod->existSource($_POST["titre"],$_POST["type"])){
$error[] = "La source existe déja";
}
}
else{
$error[] = "Le titre doit être définit";
}
if(Verification::verifNotNull($_POST["date"])){
$src = true;
$char = false;
$_POST["date"] = Verification::verifChar($_POST["date"]);
if(intval($_POST["date"],10) < 1850 or intval ( $ _POST [ " date " ] , 10 ) > date( "Y", time() )){
$error[] = "La date est invalide";
}
}
else{
$error[] = "La date doit être définit";
}
if(Verification::verifNotNull($_POST["type"])){
$_POST["type"] = Verification::verifChar($_POST["type"]);
if(!in_array($_POST["type"],$type)){
$error[] = "Le type indiquer est inexistant";
}
}
else{
$error[] = "Le type doit être définit";
}
}
elseif($_POST['req'] == "char"){
$src = false;
$char = true;
if(Verification::verifNotNull($_POST[""])){
}
}
if($error == []){
if($_POST['req'] == "both"){
}
elseif($_POST['req'] == "src"){
$this->srcMod->createSource($_POST["titre"], $_POST["date"], $_POST["type"]);
}
elseif($_POST['req'] == "char"){
}
}
else{
require_once($vues["create"]);
var_dump($error);
}
}
}