| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- <?php
- /**
- * MINZ - Copyright 2011 Marien Fressinaud
- * Sous licence AGPL3 <http://www.gnu.org/licenses/>
- */
- /**
- * The Minz_Paginator is used to handle paging
- */
- class Minz_Paginator {
- /**
- * @var array<Minz_Model> tableau des éléments à afficher/gérer
- */
- private $items = array ();
- /**
- * @var int le nombre d'éléments par page
- */
- private $nbItemsPerPage = 10;
- /**
- * @var int page actuelle à gérer
- */
- private $currentPage = 1;
- /**
- * @var int le nombre de pages de pagination
- */
- private $nbPage = 1;
- /**
- * @var int le nombre d'éléments
- */
- private $nbItems = 0;
- /**
- * Constructeur
- * @param array<Minz_Model> $items les éléments à gérer
- */
- public function __construct ($items) {
- $this->_items ($items);
- $this->_nbItems (count ($this->items (true)));
- $this->_nbItemsPerPage ($this->nbItemsPerPage);
- $this->_currentPage ($this->currentPage);
- }
- /**
- * Permet d'afficher la pagination
- * @param string $view nom du fichier de vue situé dans /app/views/helpers/
- * @param int $getteur variable de type $_GET[] permettant de retrouver la page
- */
- public function render ($view, $getteur) {
- $view = APP_PATH . '/views/helpers/' . $view;
- if (file_exists ($view)) {
- include ($view);
- }
- }
- /**
- * Permet de retrouver la page d'un élément donné
- * @param Minz_Model $item l'élément à retrouver
- * @return float|false la page à laquelle se trouve l’élément, false si non trouvé
- */
- public function pageByItem ($item) {
- $i = 0;
- do {
- if ($item == $this->items[$i]) {
- return ceil(($i + 1) / $this->nbItemsPerPage);
- }
- $i++;
- } while ($i < $this->nbItems());
- return false;
- }
- /**
- * Permet de retrouver la position d'un élément donné (à partir de 0)
- * @param Minz_Model $item l'élément à retrouver
- * @return float|false la position à laquelle se trouve l’élément, false si non trouvé
- */
- public function positionByItem ($item) {
- $i = 0;
- do {
- if ($item == $this->items[$i]) {
- return $i;
- }
- $i++;
- } while ($i < $this->nbItems ());
- return false;
- }
- /**
- * Permet de récupérer un item par sa position
- * @param int $pos la position de l'élément
- * @return mixed item situé à $pos (dernier item si $pos<0, 1er si $pos>=count($items))
- */
- public function itemByPosition ($pos) {
- if ($pos < 0) {
- $pos = $this->nbItems () - 1;
- }
- if ($pos >= count($this->items)) {
- $pos = 0;
- }
- return $this->items[$pos];
- }
- /**
- * GETTEURS
- */
- /**
- * @param bool $all si à true, retourne tous les éléments sans prendre en compte la pagination
- * @return array<Minz_Model>
- */
- public function items ($all = false) {
- $array = array ();
- $nbItems = $this->nbItems ();
- if ($nbItems <= $this->nbItemsPerPage || $all) {
- $array = $this->items;
- } else {
- $begin = ($this->currentPage - 1) * $this->nbItemsPerPage;
- $counter = 0;
- $i = 0;
- foreach ($this->items as $key => $item) {
- if ($i >= $begin) {
- $array[$key] = $item;
- $counter++;
- }
- if ($counter >= $this->nbItemsPerPage) {
- break;
- }
- $i++;
- }
- }
- return $array;
- }
- public function nbItemsPerPage () {
- return $this->nbItemsPerPage;
- }
- public function currentPage () {
- return $this->currentPage;
- }
- public function nbPage () {
- return $this->nbPage;
- }
- public function nbItems () {
- return $this->nbItems;
- }
- /**
- * SETTEURS
- */
- public function _items ($items) {
- if (is_array ($items)) {
- $this->items = $items;
- }
- $this->_nbPage ();
- }
- public function _nbItemsPerPage ($nbItemsPerPage) {
- if ($nbItemsPerPage > $this->nbItems ()) {
- $nbItemsPerPage = $this->nbItems ();
- }
- if ($nbItemsPerPage < 0) {
- $nbItemsPerPage = 0;
- }
- $this->nbItemsPerPage = $nbItemsPerPage;
- $this->_nbPage ();
- }
- public function _currentPage ($page) {
- if ($page < 1 || ($page > $this->nbPage && $this->nbPage > 0)) {
- throw new Minz_CurrentPagePaginationException($page);
- }
- $this->currentPage = $page;
- }
- private function _nbPage () {
- if ($this->nbItemsPerPage > 0) {
- $this->nbPage = (int)ceil($this->nbItems() / $this->nbItemsPerPage);
- }
- }
- public function _nbItems ($value) {
- $this->nbItems = $value;
- }
- }
|