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 text used for paginator link
28 function get_paginator_link($box, $start_msg, $text) {
29 sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER
);
30 return create_hyperlink("$php_self?startMessage=$start_msg&mailbox=$box", $text);
35 * This function computes the comapact paginator string.
37 * @param string $box mailbox name
38 * @param integer $iOffset offset in total number of messages
39 * @param integer $iTotal total number of messages
40 * @param integer $iLimit maximum number of messages to show on a page
41 * @param bool $bShowAll whether or not to show all messages at once
42 * ("show all" == non paginate mode)
43 * @param bool $javascript_on whether or not javascript is currently enabled
44 * @param bool $page_selector whether or not to show the page selection widget
46 * @return string $result paginate string with links to pages
49 function get_compact_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll, $javascript_on, $page_selector) {
51 /* This will be used as a space. */
52 global $oTemplate, $nbsp;
54 // keeps count of how many times
55 // the paginator is used, avoids
56 // duplicate naming of <select>
58 static $display_iterations = 0;
59 $display_iterations++
;
61 sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER
);
63 /* Initialize paginator string chunks. */
69 $box = urlencode($box);
71 /* Create simple strings that will be creating the paginator. */
72 /* This will be used as a seperator. */
75 /* Make sure that our start message number is not too big. */
76 $iOffset = min($iOffset, $iTotal);
78 /* Compute the starting message of the previous and next page group. */
79 $next_grp = $iOffset +
$iLimit;
80 $prev_grp = $iOffset - $iLimit;
83 /* Compute the basic previous and next strings. */
84 if (($next_grp <= $iTotal) && ($prev_grp >= 0)) {
85 $prv_str = get_paginator_link($box, $prev_grp, '<');
86 $nxt_str = get_paginator_link($box, $next_grp, '>');
87 } else if (($next_grp > $iTotal) && ($prev_grp >= 0)) {
88 $prv_str = get_paginator_link($box, $prev_grp, '<');
90 } else if (($next_grp <= $iTotal) && ($prev_grp < 0)) {
92 $nxt_str = get_paginator_link($box, $next_grp, '>');
95 /* Page selector block. Following code computes page links. */
96 if ($iLimit != 0 && $page_selector && ($iTotal > $iLimit)) {
97 /* Most importantly, what is the current page!!! */
98 $cur_pg = intval($iOffset / $iLimit) +
1;
100 /* Compute total # of pages and # of paginator page links. */
101 $tot_pgs = ceil($iTotal / $iLimit); /* Total number of Pages */
103 $last_grp = (($tot_pgs - 1) * $iLimit) +
1;
106 $pg_str = create_hyperlink("$php_self?showall=0&startMessage=1&mailbox=$box", _("Paginate"));
109 /* Put all the pieces of the paginator string together. */
111 * Hairy code... But let's leave it like it is since I am not certain
112 * a different approach would be any easier to read. ;)
115 if ( $prv_str ||
$nxt_str ) {
117 /* Compute the 'show all' string. */
118 $all_str = create_hyperlink("$php_self?showall=1&startMessage=1&mailbox=$box", _("Show All"));
120 $result .= '[' . get_paginator_link($box, 1, '<<') . ']';
121 $result .= '[' . $prv_str . ']';
123 $pg_url = $php_self . '?mailbox=' . $box;
125 $result .= '[' . $nxt_str . ']';
126 $result .= '[' . get_paginator_link($box, $last_grp, '>>') . ']';
128 if ($page_selector) {
130 for ($p = 0; $p < $tot_pgs; $p++
) {
131 $options[(($p*$iLimit)+
1) . '_' . $box] = ($p+
1) . "/$tot_pgs";
133 $result .= $nbsp . addSelect('startMessage_' . $display_iterations,
135 ((($cur_pg-1)*$iLimit)+
1),
137 ($javascript_on ?
array('onchange' => 'JavaScript:SubmitOnSelect(this, \'' . $pg_url . '&startMessage=\')') : array()));
139 if ($javascript_on) {
140 //FIXME: What in the world? Two issues here: for one, $javascript_on is supposed
141 // to have already detected whether or not JavaScript is available and enabled.
142 // Secondly, we need to rid ourselves of any HTML output in the core. This
143 // is being removed (but left in case the original author points out why it
144 // should not be) and we'll trust $javascript_on to do the right thing.
145 // $result .= '<noscript language="JavaScript">'
146 // . addSubmit(_("Go"), 'paginator_submit_' . $display_iterations)
149 $result .= addSubmit(_("Go"), 'paginator_submit_' . $display_iterations);
154 $result .= ($pg_str != '' ?
'['.$pg_str.']' . $nbsp : '');
155 $result .= ($all_str != '' ?
$nbsp . '['.$all_str.']' . $nbsp . $nbsp : '');
157 /* If the resulting string is blank, return a non-breaking space. */
161 /* Return our final magical paginator string. */
167 * This function computes the paginator string.
169 * @param string $box mailbox name
170 * @param integer $iOffset offset in total number of messages
171 * @param integer $iTotal total number of messages
172 * @param integer $iLimit maximum number of messages to show on a page
173 * @param bool $bShowAll whether or not to show all messages at once
174 * ("show all" == non paginate mode)
175 * @param bool $page_selector whether or not to show the page selection widget
176 * @param integer $page_selector_max maximum number of pages to show on the screen
178 * @return string $result paginate string with links to pages
181 function get_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll,$page_selector, $page_selector_max) {
183 /* This will be used as a space. */
184 global $oTemplate, $nbsp;
185 sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER
);
187 /* Initialize paginator string chunks. */
193 $box = urlencode($box);
195 /* Create simple strings that will be creating the paginator. */
196 /* This will be used as a seperator. */
199 /* Make sure that our start message number is not too big. */
200 $iOffset = min($iOffset, $iTotal);
202 /* Compute the starting message of the previous and next page group. */
203 $next_grp = $iOffset +
$iLimit;
204 $prev_grp = $iOffset - $iLimit;
207 /* Compute the basic previous and next strings. */
209 if (($next_grp <= $iTotal) && ($prev_grp >= 0)) {
210 $prv_str = get_paginator_link($box, $prev_grp, _("Previous"));
211 $nxt_str = get_paginator_link($box, $next_grp, _("Next"));
212 } else if (($next_grp > $iTotal) && ($prev_grp >= 0)) {
213 $prv_str = get_paginator_link($box, $prev_grp, _("Previous"));
214 $nxt_str = _("Next");
215 } else if (($next_grp <= $iTotal) && ($prev_grp < 0)) {
216 $prv_str = _("Previous");
217 $nxt_str = get_paginator_link($box, $next_grp, _("Next"));
220 /* Page selector block. Following code computes page links. */
221 if ($iLimit != 0 && $page_selector && ($iTotal > $iLimit)) {
222 /* Most importantly, what is the current page!!! */
223 $cur_pg = intval($iOffset / $iLimit) +
1;
225 /* Compute total # of pages and # of paginator page links. */
226 $tot_pgs = ceil($iTotal / $iLimit); /* Total number of Pages */
228 $vis_pgs = min($page_selector_max, $tot_pgs - 1); /* Visible Pages */
230 /* Compute the size of the four quarters of the page links. */
232 /* If we can, just show all the pages. */
233 if (($tot_pgs - 1) <= $page_selector_max) {
234 $q1_pgs = $cur_pg - 1;
235 $q2_pgs = $q3_pgs = 0;
236 $q4_pgs = $tot_pgs - $cur_pg;
238 /* Otherwise, compute some magic to choose the four quarters. */
241 * Compute the magic base values. Added together,
242 * these values will always equal to the $pag_pgs.
243 * NOTE: These are DEFAULT values and do not take
244 * the current page into account. That is below.
246 $q1_pgs = floor($vis_pgs/4);
247 $q2_pgs = round($vis_pgs/4, 0);
248 $q3_pgs = ceil($vis_pgs/4);
249 $q4_pgs = round(($vis_pgs - $q2_pgs)/3, 0);
251 /* Adjust if the first quarter contains the current page. */
252 if (($cur_pg - $q1_pgs) < 1) {
253 $extra_pgs = ($q1_pgs - ($cur_pg - 1)) +
$q2_pgs;
254 $q1_pgs = $cur_pg - 1;
256 $q3_pgs +
= ceil($extra_pgs / 2);
257 $q4_pgs +
= floor($extra_pgs / 2);
259 /* Adjust if the first and second quarters intersect. */
260 } else if (($cur_pg - $q2_pgs - ceil($q2_pgs/3)) <= $q1_pgs) {
261 $extra_pgs = $q2_pgs;
262 $extra_pgs -= ceil(($cur_pg - $q1_pgs - 1) * 3/4);
263 $q2_pgs = ceil(($cur_pg - $q1_pgs - 1) * 3/4);
264 $q3_pgs +
= ceil($extra_pgs / 2);
265 $q4_pgs +
= floor($extra_pgs / 2);
267 /* Adjust if the fourth quarter contains the current page. */
268 } else if (($cur_pg +
$q4_pgs) >= $tot_pgs) {
269 $extra_pgs = ($q4_pgs - ($tot_pgs - $cur_pg)) +
$q3_pgs;
271 $q4_pgs = $tot_pgs - $cur_pg;
272 $q1_pgs +
= floor($extra_pgs / 2);
273 $q2_pgs +
= ceil($extra_pgs / 2);
275 /* Adjust if the third and fourth quarter intersect. */
276 } else if (($cur_pg +
$q3_pgs +
1) >= ($tot_pgs - $q4_pgs +
1)) {
277 $extra_pgs = $q3_pgs;
278 $extra_pgs -= ceil(($tot_pgs - $cur_pg - $q4_pgs) * 3/4);
279 $q3_pgs = ceil(($tot_pgs - $cur_pg - $q4_pgs) * 3/4);
280 $q1_pgs +
= floor($extra_pgs / 2);
281 $q2_pgs +
= ceil($extra_pgs / 2);
286 * I am leaving this debug code here, commented out, because
287 * it is a really nice way to see what the above code is doing.
288 * echo "qts = $q1_pgs/$q2_pgs/$q3_pgs/$q4_pgs = "
289 * . ($q1_pgs + $q2_pgs + $q3_pgs + $q4_pgs) . '<br />';
292 /* Print out the page links from the compute page quarters. */
294 /* Start with the first quarter. */
295 if (($q1_pgs == 0) && ($cur_pg > 1)) {
296 $pg_str .= "...$nbsp";
298 for ($pg = 1; $pg <= $q1_pgs; ++
$pg) {
299 $start = (($pg-1) * $iLimit) +
1;
300 $pg_str .= get_paginator_link($box, $start, $pg) . $nbsp;
302 if ($cur_pg - $q2_pgs - $q1_pgs > 1) {
303 $pg_str .= "...$nbsp";
307 /* Continue with the second quarter. */
308 for ($pg = $cur_pg - $q2_pgs; $pg < $cur_pg; ++
$pg) {
309 $start = (($pg-1) * $iLimit) +
1;
310 $pg_str .= get_paginator_link($box, $start, $pg) . $nbsp;
313 /* Now print the current page. */
314 $pg_str .= $cur_pg . $nbsp;
316 /* Next comes the third quarter. */
317 for ($pg = $cur_pg +
1; $pg <= $cur_pg +
$q3_pgs; ++
$pg) {
318 $start = (($pg-1) * $iLimit) +
1;
319 $pg_str .= get_paginator_link($box, $start, $pg) . $nbsp;
322 /* And last, print the forth quarter page links. */
323 if (($q4_pgs == 0) && ($cur_pg < $tot_pgs)) {
324 $pg_str .= "...$nbsp";
326 if (($tot_pgs - $q4_pgs) > ($cur_pg +
$q3_pgs)) {
327 $pg_str .= "...$nbsp";
329 for ($pg = $tot_pgs - $q4_pgs +
1; $pg <= $tot_pgs; ++
$pg) {
330 $start = (($pg-1) * $iLimit) +
1;
331 $pg_str .= get_paginator_link($box, $start,$pg) . $nbsp;
335 $last_grp = (($tot_pgs - 1) * $iLimit) +
1;
338 $pg_str = create_hyperlink("$php_self?showall=0&startMessage=1&mailbox=$box", _("Paginate"));
341 /* Put all the pieces of the paginator string together. */
343 * Hairy code... But let's leave it like it is since I am not certain
344 * a different approach would be any easier to read. ;)
347 if ( $prv_str ||
$nxt_str ) {
349 /* Compute the 'show all' string. */
350 $all_str = create_hyperlink("$php_self?showall=1&startMessage=1&mailbox=$box", _("Show All"));
353 $result .= ($prv_str != '' ?
$prv_str . $nbsp . $sep . $nbsp : '');
354 $result .= ($nxt_str != '' ?
$nxt_str : '');
355 $result .= ']' . $nbsp ;
358 $result .= ($pg_str != '' ?
$nbsp . '['.$nbsp.$pg_str.']' . $nbsp : '');
359 $result .= ($all_str != '' ?
$nbsp . '['.$all_str.']' . $nbsp . $nbsp : '');
361 /* If the resulting string is blank, return a non-breaking space. */
365 /* Return our final magical compact paginator string. */