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