Paginator.php 4.1 KB

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