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