Paginator.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 la page à laquelle se trouve l'élément (false si non trouvé)
  55. */
  56. public function pageByItem ($item) {
  57. $page = false;
  58. $i = 0;
  59. do {
  60. if ($item == $this->items[$i]) {
  61. $page = ceil (($i + 1) / $this->nbItemsPerPage);
  62. }
  63. $i++;
  64. } while (!$page && $i < $this->nbItems ());
  65. return $page;
  66. }
  67. /**
  68. * Permet de retrouver la position d'un élément donné (à partir de 0)
  69. * @param $item l'élément à retrouver
  70. * @return la position à laquelle se trouve l'élément (false si non trouvé)
  71. */
  72. public function positionByItem ($item) {
  73. $find = false;
  74. $i = 0;
  75. do {
  76. if ($item == $this->items[$i]) {
  77. $find = true;
  78. } else {
  79. $i++;
  80. }
  81. } while (!$find && $i < $this->nbItems ());
  82. return $i;
  83. }
  84. /**
  85. * Permet de récupérer un item par sa position
  86. * @param $pos la position de l'élément
  87. * @return l'item situé à $pos (dernier item si $pos<0, 1er si $pos>=count($items))
  88. */
  89. public function itemByPosition ($pos) {
  90. if ($pos < 0) {
  91. $pos = $this->nbItems () - 1;
  92. }
  93. if ($pos >= count($this->items)) {
  94. $pos = 0;
  95. }
  96. return $this->items[$pos];
  97. }
  98. /**
  99. * GETTEURS
  100. */
  101. /**
  102. * @param $all si à true, retourne tous les éléments sans prendre en compte la pagination
  103. */
  104. public function items ($all = false) {
  105. $array = array ();
  106. $nbItems = $this->nbItems ();
  107. if ($nbItems <= $this->nbItemsPerPage || $all) {
  108. $array = $this->items;
  109. } else {
  110. $begin = ($this->currentPage - 1) * $this->nbItemsPerPage;
  111. $counter = 0;
  112. $i = 0;
  113. foreach ($this->items as $key => $item) {
  114. if ($i >= $begin) {
  115. $array[$key] = $item;
  116. $counter++;
  117. }
  118. if ($counter >= $this->nbItemsPerPage) {
  119. break;
  120. }
  121. $i++;
  122. }
  123. }
  124. return $array;
  125. }
  126. public function nbItemsPerPage () {
  127. return $this->nbItemsPerPage;
  128. }
  129. public function currentPage () {
  130. return $this->currentPage;
  131. }
  132. public function nbPage () {
  133. return $this->nbPage;
  134. }
  135. public function nbItems () {
  136. return $this->nbItems;
  137. }
  138. /**
  139. * SETTEURS
  140. */
  141. public function _items ($items) {
  142. if (is_array ($items)) {
  143. $this->items = $items;
  144. }
  145. $this->_nbPage ();
  146. }
  147. public function _nbItemsPerPage ($nbItemsPerPage) {
  148. if ($nbItemsPerPage > $this->nbItems ()) {
  149. $nbItemsPerPage = $this->nbItems ();
  150. }
  151. if ($nbItemsPerPage < 0) {
  152. $nbItemsPerPage = 0;
  153. }
  154. $this->nbItemsPerPage = $nbItemsPerPage;
  155. $this->_nbPage ();
  156. }
  157. public function _currentPage ($page) {
  158. if($page < 1 || ($page > $this->nbPage && $this->nbPage > 0)) {
  159. throw new CurrentPagePaginationException ($page);
  160. }
  161. $this->currentPage = $page;
  162. }
  163. private function _nbPage () {
  164. if ($this->nbItemsPerPage > 0) {
  165. $this->nbPage = ceil ($this->nbItems () / $this->nbItemsPerPage);
  166. }
  167. }
  168. public function _nbItems ($value) {
  169. $this->nbItems = $value;
  170. }
  171. }