Use the new package system to get pagination to work
[isso.git] / pagination.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Iris Studios Shared Object Framework [#]version[#]
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
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 Iris Studios, Inc.
42 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
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 * ISSO fields
114 * @var array
115 * @access private
116 */
117 var $fields = array(
118 'total' => array(REQ_YES, null, false),
119 'pagelinks' => array(REQ_YES, null, false),
120 'pagevar' => array(REQ_YES, null, false),
121 'perpagevar' => array(REQ_YES, null, false),
122 'maxperpage' => array(REQ_YES, null, true),
123 'defaultperpage' => array(REQ_YES, null, true)
124 );
125
126 // ###################################################################
127 /**
128 * Constructor
129 */
130 function __construct(&$registry)
131 {
132 $this->registry =& $registry;
133 }
134
135 // ###################################################################
136 /**
137 * (PHP 4) Constructor
138 */
139 function Pagination(&$registry)
140 {
141 $this->__construct($registry);
142 }
143
144 /**
145 * Constructor: sanitize incoming variables
146 *
147 * @param string Name of page number variable
148 * @param string Name of per-page variable
149 */
150 function Pagination($page, $perpage)
151 {
152 global $bugsys;
153
154 }
155
156 // ###################################################################
157 /**
158 * Sets an ISSO field
159 *
160 * @access public
161 *
162 * @param string Field name
163 * @param mixed Value of the field
164 */
165 function set($name, $value)
166 {
167 $this->registry->do_set($name, $value, 'pagination');
168 }
169
170 // ###################################################################
171 /**
172 * Gets an ISSO field
173 *
174 * @access public
175 *
176 * @param string Field name
177 *
178 * @return mixed Value of the field
179 */
180 function get($fieldname)
181 {
182 return $this->registry->do_get($fieldname, 'pagination');
183 }
184
185 /**
186 * Takes the variables and splits up the pages
187 *
188 * @access public
189 */
190 function split_pages()
191 {
192 $this->page = $this->registry->input_clean($this->pagevar, TYPE_INT);
193 $this->perpage = $this->registry->input_clean($this->perpagevar, TYPE_INT);
194 $this->pagelinks = $this->registry->input_clean($this->pagelinkx, TYPE_INT);
195
196 if ($this->page <= 0)
197 {
198 $this->page = 1;
199 }
200
201 if ($this->perpage <= 0)
202 {
203 $this->perpage = $this->defaultperpage;
204 }
205 if ($this->perpage > $this->maxperpage['maxpp'])
206 {
207 $this->perpage = $this->maxperpage['maxpp'];
208 }
209
210 $this->perpage = $this->registry->clean($this->perpage, TYPE_INT);
211
212 $this->pagecount = ceil($this->total / $this->perpage);
213 }
214
215 /**
216 * Returns the lower limit of the pages
217 *
218 * @access public
219 *
220 * @param integer Page number
221 *
222 * @return integer Lower result limit
223 */
224 function fetch_limit($page = null)
225 {
226 if ($page === null)
227 {
228 $page = $this->page;
229 }
230
231 $limit = $page * $this->perpage;
232
233 if ($page < 1)
234 {
235 $page = 1;
236 $limit = 0;
237 }
238 else if ($page > $this->pagecount)
239 {
240 $page = $this->pagecount - 1;
241 $limit = $this->total;
242 }
243
244 if ($limit < 0)
245 {
246 return 0;
247 }
248 else if ($limit > $this->total)
249 {
250 return $this->total;
251 }
252 else
253 {
254 return $limit;
255 }
256 }
257
258 /**
259 * Constructs the page navigator
260 *
261 * @access public
262 *
263 * @param string Base link path
264 *
265 * @return string Generated HTML page navigator
266 */
267 function construct_page_nav($baselink)
268 {
269 global $bugsys;
270
271 // handle base link
272 if (strpos($baselink, '?') === false)
273 {
274 $baselink .= '?';
275 }
276 else if (!preg_match('#\?$#', $baselink) AND !preg_match('#(&|&amp;)$#', $baselink))
277 {
278 $baselink .= '&amp;';
279 }
280
281 // first page number in page nav
282 $startpage = $this->page - $this->pagelinks;
283 if ($startpage < 1)
284 {
285 $startpage = 1;
286 }
287
288 // last page number in page nav
289 $endpage = $this->page + $this->pagelinks;
290 if ($endpage > $this->pagecount)
291 {
292 $endpage = $this->pagecount;
293 }
294
295 // prev page in page nav
296 $prevpage = $this->page - 1;
297 if ($prevpage < 1)
298 {
299 $prevpage = 1;
300 }
301
302 // next page in page nav
303 $nextpage = $this->page + 1;
304 if ($nextpage > $this->pagecount)
305 {
306 $nextpage = $this->pagecount;
307 }
308
309 // show the prev page
310 $show['prev'] = true;
311 if ($this->page == $startpage)
312 {
313 $show['prev'] = false;
314 }
315
316 // show the next page
317 $show['next'] = true;
318 if ($this->page == $endpage)
319 {
320 $show['next'] = false;
321 }
322
323 // show the first page
324 $show['first'] = false;
325 if ($startpage > 1)
326 {
327 $show['first'] = true;
328 }
329
330 // show the last page
331 $show['last'] = false;
332 if ($endpage < $this->pagecount)
333 {
334 $show['last'] = true;
335 }
336
337 // construct the page bits
338 for ($i = $startpage; $i <= $endpage; $i++)
339 {
340 if ($i == $this->page)
341 {
342 $nolink = true;
343 }
344 else
345 {
346 $nolink = false;
347 }
348
349 eval('$pagebits[] .= "' . $this->registry->modules['template']->fetch('pagenav_bit') . '";');
350 }
351
352 $pagebits = implode(",\n", $pagebits);
353
354 eval('$pagenav = "' . $this->registry->modules['template']->fetch('pagenav') . '";');
355
356 return $pagenav;
357 }
358 }
359
360 /*=====================================================================*\
361 || ###################################################################
362 || # $HeadURL$
363 || # $Id$
364 || ###################################################################
365 \*=====================================================================*/
366 ?>