Template updated
[isso.git] / pagination.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework [#]issoversion[#]
5 || # Copyright ©2002-[#]year[#] Blue Static
6 || #
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version [#]gpl[#] of the License.
10 || #
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 || # more details.
15 || #
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
21
22 /**
23 * Pagination System
24 * pagination.php
25 *
26 * @package ISSO
27 */
28
29 /**
30 * Pagination System
31 *
32 * On many pages, it is necessary to limit the amount of records to display.
33 * Using this class, you can set the maximum and minimum values to display,
34 * and then the input variables for page number and perpage. This will
35 * then create a page navigator and manage the SQL LIMIT statements.
36 *
37 * Templates required:
38 * pagenav_bit - The individual page numbers in the page navigator
39 * pagenav - The entirity of the page navigtaor
40 *
41 * @author Blue Static
42 * @copyright Copyright ©2002 - [#]year[#], Blue Static
43 * @version $Revision$
44 * @package ISSO
45 *
46 */
47 class Pagination
48 {
49 /**
50 * Current page number
51 * @var integer
52 * @access private
53 */
54 var $page;
55
56 /**
57 * Per-page value
58 * @var integer
59 * @access private
60 */
61 var $perpage;
62
63 /**
64 * Number of page links
65 * @var integer
66 * @access private
67 */
68 var $pagelinks;
69
70 /**
71 * Total number of results
72 * @var integer
73 * @access private
74 */
75 var $total;
76
77 /**
78 * Total number of pages
79 * @var integer
80 * @access private
81 */
82 var $pagecount;
83
84 /**
85 * Name of page variable
86 * @var array
87 * @access private
88 */
89 var $pagevar;
90
91 /**
92 * Name of per-page variable
93 * @var integer
94 * @access private
95 */
96 var $perpagevar;
97
98 /**
99 * Maximum number of per-page results
100 * @var integer
101 * @access private
102 */
103 var $maxperpage = 100;
104
105 /**
106 * Default number of per-page results
107 * @var integer
108 * @access private
109 */
110 var $defaultperpage = 20;
111
112 // ###################################################################
113 /**
114 * Constructor
115 */
116 function __construct(&$registry)
117 {
118 $this->registry =& $registry;
119 }
120
121 // ###################################################################
122 /**
123 * (PHP 4) Constructor
124 */
125 function Pagination(&$registry)
126 {
127 $this->__construct($registry);
128 }
129
130 // ###################################################################
131 /**
132 * Sets total
133 *
134 * @access public
135 *
136 * @param integer Total number
137 */
138 function setTotal($total)
139 {
140 $this->total = $total;
141 }
142
143 // ###################################################################
144 /**
145 * Sets pagelinks
146 *
147 * @access public
148 *
149 * @param integer Number of page links
150 */
151 function setPageLinks($pagelinks)
152 {
153 $this->pagelinks = $pagelinks;
154 }
155
156 // ###################################################################
157 /**
158 * Sets pagevar
159 *
160 * @access public
161 *
162 * @param string Page variable
163 */
164 function setPageVar($pagevar)
165 {
166 $this->pagevar = $pagevar;
167 }
168
169 // ###################################################################
170 /**
171 * Sets perpagevar
172 *
173 * @access public
174 *
175 * @param string Per-page variable
176 */
177 function setPerPageVar($perpagevar)
178 {
179 $this->perpagevar = $perpagevar;
180 }
181
182 // ###################################################################
183 /**
184 * Sets maxperpage
185 *
186 * @access public
187 *
188 * @param integer Maximum number per page
189 */
190 function setMaxPerPage($maxperpage)
191 {
192 $this->maxperpage = $maxperpage;
193 }
194
195 // ###################################################################
196 /**
197 * Sets defaultperpage
198 *
199 * @access public
200 *
201 * @param integer Total number
202 */
203 function setDefaultPerPage($defaultperpage)
204 {
205 $this->defaultperpage = $defaultperpage;
206 }
207
208 // ###################################################################
209 /**
210 * Takes the variables and splits up the pages
211 *
212 * @access public
213 */
214 function split_pages()
215 {
216 $this->page = $this->registry->input_clean($this->pagevar, TYPE_INT);
217 $this->perpage = $this->registry->input_clean($this->perpagevar, TYPE_INT);
218 $this->pagelinks = $this->registry->input_clean($this->pagelinks, TYPE_INT);
219
220 if ($this->page <= 0)
221 {
222 $this->page = 1;
223 }
224
225 if ($this->perpage <= 0)
226 {
227 $this->perpage = $this->defaultperpage;
228 }
229 if ($this->perpage > $this->maxperpage['maxpp'])
230 {
231 $this->perpage = $this->maxperpage['maxpp'];
232 }
233
234 $this->perpage = $this->registry->clean($this->perpage, TYPE_INT);
235
236 $this->pagecount = ceil($this->total / $this->perpage);
237 }
238
239 // ###################################################################
240 /**
241 * Returns the lower limit of the pages
242 *
243 * @access public
244 *
245 * @param integer Page number
246 *
247 * @return integer Lower result limit
248 */
249 function fetch_limit($page = null)
250 {
251 if ($page === null)
252 {
253 $page = $this->page;
254 }
255
256 $limit = $page * $this->perpage;
257
258 if ($page < 1)
259 {
260 $page = 1;
261 $limit = 0;
262 }
263 else if ($page > $this->pagecount)
264 {
265 $page = $this->pagecount - 1;
266 $limit = $this->total;
267 }
268
269 if ($limit < 0)
270 {
271 return 0;
272 }
273 else if ($limit > $this->total)
274 {
275 return $this->total;
276 }
277 else
278 {
279 return $limit;
280 }
281 }
282
283 // ###################################################################
284 /**
285 * Constructs the page navigator
286 *
287 * @access public
288 *
289 * @param string Base link path
290 *
291 * @return string Generated HTML page navigator
292 */
293 function construct_page_nav($baselink)
294 {
295 global $bugsys;
296
297 // handle base link
298 if (strpos($baselink, '?') === false)
299 {
300 $baselink .= '?';
301 }
302 else if (!preg_match('#\?$#', $baselink) AND !preg_match('#(&|&amp;)$#', $baselink))
303 {
304 $baselink .= '&amp;';
305 }
306
307 // first page number in page nav
308 $startpage = $this->page - $this->pagelinks;
309 if ($startpage < 1)
310 {
311 $startpage = 1;
312 }
313
314 // last page number in page nav
315 $endpage = $this->page + $this->pagelinks;
316 if ($endpage > $this->pagecount)
317 {
318 $endpage = $this->pagecount;
319 }
320
321 // prev page in page nav
322 $prevpage = $this->page - 1;
323 if ($prevpage < 1)
324 {
325 $prevpage = 1;
326 }
327
328 // next page in page nav
329 $nextpage = $this->page + 1;
330 if ($nextpage > $this->pagecount)
331 {
332 $nextpage = $this->pagecount;
333 }
334
335 // show the prev page
336 $show['prev'] = true;
337 if ($this->page == $startpage)
338 {
339 $show['prev'] = false;
340 }
341
342 // show the next page
343 $show['next'] = true;
344 if ($this->page == $endpage)
345 {
346 $show['next'] = false;
347 }
348
349 // show the first page
350 $show['first'] = false;
351 if ($startpage > 1)
352 {
353 $show['first'] = true;
354 }
355
356 // show the last page
357 $show['last'] = false;
358 if ($endpage < $this->pagecount)
359 {
360 $show['last'] = true;
361 }
362
363 // construct the page bits
364 for ($i = $startpage; $i <= $endpage; $i++)
365 {
366 if ($i == $this->page)
367 {
368 $nolink = true;
369 }
370 else
371 {
372 $nolink = false;
373 }
374
375 eval('$pagebits[] .= "' . $this->registry->modules['template']->fetch('pagenav_bit') . '";');
376 }
377
378 $pagebits = implode(",\n", $pagebits);
379
380 eval('$pagenav = "' . $this->registry->modules['template']->fetch('pagenav') . '";');
381
382 return $pagenav;
383 }
384 }
385
386 /*=====================================================================*\
387 || ###################################################################
388 || # $HeadURL$
389 || # $Id$
390 || ###################################################################
391 \*=====================================================================*/
392 ?>