From 180b293b8c2b6eedb47679db57d564f8f167b584 Mon Sep 17 00:00:00 2001 From: Sofiane Lasri <alasri250@gmail.com> Date: Thu, 27 Oct 2022 22:55:27 +0200 Subject: [PATCH] Fin de TP4 ex3. --- TP4/ex1/controller/ControleurLivre.php | 30 +++++++++ TP4/ex1/index.php | 5 ++ TP4/ex1/models/Adherent.php | 3 +- TP4/ex1/models/Livre.php | 65 +++++++++++++++++-- .../resources/views/adherents/un-adherent.php | 2 +- TP4/ex1/resources/views/auteurs/un-auteur.php | 2 +- TP4/ex1/resources/views/components/navbar.php | 3 + TP4/ex1/resources/views/livres/les-livres.php | 25 +++++++ TP4/ex1/resources/views/livres/un-livre.php | 10 +++ 9 files changed, 135 insertions(+), 10 deletions(-) create mode 100644 TP4/ex1/controller/ControleurLivre.php create mode 100644 TP4/ex1/resources/views/livres/les-livres.php create mode 100644 TP4/ex1/resources/views/livres/un-livre.php diff --git a/TP4/ex1/controller/ControleurLivre.php b/TP4/ex1/controller/ControleurLivre.php new file mode 100644 index 0000000..52cbc86 --- /dev/null +++ b/TP4/ex1/controller/ControleurLivre.php @@ -0,0 +1,30 @@ +<?php +require_once("models/Livre.php"); + +class ControleurLivre +{ + /** + * Charge la page de la liste des adhérents. + * @return string + * @throws Exception + */ + public static function lireLivres(): string + { + $livres = Livre::getAllLivres(); + return view('livres.les-livres', ['livres' => $livres]); + } + + /** + * Charge la page du détail d'un adhérent. + * @return string + * @throws Exception + */ + public static function lireLivre(): string + { + if (empty($_GET["numLivre"])) { + die("Le paramètre numLivre n'est pas spécifié."); + } + $livre = Livre::getLivreByNumLivre($_GET["numLivre"]); + return view('livres.un-livre', ['livre' => $livre]); + } +} \ No newline at end of file diff --git a/TP4/ex1/index.php b/TP4/ex1/index.php index 9c113ff..17c98ca 100644 --- a/TP4/ex1/index.php +++ b/TP4/ex1/index.php @@ -10,6 +10,8 @@ require_once("app/models/Database.php"); require_once("controller/ControleurAuteur.php"); require_once("controller/ControleurAdherent.php"); +require_once("controller/ControleurLivre.php"); + Database::connect(); if (empty($_GET["action"])) { @@ -21,6 +23,9 @@ if (empty($_GET["action"])) { } elseif (in_array($_GET["action"], get_class_methods('ControleurAdherent'))) { $action = $_GET["action"]; echo ControleurAdherent::$action(); + } elseif (in_array($_GET["action"], get_class_methods('ControleurLivre'))) { + $action = $_GET["action"]; + echo ControleurLivre::$action(); } } diff --git a/TP4/ex1/models/Adherent.php b/TP4/ex1/models/Adherent.php index 60de2e8..5b85281 100644 --- a/TP4/ex1/models/Adherent.php +++ b/TP4/ex1/models/Adherent.php @@ -210,7 +210,8 @@ class Adherent /** * @return void */ - public function afficher(){ + public function afficher(): void + { echo "<p>" . $this->prenomAdherent . " " . $this->nomAdherent . " a rejoint le " .$this->dateAdhesion->format('Y-m-d H:i:s'); } diff --git a/TP4/ex1/models/Livre.php b/TP4/ex1/models/Livre.php index 0cccdf3..91e7a83 100644 --- a/TP4/ex1/models/Livre.php +++ b/TP4/ex1/models/Livre.php @@ -2,7 +2,7 @@ class Livre { - private int $nulLibre; + private int $numLivre; private string $titre; private int $anneeParution; private int $numGenre; @@ -15,26 +15,68 @@ class Livre */ public function __construct(int $numLivre, string $titre, int $anneeParution, int $numGenre) { - $this->nulLibre = $numLivre; + $this->numLivre = $numLivre; $this->titre = $titre; $this->anneeParution = $anneeParution; $this->numGenre = $numGenre; } + /** + * @return array + */ + public static function getAllLivres(): array + { + $query = Database::pdo()->query("SELECT * FROM Livre"); + $query->setFetchmode(PDO::FETCH_ASSOC); + $queryResult = $query->fetchAll(); + + $tableau = array(); + foreach ($queryResult as $row) { + $tableau[] = new Livre($row['numLivre'], + $row['titre'], + $row['anneeParution'], + $row['numGenre']); + } + return $tableau; + } + + /** + * @param int $numLivre + * @return Livre + */ + public static function getLivreByNumLivre(int $numLivre): Livre + { + $query = Database::pdo()->prepare("SELECT * FROM Livre WHERE numLivre = :numLivre"); + $response = null; + try { + // envoi de la requête + $query->execute(['numLivre' => $numLivre]); + $query->setFetchmode(PDO::FETCH_ASSOC); + $queryResult = $query->fetch(); + $response = new Livre($queryResult['numLivre'], + $queryResult['titre'], + $queryResult['anneeParution'], + $queryResult['numGenre']); + } catch (PDOException $e) { + echo $e->getMessage(); + } + return $response; + } + /** * @return int */ - public function getNulLibre(): int + public function getNumLivre(): int { - return $this->nulLibre; + return $this->numLivre; } /** - * @param int $nulLibre + * @param int $numLivre */ - public function setNulLibre(int $nulLibre): void + public function setNumLivre(int $numLivre): void { - $this->nulLibre = $nulLibre; + $this->numLivre = $numLivre; } /** @@ -84,4 +126,13 @@ class Livre { $this->numGenre = $numGenre; } + + /** + * @return void + */ + public function afficher(): void + { + echo "<p>" . $this->titre . " est paru en " + . $this->anneeParution; + } } \ No newline at end of file diff --git a/TP4/ex1/resources/views/adherents/un-adherent.php b/TP4/ex1/resources/views/adherents/un-adherent.php index 217691b..664a991 100644 --- a/TP4/ex1/resources/views/adherents/un-adherent.php +++ b/TP4/ex1/resources/views/adherents/un-adherent.php @@ -1,6 +1,6 @@ @extends('layouts.page-layout') -@section('title', 'Info de l'adhérent') +@section('title', 'Information de l'adhérent') @section('content') <div class="container"> diff --git a/TP4/ex1/resources/views/auteurs/un-auteur.php b/TP4/ex1/resources/views/auteurs/un-auteur.php index 37bb1ec..dcc3dc3 100644 --- a/TP4/ex1/resources/views/auteurs/un-auteur.php +++ b/TP4/ex1/resources/views/auteurs/un-auteur.php @@ -1,6 +1,6 @@ @extends('layouts.page-layout') -@section('title', 'Info de l'auteur') +@section('title', 'Information de l'auteur') @section('content') <div class="container"> diff --git a/TP4/ex1/resources/views/components/navbar.php b/TP4/ex1/resources/views/components/navbar.php index cda28a4..542d960 100644 --- a/TP4/ex1/resources/views/components/navbar.php +++ b/TP4/ex1/resources/views/components/navbar.php @@ -14,6 +14,9 @@ <li class="nav-item"> <a class="nav-link" href="?action=lireAdherents">Liste des adéherents</a> </li> + <li class="nav-item"> + <a class="nav-link" href="?action=lireLivres">Liste des livres</a> + </li> </ul> </div> </div> diff --git a/TP4/ex1/resources/views/livres/les-livres.php b/TP4/ex1/resources/views/livres/les-livres.php new file mode 100644 index 0000000..f33924d --- /dev/null +++ b/TP4/ex1/resources/views/livres/les-livres.php @@ -0,0 +1,25 @@ +@extends('layouts.page-layout') + +@section('title', 'Liste des livres') + +@section('content') + <div class="container"> + <h1>Liste de tous les livres</h1> + <p>Voici la liste de tous les livres</p> + <div class="d-flex flex-wrap justify-content-between"> + @foreach($livres as $livre) + <div class="card mb-2" style="width: 18rem;"> + <div class="card-body"> + <h5 class="card-title">{{ $livre->getTitre() }}</h5> + <p class="card-text">ID: {{ $livre->getNumLivre() }}<br> + Année de parution: {{ $livre->getAnneeParution() }}</p> + <a class="btn btn-primary" + href="index.php?action=lireLivre&numLivre={{ $livre->getNumLivre() }}"> + Lire les détails + </a> + </div> + </div> + @endforeach + </div> + </div> +@endsection \ No newline at end of file diff --git a/TP4/ex1/resources/views/livres/un-livre.php b/TP4/ex1/resources/views/livres/un-livre.php new file mode 100644 index 0000000..ef0c1b6 --- /dev/null +++ b/TP4/ex1/resources/views/livres/un-livre.php @@ -0,0 +1,10 @@ +@extends('layouts.page-layout') + +@section('title', 'Information du livre') + +@section('content') + <div class="container"> + <h1>Information du livre</h1> + {{ $livre->afficher() }} + </div> +@endsection \ No newline at end of file -- GitLab