diff --git a/TP4/ex1/controller/ControleurLivre.php b/TP4/ex1/controller/ControleurLivre.php new file mode 100644 index 0000000000000000000000000000000000000000..52cbc8602e9de3e5a99ce4d75080631dbd2a07a0 --- /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 9c113ffb562f43c42a979d5a70042fae9d49afe4..17c98ca6cbdfef2a3129d2c8c23fb5a85fd9cb08 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 60de2e832697f8ac68b66a4611fd1e6323a69902..5b85281c96c52fb9db425d800f4c912b12a6c429 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 0cccdf3b30b20684f9ddb7f29623c10c36b9445b..91e7a832d672b3ef2b5af487cff7edc07f703445 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 217691b61ce37ddf7426d457d19aa91da625bf01..664a9910446860ac1e5a3c5313fb2c9029c33eba 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 37bb1ecf7f379c77898db22b3cd94a664e090233..dcc3dc3d7daacd81a3ce5ef1cb7145dcf8bb16c8 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 cda28a458688619ff987c1c649dfdfa3c396be30..542d9600855951d0a48daec394598139ebfe3d6e 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 0000000000000000000000000000000000000000..f33924d96df79a14544660d2a0a209428f3f889d --- /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 0000000000000000000000000000000000000000..ef0c1b6ca8169fb50c926d6eae738fe49adc177e --- /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