6 * The following functions are utility functions for templates. Do not
7 * echo output in these functions.
9 * @copyright © 2005-2006 The SquirrelMail Project Team
10 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
12 * @package squirrelmail
16 /** Load forms functions, needed for addsubmit(). */
17 include_once(SM_PATH
. 'functions/forms.php');
21 * Generate a paginator link.
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
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&mailbox=$box",
32 $text, '', '', '', '', '',
33 array('accesskey' => $accesskey));
38 * This function computes the comapact paginator string.
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
49 * @return string $result paginate string with links to pages
52 function get_compact_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll, $javascript_on, $page_selector) {
54 static $accesskeys_constructed = FALSE;
56 /* This will be used as a space. */
57 global $oTemplate, $nbsp;
59 // keeps count of how many times
60 // the paginator is used, avoids
61 // duplicate naming of <select>
63 static $display_iterations = 0;
64 $display_iterations++
;
66 sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER
);
68 /* Initialize paginator string chunks. */
74 $box = urlencode($box);
76 /* Create simple strings that will be creating the paginator. */
77 /* This will be used as a seperator. */
80 /* Make sure that our start message number is not too big. */
81 $iOffset = min($iOffset, $iTotal);
83 /* Compute the starting message of the previous and next page group. */
84 $next_grp = $iOffset +
$iLimit;
85 $prev_grp = $iOffset - $iLimit;
89 /* Compute the basic previous and next strings. */
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));
104 } else if (($next_grp <= $iTotal) && ($prev_grp < 0)) {
106 $nxt_str = get_paginator_link($box, $next_grp, '>',
107 ($accesskeys_constructed
108 ?
'' : $accesskey_mailbox_next));
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;
116 /* Compute total # of pages and # of paginator page links. */
117 $tot_pgs = ceil($iTotal / $iLimit); /* Total number of Pages */
119 $last_grp = (($tot_pgs - 1) * $iLimit) +
1;
122 global $accesskey_mailbox_all_paginate;
123 $pg_str = create_hyperlink("$php_self?showall=0&startMessage=1&mailbox=$box", _("Paginate"), '', '', '', '', '', array('accesskey' => ($accesskeys_constructed ?
'NONE' : $accesskey_mailbox_all_paginate)));
126 /* Put all the pieces of the paginator string together. */
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. ;)
132 if ( $prv_str ||
$nxt_str ) {
134 /* Compute the 'show all' string. */
135 global $accesskey_mailbox_all_paginate;
136 $all_str = create_hyperlink("$php_self?showall=1&startMessage=1&mailbox=$box", _("Show All"), '', '', '', '', '', array('accesskey' => ($accesskeys_constructed ?
'NONE' : $accesskey_mailbox_all_paginate)));
138 $result .= '[' . get_paginator_link($box, 1, '<<') . ']';
139 $result .= '[' . $prv_str . ']';
141 $pg_url = $php_self . '?mailbox=' . $box;
143 $result .= '[' . $nxt_str . ']';
144 $result .= '[' . get_paginator_link($box, $last_grp, '>>') . ']';
146 if ($page_selector) {
148 for ($p = 0; $p < $tot_pgs; $p++
) {
149 $options[(($p*$iLimit)+
1) . '_' . $box] = ($p+
1) . "/$tot_pgs";
151 $result .= $nbsp . addSelect('startMessage_' . $display_iterations,
153 ((($cur_pg-1)*$iLimit)+
1),
155 ($javascript_on ?
array('onchange' => 'JavaScript:SubmitOnSelect(this, \'' . $pg_url . '&startMessage=\')') : array()));
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)
167 $result .= addSubmit(_("Go"), 'paginator_submit_' . $display_iterations);
172 $result .= ($pg_str != '' ?
'['.$pg_str.']' . $nbsp : '');
173 $result .= ($all_str != '' ?
$nbsp . '['.$all_str.']' . $nbsp . $nbsp : '');
175 /* If the resulting string is blank, return a non-breaking space. */
180 $accesskeys_constructed = TRUE;
182 /* Return our final magical paginator string. */
188 * This function computes the paginator string.
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
199 * @return string $result paginate string with links to pages
202 function get_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll,$page_selector, $page_selector_max) {
204 static $accesskeys_constructed = FALSE;
206 /* This will be used as a space. */
207 global $oTemplate, $nbsp;
208 sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER
);
210 /* Initialize paginator string chunks. */
216 $box = urlencode($box);
218 /* Create simple strings that will be creating the paginator. */
219 /* This will be used as a seperator. */
222 /* Make sure that our start message number is not too big. */
223 $iOffset = min($iOffset, $iTotal);
225 /* Compute the starting message of the previous and next page group. */
226 $next_grp = $iOffset +
$iLimit;
227 $prev_grp = $iOffset - $iLimit;
231 /* Compute the basic previous and next strings. */
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));
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;
258 /* Compute total # of pages and # of paginator page links. */
259 $tot_pgs = ceil($iTotal / $iLimit); /* Total number of Pages */
261 $vis_pgs = min($page_selector_max, $tot_pgs - 1); /* Visible Pages */
263 /* Compute the size of the four quarters of the page links. */
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;
271 /* Otherwise, compute some magic to choose the four quarters. */
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.
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);
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;
289 $q3_pgs +
= ceil($extra_pgs / 2);
290 $q4_pgs +
= floor($extra_pgs / 2);
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);
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;
304 $q4_pgs = $tot_pgs - $cur_pg;
305 $q1_pgs +
= floor($extra_pgs / 2);
306 $q2_pgs +
= ceil($extra_pgs / 2);
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);
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 />';
325 /* Print out the page links from the compute page quarters. */
327 /* Start with the first quarter. */
328 if (($q1_pgs == 0) && ($cur_pg > 1)) {
329 $pg_str .= "...$nbsp";
331 for ($pg = 1; $pg <= $q1_pgs; ++
$pg) {
332 $start = (($pg-1) * $iLimit) +
1;
333 $pg_str .= get_paginator_link($box, $start, $pg) . $nbsp;
335 if ($cur_pg - $q2_pgs - $q1_pgs > 1) {
336 $pg_str .= "...$nbsp";
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;
346 /* Now print the current page. */
347 $pg_str .= $cur_pg . $nbsp;
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;
355 /* And last, print the forth quarter page links. */
356 if (($q4_pgs == 0) && ($cur_pg < $tot_pgs)) {
357 $pg_str .= "...$nbsp";
359 if (($tot_pgs - $q4_pgs) > ($cur_pg +
$q3_pgs)) {
360 $pg_str .= "...$nbsp";
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;
368 $last_grp = (($tot_pgs - 1) * $iLimit) +
1;
371 global $accesskey_mailbox_all_paginate;
372 $pg_str = create_hyperlink("$php_self?showall=0&startMessage=1&mailbox=$box", _("Paginate"), '', '', '', '', '', array('accesskey' => ($accesskeys_constructed ?
'NONE' : $accesskey_mailbox_all_paginate)));
375 /* Put all the pieces of the paginator string together. */
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. ;)
381 if ( $prv_str ||
$nxt_str ) {
383 /* Compute the 'show all' string. */
384 global $accesskey_mailbox_all_paginate;
385 $all_str = create_hyperlink("$php_self?showall=1&startMessage=1&mailbox=$box", _("Show All"), '', '', '', '', '', array('accesskey' => ($accesskeys_constructed ?
'NONE' : $accesskey_mailbox_all_paginate)));
388 $result .= ($prv_str != '' ?
$prv_str . $nbsp . $sep . $nbsp : '');
389 $result .= ($nxt_str != '' ?
$nxt_str : '');
390 $result .= ']' . $nbsp ;
393 $result .= ($pg_str != '' ?
$nbsp . '['.$nbsp.$pg_str.']' . $nbsp : '');
394 $result .= ($all_str != '' ?
$nbsp . '['.$all_str.']' . $nbsp . $nbsp : '');
396 /* If the resulting string is blank, return a non-breaking space. */
401 $accesskeys_constructed = TRUE;
403 /* Return our final magical compact paginator string. */