Paginator.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 Minz_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. * $nbItems le nombre d'éléments
  28. */
  29. private $nbItems = 0;
  30. /**
  31. * Constructeur
  32. * @param $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 $view nom du fichier de vue situé dans /app/views/helpers/
  43. * @param $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 $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 $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 $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 $all si à true, retourne tous les éléments sans prendre en compte la pagination
  100. */
  101. public function items ($all = false) {
  102. $array = array ();
  103. $nbItems = $this->nbItems ();
  104. if ($nbItems <= $this->nbItemsPerPage || $all) {
  105. $array = $this->items;
  106. } else {
  107. $begin = ($this->currentPage - 1) * $this->nbItemsPerPage;
  108. $counter = 0;
  109. $i = 0;
  110. foreach ($this->items as $key => $item) {
  111. if ($i >= $begin) {
  112. $array[$key] = $item;
  113. $counter++;
  114. }
  115. if ($counter >= $this->nbItemsPerPage) {
  116. break;
  117. }
  118. $i++;
  119. }
  120. }
  121. return $array;
  122. }
  123. public function nbItemsPerPage () {
  124. return $this->nbItemsPerPage;
  125. }
  126. public function currentPage () {
  127. return $this->currentPage;
  128. }
  129. public function nbPage () {
  130. return $this->nbPage;
  131. }
  132. public function nbItems () {
  133. return $this->nbItems;
  134. }
  135. /**
  136. * SETTEURS
  137. */
  138. public function _items ($items) {
  139. if (is_array ($items)) {
  140. $this->items = $items;
  141. }
  142. $this->_nbPage ();
  143. }
  144. public function _nbItemsPerPage ($nbItemsPerPage) {
  145. if ($nbItemsPerPage > $this->nbItems ()) {
  146. $nbItemsPerPage = $this->nbItems ();
  147. }
  148. if ($nbItemsPerPage < 0) {
  149. $nbItemsPerPage = 0;
  150. }
  151. $this->nbItemsPerPage = $nbItemsPerPage;
  152. $this->_nbPage ();
  153. }
  154. public function _currentPage ($page) {
  155. if($page < 1 || ($page > $this->nbPage && $this->nbPage > 0)) {
  156. throw new CurrentPagePaginationException ($page);
  157. }
  158. $this->currentPage = $page;
  159. }
  160. private function _nbPage () {
  161. if ($this->nbItemsPerPage > 0) {
  162. $this->nbPage = ceil ($this->nbItems () / $this->nbItemsPerPage);
  163. }
  164. }
  165. public function _nbItems ($value) {
  166. $this->nbItems = $value;
  167. }
  168. }