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) {
53 // keeps count of how many times
54 // the paginator is used, avoids
55 // duplicate naming of <select>
57 static $display_iterations = 0;
58 $display_iterations++
;
60 sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER
);
62 /* Initialize paginator string chunks. */
68 $box = urlencode($box);
70 /* Create simple strings that will be creating the paginator. */
71 /* This will be used as a space. */
72 $spc = $oTemplate->fetch('non_breaking_space.tpl');
73 /* This will be used as a seperator. */
76 /* Make sure that our start message number is not too big. */
77 $iOffset = min($iOffset, $iTotal);
79 /* Compute the starting message of the previous and next page group. */
80 $next_grp = $iOffset +
$iLimit;
81 $prev_grp = $iOffset - $iLimit;
84 /* Compute the basic previous and next strings. */
85 if (($next_grp <= $iTotal) && ($prev_grp >= 0)) {
86 $prv_str = get_paginator_link($box, $prev_grp, '<');
87 $nxt_str = get_paginator_link($box, $next_grp, '>');
88 } else if (($next_grp > $iTotal) && ($prev_grp >= 0)) {
89 $prv_str = get_paginator_link($box, $prev_grp, '<');
91 } else if (($next_grp <= $iTotal) && ($prev_grp < 0)) {
93 $nxt_str = get_paginator_link($box, $next_grp, '>');
96 /* Page selector block. Following code computes page links. */
97 if ($iLimit != 0 && $page_selector && ($iTotal > $iLimit)) {
98 /* Most importantly, what is the current page!!! */
99 $cur_pg = intval($iOffset / $iLimit) +
1;
101 /* Compute total # of pages and # of paginator page links. */
102 $tot_pgs = ceil($iTotal / $iLimit); /* Total number of Pages */
104 $last_grp = (($tot_pgs - 1) * $iLimit) +
1;
107 $pg_str = create_hyperlink("$php_self?showall=0&startMessage=1&mailbox=$box", _("Paginate"));
110 /* Put all the pieces of the paginator string together. */
112 * Hairy code... But let's leave it like it is since I am not certain
113 * a different approach would be any easier to read. ;)
116 if ( $prv_str ||
$nxt_str ) {
118 /* Compute the 'show all' string. */
119 $all_str = create_hyperlink("$php_self?showall=1&startMessage=1&mailbox=$box", _("Show All"));
121 $result .= '[' . get_paginator_link($box, 1, '<<') . ']';
122 $result .= '[' . $prv_str . ']';
124 $pg_url = $php_self . '?mailbox=' . $box;
126 $result .= '[' . $nxt_str . ']';
127 $result .= '[' . get_paginator_link($box, $last_grp, '>>') . ']';
129 if ($page_selector) {
131 for ($p = 0; $p < $tot_pgs; $p++
) {
132 $options[(($p*$iLimit)+
1) . '_' . $box] = ($p+
1) . "/$tot_pgs";
134 $result .= $spc . addSelect('startMessage_' . $display_iterations,
136 ((($cur_pg-1)*$iLimit)+
1),
138 ($javascript_on ?
array('onchange' => 'JavaScript:SubmitOnSelect(this, \'' . $pg_url . '&startMessage=\')') : array()));
140 if ($javascript_on) {
141 //FIXME: What in the world? Two issues here: for one, $javascript_on is supposed
142 // to have already detected whether or not JavaScript is available and enabled.
143 // Secondly, we need to rid ourselves of any HTML output in the core. This
144 // is being removed (but left in case the original author points out why it
145 // should not be) and we'll trust $javascript_on to do the right thing.
146 // $result .= '<noscript language="JavaScript">'
147 // . addSubmit(_("Go"), 'paginator_submit_' . $display_iterations)
150 $result .= addSubmit(_("Go"), 'paginator_submit_' . $display_iterations);
155 $result .= ($pg_str != '' ?
'['.$pg_str.']' . $spc : '');
156 $result .= ($all_str != '' ?
$spc . '['.$all_str.']' . $spc . $spc : '');
158 /* If the resulting string is blank, return a non-breaking space. */
162 /* Return our final magical paginator string. */
168 * This function computes the paginator string.
170 * @param string $box mailbox name
171 * @param integer $iOffset offset in total number of messages
172 * @param integer $iTotal total number of messages
173 * @param integer $iLimit maximum number of messages to show on a page
174 * @param bool $bShowAll whether or not to show all messages at once
175 * ("show all" == non paginate mode)
176 * @param bool $page_selector whether or not to show the page selection widget
177 * @param integer $page_selector_max maximum number of pages to show on the screen
179 * @return string $result paginate string with links to pages
182 function get_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll,$page_selector, $page_selector_max) {
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 space. */
197 $spc = $oTemplate->fetch('non_breaking_space.tpl');
198 /* This will be used as a seperator. */
201 /* Make sure that our start message number is not too big. */
202 $iOffset = min($iOffset, $iTotal);
204 /* Compute the starting message of the previous and next page group. */
205 $next_grp = $iOffset +
$iLimit;
206 $prev_grp = $iOffset - $iLimit;
209 /* Compute the basic previous and next strings. */
211 if (($next_grp <= $iTotal) && ($prev_grp >= 0)) {
212 $prv_str = get_paginator_link($box, $prev_grp, _("Previous"));
213 $nxt_str = get_paginator_link($box, $next_grp, _("Next"));
214 } else if (($next_grp > $iTotal) && ($prev_grp >= 0)) {
215 $prv_str = get_paginator_link($box, $prev_grp, _("Previous"));
216 $nxt_str = _("Next");
217 } else if (($next_grp <= $iTotal) && ($prev_grp < 0)) {
218 $prv_str = _("Previous");
219 $nxt_str = get_paginator_link($box, $next_grp, _("Next"));
222 /* Page selector block. Following code computes page links. */
223 if ($iLimit != 0 && $page_selector && ($iTotal > $iLimit)) {
224 /* Most importantly, what is the current page!!! */
225 $cur_pg = intval($iOffset / $iLimit) +
1;
227 /* Compute total # of pages and # of paginator page links. */
228 $tot_pgs = ceil($iTotal / $iLimit); /* Total number of Pages */
230 $vis_pgs = min($page_selector_max, $tot_pgs - 1); /* Visible Pages */
232 /* Compute the size of the four quarters of the page links. */
234 /* If we can, just show all the pages. */
235 if (($tot_pgs - 1) <= $page_selector_max) {
236 $q1_pgs = $cur_pg - 1;
237 $q2_pgs = $q3_pgs = 0;
238 $q4_pgs = $tot_pgs - $cur_pg;
240 /* Otherwise, compute some magic to choose the four quarters. */
243 * Compute the magic base values. Added together,
244 * these values will always equal to the $pag_pgs.
245 * NOTE: These are DEFAULT values and do not take
246 * the current page into account. That is below.
248 $q1_pgs = floor($vis_pgs/4);
249 $q2_pgs = round($vis_pgs/4, 0);
250 $q3_pgs = ceil($vis_pgs/4);
251 $q4_pgs = round(($vis_pgs - $q2_pgs)/3, 0);
253 /* Adjust if the first quarter contains the current page. */
254 if (($cur_pg - $q1_pgs) < 1) {
255 $extra_pgs = ($q1_pgs - ($cur_pg - 1)) +
$q2_pgs;
256 $q1_pgs = $cur_pg - 1;
258 $q3_pgs +
= ceil($extra_pgs / 2);
259 $q4_pgs +
= floor($extra_pgs / 2);
261 /* Adjust if the first and second quarters intersect. */
262 } else if (($cur_pg - $q2_pgs - ceil($q2_pgs/3)) <= $q1_pgs) {
263 $extra_pgs = $q2_pgs;
264 $extra_pgs -= ceil(($cur_pg - $q1_pgs - 1) * 3/4);
265 $q2_pgs = ceil(($cur_pg - $q1_pgs - 1) * 3/4);
266 $q3_pgs +
= ceil($extra_pgs / 2);
267 $q4_pgs +
= floor($extra_pgs / 2);
269 /* Adjust if the fourth quarter contains the current page. */
270 } else if (($cur_pg +
$q4_pgs) >= $tot_pgs) {
271 $extra_pgs = ($q4_pgs - ($tot_pgs - $cur_pg)) +
$q3_pgs;
273 $q4_pgs = $tot_pgs - $cur_pg;
274 $q1_pgs +
= floor($extra_pgs / 2);
275 $q2_pgs +
= ceil($extra_pgs / 2);
277 /* Adjust if the third and fourth quarter intersect. */
278 } else if (($cur_pg +
$q3_pgs +
1) >= ($tot_pgs - $q4_pgs +
1)) {
279 $extra_pgs = $q3_pgs;
280 $extra_pgs -= ceil(($tot_pgs - $cur_pg - $q4_pgs) * 3/4);
281 $q3_pgs = ceil(($tot_pgs - $cur_pg - $q4_pgs) * 3/4);
282 $q1_pgs +
= floor($extra_pgs / 2);
283 $q2_pgs +
= ceil($extra_pgs / 2);
288 * I am leaving this debug code here, commented out, because
289 * it is a really nice way to see what the above code is doing.
290 * echo "qts = $q1_pgs/$q2_pgs/$q3_pgs/$q4_pgs = "
291 * . ($q1_pgs + $q2_pgs + $q3_pgs + $q4_pgs) . '<br />';
294 /* Print out the page links from the compute page quarters. */
296 /* Start with the first quarter. */
297 if (($q1_pgs == 0) && ($cur_pg > 1)) {
298 $pg_str .= "...$spc";
300 for ($pg = 1; $pg <= $q1_pgs; ++
$pg) {
301 $start = (($pg-1) * $iLimit) +
1;
302 $pg_str .= get_paginator_link($box, $start, $pg) . $spc;
304 if ($cur_pg - $q2_pgs - $q1_pgs > 1) {
305 $pg_str .= "...$spc";
309 /* Continue with the second quarter. */
310 for ($pg = $cur_pg - $q2_pgs; $pg < $cur_pg; ++
$pg) {
311 $start = (($pg-1) * $iLimit) +
1;
312 $pg_str .= get_paginator_link($box, $start, $pg) . $spc;
315 /* Now print the current page. */
316 $pg_str .= $cur_pg . $spc;
318 /* Next comes the third quarter. */
319 for ($pg = $cur_pg +
1; $pg <= $cur_pg +
$q3_pgs; ++
$pg) {
320 $start = (($pg-1) * $iLimit) +
1;
321 $pg_str .= get_paginator_link($box, $start, $pg) . $spc;
324 /* And last, print the forth quarter page links. */
325 if (($q4_pgs == 0) && ($cur_pg < $tot_pgs)) {
326 $pg_str .= "...$spc";
328 if (($tot_pgs - $q4_pgs) > ($cur_pg +
$q3_pgs)) {
329 $pg_str .= "...$spc";
331 for ($pg = $tot_pgs - $q4_pgs +
1; $pg <= $tot_pgs; ++
$pg) {
332 $start = (($pg-1) * $iLimit) +
1;
333 $pg_str .= get_paginator_link($box, $start,$pg) . $spc;
337 $last_grp = (($tot_pgs - 1) * $iLimit) +
1;
340 $pg_str = create_hyperlink("$php_self?showall=0&startMessage=1&mailbox=$box", _("Paginate"));
343 /* Put all the pieces of the paginator string together. */
345 * Hairy code... But let's leave it like it is since I am not certain
346 * a different approach would be any easier to read. ;)
349 if ( $prv_str ||
$nxt_str ) {
351 /* Compute the 'show all' string. */
352 $all_str = create_hyperlink("$php_self?showall=1&startMessage=1&mailbox=$box", _("Show All"));
355 $result .= ($prv_str != '' ?
$prv_str . $spc . $sep . $spc : '');
356 $result .= ($nxt_str != '' ?
$nxt_str : '');
357 $result .= ']' . $spc ;
360 $result .= ($pg_str != '' ?
$spc . '['.$spc.$pg_str.']' . $spc : '');
361 $result .= ($all_str != '' ?
$spc . '['.$all_str.']' . $spc . $spc : '');
363 /* If the resulting string is blank, return a non-breaking space. */
365 $result = $oTemplate->fetch('non_breaking_space.tpl');
367 /* Return our final magical compact paginator string. */