r356: Pagination engine works
[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 * Constructor: sanitize incoming variables
51 *
52 * @param string Name of page number variable
53 * @param string Name of per-page variable
54 */
55 function Pagination($page, $perpage)
56 {
57 global $bugsys;
58
59 $this->page = intval($bugsys->in["$page"]);
60 $this->perpage = intval($bugsys->in["$perpage"]);
61 $this->pagelinks = intval($bugsys->options['pagelinks']);
62
63 if ($this->page <= 0)
64 {
65 $this->page = 1;
66 }
67
68 if ($this->perpage <= 0)
69 {
70 $this->perpage = $bugsys->options['defaultpp'];
71 }
72 if ($this->perpage > $bugsys->options['maxpp'])
73 {
74 $this->perpage = $bugsys->options['maxpp'];
75 }
76 }
77
78 /**
79 * Takes the variables and splits up the pages
80 *
81 * @access protected
82 */
83 function split_pages()
84 {
85 $this->pagecount = ceil($this->total / $this->perpage);
86 }
87
88 /**
89 * Returns the lower limit of the pages
90 *
91 * @access public
92 *
93 * @param integer Page number
94 *
95 * @return integer Lower result limit
96 */
97 function fetch_limit($page = null)
98 {
99 if ($page === null)
100 {
101 $page = $this->page;
102 }
103
104 if ($page < 1)
105 {
106 $page = 1;
107 }
108 else if ($page > $this->pagecount)
109 {
110 $page = $this->pagecount - 1;
111 }
112
113 $limit = $page * $this->perpage;
114
115 if ($limit < 0)
116 {
117 return 0;
118 }
119 else if ($limit > $this->total)
120 {
121 return $this->total;
122 }
123 else
124 {
125 return $limit;
126 }
127 }
128
129 /**
130 * Constructs the page navigator
131 *
132 * @access public
133 *
134 * @return string Generated HTML page navigator
135 */
136 function construct_page_nav()
137 {
138 // first page number in page nav
139 $startpage = $this->page - $this->pagelinks;
140 if ($startpage < 1)
141 {
142 $startpage = 1;
143 }
144
145 // last page number in page nav
146 $endpage = $this->page + $this->pagelinks;
147 if ($endpage > $this->pagecount)
148 {
149 $endpage = $this->pagecount;
150 }
151
152 // prev page in page nav
153 $prevpage = $this->page - 1;
154 if ($prevpage < 1)
155 {
156 $prevpage = 1;
157 }
158
159 // next page in page nav
160 $nextpage = $this->page + 1;
161 if ($nextpage > $this->pagecount)
162 {
163 $nextpage = $this->pagecount;
164 }
165
166 // show the prev page
167 $doprev = true;
168 if ($this->page == $startpage)
169 {
170 $doprev = false;
171 }
172
173 // show the next page
174 $donext = true;
175 if ($this->page == $endpage)
176 {
177 $donext = false;
178 }
179
180 // show the first page
181 $dofirst = false;
182 if ($startpage > 1)
183 {
184 $dofirst = true;
185 }
186
187 // show the last page
188 $dolast = false;
189 if ($endpage < $this->pagecount)
190 {
191 $dolast = true;
192 }
193
194 // add the special links
195 if ($dofirst)
196 {
197 $pages[] = 'first';
198 }
199 if ($doprev)
200 {
201 $pages[] = 'prev';
202 }
203
204 // construct the page bits
205 for ($i = $startpage; $i <= $endpage; $i++)
206 {
207 $pages[] = ($i == $this->page ? "<strong>$i</strong>" : $i);
208 }
209
210 // last special links
211 if ($donext)
212 {
213 $pages[] = 'next';
214 }
215 if ($dolast)
216 {
217 $pages[] = 'last';
218 }
219
220 return $pages;
221 }
222 }
223
224 /*=====================================================================*\
225 || ###################################################################
226 || # $HeadURL$
227 || # $Id$
228 || ###################################################################
229 \*=====================================================================*/
230 ?>