diff --git a/project/src/config/Validation.php b/project/src/config/Validation.php index 597c0a8..3c2fc37 100755 --- a/project/src/config/Validation.php +++ b/project/src/config/Validation.php @@ -91,6 +91,13 @@ class Validation return $pendu; } + public static function valPosInt($val) : int { + if(is_int($val) && $val > 0) { + return 0; + } + return $val; + } + public static function valMdlScienceQuizz($scienceQuizz, array &$dVueErreur): MdlScienceQuizz { if(! $scienceQuizz instanceof MdlScienceQuizz){ diff --git a/project/src/controller/AdminController.php b/project/src/controller/AdminController.php index 2b9f4a8..0f1e7c1 100755 --- a/project/src/controller/AdminController.php +++ b/project/src/controller/AdminController.php @@ -1,5 +1,6 @@ render('admin/accueil.html'); - break; - case 'stats': - echo $twig->render('admin/stats.html'); - break; - case 'ajouterScientifiques': - $sexe = new MdlSexe(); - $theme = new MdlThematique(); - $diff = new MdlDifficulte(); - $scient=null; - if(!empty($_POST)){ - $id=0; - if(isset($_GET["id"])){ - $id=intval($_GET["id"]); - } - $sci = new Scientifique( - $id, - $_POST["name"], - $_POST["prenom"], - $_POST["url"], - \DateTime::createFromFormat("Y-m-d", $_POST["date"]), - $_POST["description"], - 0, - $theme->getFromId(intval($_POST["theme"])), - $diff->getFromId(intval($_POST["difficulte"])), - $sexe->getFromId(intval($_POST["sexe"])) - ); - $mdlsci=new MdlScientifique(); - if(isset($_GET["id"])){ - $mdlsci->editScientifique($sci); - } else { - $mdlsci->addScientifique($sci); - } - } - if(isset($_GET["id"])){ - $scient=new MdlScientifique(); - $scient=$scient->getScientifique($_GET["id"]); - } - - echo $twig->render('admin/ajouterScientifiques.html',['sexe' => $sexe->getAll(), 'themes' => $theme->getAll(), 'difficultes' => $diff->getAll(), 'scientifique' => $scient]); - break; - //mauvaise action - default: - $dVueErreur[] = "Erreur d'appel php"; - echo $twig->render('erreur.html', ['dVueErreur' => $dVueErreur]); - break; - } - } catch (\PDOException $e) { - $dVueErreur[] = 'Erreur avec la base de données !'; - echo $twig->render('erreur.html', ['dVueErreur' => $dVueErreur]); - } catch (\Exception $e2) { - $dVueErreur[] = 'Erreur inattendue !'; - echo $twig->render('erreur.html', ['dVueErreur' => $dVueErreur]); - } catch (\Throwable $e2) { - $dVueErreur[] = 'Erreur !'; - echo $twig->render('erreur.html', ['dVueErreur' => $dVueErreur]); - } - } - } - else if(isset($_SESSION["isLogged"])){ - //verifier si l'utilisateur est connecté mais pas admin - if($_SESSION["isLogged"]==true) { - //dire acces interdit aux non admins - $dVueErreur[] = 'Erreur 403 : Accès interdit !'; - echo $twig->render('erreur.html', ['dVueErreur' => $dVueErreur]); - exit(0); - } - } else { - //renvoyer a la page de connexion pour les non connectés - echo ''; - } - exit(0); - } - -} + public function defaultAction(array $params) { + global $twig; + + echo $twig->render('admin/accueil.html'); + } + public function notLogged(array $params) { + global $twig; + //dire acces interdit aux non admins + $dVueErreur[] = 'Erreur 403 : Accès interdit !'; + echo $twig->render('erreur.html', ['dVueErreur' => $dVueErreur]); + } + public function stats(array $params) { + global $twig; + + echo $twig->render('admin/stats.html'); + } + public function ajouterScientifiques(array $params) { + global $twig; + + $sexe = new MdlSexe(); + $theme = new MdlThematique(); + $diff = new MdlDifficulte(); + $scient=null; + if(!empty($_POST)){ + $id=0; + if(isset($_GET["id"])){ + $id=intval($_GET["id"]); + } + $sci = new Scientifique( + $id, + $_POST["name"], + $_POST["prenom"], + $_POST["url"], + \DateTime::createFromFormat("Y-m-d", $_POST["date"]), + $_POST["description"], + 0, + $theme->getFromId(intval($_POST["theme"])), + $diff->getFromId(intval($_POST["difficulte"])), + $sexe->getFromId(intval($_POST["sexe"])) + ); + $mdlsci=new MdlScientifique(); + if(isset($_GET["id"])){ + $mdlsci->editScientifique($sci); + } else { + $mdlsci->addScientifique($sci); + } + } + if(isset($_GET["id"])){ + $scient=new MdlScientifique(); + $scient=$scient->getScientifique($_GET["id"]); + } + echo $twig->render('admin/ajouterScientifiques.html',['sexe' => $sexe->getAll(), 'themes' => $theme->getAll(), 'difficultes' => $diff->getAll(), 'scientifique' => $scient]); + } + public function listeScientifiques(array $params) { + global $twig; + $ms = new MdlScientifique(); + if (!isset($params['id'])) { + $page = 1; + } else { + $page = Validation::valPosInt($params['id']); + } + $dVue['listeScientifiques'] = $ms->getScientifiquesParPage($page); + $dVue['pageMax'] = $ms->getMaxPages(); + $dVue['page'] = $page; + if ($page - 1 <= 0) { + $dVue['pagePrec'] = 1; + } else { + $dVue['pagePrec'] = $page - 1; + } + if ($page + 1 >= $dVue['pageMax']) { + $dVue['pageSuiv'] = $dVue['pageMax']; + } else { + $dVue['pageSuiv'] = $page + 1; + } + echo $twig->render('admin/listeScientifiques.html',['dVue' => $dVue]); + } + + +} ?> \ No newline at end of file diff --git a/project/src/controller/FrontController.php b/project/src/controller/FrontController.php index 4e04c55..3cae8f2 100755 --- a/project/src/controller/FrontController.php +++ b/project/src/controller/FrontController.php @@ -27,6 +27,7 @@ class FrontController global $twig, $router, $dVue; global $basePath; + global $dVueErreur; //altorouter $router = new AltoRouter(); @@ -36,7 +37,7 @@ class FrontController $router->map('GET|POST','/index.php','UserController'); $router->map('GET|POST','/pseudo/[a:action]?','PseudoController'); - $router->map('GET|POST','/admin/[a:action]','AdminController'); + $router->map('GET|POST','/admin/[a:action]?/[i:id]?','AdminController'); $router->map('GET|POST','/[a:action]?','UserController'); try { @@ -62,11 +63,11 @@ class FrontController break; case 'AdminController': - $action = $match['params']['action']; - //if (!MdlAdmin::isAdmin()) { - // $action = 'login'; - //} - new AdminController($action); + $action = $match['params']['action'] ?? ''; + if (!MdlAdmin::isAdmin()) { + $match['params']['action'] = 'notLogged'; + } + $this->callController('AdminController',$match); break; case 'PseudoController': @@ -78,15 +79,11 @@ class FrontController echo $twig->render('accueil.html', ['dVueErreur' => $dVueErreur]); break; } - } catch (PDOException $e) { - $dVueErreur[] = 'Erreur avec la base de données !'; - $dVueErreur[] = $e->getMessage(); - echo $twig->render('erreur.html', ['dVueErreur' => $dVueErreur]); } catch (LoginException $e) { echo $twig->render('erreur.html', ['dVueErreur' => $dVueErreur]); echo $twig->render('login.html'); } catch (Exception $e2) { - $dVueErreur[] = 'Erreur inattendue !'.$e2->getMessage(); + $dVueErreur[] = 'Erreur inattendue !'; echo $twig->render('erreur.html', ['dVueErreur' => $dVueErreur]); } diff --git a/project/src/index.php b/project/src/index.php index f88cb65..21fb24f 100755 --- a/project/src/index.php +++ b/project/src/index.php @@ -16,7 +16,6 @@ // Tableau qui contient les messages d'erreur $dVueErreur = []; - $dVue = []; $dVue['basePath'] = $basePath; $cont = new FrontController(); diff --git a/project/src/model/gateways/AdminGateway.php b/project/src/model/gateways/AdminGateway.php index f2837bf..e1b2a97 100755 --- a/project/src/model/gateways/AdminGateway.php +++ b/project/src/model/gateways/AdminGateway.php @@ -7,8 +7,7 @@ use PDOStatement; class AdminGateway { - private PDO $con; - private PDOStatement $stmt; + private Connection $con; public function __construct(Connection $con) { $this->con=$con; diff --git a/project/src/model/gateways/ScientifiqueGateway.php b/project/src/model/gateways/ScientifiqueGateway.php index 37c2595..d47dbb3 100755 --- a/project/src/model/gateways/ScientifiqueGateway.php +++ b/project/src/model/gateways/ScientifiqueGateway.php @@ -48,6 +48,23 @@ class ScientifiqueGateway ]); } + public function getScientifiquesParPages(int $currentPage, int $nbElemByPage) : array { + $query = 'SELECT * FROM Scientifique LIMIT :nbElem OFFSET :ind '; + $index = ($currentPage-1)*$nbElemByPage; + $this->con->executeQuery($query,array( + ':ind' => array($index,\PDO::PARAM_INT), + ':nbElem' => array($nbElemByPage,\PDO::PARAM_INT) + )); + return $this->con->getResults(); + } + + public function getNbScientifique() : int { + $query = 'SELECT DISTINCT count(*) as val FROM Scientifique'; + $this->con->executeQuery($query); + return $this->con->getResults()[0]['val']; + } + + /** * @throws Exception */ diff --git a/project/src/model/gateways/UtilisateurConnecteGateway.php b/project/src/model/gateways/UtilisateurConnecteGateway.php new file mode 100644 index 0000000..066483e --- /dev/null +++ b/project/src/model/gateways/UtilisateurConnecteGateway.php @@ -0,0 +1,25 @@ +con = $con; + } + + public function login(string $email, string $password): bool + { + $sql = "SELECT * FROM Utilisateur WHERE email=:email"; + $this->con->executeQuery($sql, array( + ':email' => array($email, \PDO::PARAM_STR) + )); + + $result = $this->con->getOneResult(); + + if (!empty($result)) { + return password_verify($password,$result['password']); + } + return false; + } +} \ No newline at end of file diff --git a/project/src/model/mdl/MdlAdmin.php b/project/src/model/mdl/MdlAdmin.php index e189c50..ea3a10f 100755 --- a/project/src/model/mdl/MdlAdmin.php +++ b/project/src/model/mdl/MdlAdmin.php @@ -10,15 +10,20 @@ class MdlAdmin extends MdlBase{ $this->gw = new AdminGateway($this->con); } public function login(string $username, string $password): bool{ - return $this->gw->login($username, $password); + if ($this->gw->login($username, $password)) { + $_SESSION['pseudo'] = $username; + $_SESSION['admin'] = true; + return true; + } + return false; } public static function isAdmin(): bool { if(!isset($_SESSION['admin']) || !$_SESSION['admin'] - || !isset($_SESSION['email']) - || $_SESSION['email'] == null) { + || !isset($_SESSION['pseudo']) + || $_SESSION['pseudo'] == null) { return false; } diff --git a/project/src/model/mdl/MdlScientifique.php b/project/src/model/mdl/MdlScientifique.php index 33d6378..dec31da 100755 --- a/project/src/model/mdl/MdlScientifique.php +++ b/project/src/model/mdl/MdlScientifique.php @@ -50,11 +50,40 @@ class MdlScientifique extends MdlBase{ return $this->gw->addScientifique($s); } - /** - * @throws Exception - */ - public function editScientifique(Scientifique $s): bool - { + public function getScientifiquesParPage(int $page) : array { + $nbElemParPage = 20; + $pageMax = ceil($this->gw->getNbScientifique()/$nbElemParPage); + if ($page <= 0) { + $page = 1; + } elseif ($page > $pageMax) { + $page = $pageMax; + } + $result = $this->gw->getScientifiquesParPages($page,$nbElemParPage); + $scientifiques = array(); + foreach ($result as $scientifique) { + $sexe = $this->mdlSexe->getFromId($scientifique['idsexe']); + $difficulte = $this->mdlDifficulte->getFromId($scientifique['iddifficulte']); + $thematique = $this->mdlThematique->getFromId($scientifique['idthematique']); + $scientifiques[] = new Scientifique($scientifique['id'], + $scientifique['nom'], + $scientifique['prenom'], + $scientifique['photo'], + new DateTime($scientifique['datenaissance']), + $scientifique['descriptif'], + $scientifique['ratiotrouvee'], + $thematique, + $difficulte, + $sexe); + } + return $scientifiques; + } + + public function getMaxPages() : int { + $nbElemParPage = 20; + return ceil($this->gw->getNbScientifique()/$nbElemParPage); + } + + public function editScientifique(Scientifique $s){ return $this->gw->editScientifique($s); } diff --git a/project/src/model/mdl/MdlUser.php b/project/src/model/mdl/MdlUser.php index 7693ed5..1638684 100755 --- a/project/src/model/mdl/MdlUser.php +++ b/project/src/model/mdl/MdlUser.php @@ -3,11 +3,11 @@ namespace model; class MdlUser extends MdlBase{ - private UserGateway $gw; + private JoueurGateway $gw; public function __construct(){ parent::__construct(); - $this->gw = new UserGateway($this->con); + $this->gw = new UtilisateurConnecteGateway($this->con); } public function login(string $username, string $password): bool{ return $this->gw->login($username, $password); diff --git a/project/src/model/metier/Difficulte.php b/project/src/model/metier/Difficulte.php index 838d89a..e511320 100755 --- a/project/src/model/metier/Difficulte.php +++ b/project/src/model/metier/Difficulte.php @@ -30,4 +30,8 @@ class Difficulte public function getLibelle(): string{ return $this->libelle; } + + public function __toString() { + return $this->libelle; + } } \ No newline at end of file diff --git a/project/src/model/metier/Sexe.php b/project/src/model/metier/Sexe.php index 86e6f89..740ab33 100755 --- a/project/src/model/metier/Sexe.php +++ b/project/src/model/metier/Sexe.php @@ -22,4 +22,9 @@ class Sexe { return $this->libelle; } + + public function __toString() + { + return $this->libelle; + } } \ No newline at end of file diff --git a/project/src/model/metier/Thematique.php b/project/src/model/metier/Thematique.php index 7258408..69fe77d 100755 --- a/project/src/model/metier/Thematique.php +++ b/project/src/model/metier/Thematique.php @@ -22,4 +22,9 @@ class Thematique { return $this->libelle; } + + public function __toString() + { + return $this->libelle; + } } \ No newline at end of file diff --git a/project/src/templates/admin/accueil.html b/project/src/templates/admin/accueil.html index 71ebfb5..c98e2b6 100644 --- a/project/src/templates/admin/accueil.html +++ b/project/src/templates/admin/accueil.html @@ -17,7 +17,9 @@

{{dVue.pseudo}}





- Ajouter Scientifiques + Ajouter Scientifiques +
+ Lister les scientifiques
diff --git a/project/src/templates/admin/ajouterScientifiques.html b/project/src/templates/admin/ajouterScientifiques.html index a74d431..700550d 100644 --- a/project/src/templates/admin/ajouterScientifiques.html +++ b/project/src/templates/admin/ajouterScientifiques.html @@ -2,7 +2,7 @@ - Créer un.e scientifique + Ajouter un(e) scientifique + Liste des scientifiques + + + +

Voici la liste des scientifiques dans la base de données :

+

{{dVue.pseudo}}

+ +



+ +
+ Ajouter un(e) scientifique + {% for scientifique in dVue.listeScientifiques %} +
+

{{ scientifique.getNom() }} {{ scientifique.getPrenom() }}

+

Sexe : {{ scientifique.getSexe() }}

+

Né(e) le {{ scientifique.getDateNess() }}

+

Thematique : {{ scientifique.getThematique() }}

+

Difficulté à trouver : {{ scientifique.getDifficulte() }}

+ Modifier +
+
+
+ {% endfor %} +
+ < + 1 +

{{ dVue.page }}

+ 1 + > +
+
+ + + +