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