diff --git a/src/App/Pagination.php b/src/App/Pagination.php index b784194..6e25b8a 100644 --- a/src/App/Pagination.php +++ b/src/App/Pagination.php @@ -2,32 +2,58 @@ namespace App; -Class Pagination{ - - private $cnx; - private $statement; - private $attr; - private $perpage; - private $count; - private $router; - private $flash; - - public function __construct( - $cnx, - mixed $router, - string $statement, - ?int $attr = null, - ?int $perpage = 10, - mixed $flash = null - ) - { - $this->cnx = $cnx; - $this->statement = $statement; - $this->attr = $attr; - $this->perpage = $perpage; - $this->router = $router; - $this->flash = $flash; - } +/** + * Class Pagination + * Gère la pagination SQL et HTML pour les listes (forums, réponses, etc.). + */ +class Pagination +{ + /** @var \PDO */ + private $cnx; + + /** @var string */ + private $statement; + + /** @var mixed|null */ + private $attr; + + /** @var int */ + private $perpage; + + /** @var mixed */ + private $count; + + /** @var mixed */ + private $router; + + /** @var mixed */ + private $flash; + + /** + * Pagination constructor. + * + * @param \PDO $cnx Connexion PDO + * @param mixed $router Router pour générer des routes + * @param string $statement Nom de la requête SQL + * @param int|null $attr Paramètre de filtre éventuel + * @param int|null $perpage Résultats par page + * @param mixed|null $flash Service de flash message + */ + public function __construct( + $cnx, + mixed $router, + string $statement, + ?int $attr = null, + ?int $perpage = 10, + mixed $flash = null + ) { + $this->cnx = $cnx; + $this->statement = $statement; + $this->attr = $attr; + $this->perpage = $perpage; + $this->router = $router; + $this->flash = $flash; + } /** * Perpage @@ -102,7 +128,7 @@ private function getPositiveInt(string $name, ?int $default = null): ?int * * @return void */ - public function isExistPage() + public function isExistPage(): void { $match = $this->router->matchRoute(); if($this->CurrentPage() > $this->PageTotal() && $this->CurrentPage() > 1) { @@ -117,7 +143,9 @@ public function isExistPage() } } - + /** + * Retourne la page courante (GET ?page=...). + */ public function CurrentPage(): int { return $this->getPositiveInt('page', 1); @@ -136,13 +164,8 @@ public function PageTotal() /** * userLinkPage retourn un lien qui redirige vers la dernière réponse - * - * @param mixed $id - * @param mixed $idrep - * @param mixed $countid - * @return void */ - public function userLinkPage(string $id,int $idrep,int $countid) + public function userLinkPage(int $id,int $idrep,int $countid): string { $t = (int) ceil($countid/$this->perpage); if($this->perpage >= 1){ @@ -155,27 +178,26 @@ public function userLinkPage(string $id,int $idrep,int $countid) return $userLinkPage; } - /** - * offset - * - * @return int - */ + /** + * Calcule l'offset SQL. + */ private function offset(): int { return $this->perpage * ($this->CurrentPage() - 1); } - /** - * setOffset - * - * @return void - */ - public function setOffset() + /** + * Retourne la clause SQL OFFSET LIMIT. + */ + public function setOffset(): string { return ' '. intval($this->perpage) .' OFFSET '. intval($this->offset()); } - public function Prev($url) + /** + * Génère le bouton "Page précédente" pour une pagination classique. + */ + public function Prev(string $url): ?string { $match = $this->router->matchRoute(); if($this->PageTotal() >= 2): @@ -192,7 +214,10 @@ public function Prev($url) endif; } - public function Next($url) + /** + * Génère le bouton "Page suivante" pour une pagination classique. + */ + public function Next(string $url): ?string { $match = $this->router->matchRoute(); if($this->PageTotal() >= 2): @@ -207,67 +232,101 @@ public function Next($url) endif; } - /* - * génère un lien qui pointe vers la bonne page - * javascript si les liens généré sont superieur a 10 alors faire une fonction pour slider les nombres avec les flèche - */ - public function tinyLinkPage(int $id,int $countid) + /** + * Génère une mini-pagination en carrousel (tinyPagination). + * Affiche un nombre fixe de pages visibles avec des flèches pour défiler. + */ + public function tinyPagination(int $id, int $countid) { - $t = (int) ceil($countid/$this->perpage); - if($t > 1){ + $t = (int) ceil($countid / $this->perpage); + if ($t > 1) { echo '