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