From 7d8eb40879af5456500a7e76fd28ce7b64d605fb Mon Sep 17 00:00:00 2001 From: "renaud.beuret" Date: Tue, 21 Nov 2023 19:29:08 +0100 Subject: [PATCH] ADD : liste de scientifique pour l'admin --- project/src/config/Validation.php | 7 +++ project/src/config/config.php | 6 +-- project/src/controller/AdminController.php | 38 ++++++++++++--- project/src/controller/FrontController.php | 11 ++--- project/src/index.php | 1 - .../model/gateways/ScientifiqueGateway.php | 16 +++++++ .../src/model/gateways/ScientistGateway.php | 16 ------- project/src/model/mdl/MdlScientifique.php | 33 +++++++++++++ project/src/model/metier/Difficulte.php | 4 ++ project/src/model/metier/Sexe.php | 5 ++ project/src/model/metier/Thematique.php | 5 ++ project/src/templates/admin/accueil.html | 4 +- .../templates/admin/ajouterScientifiques.html | 2 +- .../templates/admin/listeScientifiques.html | 46 +++++++++++++++++++ 14 files changed, 159 insertions(+), 35 deletions(-) delete mode 100755 project/src/model/gateways/ScientistGateway.php create mode 100644 project/src/templates/admin/listeScientifiques.html diff --git a/project/src/config/Validation.php b/project/src/config/Validation.php index 7648117..268b09d 100755 --- a/project/src/config/Validation.php +++ b/project/src/config/Validation.php @@ -90,4 +90,11 @@ class Validation return $pendu; } + public static function valPosInt($val) : int { + if(is_int($val) && $val > 0) { + return 0; + } + return $val; + } + } diff --git a/project/src/config/config.php b/project/src/config/config.php index 4194993..e525689 100755 --- a/project/src/config/config.php +++ b/project/src/config/config.php @@ -4,9 +4,9 @@ namespace config; $config = [ "rep" => __DIR__.'/../', - "db" => ["dsn" => 'pgsql:host=localhost;dbname=dbrebeuret', - "login" => 'rebeuret', - "mdp" => 'achanger'], + "db" => ["dsn" => 'pgsql:host=localhost;dbname=sae2a', + "login" => 'postgres', + "mdp" => 'SKf43V4hmD7a'], "templates" => ["index" => 'vues/index.php', "pseudo" => 'pseudo.html', "jouer" => "jouer.html", diff --git a/project/src/controller/AdminController.php b/project/src/controller/AdminController.php index 9cc7486..2ababbc 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(); @@ -47,15 +52,36 @@ class AdminController { } echo $twig->render('admin/ajouterScientifiques.html',['sexe' => $sexe->getAll(), 'themes' => $theme->getAll(), 'difficultes' => $diff->getAll()]); break; - //mauvaise action + + case 'listeScientifiques': + $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]); + 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]); diff --git a/project/src/controller/FrontController.php b/project/src/controller/FrontController.php index 4ea36d4..52e8189 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'); session_start(); @@ -58,11 +59,11 @@ class FrontController break; case 'AdminController': - $action = $match['params']['action']; + $action = $match['params']['action'] ?? ''; //if (!MdlAdmin::isAdmin()) { // $action = 'login'; //} - new AdminController($action); + new AdminController($match['params']); break; case 'PseudoController': @@ -74,10 +75,6 @@ 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'); 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/ScientifiqueGateway.php b/project/src/model/gateways/ScientifiqueGateway.php index b48c8d9..3fa1519 100755 --- a/project/src/model/gateways/ScientifiqueGateway.php +++ b/project/src/model/gateways/ScientifiqueGateway.php @@ -41,4 +41,20 @@ class ScientifiqueGateway ":idSexe"=>[$sci->getSexe()->getId(),$this->con::PARAM_STR] ]); } + + 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']; + } } \ No newline at end of file diff --git a/project/src/model/gateways/ScientistGateway.php b/project/src/model/gateways/ScientistGateway.php deleted file mode 100755 index d200eec..0000000 --- a/project/src/model/gateways/ScientistGateway.php +++ /dev/null @@ -1,16 +0,0 @@ -con = $co; - } - -// function findByName(string $name) :? Scientist { -// $usr = null; -// } -} \ No newline at end of file diff --git a/project/src/model/mdl/MdlScientifique.php b/project/src/model/mdl/MdlScientifique.php index bcac636..8c389b8 100755 --- a/project/src/model/mdl/MdlScientifique.php +++ b/project/src/model/mdl/MdlScientifique.php @@ -44,4 +44,37 @@ class MdlScientifique extends MdlBase{ public function addScientifique(Scientifique $s){ return $this->gw->addScientifique($s); } + + 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); + } } \ No newline at end of file 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 82e80d9..ef3a6a7 100644 --- a/project/src/templates/admin/ajouterScientifiques.html +++ b/project/src/templates/admin/ajouterScientifiques.html @@ -2,7 +2,7 @@ - Créer une partie + 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 + > +
+
+ + + +