Add access keys to mailbox list
[squirrelmail.git] / functions / template / paginator_util.php
1 <?php
2
3 /**
4 * paginator_util.php
5 *
6 * The following functions are utility functions for templates. Do not
7 * echo output in these functions.
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 /** Load forms functions, needed for addsubmit(). */
17 include_once(SM_PATH . 'functions/forms.php');
18
19
20 /**
21 * Generate a paginator link.
22 *
23 * @param string $box Mailbox name
24 * @param integer $start_msg Message Offset
25 * @param string $text The text used for paginator link
26 * @param string $accesskey The access key for the link, if any
27 * @return string
28 */
29 function get_paginator_link($box, $start_msg, $text, $accesskey='NONE') {
30 sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER);
31 return create_hyperlink("$php_self?startMessage=$start_msg&amp;mailbox=$box",
32 $text, '', '', '', '', '',
33 array('accesskey' => $accesskey));
34 }
35
36
37 /**
38 * This function computes the comapact paginator string.
39 *
40 * @param string $box mailbox name
41 * @param integer $iOffset offset in total number of messages
42 * @param integer $iTotal total number of messages
43 * @param integer $iLimit maximum number of messages to show on a page
44 * @param bool $bShowAll whether or not to show all messages at once
45 * ("show all" == non paginate mode)
46 * @param bool $javascript_on whether or not javascript is currently enabled
47 * @param bool $page_selector whether or not to show the page selection widget
48 *
49 * @return string $result paginate string with links to pages
50 *
51 */
52 function get_compact_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll, $javascript_on, $page_selector) {
53
54 static $accesskeys_constructed = FALSE;
55
56 /* This will be used as a space. */
57 global $oTemplate, $nbsp;
58
59 // keeps count of how many times
60 // the paginator is used, avoids
61 // duplicate naming of <select>
62 // and GO button
63 static $display_iterations = 0;
64 $display_iterations++;
65
66 sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER);
67
68 /* Initialize paginator string chunks. */
69 $prv_str = '';
70 $nxt_str = '';
71 $pg_str = '';
72 $all_str = '';
73
74 $box = urlencode($box);
75
76 /* Create simple strings that will be creating the paginator. */
77 /* This will be used as a seperator. */
78 $sep = '|';
79
80 /* Make sure that our start message number is not too big. */
81 $iOffset = min($iOffset, $iTotal);
82
83 /* Compute the starting message of the previous and next page group. */
84 $next_grp = $iOffset + $iLimit;
85 $prev_grp = $iOffset - $iLimit;
86
87 if (!$bShowAll) {
88
89 /* Compute the basic previous and next strings. */
90
91 global $accesskey_mailbox_previous, $accesskey_mailbox_next;
92 if (($next_grp <= $iTotal) && ($prev_grp >= 0)) {
93 $prv_str = get_paginator_link($box, $prev_grp, '<',
94 ($accesskeys_constructed
95 ? '' : $accesskey_mailbox_previous));
96 $nxt_str = get_paginator_link($box, $next_grp, '>',
97 ($accesskeys_constructed
98 ? '' : $accesskey_mailbox_next));
99 } else if (($next_grp > $iTotal) && ($prev_grp >= 0)) {
100 $prv_str = get_paginator_link($box, $prev_grp, '<',
101 ($accesskeys_constructed
102 ? '' : $accesskey_mailbox_previous));
103 $nxt_str = '>';
104 } else if (($next_grp <= $iTotal) && ($prev_grp < 0)) {
105 $prv_str = '<';
106 $nxt_str = get_paginator_link($box, $next_grp, '>',
107 ($accesskeys_constructed
108 ? '' : $accesskey_mailbox_next));
109 }
110
111 /* Page selector block. Following code computes page links. */
112 if ($iLimit != 0 && $page_selector && ($iTotal > $iLimit)) {
113 /* Most importantly, what is the current page!!! */
114 $cur_pg = intval($iOffset / $iLimit) + 1;
115
116 /* Compute total # of pages and # of paginator page links. */
117 $tot_pgs = ceil($iTotal / $iLimit); /* Total number of Pages */
118
119 $last_grp = (($tot_pgs - 1) * $iLimit) + 1;
120 }
121 } else {
122 global $accesskey_mailbox_all_paginate;
123 $pg_str = create_hyperlink("$php_self?showall=0&amp;startMessage=1&amp;mailbox=$box", _("Paginate"), '', '', '', '', '', array('accesskey' => ($accesskeys_constructed ? 'NONE' : $accesskey_mailbox_all_paginate)));
124 }
125
126 /* Put all the pieces of the paginator string together. */
127 /**
128 * Hairy code... But let's leave it like it is since I am not certain
129 * a different approach would be any easier to read. ;)
130 */
131 $result = '';
132 if ( $prv_str || $nxt_str ) {
133
134 /* Compute the 'show all' string. */
135 global $accesskey_mailbox_all_paginate;
136 $all_str = create_hyperlink("$php_self?showall=1&amp;startMessage=1&amp;mailbox=$box", _("Show All"), '', '', '', '', '', array('accesskey' => ($accesskeys_constructed ? 'NONE' : $accesskey_mailbox_all_paginate)));
137
138 $result .= '[' . get_paginator_link($box, 1, '<<') . ']';
139 $result .= '[' . $prv_str . ']';
140
141 $pg_url = $php_self . '?mailbox=' . $box;
142
143 $result .= '[' . $nxt_str . ']';
144 $result .= '[' . get_paginator_link($box, $last_grp, '>>') . ']';
145
146 if ($page_selector) {
147 $options = array();
148 for ($p = 0; $p < $tot_pgs; $p++) {
149 $options[(($p*$iLimit)+1) . '_' . $box] = ($p+1) . "/$tot_pgs";
150 }
151 $result .= $nbsp . addSelect('startMessage_' . $display_iterations,
152 $options,
153 ((($cur_pg-1)*$iLimit)+1),
154 TRUE,
155 ($javascript_on ? array('onchange' => 'JavaScript:SubmitOnSelect(this, \'' . $pg_url . '&startMessage=\')') : array()));
156
157 if ($javascript_on) {
158 //FIXME: What in the world? Two issues here: for one, $javascript_on is supposed
159 // to have already detected whether or not JavaScript is available and enabled.
160 // Secondly, we need to rid ourselves of any HTML output in the core. This
161 // is being removed (but left in case the original author points out why it
162 // should not be) and we'll trust $javascript_on to do the right thing.
163 // $result .= '<noscript language="JavaScript">'
164 // . addSubmit(_("Go"), 'paginator_submit_' . $display_iterations)
165 // . '</noscript>';
166 } else {
167 $result .= addSubmit(_("Go"), 'paginator_submit_' . $display_iterations);
168 }
169 }
170 }
171
172 $result .= ($pg_str != '' ? '['.$pg_str.']' . $nbsp : '');
173 $result .= ($all_str != '' ? $nbsp . '['.$all_str.']' . $nbsp . $nbsp : '');
174
175 /* If the resulting string is blank, return a non-breaking space. */
176 if ($result == '') {
177 $result = '&nbsp;';
178 }
179
180 $accesskeys_constructed = TRUE;
181
182 /* Return our final magical paginator string. */
183 return ($result);
184 }
185
186
187 /**
188 * This function computes the paginator string.
189 *
190 * @param string $box mailbox name
191 * @param integer $iOffset offset in total number of messages
192 * @param integer $iTotal total number of messages
193 * @param integer $iLimit maximum number of messages to show on a page
194 * @param bool $bShowAll whether or not to show all messages at once
195 * ("show all" == non paginate mode)
196 * @param bool $page_selector whether or not to show the page selection widget
197 * @param integer $page_selector_max maximum number of pages to show on the screen
198 *
199 * @return string $result paginate string with links to pages
200 *
201 */
202 function get_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll,$page_selector, $page_selector_max) {
203
204 static $accesskeys_constructed = FALSE;
205
206 /* This will be used as a space. */
207 global $oTemplate, $nbsp;
208 sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER);
209
210 /* Initialize paginator string chunks. */
211 $prv_str = '';
212 $nxt_str = '';
213 $pg_str = '';
214 $all_str = '';
215
216 $box = urlencode($box);
217
218 /* Create simple strings that will be creating the paginator. */
219 /* This will be used as a seperator. */
220 $sep = '|';
221
222 /* Make sure that our start message number is not too big. */
223 $iOffset = min($iOffset, $iTotal);
224
225 /* Compute the starting message of the previous and next page group. */
226 $next_grp = $iOffset + $iLimit;
227 $prev_grp = $iOffset - $iLimit;
228
229 if (!$bShowAll) {
230
231 /* Compute the basic previous and next strings. */
232
233 global $accesskey_mailbox_previous, $accesskey_mailbox_next;
234 if (($next_grp <= $iTotal) && ($prev_grp >= 0)) {
235 $prv_str = get_paginator_link($box, $prev_grp, _("Previous"),
236 ($accesskeys_constructed
237 ? '' : $accesskey_mailbox_previous));
238 $nxt_str = get_paginator_link($box, $next_grp, _("Next"),
239 ($accesskeys_constructed
240 ? '' : $accesskey_mailbox_next));
241 } else if (($next_grp > $iTotal) && ($prev_grp >= 0)) {
242 $prv_str = get_paginator_link($box, $prev_grp, _("Previous"),
243 ($accesskeys_constructed
244 ? '' : $accesskey_mailbox_previous));
245 $nxt_str = _("Next");
246 } else if (($next_grp <= $iTotal) && ($prev_grp < 0)) {
247 $prv_str = _("Previous");
248 $nxt_str = get_paginator_link($box, $next_grp, _("Next"),
249 ($accesskeys_constructed
250 ? '' : $accesskey_mailbox_next));
251 }
252
253 /* Page selector block. Following code computes page links. */
254 if ($iLimit != 0 && $page_selector && ($iTotal > $iLimit)) {
255 /* Most importantly, what is the current page!!! */
256 $cur_pg = intval($iOffset / $iLimit) + 1;
257
258 /* Compute total # of pages and # of paginator page links. */
259 $tot_pgs = ceil($iTotal / $iLimit); /* Total number of Pages */
260
261 $vis_pgs = min($page_selector_max, $tot_pgs - 1); /* Visible Pages */
262
263 /* Compute the size of the four quarters of the page links. */
264
265 /* If we can, just show all the pages. */
266 if (($tot_pgs - 1) <= $page_selector_max) {
267 $q1_pgs = $cur_pg - 1;
268 $q2_pgs = $q3_pgs = 0;
269 $q4_pgs = $tot_pgs - $cur_pg;
270
271 /* Otherwise, compute some magic to choose the four quarters. */
272 } else {
273 /*
274 * Compute the magic base values. Added together,
275 * these values will always equal to the $pag_pgs.
276 * NOTE: These are DEFAULT values and do not take
277 * the current page into account. That is below.
278 */
279 $q1_pgs = floor($vis_pgs/4);
280 $q2_pgs = round($vis_pgs/4, 0);
281 $q3_pgs = ceil($vis_pgs/4);
282 $q4_pgs = round(($vis_pgs - $q2_pgs)/3, 0);
283
284 /* Adjust if the first quarter contains the current page. */
285 if (($cur_pg - $q1_pgs) < 1) {
286 $extra_pgs = ($q1_pgs - ($cur_pg - 1)) + $q2_pgs;
287 $q1_pgs = $cur_pg - 1;
288 $q2_pgs = 0;
289 $q3_pgs += ceil($extra_pgs / 2);
290 $q4_pgs += floor($extra_pgs / 2);
291
292 /* Adjust if the first and second quarters intersect. */
293 } else if (($cur_pg - $q2_pgs - ceil($q2_pgs/3)) <= $q1_pgs) {
294 $extra_pgs = $q2_pgs;
295 $extra_pgs -= ceil(($cur_pg - $q1_pgs - 1) * 3/4);
296 $q2_pgs = ceil(($cur_pg - $q1_pgs - 1) * 3/4);
297 $q3_pgs += ceil($extra_pgs / 2);
298 $q4_pgs += floor($extra_pgs / 2);
299
300 /* Adjust if the fourth quarter contains the current page. */
301 } else if (($cur_pg + $q4_pgs) >= $tot_pgs) {
302 $extra_pgs = ($q4_pgs - ($tot_pgs - $cur_pg)) + $q3_pgs;
303 $q3_pgs = 0;
304 $q4_pgs = $tot_pgs - $cur_pg;
305 $q1_pgs += floor($extra_pgs / 2);
306 $q2_pgs += ceil($extra_pgs / 2);
307
308 /* Adjust if the third and fourth quarter intersect. */
309 } else if (($cur_pg + $q3_pgs + 1) >= ($tot_pgs - $q4_pgs + 1)) {
310 $extra_pgs = $q3_pgs;
311 $extra_pgs -= ceil(($tot_pgs - $cur_pg - $q4_pgs) * 3/4);
312 $q3_pgs = ceil(($tot_pgs - $cur_pg - $q4_pgs) * 3/4);
313 $q1_pgs += floor($extra_pgs / 2);
314 $q2_pgs += ceil($extra_pgs / 2);
315 }
316 }
317
318 /*
319 * I am leaving this debug code here, commented out, because
320 * it is a really nice way to see what the above code is doing.
321 * echo "qts = $q1_pgs/$q2_pgs/$q3_pgs/$q4_pgs = "
322 * . ($q1_pgs + $q2_pgs + $q3_pgs + $q4_pgs) . '<br />';
323 */
324
325 /* Print out the page links from the compute page quarters. */
326
327 /* Start with the first quarter. */
328 if (($q1_pgs == 0) && ($cur_pg > 1)) {
329 $pg_str .= "...$nbsp";
330 } else {
331 for ($pg = 1; $pg <= $q1_pgs; ++$pg) {
332 $start = (($pg-1) * $iLimit) + 1;
333 $pg_str .= get_paginator_link($box, $start, $pg) . $nbsp;
334 }
335 if ($cur_pg - $q2_pgs - $q1_pgs > 1) {
336 $pg_str .= "...$nbsp";
337 }
338 }
339
340 /* Continue with the second quarter. */
341 for ($pg = $cur_pg - $q2_pgs; $pg < $cur_pg; ++$pg) {
342 $start = (($pg-1) * $iLimit) + 1;
343 $pg_str .= get_paginator_link($box, $start, $pg) . $nbsp;
344 }
345
346 /* Now print the current page. */
347 $pg_str .= $cur_pg . $nbsp;
348
349 /* Next comes the third quarter. */
350 for ($pg = $cur_pg + 1; $pg <= $cur_pg + $q3_pgs; ++$pg) {
351 $start = (($pg-1) * $iLimit) + 1;
352 $pg_str .= get_paginator_link($box, $start, $pg) . $nbsp;
353 }
354
355 /* And last, print the forth quarter page links. */
356 if (($q4_pgs == 0) && ($cur_pg < $tot_pgs)) {
357 $pg_str .= "...$nbsp";
358 } else {
359 if (($tot_pgs - $q4_pgs) > ($cur_pg + $q3_pgs)) {
360 $pg_str .= "...$nbsp";
361 }
362 for ($pg = $tot_pgs - $q4_pgs + 1; $pg <= $tot_pgs; ++$pg) {
363 $start = (($pg-1) * $iLimit) + 1;
364 $pg_str .= get_paginator_link($box, $start,$pg) . $nbsp;
365 }
366 }
367
368 $last_grp = (($tot_pgs - 1) * $iLimit) + 1;
369 }
370 } else {
371 global $accesskey_mailbox_all_paginate;
372 $pg_str = create_hyperlink("$php_self?showall=0&amp;startMessage=1&amp;mailbox=$box", _("Paginate"), '', '', '', '', '', array('accesskey' => ($accesskeys_constructed ? 'NONE' : $accesskey_mailbox_all_paginate)));
373 }
374
375 /* Put all the pieces of the paginator string together. */
376 /**
377 * Hairy code... But let's leave it like it is since I am not certain
378 * a different approach would be any easier to read. ;)
379 */
380 $result = '';
381 if ( $prv_str || $nxt_str ) {
382
383 /* Compute the 'show all' string. */
384 global $accesskey_mailbox_all_paginate;
385 $all_str = create_hyperlink("$php_self?showall=1&amp;startMessage=1&amp;mailbox=$box", _("Show All"), '', '', '', '', '', array('accesskey' => ($accesskeys_constructed ? 'NONE' : $accesskey_mailbox_all_paginate)));
386
387 $result .= '[';
388 $result .= ($prv_str != '' ? $prv_str . $nbsp . $sep . $nbsp : '');
389 $result .= ($nxt_str != '' ? $nxt_str : '');
390 $result .= ']' . $nbsp ;
391 }
392
393 $result .= ($pg_str != '' ? $nbsp . '['.$nbsp.$pg_str.']' . $nbsp : '');
394 $result .= ($all_str != '' ? $nbsp . '['.$all_str.']' . $nbsp . $nbsp : '');
395
396 /* If the resulting string is blank, return a non-breaking space. */
397 if ($result == '') {
398 $result = $nbsp;
399 }
400
401 $accesskeys_constructed = TRUE;
402
403 /* Return our final magical compact paginator string. */
404 return ($result);
405 }
406
407