Paginator.php 3.9 KB

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