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