r358: Set limits different in fetch_limit() under certain conditions
[bugdar.git] / includes / class_pagination.php
1 <?php
2 /*=====================================================================*\
3 || ################################################################### ||
4 || # BugStrike [#]version[#]
5 || # --------------------------------------------------------------- # ||
6 || # Copyright ©2002-[#]year[#] by Iris Studios, Inc. All Rights Reserved. # ||
7 || # This file may not be reproduced in any way without permission. # ||
8 || # --------------------------------------------------------------- # ||
9 || # User License Agreement at http://www.iris-studios.com/license/ # ||
10 || ################################################################### ||
11 \*=====================================================================*/
12
13 /**
14 * Class that is responsible for paginating data. Takes passed
15 * parameters and creates pagination links.
16 */
17 class Pagination
18 {
19 /**
20 * Current page number
21 * @var integer
22 */
23 var $page;
24
25 /**
26 * Per-page value
27 * @var integer
28 */
29 var $perpage;
30
31 /**
32 * Number of page links
33 * @var integer
34 */
35 var $pagelinks;
36
37 /**
38 * Total number of results
39 * @var integer
40 */
41 var $total;
42
43 /**
44 * Total number of pages
45 * @var integer
46 */
47 var $pagecount;
48
49 /**
50 * Variable names
51 * @var array
52 */
53 var $vars;
54
55 /**
56 * Constructor: sanitize incoming variables
57 *
58 * @param string Name of page number variable
59 * @param string Name of per-page variable
60 */
61 function Pagination($page, $perpage)
62 {
63 global $bugsys;
64
65 $this->page = intval($bugsys->in["$page"]);
66 $this->perpage = intval($bugsys->in["$perpage"]);
67 $this->pagelinks = intval($bugsys->options['pagelinks']);
68
69 $this->vars = array('p' => $page, 'pp' => $perpage);
70
71 if ($this->page <= 0)
72 {
73 $this->page = 1;
74 }
75
76 if ($this->perpage <= 0)
77 {
78 $this->perpage = $bugsys->options['defaultpp'];
79 }
80 if ($this->perpage > $bugsys->options['maxpp'])
81 {
82 $this->perpage = $bugsys->options['maxpp'];
83 }
84 }
85
86 /**
87 * Takes the variables and splits up the pages
88 *
89 * @access protected
90 */
91 function split_pages()
92 {
93 $this->pagecount = ceil($this->total / $this->perpage);
94 }
95
96 /**
97 * Returns the lower limit of the pages
98 *
99 * @access public
100 *
101 * @param integer Page number
102 *
103 * @return integer Lower result limit
104 */
105 function fetch_limit($page = null)
106 {
107 if ($page === null)
108 {
109 $page = $this->page;
110 }
111
112 $limit = $page * $this->perpage;
113
114 if ($page < 1)
115 {
116 $page = 1;
117 $limit = 0;
118 }
119 else if ($page > $this->pagecount)
120 {
121 $page = $this->pagecount - 1;
122 $limit = $this->total;
123 }
124
125 if ($limit < 0)
126 {
127 return 0;
128 }
129 else if ($limit > $this->total)
130 {
131 return $this->total;
132 }
133 else
134 {
135 return $limit;
136 }
137 }
138
139 /**
140 * Constructs the page navigator
141 *
142 * @access public
143 *
144 * @param string Base link path
145 *
146 * @return string Generated HTML page navigator
147 */
148 function construct_page_nav($baselink)
149 {
150 global $bugsys;
151
152 // handle base link
153 if (strpos($baselink, '?') === false)
154 {
155 $baselink .= '?';
156 }
157 else if (!preg_match('#\?$#', $baselink) AND !preg_match('#(&|&amp;)$#', $baselink))
158 {
159 $baselink .= '&amp;';
160 }
161
162 // first page number in page nav
163 $startpage = $this->page - $this->pagelinks;
164 if ($startpage < 1)
165 {
166 $startpage = 1;
167 }
168
169 // last page number in page nav
170 $endpage = $this->page + $this->pagelinks;
171 if ($endpage > $this->pagecount)
172 {
173 $endpage = $this->pagecount;
174 }
175
176 // prev page in page nav
177 $prevpage = $this->page - 1;
178 if ($prevpage < 1)
179 {
180 $prevpage = 1;
181 }
182
183 // next page in page nav
184 $nextpage = $this->page + 1;
185 if ($nextpage > $this->pagecount)
186 {
187 $nextpage = $this->pagecount;
188 }
189
190 // show the prev page
191 $show['prev'] = true;
192 if ($this->page == $startpage)
193 {
194 $show['prev'] = false;
195 }
196
197 // show the next page
198 $show['next'] = true;
199 if ($this->page == $endpage)
200 {
201 $show['next'] = false;
202 }
203
204 // show the first page
205 $show['first'] = false;
206 if ($startpage > 1)
207 {
208 $show['first'] = true;
209 }
210
211 // show the last page
212 $show['last'] = false;
213 if ($endpage < $this->pagecount)
214 {
215 $show['last'] = true;
216 }
217
218 // construct the page bits
219 for ($i = $startpage; $i <= $endpage; $i++)
220 {
221 if ($i == $this->page)
222 {
223 $nolink = true;
224 }
225 else
226 {
227 $nolink = false;
228 }
229 eval('$pagebits[] .= "' . $bugsys->template->fetch('pagenav_bit') . '";');
230 }
231
232 $pagebits = implode(",\n", $pagebits);
233
234 eval('$pagenav = "' . $bugsys->template->fetch('pagenav') . '";');
235
236 return $pagenav;
237 }
238 }
239
240 /*=====================================================================*\
241 || ###################################################################
242 || # $HeadURL$
243 || # $Id$
244 || ###################################################################
245 \*=====================================================================*/
246 ?>