Overview

Namespaces

  • None
  • PHP
  • Pry
    • Auth
      • Interfaces
    • Config
    • Controller
    • Date
    • Db
    • Feed
      • Abstracts
      • Writers
    • File
      • Decorator
    • Form
      • Element
    • Image
    • Log
      • Writer
    • Net
      • Exception
    • Session
    • Util
    • Validate
      • Validator
    • View

Classes

  • Bench
  • CommandLineBuilder
  • Pagination
  • Registry
  • Strings
  • Token
  • UserAgent

Exceptions

  • ExceptionHandler
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * Pry Framework
  5:  *
  6:  * LICENSE
  7:  *
  8:  * This source file is subject to the new BSD license that is bundled
  9:  * with this package in the file LICENSE.txt.
 10:  * 
 11:  */
 12: 
 13: namespace Pry\Util;
 14: 
 15: /**
 16:  * Classe permettant de générer une pagination pour un jeu de résultat.
 17:  * Retourne uniquement un array , pas de formatage HTML
 18:  * <code>
 19:  * $pager = new Util_Pagination($total);
 20:  * $pagin = $pager->create();
 21:  * $data = $sql->query('SELECT id FROM planning LIMIT '.$pager->itemMini.','.$pager->nbItemParPage);
 22:  * foreach($pagin as $tab)
 23:  * {
 24:  *    if($tab['encours'])
 25:  *      echo $tab['page'];
 26:  *    else
 27:  *      echo' <a href="?p='.$tab['page'].'">'.$tab['page'].'</a> ';
 28:  * }
 29:  * </code>
 30:  * @category Pry
 31:  * @package Util
 32:  * @version 1.1.0
 33:  * @author Olivier ROGER <oroger.fr>
 34:  *       
 35:  */
 36: class Pagination
 37: {
 38: 
 39:     const BASIC    = 1;
 40:     const ADVANCED = 2;
 41: 
 42:     /**
 43:      * Nombre d'élément total
 44:      * @access private
 45:      * @var int
 46:      */
 47:     private $nbItemTotal;
 48: 
 49:     /**
 50:      * Type de pagination. BASIC/ADVANCED
 51:      * @access private
 52:      * @var int
 53:      */
 54:     private $pagerType;
 55: 
 56:     /**
 57:      * Valeur de la page
 58:      * @access private
 59:      * @var int
 60:      */
 61:     private $currentPage;
 62: 
 63:     /**
 64:      * Sortie générée
 65:      * @access private
 66:      * @var array
 67:      */
 68:     private $output;
 69: 
 70:     /**
 71:      * Nombre de page total
 72:      * @access public
 73:      * @var int
 74:      */
 75:     public $nbPageTotal;
 76: 
 77:     /**
 78:      * Nombre d'élément par page
 79:      * @access public
 80:      * @var int
 81:      */
 82:     public $nbItemParPage;
 83: 
 84:     /**
 85:      * Item minimal à utiliser pour la requete
 86:      * @access public
 87:      * @var int
 88:      */
 89:     public $itemMini;
 90: 
 91:     /**
 92:      * Nombre de page afficher à coter de la page courante
 93:      * Utilisée en mode avancé uniquement
 94:      *
 95:      * @access public
 96:      * @var int
 97:      */
 98:     public $pageAdjacente;
 99: 
100:     /**
101:      * Tableau avec les lien précédent/suivant
102:      * Utilisé en mode avancé
103:      * @access public
104:      * @var array
105:      */
106:     public $nextPrev;
107: 
108:     /**
109:      * Constructeur. Initialise la pagination
110:      *
111:      * @access public
112:      * @param int $total Nombre total de résultat
113:      * @param int $type Type de pagination
114:      * @param int $nbParPage Nombre d'éléments par page
115:      * @param string $get Nom du paramètre de page
116:      */
117:     public function __construct($total, $type = 1, $nbParPage = 10, $page = 1)
118:     {
119:         $this->nbItemTotal   = intval($total);
120:         $this->nbItemParPage = $nbParPage;
121: 
122:         $this->currentPage = intval($page);
123:         $this->nbPageTotal = ceil($this->nbItemTotal / $this->nbItemParPage);
124:         $this->pagerType   = intval($type);
125:         $this->output      = array();
126:         $this->nextPrev = array();
127:         $this->pageAdjacente = 2;
128:     }
129: 
130:     /**
131:      * Créer la pagination
132:      *
133:      * @access public
134:      * @return array
135:      */
136:     public function create()
137:     {
138: 
139:         if (isset($this->currentPage) && $this->currentPage > 1)
140:             $pageEnCours = $this->currentPage;
141:         else
142:             $pageEnCours = 1;
143: 
144:         $this->itemMini = ($pageEnCours - 1) * $this->nbItemParPage;
145: 
146:         if ($this->nbPageTotal > 1)
147:         {
148:             if ($this->pagerType == self::BASIC)
149:             {
150:                 $this->buildSimple($pageEnCours);
151:             }
152:             elseif ($this->pagerType == self::ADVANCED)
153:             {
154:                 $prev = $pageEnCours - 1;
155:                 $next = $pageEnCours + 1;
156: 
157:                 //Bouton Précédent
158:                 if ($pageEnCours > 1)
159:                     $this->nextPrev['prev'] = $prev;
160:                 else
161:                     $this->nextPrev['prev'] = '#';
162: 
163: 
164:                 //Si le nombre de page est insuffisant pour la représentation avancée
165:                 if ($this->nbPageTotal <= 3 * ($this->pageAdjacente * 2))
166:                     $this->buildSimple($pageEnCours);
167:                 else
168:                     $this->buildAdvanced($pageEnCours);
169:                 //Bouton Suivant
170: 
171:                 if ($pageEnCours < $this->nbPageTotal)
172:                     $this->nextPrev['next'] = $next;
173:                 else
174:                     $this->nextPrev['next'] = '#';
175:             }
176:             else
177:                 throw new \UnexpectedValueException('Le type de pagination doit être BASIC ou ADVANCED');
178: 
179:             return $this->output;
180:         }
181:         else
182:         {
183:             //1 seule page
184:             return false;
185:         }
186:     }
187: 
188:     /**
189:      * Construit la pagination de base
190:      * @access private
191:      * @param int $pageEnCours
192:      */
193:     private function buildSimple($pageEnCours)
194:     {
195:         for ($i = 1; $i <= $this->nbPageTotal; $i++) {
196:             if ($i == $pageEnCours)
197:                 $this->output[$i]['encours'] = true;
198:             else
199:                 $this->output[$i]['encours'] = false;
200: 
201:             $this->output[$i]['page'] = $i;
202:         }
203:     }
204: 
205:     /**
206:      * Construit la pagination avancée
207:      * @access private
208:      * @param unknown_type $pageEnCours
209:      */
210:     private function buildAdvanced($pageEnCours)
211:     {
212:         //Cas 1 : Début de pagination
213:         if ($pageEnCours < 2 + (2 * $this->pageAdjacente))
214:         {
215:             //Troncature de la fin de pagination
216:             for ($i = 1; $i < 4 + (2 * $this->pageAdjacente); $i++) {
217:                 if ($i == $pageEnCours)
218:                     $this->output[$i]['encours'] = true;
219:                 else
220:                     $this->output[$i]['encours'] = false;
221: 
222:                 $this->output[$i]['page']                        = $i;
223:             }
224:             $this->output[$i + 1]['encours']                 = true;
225:             $this->output[$i + 1]['page']                    = '...';
226:             //Fin de la pagination
227:             $this->output[$this->nbPageTotal - 1]['encours'] = false;
228:             $this->output[$this->nbPageTotal - 1]['page']    = $this->nbPageTotal - 1;
229: 
230:             $this->output[$this->nbPageTotal]['encours'] = false;
231:             $this->output[$this->nbPageTotal]['page']    = $this->nbPageTotal;
232:         }
233:         //Cas 2 : Millieu de pagination (2 au début , 2 à la fin et un groupe au millieu)
234:         elseif (($this->pageAdjacente * 2) + 1 < $pageEnCours && $pageEnCours < $this->nbPageTotal - ($this->pageAdjacente * 2))
235:         {
236:             //Affichage des 2 premiers
237:             $this->output[1]['encours'] = false;
238:             $this->output[1]['page']    = 1;
239:             $this->output[2]['encours'] = false;
240:             $this->output[2]['page']    = 2;
241:             //Affichage de la séparation
242:             $this->output[3]['encours'] = true;
243:             $this->output[3]['page']    = '...';
244: 
245:             for ($i = $pageEnCours - $this->pageAdjacente; $i <= $pageEnCours + $this->pageAdjacente; $i++) {
246:                 if ($i == $pageEnCours)
247:                     $this->output[$i]['encours'] = true;
248:                 else
249:                     $this->output[$i]['encours'] = false;
250: 
251:                 $this->output[$i]['page'] = $i;
252:             }
253: 
254:             $this->output[$i + 1]['encours']                 = true;
255:             $this->output[$i + 1]['page']                    = '...';
256:             //Fin de la pagination
257:             $this->output[$this->nbPageTotal - 1]['encours'] = false;
258:             $this->output[$this->nbPageTotal - 1]['page']    = $this->nbPageTotal - 1;
259: 
260:             $this->output[$this->nbPageTotal]['encours'] = false;
261:             $this->output[$this->nbPageTotal]['page']    = $this->nbPageTotal;
262:         }
263:         //Cas 3 : Fin de pagination. Affichage des 2 premiers et des 2 derniers
264:         else
265:         {
266:             //Affichage des 2 premiers
267:             $this->output[1]['encours'] = false;
268:             $this->output[1]['page']    = 1;
269:             $this->output[2]['encours'] = false;
270:             $this->output[2]['page']    = 2;
271:             //Affichage de la séparation
272:             $this->output[3]['encours'] = true;
273:             $this->output[3]['page']    = '...';
274: 
275:             for ($i = $this->nbPageTotal - (2 + (2 * $this->pageAdjacente)); $i <= $this->nbPageTotal; $i++) {
276:                 if ($i == $pageEnCours)
277:                     $this->output[$i]['encours'] = true;
278:                 else
279:                     $this->output[$i]['encours'] = false;
280: 
281:                 $this->output[$i]['page'] = $i;
282:             }
283:         }
284:     }
285: 
286: }
Pry API documentation generated by ApiGen 2.8.0