From fda3d1e68fadce12bc8d1ce5f5fcc65dbafd9109 Mon Sep 17 00:00:00 2001 From: "kevin.modejar" Date: Wed, 15 Jan 2025 13:31:42 +0100 Subject: [PATCH] Submit Quote --- config/config.php | 6 +- src/Controleur/FrontControler.php | 3 +- src/Controleur/UserControler.php | 126 ++++++++++++----------- src/Gateway/CharacterGateway.php | 20 +++- src/Gateway/ImageGateway.php | 20 +++- src/Gateway/QuoteGateway.php | 49 ++++++--- src/Model/CharacterModel.php | 10 +- src/Model/ImageModel.php | 7 ++ src/Model/QuoteModel.php | 6 +- src/Model/SourceModel.php | 2 +- src/Verification/Verification.php | 4 +- vue/create.php | 4 +- vue/recap.php | 12 +-- vue/templates/create.html.twig | 10 +- vue/templates/recapSubmitQuote.html.twig | 17 +-- vue/templates/submitQuote.html.twig | 3 +- 16 files changed, 175 insertions(+), 124 deletions(-) diff --git a/config/config.php b/config/config.php index 3cc6311..e46ccb7 100644 --- a/config/config.php +++ b/config/config.php @@ -13,10 +13,10 @@ $login = ''; $mdp = ''; -$racine='/~lebeaulato/WF-Website'; // /~kekentin/WF/WF-Website /~lebeaulato/WF-Website /~kemondejar/WF-Website +$racine='/~kemondejar/WF-Website'; // /~kekentin/WF/WF-Website /~lebeaulato/WF-Website /~kemondejar/WF-Website //$racine='/WF-Website'; - - +$repImg='/home/UCA/kemondejar/public_html/WF-Website/public/images/'; +//$repImg='/'; //Vues $vues['erreur'] = 'vue/erreur.php'; diff --git a/src/Controleur/FrontControler.php b/src/Controleur/FrontControler.php index ec503b3..b5c6637 100644 --- a/src/Controleur/FrontControler.php +++ b/src/Controleur/FrontControler.php @@ -16,7 +16,7 @@ Class FrontControler{ global $twig,$racine; $this->listAction = ['visitor' => array('accueil','search','quote','login','signin','validlogin','validsignin'), - 'user' => array('quiz','commentary','logout','addComment','favorite','profil','addFav','supFav','changedata', 'submit', 'add'), + 'user' => array('quiz','commentary','logout','addComment','favorite','profil','addFav','supFav','changedata', 'submit', 'validsubmit', 'add'), 'admin' => array('null')]; $dVueEreur = []; @@ -52,6 +52,7 @@ Class FrontControler{ $router->map('GET|POST', '/supFav/[i:id]', 'UserControler','supFav'); $router->map('GET|POST', '/changedata', 'UserControler','changedata'); $router->map('GET|POST', '/submit', 'UserControler', 'submit'); + $router->map('GET|POST', '/validsubmit', 'UserControler','validsubmit'); $router->map('GET|POST', '/add', 'UserControler', 'add'); diff --git a/src/Controleur/UserControler.php b/src/Controleur/UserControler.php index a7c4042..4c31ace 100644 --- a/src/Controleur/UserControler.php +++ b/src/Controleur/UserControler.php @@ -21,6 +21,8 @@ use Twig\Error\RuntimeError; use Twig\Error\SyntaxError; use Verification\Verification; use Enum\TypeSourceEnum; +use Model\ImageModel; +use Gateway\ImageGateway; class UserControler { @@ -30,6 +32,7 @@ class UserControler { private CharacterModel $caMod; private SourceModel $srcMod; private QuestionModel $questMod; + private ImageModel $iMod; public function __construct() { global $co; @@ -39,6 +42,7 @@ class UserControler { $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)); } public function profil() { @@ -337,47 +341,23 @@ 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(); - } - - $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']]; + $this -> qMod -> addQuote($content,'fr',$character,$source,$this->uMod->getIdByUsername(Verification::VerifChar($_SESSION['user']))); + $this -> recapSubmitQuote(array('content' => $content, 'src' => $nameSrc, 'character' => $nameChar)); } - return null; } /** @@ -391,6 +371,8 @@ class UserControler { global $vues; $src = true; $char = true; + $srcId = null; + $charId = null; require_once $vues['create']; exit(); } @@ -398,6 +380,8 @@ class UserControler { global $vues; $src = true; $char = false; + $srcId = null; + $charId = $_POST['character']; require_once $vues['create']; exit(); } @@ -405,32 +389,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'])) @@ -445,15 +420,17 @@ class UserControler { } public function add(){ - global $vues; + global $vues, $repImg; $error = []; + $src = false; + $char = false; + var_dump($_FILES); if($_POST['req'] == "both"){ $src = true; $char = true; } - elseif($_POST['req'] == "src" or $src == true){ - $src = true; - $char = false; + + 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"]); @@ -465,8 +442,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"; @@ -485,24 +460,57 @@ class UserControler { $error[] = "Le type doit être définit"; } } - elseif($_POST['req'] == "char" or $char == true){ - $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"], 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"]); } - require_once($vues["recap"]); //A changer pour recapSubmitQuote + $this -> toSubmit(Verification::VerifChar($_POST["content"]),$idC,$idS); } else{ require_once($vues["create"]); diff --git a/src/Gateway/CharacterGateway.php b/src/Gateway/CharacterGateway.php index 9d4c1b7..0f7b2ef 100644 --- a/src/Gateway/CharacterGateway.php +++ b/src/Gateway/CharacterGateway.php @@ -18,7 +18,7 @@ class CharacterGateway extends Gateway return $this -> co -> executeQuery($query, [ 'id_caracter' => array($id_character, PDO::PARAM_INT), 'caracter' => array($name, PDO::PARAM_STR), - 'id_img' => array($img_char, PDO::PARAM_STR) + 'id_img' => array($img_char, PDO::PARAM_INT) ]); } @@ -61,8 +61,24 @@ class CharacterGateway extends Gateway return $this -> co -> executeQuery($query, [ "id_c" => array($id_char, PDO::PARAM_INT), "caracter" => array($name, PDO::PARAM_STR), - "i" => array($img_char, PDO::PARAM_STR) + "i" => array($img_char, PDO::PARAM_INT) ]); } + public function getLastId():int{ + $query = "SELECT id_caracter FROM Caracter;"; + $this -> co -> executeQuery($query); + $res = $this -> co -> getResults(); + foreach($res as $r){ + $tab[] = $r["id_caracter"]; + } + $id = 1; + while(in_array($id,$tab)) + { + $id=$id+1; + } + return $id; + } + + } \ No newline at end of file diff --git a/src/Gateway/ImageGateway.php b/src/Gateway/ImageGateway.php index e05609a..fd467da 100644 --- a/src/Gateway/ImageGateway.php +++ b/src/Gateway/ImageGateway.php @@ -9,14 +9,13 @@ class ImageGateway extends Gateway public function createImgGateway(int $idImg, string $imgPath, bool $isImgProfile) : bool { $query = " - INSERT INTO Images - VALUES (:id_img, :img_path, :is_img_profile) + INSERT INTO Image + VALUES (:id_img, :img_path) "; return $this -> co -> executeQuery($query, [ 'id_img' => array($idImg, PDO::PARAM_INT), 'img_path' => array($imgPath, PDO::PARAM_STR), - 'is_img_profile' => array($isImgProfile, PDO::PARAM_BOOL), ]); } @@ -78,4 +77,19 @@ class ImageGateway extends Gateway 'img_path' => array($imgPath, PDO::PARAM_STR) ]); } + + public function getLastId():int{ + $query = "SELECT id_img FROM Image;"; + $this -> co -> executeQuery($query); + $res = $this -> co -> getResults(); + foreach($res as $r){ + $tab[] = $r["id_img"]; + } + $id = 1; + while(in_array($id,$tab)) + { + $id=$id+1; + } + return $id; + } } \ No newline at end of file diff --git a/src/Gateway/QuoteGateway.php b/src/Gateway/QuoteGateway.php index d5ace1f..92adefd 100644 --- a/src/Gateway/QuoteGateway.php +++ b/src/Gateway/QuoteGateway.php @@ -151,23 +151,38 @@ Class QuoteGateway extends Gateway{ return ($this -> co ->getResults())[0]['id']; } - public function insert4User(string $content, string $img_path, string $langage, int $user, int $source, int $character) : bool - { - $query = " - INSERT INTO Quote (id_quote, content, langue, reason, id_source, id_caracter, id_user_verif, img_path) - VALUES (:id, :content, :langage, :reason, :source, :character, :user, :img_path) - "; - - return $this->co->executeQuery($query, [ - ':id' => array($this->autoincrement(), PDO::PARAM_INT), - ':content' => array($content, PDO::PARAM_STR), - ':img_path' => array($img_path, PDO::PARAM_STR), - ':langage' => array($langage, PDO::PARAM_STR), - ':user' => array($user, PDO::PARAM_INT), - ':reason' => array('À vérifier', PDO::PARAM_STR), - ':source' => array($source, PDO::PARAM_STR), - ':character' => array($character, PDO::PARAM_STR) - ]); + public function addQuote(string $content, string $lang, int $idChar, int $idSrc, int $idUsr){ + $id = $this->getLastId(); + $query = "INSERT INTO Quote Values (:id,:content,0,:lang,false,'',:charac,:src,:user);"; + try { + $this->co->executeQuery($query,[ + ':id' => [$id,PDO::PARAM_INT], + ':content' => [$content,PDO::PARAM_STR], + ':lang' => [$lang,PDO::PARAM_STR], + ':charac' => [$idChar,PDO::PARAM_INT], + ':src' => [$idSrc,PDO::PARAM_INT], + ':user' => [$idUsr,PDO::PARAM_INT], + ]); + } + catch (PDOException $e){ + $error['501'] = 'Erreur lors de la création de la citation'; + return error; + } + } + + public function getLastId():int{ + $query = "SELECT id_quote FROM Quote;"; + $this -> co -> executeQuery($query); + $res = $this -> co -> getResults(); + foreach($res as $r){ + $tab[] = $r["id_quote"]; + } + $id = 1; + while(in_array($id,$tab)) + { + $id=$id+1; + } + return $id; } //======================== PARTI ADMIN ======================== diff --git a/src/Model/CharacterModel.php b/src/Model/CharacterModel.php index 5259c60..ac0655a 100644 --- a/src/Model/CharacterModel.php +++ b/src/Model/CharacterModel.php @@ -9,11 +9,17 @@ use Gateway\Gateway; class CharacterModel extends Model { - public function createCharacter(int $id_character, string $name , string $img_char) : bool + public function createCharacter(int $id_character, string $name , int $img_char) : bool { return $this -> gateway -> create($id_character, $name, $img_char); } + public function createCharacterWithoutId(string $name , int $img_char) : bool + { + $id_character = $this -> gateway -> getLastId(); + return $this -> gateway -> create($id_character, $name, $img_char); + } + public function getCharacterById(int $id_character) : ?CharacterEntity { $c = $this -> gateway -> findById($id_character); @@ -72,7 +78,7 @@ class CharacterModel extends Model return $this -> gateway -> delete($id_character); } - public function updateCharacter(int $id_character, string $name, string $img_char) : bool + public function updateCharacter(int $id_character, string $name, int $img_char) : bool { return $this -> gateway -> update($id_character, $name, $img_char); } diff --git a/src/Model/ImageModel.php b/src/Model/ImageModel.php index e9ac02b..4b1b046 100644 --- a/src/Model/ImageModel.php +++ b/src/Model/ImageModel.php @@ -13,6 +13,13 @@ class ImageModel extends Model return $this -> gateway -> createImgGateway($idImg, $imgPath, $isImgProfile); } + public function createImgModelWithoutId (string $imgPath) : int + { + $idImg = $this->gateway ->getLastId() ; + $this -> gateway -> createImgGateway($idImg, $imgPath, false); + return $idImg; + } + public function getImgById(int $idImg) : ?ImageEntity { $res = $this -> gateway -> findImgById($idImg); diff --git a/src/Model/QuoteModel.php b/src/Model/QuoteModel.php index b0bcff3..e2f5946 100644 --- a/src/Model/QuoteModel.php +++ b/src/Model/QuoteModel.php @@ -46,11 +46,9 @@ return $tabQ; } - public function insert4User(string $content, string $img_path, string $langage, int $user, int $source, int $character) : bool - { - return $this->gateway->insert4User($content, $img_path, $langage, $user, $source, $character); + public function addQuote(string $content, string $lang, int $idChar, int $idSrc, int $idUsr){ + $this -> gateway -> addQuote($content,$lang,$idChar,$idSrc,$idUsr); } - } ?> diff --git a/src/Model/SourceModel.php b/src/Model/SourceModel.php index 000a01e..9c54cce 100644 --- a/src/Model/SourceModel.php +++ b/src/Model/SourceModel.php @@ -106,7 +106,7 @@ class SourceModel extends Model public function existSource(string $name, string $type) : bool{ $q = $this -> getSourceByTitle($name); - return isset($q[0]); + return isset($q); } } diff --git a/src/Verification/Verification.php b/src/Verification/Verification.php index eef98ab..e8ae8ba 100644 --- a/src/Verification/Verification.php +++ b/src/Verification/Verification.php @@ -7,8 +7,8 @@ namespace Verification; if($text==NULL){ return NULL; } - $charInterdi=['|','/','\\','%','$','=','<','>','(',')']; - $chaineInterdi=["AND","OR","WHERE","FROM","SELECT"]; + $charInterdi=['|','/','\\','%','$','=','<','>','(',')',';']; + $chaineInterdi=["AND","OR","WHERE","FROM","SELECT","DROP","DELETE","TABLE","Users"]; $textVerif=""; foreach( str_split($text,1) as $char){ diff --git a/vue/create.php b/vue/create.php index 451cbc2..f7c7f5b 100644 --- a/vue/create.php +++ b/vue/create.php @@ -15,6 +15,8 @@ echo $twig->render('bandeau.html.twig'); echo $twig->render('create.html.twig', [ 'src' => $src, 'char' => $char, - 'content' => $_POST['content'] + 'content' => $_POST['content'], + 'charId' => $charId, + 'srcId' => $srcId ]); ?> diff --git a/vue/recap.php b/vue/recap.php index ad7c557..2d9115e 100644 --- a/vue/recap.php +++ b/vue/recap.php @@ -4,13 +4,13 @@ echo $twig->render('head.html.twig', array( 'title' => "Recap Quote", - 'style' => "../public/styles/styleSubmitQuote.css", - 'scripts' => array("../../public/script/theme-toggle.js") + 'style' => "public/styles/styleSubmitQuote.css", + 'scripts' => array("public/script/theme-toggle.js") )); echo $twig->render('bandeau.html.twig'); - echo $twig -> render("recapSubmitQuote.html.twig", [' - content' => $_POST["content"], - 'character' => $_POST["character"], - 'source' => $_POST["src"] + echo $twig -> render("recapSubmitQuote.html.twig", [ + 'content' => $recap["content"], + 'character' => $recap["character"], + 'source' => $recap["src"] ]); ?> \ No newline at end of file diff --git a/vue/templates/create.html.twig b/vue/templates/create.html.twig index 4f85cc0..f64a582 100644 --- a/vue/templates/create.html.twig +++ b/vue/templates/create.html.twig @@ -1,5 +1,5 @@
-
+ {% if src == true and char == true %} @@ -8,13 +8,13 @@ {% elseif src == true %} - {# a completer #} + {% elseif char == true %} - - {# a completer #} + + {% endif %} @@ -45,7 +45,7 @@ - + {% endif %} diff --git a/vue/templates/recapSubmitQuote.html.twig b/vue/templates/recapSubmitQuote.html.twig index d542dd3..0211f18 100644 --- a/vue/templates/recapSubmitQuote.html.twig +++ b/vue/templates/recapSubmitQuote.html.twig @@ -1,18 +1,3 @@ - - - - - - Wiki Fantasy : Submit Quote - - - - - - - -{% include "bandeau.html.twig" %} -

▶ Your quote has been submitted successfully ◀

@@ -41,7 +26,7 @@
- +
diff --git a/vue/templates/submitQuote.html.twig b/vue/templates/submitQuote.html.twig index d2882fc..64246b7 100644 --- a/vue/templates/submitQuote.html.twig +++ b/vue/templates/submitQuote.html.twig @@ -43,8 +43,7 @@

Contenu *

- +