Adding pagination class... it kind of works
[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 * @author Iris Studios, Inc.
38 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
39 * @version $Revision$
40 * @package ISSO
41 *
42 */
43 class Pagination
44 {
45 /**
46 * Current page number
47 * @var integer
48 * @access private
49 */
50 var $page;
51
52 /**
53 * Per-page value
54 * @var integer
55 * @access private
56 */
57 var $perpage;
58
59 /**
60 * Number of page links
61 * @var integer
62 * @access private
63 */
64 var $pagelinks;
65
66 /**
67 * Total number of results
68 * @var integer
69 * @access private
70 */
71 var $total;
72
73 /**
74 * Total number of pages
75 * @var integer
76 * @access private
77 */
78 var $pagecount;
79
80 /**
81 * Name of page variable
82 * @var array
83 * @access private
84 */
85 var $pagevar;
86
87 /**
88 * Name of per-page variable
89 * @var integer
90 * @access private
91 */
92 var $perpagevar;
93
94 /**
95 * Maximum number of per-page results
96 * @var integer
97 * @access private
98 */
99 var $maxperpage = 100;
100
101 /**
102 * Default number of per-page results
103 * @var integer
104 * @access private
105 */
106 var $defaultperpage = 20;
107
108 /**
109 * ISSO fields
110 * @var array
111 * @access private
112 */
113 var $fields = array(
114 'total' => array(REQ_YES, null, false),
115 'pagelinks' => array(REQ_YES, null, false),
116 'pagevar' => array(REQ_YES, null, false),
117 'perpagevar' => array(REQ_YES, null, false),
118 'maxperpage' => array(REQ_YES, null, true),
119 'defaultperpage' => array(REQ_YES, null, true)
120 );
121
122 // ###################################################################
123 /**
124 * Constructor
125 */
126 function __construct(&$registry)
127 {
128 $this->registry =& $registry;
129 }
130
131 // ###################################################################
132 /**
133 * (PHP 4) Constructor
134 */
135 function Pagination(&$registry)
136 {
137 $this->__construct($registry);
138 }
139
140 /**
141 * Constructor: sanitize incoming variables
142 *
143 * @param string Name of page number variable
144 * @param string Name of per-page variable
145 */
146 function Pagination($page, $perpage)
147 {
148 global $bugsys;
149
150 }
151
152 // ###################################################################
153 /**
154 * Sets an ISSO field
155 *
156 * @access public
157 *
158 * @param string Field name
159 * @param mixed Value of the field
160 */
161 function set($name, $value)
162 {
163 $this->registry->do_set($name, $value, 'pagination');
164 }
165
166 // ###################################################################
167 /**
168 * Gets an ISSO field
169 *
170 * @access public
171 *
172 * @param string Field name
173 *
174 * @return mixed Value of the field
175 */
176 function get($fieldname)
177 {
178 return $this->registry->do_get($fieldname, 'pagination');
179 }
180
181 /**
182 * Takes the variables and splits up the pages
183 *
184 * @access public
185 */
186 function split_pages()
187 {
188 $this->page = $this->registry->input_clean($this->pagevar, TYPE_INT);
189 $this->perpage = $this->registry->input_clean($this->perpagevar, TYPE_INT);
190 $this->pagelinks = $this->registry->input_clean($this->pagelinkx, TYPE_INT);
191
192 if ($this->page <= 0)
193 {
194 $this->page = 1;
195 }
196
197 if ($this->perpage <= 0)
198 {
199 $this->perpage = $this->defaultperpage;
200 }
201 if ($this->perpage > $this->maxperpage['maxpp'])
202 {
203 $this->perpage = $this->maxperpage['maxpp'];
204 }
205
206 $this->perpage = $this->registry->clean($this->perpage, TYPE_INT);
207
208 $this->pagecount = ceil($this->total / $this->perpage);
209 }
210
211 /**
212 * Returns the lower limit of the pages
213 *
214 * @access public
215 *
216 * @param integer Page number
217 *
218 * @return integer Lower result limit
219 */
220 function fetch_limit($page = null)
221 {
222 if ($page === null)
223 {
224 $page = $this->page;
225 }
226
227 $limit = $page * $this->perpage;
228
229 if ($page < 1)
230 {
231 $page = 1;
232 $limit = 0;
233 }
234 else if ($page > $this->pagecount)
235 {
236 $page = $this->pagecount - 1;
237 $limit = $this->total;
238 }
239
240 if ($limit < 0)
241 {
242 return 0;
243 }
244 else if ($limit > $this->total)
245 {
246 return $this->total;
247 }
248 else
249 {
250 return $limit;
251 }
252 }
253
254 /**
255 * Constructs the page navigator
256 *
257 * @access public
258 *
259 * @param string Base link path
260 *
261 * @return string Generated HTML page navigator
262 */
263 function construct_page_nav($baselink)
264 {
265 global $bugsys;
266
267 // handle base link
268 if (strpos($baselink, '?') === false)
269 {
270 $baselink .= '?';
271 }
272 else if (!preg_match('#\?$#', $baselink) AND !preg_match('#(&|&amp;)$#', $baselink))
273 {
274 $baselink .= '&amp;';
275 }
276
277 // first page number in page nav
278 $startpage = $this->page - $this->pagelinks;
279 if ($startpage < 1)
280 {
281 $startpage = 1;
282 }
283
284 // last page number in page nav
285 $endpage = $this->page + $this->pagelinks;
286 if ($endpage > $this->pagecount)
287 {
288 $endpage = $this->pagecount;
289 }
290
291 // prev page in page nav
292 $prevpage = $this->page - 1;
293 if ($prevpage < 1)
294 {
295 $prevpage = 1;
296 }
297
298 // next page in page nav
299 $nextpage = $this->page + 1;
300 if ($nextpage > $this->pagecount)
301 {
302 $nextpage = $this->pagecount;
303 }
304
305 // show the prev page
306 $show['prev'] = true;
307 if ($this->page == $startpage)
308 {
309 $show['prev'] = false;
310 }
311
312 // show the next page
313 $show['next'] = true;
314 if ($this->page == $endpage)
315 {
316 $show['next'] = false;
317 }
318
319 // show the first page
320 $show['first'] = false;
321 if ($startpage > 1)
322 {
323 $show['first'] = true;
324 }
325
326 // show the last page
327 $show['last'] = false;
328 if ($endpage < $this->pagecount)
329 {
330 $show['last'] = true;
331 }
332
333 // construct the page bits
334 for ($i = $startpage; $i <= $endpage; $i++)
335 {
336 if ($i == $this->page)
337 {
338 $nolink = true;
339 }
340 else
341 {
342 $nolink = false;
343 }
344
345 eval('$pagebits[] .= "' . $bugsys->template->fetch('pagenav_bit') . '";');
346 }
347
348 $pagebits = implode(",\n", $pagebits);
349
350 eval('$pagenav = "' . $bugsys->template->fetch('pagenav') . '";');
351
352 return $pagenav;
353 }
354 }
355
356 /*=====================================================================*\
357 || ###################################################################
358 || # $HeadURL$
359 || # $Id$
360 || ###################################################################
361 \*=====================================================================*/
362 ?>