@ -21,25 +21,30 @@ use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
use Verification\Verification;
use Enum\TypeSourceEnum;
use Model\ImageModel;
use Gateway\ImageGateway;
class UserControler {
private CommentaryModel $cMod;
private UserModel $uMod;
private QuoteModel $qMod;
private QuestionModel $mdl;
private CharacterModel $caMod;
private SourceModel $srcMod;
private QuestionModel $questMod;
private ImageModel $iMod;
private QuizModel $quizModel;
public function __construct() {
global $co;
$this->cMod = new CommentaryModel(new CommentaryGateway($co));
$this->uMod = new UserModel(new UserGateway($co));
$this->qMod = new QuoteModel(new QuoteGateway($co));
$this -> mdl = new QuestionModel(new QuestionGateway($co));
$this -> questMod = new QuestionModel(new QuestionGateway($co));
$this -> caMod = new CharacterModel(new CharacterGateway($co));
$this->srcMod = new SourceModel(new SourceGateway($co));
$this->iMod = new ImageModel(new ImageGateway($co));
$this->quizModel = new QuizModel(new QuizGateway($co));
}
@ -53,6 +58,7 @@ class UserControler {
global $vues;
$p = $this->uMod->getUsername($_SESSION["user"]);
$listImg = $this->iMod->getAllImg() ;
// Pour les messages d'erreur
$error_message = null;
@ -125,8 +131,7 @@ class UserControler {
* @throws RuntimeError
* @throws LoaderError
*/
public function continueQuiz(int $id_quiz, int $total_questions) : void
{
public function continueQuiz(int $id_quiz, int $total_questions) : void{
global $racine;
$score = $_SESSION['score'];
$_SESSION['no_question'] = Verification::verifChar( isset($_SESSION['no_question']) ? ($_SESSION['no_question'] + 1) : 1);
@ -144,8 +149,7 @@ class UserControler {
* @throws RuntimeError
* @throws LoaderError
*/
public function endQuiz(int $id_quiz, int $score) : void
{
public function endQuiz(int $id_quiz, int $score) : void{
global $vues,$co;
$gw = new QuizGateway($co);
@ -159,8 +163,7 @@ class UserControler {
require_once $vues['endQuiz'];
}
public function CorrectAnswer() : bool
{
public function CorrectAnswer() : bool{
$answera = Verification::verifChar($_POST['answera'] ?? null);
$answerb = Verification::verifChar($_POST['answerb'] ?? null);
$answerc = Verification::verifChar($_POST['answerc'] ?? null);
@ -182,12 +185,11 @@ class UserControler {
$answer = explode('-', $answerd)[0];
$id = (int) explode('-', $answerd)[1];
}
$res = $this->mdl ->getQuestion($id);
$res = $this->questMod ->getQuestion($id);
return $answer == $res->getCanswer();
}
public function GetQuestion(int $id): array
{
public function GetQuestion(int $id): array{
global $co;
$gw = new QuizQuestionGateway($co);
$mdl = new QuizQuestionModel($gw);
@ -199,8 +201,7 @@ class UserControler {
* @throws SyntaxError
* @throws LoaderError
*/
public function showQuestion(int $id, int $num) : void
{
public function showQuestion(int $id, int $num) : void{
global $vues,$twig;
$q = $this->GetQuestion($id);
$question = $q[$num] ?? $q[0];
@ -220,8 +221,7 @@ class UserControler {
}
public function getNumberOfQuestion(int $id) : int
{
public function getNumberOfQuestion(int $id) : int{
global $co;
$gw = new QuizGateway($co);
$mdl = new QuizModel($gw);
@ -244,8 +244,8 @@ class UserControler {
}
// ===================== UPDATE DATA USER FUNCTION =====================
// ===================== UPDATE DATA USER FUNCTION =====================
public function changedata() : void{
global $vues, $racine;
if ($_POST)
@ -263,12 +263,13 @@ class UserControler {
}
else if($newEmail){//Modif l'email
$this->updateEmail($newEmail);
$this->sendEmailChangeLogin($newEmail); //Envoie un email confirmant le changement d'email
}
else if($newMdpFirst & & $newMdpSecond){ //Modif le mot de passe
$this->updatePassWd($oldPasswd, $newMdpFirst,$newMdpSecond);
}
else if($newImage){//Modif l'image
$this->updateImg();
$this->updateImg($newImage );
}
}
header("Location: ". $racine."/profil");
@ -325,8 +326,8 @@ class UserControler {
}
}
public function updateImg(){
$user = $this->uMod->setImage($_SESSION['user']);
public function updateImg(string $newImage ){
$user = $this->uMod->setImage($_SESSION['user'],$newImage );
}
@ -340,8 +341,7 @@ class UserControler {
* @throws RuntimeError
* @throws LoaderError
*/
public function submit() : void
{
public function submit() : void{
global $vues;
$p = $this->caMod->getAllPerso();
$s = $this->srcMod->getAllSources();
@ -353,47 +353,24 @@ class UserControler {
* @throws RuntimeError
* @throws LoaderError
*/
public function toSubmit() : ?array
public function toSubmit(string $content,string $character, string $source)
{
global $co;
if ($_POST)
{
$content = $_POST['content'] ?? null;
$character = $_POST['character'] ?? null;
$source = $_POST['src'] ?? null;
//$img = $_POST['img'] ?? null;
$errors = [null, null];
$gw = new CharacterGateway($co);
$mdl = new CharacterModel($gw);
$character = $mdl -> getCharacterById($character);
$gw = new SourceGateway($co);
$mdl = new SourceModel($gw);
$source = $mdl -> getSourceById($source);
$nameSrc = $this->srcMod->getSourceById($source)->getTitle();
$nameChar = $this->caMod->getCharacterById($character)->getName();
if (!$character)
$errors[0] = "Personnage inexistant";
if (!$source)
$errors[1] = "Source inexistante";
if ($errors[0] || $errors[1])
{
global $twig;
$this->submit();
exit();
$this -> qMod -> addQuote($content,'fr',$character,$source,$this->uMod->getIdByUsername(Verification::VerifChar($_SESSION['user'])));
$this -> recapSubmitQuote(array('content' => $content, 'src' => $nameSrc, 'character' => $nameChar));
}
$gw = new QuoteGateway($co);
$mdl = new QuoteModel($gw);
//$mdl -> insert4User($content, '/imgPath', 'fr', $this -> getIdOfUser(), $source->getIdSource(), $character->getIdCharacter());
return [$content, $_POST['character'], $_POST['src']];
}
return null;
}
/**
@ -407,6 +384,8 @@ class UserControler {
global $vues;
$src = true;
$char = true;
$srcId = null;
$charId = null;
require_once $vues['create'];
exit();
}
@ -414,6 +393,8 @@ class UserControler {
global $vues;
$src = true;
$char = false;
$srcId = null;
$charId = $_POST['character'];
require_once $vues['create'];
exit();
}
@ -421,32 +402,23 @@ class UserControler {
global $vues;
$src = false;
$char = true;
$srcId = $_POST['src'];
$charId = null;
require_once $vues['create'];
exit();
}
if($_POST)
$recap = $this -> toSubmit();
if ($recap)
{
$this -> recapSubmitQuote($recap);
$recap = $this -> toSubmit($_POST['content'],$_POST['character'],$_POST['src']);
}
public function recapSubmitQuote(array $recap){
global $vues;
require_once($vues["recap"]);
}
// ===================== SUBMIT QUOTE FUNCTION =====================
/**
* @throws RuntimeError
* @throws SyntaxError
* @throws LoaderError
*/
public function recapSubmitQuote(?array $recap) : void
{
global $twig;
echo $twig -> render("recapSubmitQuote.html.twig", ['content' => $recap[0], 'character' => $recap[1], 'source' => $recap[2]]);
}
public function getIdOfUser() : ?int
{
if (isset($_SESSION['user']))
@ -461,17 +433,18 @@ class UserControler {
}
public function add(){
global $vues;
var_dump($_POST);
global $vues, $repImg;
$error = [];
$src = false;
$char = false;
var_dump($_FILES);
if($_POST['req'] == "both"){
$src = true;
$char = true;
}
elseif($_POST['req'] == "src"){
$src = true;
$char = false;
$type = array("Movie","Serie","VideoGame","Anime");
if($_POST['req'] == "src" or $src == true){
$type = array("movie","serie","video-game","anime");
if(Verification::verifNotNull($_POST["titre"])){
$_POST["titre"] = Verification::verifChar($_POST["titre"]);
if($this->srcMod->existSource($_POST["titre"],$_POST["type"])){
@ -482,8 +455,6 @@ class UserControler {
$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";
@ -502,27 +473,100 @@ class UserControler {
$error[] = "Le type doit être définit";
}
}
elseif($_POST['req'] == "char"){
$src = false;
$char = true;
if(Verification::verifNotNull($_POST[""])){
if($_POST['req'] == "char" or $char == true){
if(Verification::verifNotNull($_POST["name"])){
$_POST["name"] = Verification::verifChar($_POST["name"]);
}
else{
$error[] = "Le nom doit être définit";
}
// upload de la photo
if (isset($_FILES['avatar'])) {
$tabExtension = explode('.', $_FILES['avatar']['name']);
$extension = strtolower(end($tabExtension));
$extensions = ['jpg', 'png', 'jpeg'];
if(in_array($extension, $extensions)){
move_uploaded_file($_FILES['avatar']['tmp_name'], $repImg.$_FILES['avatar']['name']);
$filePath = $repImg.$_FILES['avatar']['name'];
}
else{
$error[] = "Mauvaise extension";
exit();
}
}
else{
$error[] = "Aucune photo";
}
}
if($error == []){
$recap['content'] = $_POST['content'];
if($_POST['req'] == "both"){
$this->srcMod->createSource($_POST["titre"], $_POST["date"], TypeSourceEnum::from($_POST["type"]));
$idImg = $this->iMod->createImgModelWithoutId($filePath);
$this->caMod->createCharacterWithoutId($_POST["name"], $idImg);
$idC = $this-> caMod -> getCharacterByName($_POST["name"])->getIdCharacter();
$idS = $this -> srcMod -> getSourceByTitle($_POST["titre"])->getIdSource();
}
elseif($_POST['req'] == "src"){
$this->srcMod->createSource($_POST["titre"], $_POST["date"], $_POST["type"]);
$this->srcMod->createSource($_POST["titre"], $_POST["date"], TypeSourceEnum::from($_POST["type"]));
$idC = Verification::VerifChar($_POST["character"]);
$idS = $this -> srcMod -> getSourceByTitle($_POST["titre"])->getIdSource();
}
elseif($_POST['req'] == "char"){
$idImg = $this->iMod->createImgModelWithoutId($filePath);
$this->caMod->createCharacterWithoutId($_POST["name"], $idImg);
$idC = $this-> caMod -> getCharacterByName($_POST["name"])->getIdCharacter();
$idS = Verification::VerifChar($_POST["src"]);
}
$this -> toSubmit(Verification::VerifChar($_POST["content"]),$idC,$idS);
}
else{
require_once($vues["create"]);
var_dump($error);
}
}
// Quiz aléatoire
// argument = Langue / type question / type reponse
public function quizAleatoire(){
global $vues;
if(!isset($_POST["nbVie"])){
$vie = 3;
}
$question = $questMod->getRandomQuestion([$_POST["questionType"],$_POST["awnserType"]],$_POST["langue"]);
addTab($question);
if($question == null){
$require_once($vues["erreur"]);
}
else{
$require_once($vues['quiz']);
}
}
public function questionSuivantAleatoire(){
}
public function sendEmailChangeLogin(string $email) : void{
$to = $email; // Adresse email de destination
$subject = "What The Fantasy - Changement d'Email"; // Sujet de l'email
$message = "
Bonjour,
L'adresse mail $email est désormais votre nouvelle adresse.
L'équipe du site
"; // Contenu de l'email
$headers = "From: noreply@whatTheFantasy.com"; // Adresse email de l'expéditeur
// Envoyer l'email
mail($to, $subject, $message, $headers);
}
}