updating error_box() function calls. second argument was modified.
[squirrelmail.git] / templates / util_paginator.php
1 <?php
2
3 /**
4 * Template logic
5 *
6 * The following functions are utility functions for this template. Do not
7 * echo output in those functions. Output is generated above this comment block
8 *
9 * @copyright &copy; 2005-2006 The SquirrelMail Project Team
10 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
11 * @version $Id$
12 * @package squirrelmail
13 */
14
15 /**
16 * Generate a paginator link.
17 *
18 * @param string $box Mailbox name
19 * @param integer $start_msg Message Offset
20 * @param string $text text used for paginator link
21 * @return string
22 */
23 function get_paginator_link($box, $start_msg, $text) {
24 sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER);
25 $result = "<a href=\"$php_self?startMessage=$start_msg&amp;mailbox=$box\" "
26 . ">$text</a>";
27
28 return ($result);
29 }
30
31 /**
32 * This function computes the comapact paginator string.
33 *
34 * @param string $box mailbox name
35 * @param integer $iOffset offset in total number of messages
36 * @param integer $iTotal total number of messages
37 * @param integer $iLimit maximum number of messages to show on a page
38 * @param bool $bShowAll show all messages at once (non paginate mode)
39 * @return string $result paginate string with links to pages
40 */
41 function get_compact_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll, $javascript_on, $page_selector) {
42
43 sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER);
44
45 /* Initialize paginator string chunks. */
46 $prv_str = '';
47 $nxt_str = '';
48 $pg_str = '';
49 $all_str = '';
50
51 $box = urlencode($box);
52
53 /* Create simple strings that will be creating the paginator. */
54 $spc = '&nbsp;'; /* This will be used as a space. */
55 $sep = '|'; /* This will be used as a seperator. */
56
57 /* Make sure that our start message number is not too big. */
58 $iOffset = min($iOffset, $iTotal);
59
60 /* Compute the starting message of the previous and next page group. */
61 $next_grp = $iOffset + $iLimit;
62 $prev_grp = $iOffset - $iLimit;
63
64 if (!$bShowAll) {
65 /* Compute the basic previous and next strings. */
66 if (($next_grp <= $iTotal) && ($prev_grp >= 0)) {
67 $prv_str = get_paginator_link($box, $prev_grp, '<');
68 $nxt_str = get_paginator_link($box, $next_grp, '>');
69 } else if (($next_grp > $iTotal) && ($prev_grp >= 0)) {
70 $prv_str = get_paginator_link($box, $prev_grp, '<');
71 $nxt_str = '>';
72 } else if (($next_grp <= $iTotal) && ($prev_grp < 0)) {
73 $prv_str = '<';
74 $nxt_str = get_paginator_link($box, $next_grp, '>');
75 }
76
77 /* Page selector block. Following code computes page links. */
78 if ($iLimit != 0 && $page_selector && ($iTotal > $iLimit)) {
79 /* Most importantly, what is the current page!!! */
80 $cur_pg = intval($iOffset / $iLimit) + 1;
81
82 /* Compute total # of pages and # of paginator page links. */
83 $tot_pgs = ceil($iTotal / $iLimit); /* Total number of Pages */
84
85 $last_grp = (($tot_pgs - 1) * $iLimit) + 1;
86 }
87 } else {
88 $pg_str = "<a href=\"$php_self?showall=0"
89 . "&amp;startMessage=1&amp;mailbox=$box\" "
90 . ">" ._("Paginate") . '</a>';
91 }
92
93 /* Put all the pieces of the paginator string together. */
94 /**
95 * Hairy code... But let's leave it like it is since I am not certain
96 * a different approach would be any easier to read. ;)
97 */
98 $result = '';
99 if ( $prv_str || $nxt_str ) {
100
101 /* Compute the 'show all' string. */
102 $all_str = "<a href=\"$php_self?showall=1"
103 . "&amp;startMessage=1&amp;mailbox=$box\" "
104 . ">" . _("Show All") . '</a>';
105
106 $result .= '[' . get_paginator_link($box, 1, '<<') . ']';
107 $result .= '[' . $prv_str . ']';
108
109 $pg_url = $php_self . '?mailbox=' . $box;
110
111 $result .= '[' . $nxt_str . ']';
112 $result .= '[' . get_paginator_link($box, $last_grp, '>>') . ']';
113
114 if ($page_selector) {
115 $result .= $spc . '<select name="startMessage"';
116 if ($javascript_on) {
117 $result .= ' onchange="JavaScript:SubmitOnSelect'
118 . '(this, \'' . $pg_url . '&startMessage=\')"';
119 }
120 $result .='>';
121
122 for ($p = 0; $p < $tot_pgs; $p++) {
123 $result .= '<option ';
124 if (($p+1) == $cur_pg) $result .= 'selected ';
125 $result .= 'value="' . (($p*$iLimit)+1) . '">'
126 . ($p+1) . "/$tot_pgs" . '</option>';
127 }
128
129 $result .= '</select>';
130
131 if ($javascript_on) {
132 $result .= '<noscript language="JavaScript">'
133 . addSubmit(_("Go"))
134 . '</noscript>';
135 } else {
136 $result .= addSubmit(_("Go"));
137 }
138 }
139 }
140
141 $result .= ($pg_str != '' ? '['.$pg_str.']' . $spc : '');
142 $result .= ($all_str != '' ? $spc . '['.$all_str.']' . $spc . $spc : '');
143
144 /* If the resulting string is blank, return a non-breaking space. */
145 if ($result == '') {
146 $result = '&nbsp;';
147 }
148 /* Return our final magical paginator string. */
149 return ($result);
150 }
151
152 /**
153 * This function computes the paginator string.
154 *
155 * @param string $box mailbox name
156 * @param integer $iOffset offset in total number of messages
157 * @param integer $iTotal total number of messages
158 * @param integer $iLimit maximum number of messages to show on a page
159 * @param bool $bShowAll show all messages at once (non paginate mode)
160 * @return string $result paginate string with links to pages
161 */
162 function get_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll,$page_selector, $page_selector_max) {
163
164 sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER);
165
166 /* Initialize paginator string chunks. */
167 $prv_str = '';
168 $nxt_str = '';
169 $pg_str = '';
170 $all_str = '';
171
172 $box = urlencode($box);
173
174 /* Create simple strings that will be creating the paginator. */
175 $spc = '&nbsp;'; /* This will be used as a space. */
176 $sep = '|'; /* This will be used as a seperator. */
177
178 /* Make sure that our start message number is not too big. */
179 $iOffset = min($iOffset, $iTotal);
180
181 /* Compute the starting message of the previous and next page group. */
182 $next_grp = $iOffset + $iLimit;
183 $prev_grp = $iOffset - $iLimit;
184
185 if (!$bShowAll) {
186 /* Compute the basic previous and next strings. */
187
188 if (($next_grp <= $iTotal) && ($prev_grp >= 0)) {
189 $prv_str = get_paginator_link($box, $prev_grp, _("Previous"));
190 $nxt_str = get_paginator_link($box, $next_grp, _("Next"));
191 } else if (($next_grp > $iTotal) && ($prev_grp >= 0)) {
192 $prv_str = get_paginator_link($box, $prev_grp, _("Previous"));
193 $nxt_str = _("Next");
194 } else if (($next_grp <= $iTotal) && ($prev_grp < 0)) {
195 $prv_str = _("Previous");
196 $nxt_str = get_paginator_link($box, $next_grp, _("Next"));
197 }
198
199 /* Page selector block. Following code computes page links. */
200 if ($iLimit != 0 && $page_selector && ($iTotal > $iLimit)) {
201 /* Most importantly, what is the current page!!! */
202 $cur_pg = intval($iOffset / $iLimit) + 1;
203
204 /* Compute total # of pages and # of paginator page links. */
205 $tot_pgs = ceil($iTotal / $iLimit); /* Total number of Pages */
206
207 $vis_pgs = min($page_selector_max, $tot_pgs - 1); /* Visible Pages */
208
209 /* Compute the size of the four quarters of the page links. */
210
211 /* If we can, just show all the pages. */
212 if (($tot_pgs - 1) <= $page_selector_max) {
213 $q1_pgs = $cur_pg - 1;
214 $q2_pgs = $q3_pgs = 0;
215 $q4_pgs = $tot_pgs - $cur_pg;
216
217 /* Otherwise, compute some magic to choose the four quarters. */
218 } else {
219 /*
220 * Compute the magic base values. Added together,
221 * these values will always equal to the $pag_pgs.
222 * NOTE: These are DEFAULT values and do not take
223 * the current page into account. That is below.
224 */
225 $q1_pgs = floor($vis_pgs/4);
226 $q2_pgs = round($vis_pgs/4, 0);
227 $q3_pgs = ceil($vis_pgs/4);
228 $q4_pgs = round(($vis_pgs - $q2_pgs)/3, 0);
229
230 /* Adjust if the first quarter contains the current page. */
231 if (($cur_pg - $q1_pgs) < 1) {
232 $extra_pgs = ($q1_pgs - ($cur_pg - 1)) + $q2_pgs;
233 $q1_pgs = $cur_pg - 1;
234 $q2_pgs = 0;
235 $q3_pgs += ceil($extra_pgs / 2);
236 $q4_pgs += floor($extra_pgs / 2);
237
238 /* Adjust if the first and second quarters intersect. */
239 } else if (($cur_pg - $q2_pgs - ceil($q2_pgs/3)) <= $q1_pgs) {
240 $extra_pgs = $q2_pgs;
241 $extra_pgs -= ceil(($cur_pg - $q1_pgs - 1) * 3/4);
242 $q2_pgs = ceil(($cur_pg - $q1_pgs - 1) * 3/4);
243 $q3_pgs += ceil($extra_pgs / 2);
244 $q4_pgs += floor($extra_pgs / 2);
245
246 /* Adjust if the fourth quarter contains the current page. */
247 } else if (($cur_pg + $q4_pgs) >= $tot_pgs) {
248 $extra_pgs = ($q4_pgs - ($tot_pgs - $cur_pg)) + $q3_pgs;
249 $q3_pgs = 0;
250 $q4_pgs = $tot_pgs - $cur_pg;
251 $q1_pgs += floor($extra_pgs / 2);
252 $q2_pgs += ceil($extra_pgs / 2);
253
254 /* Adjust if the third and fourth quarter intersect. */
255 } else if (($cur_pg + $q3_pgs + 1) >= ($tot_pgs - $q4_pgs + 1)) {
256 $extra_pgs = $q3_pgs;
257 $extra_pgs -= ceil(($tot_pgs - $cur_pg - $q4_pgs) * 3/4);
258 $q3_pgs = ceil(($tot_pgs - $cur_pg - $q4_pgs) * 3/4);
259 $q1_pgs += floor($extra_pgs / 2);
260 $q2_pgs += ceil($extra_pgs / 2);
261 }
262 }
263
264 /*
265 * I am leaving this debug code here, commented out, because
266 * it is a really nice way to see what the above code is doing.
267 * echo "qts = $q1_pgs/$q2_pgs/$q3_pgs/$q4_pgs = "
268 * . ($q1_pgs + $q2_pgs + $q3_pgs + $q4_pgs) . '<br />';
269 */
270
271 /* Print out the page links from the compute page quarters. */
272
273 /* Start with the first quarter. */
274 if (($q1_pgs == 0) && ($cur_pg > 1)) {
275 $pg_str .= "...$spc";
276 } else {
277 for ($pg = 1; $pg <= $q1_pgs; ++$pg) {
278 $start = (($pg-1) * $iLimit) + 1;
279 $pg_str .= get_paginator_link($box, $start, $pg) . $spc;
280 }
281 if ($cur_pg - $q2_pgs - $q1_pgs > 1) {
282 $pg_str .= "...$spc";
283 }
284 }
285
286 /* Continue with the second quarter. */
287 for ($pg = $cur_pg - $q2_pgs; $pg < $cur_pg; ++$pg) {
288 $start = (($pg-1) * $iLimit) + 1;
289 $pg_str .= get_paginator_link($box, $start, $pg) . $spc;
290 }
291
292 /* Now print the current page. */
293 $pg_str .= $cur_pg . $spc;
294
295 /* Next comes the third quarter. */
296 for ($pg = $cur_pg + 1; $pg <= $cur_pg + $q3_pgs; ++$pg) {
297 $start = (($pg-1) * $iLimit) + 1;
298 $pg_str .= get_paginator_link($box, $start, $pg) . $spc;
299 }
300
301 /* And last, print the forth quarter page links. */
302 if (($q4_pgs == 0) && ($cur_pg < $tot_pgs)) {
303 $pg_str .= "...$spc";
304 } else {
305 if (($tot_pgs - $q4_pgs) > ($cur_pg + $q3_pgs)) {
306 $pg_str .= "...$spc";
307 }
308 for ($pg = $tot_pgs - $q4_pgs + 1; $pg <= $tot_pgs; ++$pg) {
309 $start = (($pg-1) * $iLimit) + 1;
310 $pg_str .= get_paginator_link($box, $start,$pg) . $spc;
311 }
312 }
313
314 $last_grp = (($tot_pgs - 1) * $iLimit) + 1;
315 }
316 } else {
317 $pg_str = "<a href=\"$php_self?showall=0"
318 . "&amp;startMessage=1&amp;mailbox=$box\" "
319 . ">" ._("Paginate") . '</a>';
320 }
321
322 /* Put all the pieces of the paginator string together. */
323 /**
324 * Hairy code... But let's leave it like it is since I am not certain
325 * a different approach would be any easier to read. ;)
326 */
327 $result = '';
328 if ( $prv_str || $nxt_str ) {
329
330 /* Compute the 'show all' string. */
331 $all_str = "<a href=\"$php_self?showall=1"
332 . "&amp;startMessage=1&amp;mailbox=$box\" "
333 . ">" . _("Show All") . '</a>';
334
335 $result .= '[';
336 $result .= ($prv_str != '' ? $prv_str . $spc . $sep . $spc : '');
337 $result .= ($nxt_str != '' ? $nxt_str : '');
338 $result .= ']' . $spc ;
339 }
340
341 $result .= ($pg_str != '' ? $spc . '['.$spc.$pg_str.']' . $spc : '');
342 $result .= ($all_str != '' ? $spc . '['.$all_str.']' . $spc . $spc : '');
343
344 /* If the resulting string is blank, return a non-breaking space. */
345 if ($result == '') {
346 $result = '&nbsp;';
347 }
348 /* Return our final magical compact paginator string. */
349 return ($result);
350 }
351 ?>