Paginator.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * MINZ - Copyright 2011 Marien Fressinaud
  5. * Sous licence AGPL3 <http://www.gnu.org/licenses/>
  6. */
  7. /**
  8. * The Minz_Paginator is used to handle paging
  9. */
  10. class Minz_Paginator {
  11. /**
  12. * @var list<Minz_Model> tableau des éléments à afficher/gérer
  13. */
  14. private array $items = [];
  15. /**
  16. * le nombre d'éléments par page
  17. */
  18. private int $nbItemsPerPage = 10;
  19. /**
  20. * page actuelle à gérer
  21. */
  22. private int $currentPage = 1;
  23. /**
  24. * le nombre de pages de pagination
  25. */
  26. private int $nbPage = 1;
  27. /**
  28. * le nombre d'éléments
  29. */
  30. private int $nbItems = 0;
  31. /**
  32. * Constructeur
  33. * @param list<Minz_Model> $items les éléments à gérer
  34. */
  35. public function __construct(array $items) {
  36. $this->_items($items);
  37. $this->_nbItems(count($this->items(true)));
  38. $this->_nbItemsPerPage($this->nbItemsPerPage);
  39. $this->_currentPage($this->currentPage);
  40. }
  41. /**
  42. * Permet d'afficher la pagination
  43. * @param string $view nom du fichier de vue situé dans /app/views/helpers/
  44. * @param string $getteur variable de type $_GET[] permettant de retrouver la page
  45. */
  46. public function render(string $view, string $getteur = 'page'): void {
  47. $view = APP_PATH . '/views/helpers/' . $view;
  48. if (file_exists($view)) {
  49. include $view;
  50. }
  51. }
  52. /**
  53. * Permet de retrouver la page d'un élément donné
  54. * @param Minz_Model $item l'élément à retrouver
  55. * @return int|false la page à laquelle se trouve l’élément, false si non trouvé
  56. */
  57. public function pageByItem(Minz_Model $item): int|false {
  58. $i = 0;
  59. do {
  60. if ($item === $this->items[$i]) {
  61. return (int)(ceil(($i + 1) / $this->nbItemsPerPage));
  62. }
  63. $i++;
  64. } while ($i < $this->nbItems());
  65. return false;
  66. }
  67. /**
  68. * Search the position (index) of a given element
  69. * @param Minz_Model $item the element to search
  70. * @return int|false the position of the element, or false if not found
  71. */
  72. public function positionByItem(Minz_Model $item): int|false {
  73. $i = 0;
  74. do {
  75. if ($item === $this->items[$i]) {
  76. return $i;
  77. }
  78. $i++;
  79. } while ($i < $this->nbItems());
  80. return false;
  81. }
  82. /**
  83. * Permet de récupérer un item par sa position
  84. * @param int $pos la position de l'élément
  85. * @return Minz_Model item situé à $pos (dernier item si $pos<0, 1er si $pos>=count($items))
  86. */
  87. public function itemByPosition(int $pos): Minz_Model {
  88. if ($pos < 0) {
  89. $pos = $this->nbItems() - 1;
  90. }
  91. if ($pos >= count($this->items)) {
  92. $pos = 0;
  93. }
  94. return $this->items[$pos];
  95. }
  96. /**
  97. * GETTEURS
  98. */
  99. /**
  100. * @param bool $all si à true, retourne tous les éléments sans prendre en compte la pagination
  101. * @return list<Minz_Model>
  102. */
  103. public function items(bool $all = false): array {
  104. $array = [];
  105. $nbItems = $this->nbItems();
  106. if ($nbItems <= $this->nbItemsPerPage || $all) {
  107. $array = $this->items;
  108. } else {
  109. $begin = ($this->currentPage - 1) * $this->nbItemsPerPage;
  110. $counter = 0;
  111. $i = 0;
  112. foreach ($this->items as $item) {
  113. if ($i >= $begin) {
  114. $array[] = $item;
  115. $counter++;
  116. }
  117. if ($counter >= $this->nbItemsPerPage) {
  118. break;
  119. }
  120. $i++;
  121. }
  122. }
  123. return $array;
  124. }
  125. public function nbItemsPerPage(): int {
  126. return $this->nbItemsPerPage;
  127. }
  128. public function currentPage(): int {
  129. return $this->currentPage;
  130. }
  131. public function nbPage(): int {
  132. return $this->nbPage;
  133. }
  134. public function nbItems(): int {
  135. return $this->nbItems;
  136. }
  137. /**
  138. * SETTEURS
  139. */
  140. /** @param list<Minz_Model> $items */
  141. public function _items(?array $items): void {
  142. $this->items = $items ?? [];
  143. $this->_nbPage();
  144. }
  145. public function _nbItemsPerPage(int $nbItemsPerPage): void {
  146. if ($nbItemsPerPage > $this->nbItems()) {
  147. $nbItemsPerPage = $this->nbItems();
  148. }
  149. if ($nbItemsPerPage < 0) {
  150. $nbItemsPerPage = 0;
  151. }
  152. $this->nbItemsPerPage = $nbItemsPerPage;
  153. $this->_nbPage();
  154. }
  155. public function _currentPage(int $page): void {
  156. if ($page < 1 || ($page > $this->nbPage && $this->nbPage > 0)) {
  157. throw new Minz_CurrentPagePaginationException($page);
  158. }
  159. $this->currentPage = $page;
  160. }
  161. private function _nbPage(): void {
  162. if ($this->nbItemsPerPage > 0) {
  163. $this->nbPage = (int)ceil($this->nbItems() / $this->nbItemsPerPage);
  164. }
  165. }
  166. public function _nbItems(int $value): void {
  167. $this->nbItems = $value;
  168. }
  169. }