5 * The following functions are utility functions for this template. Do not
6 * echo output in those functions. Output is generated above this comment block
7 * @package squirrelmail
11 * Generate a paginator link.
13 * @param string $box Mailbox name
14 * @param integer $start_msg Message Offset
15 * @param string $text text used for paginator link
18 function get_paginator_link($box, $start_msg, $text) {
19 sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER
);
20 $result = "<a href=\"$php_self?startMessage=$start_msg&mailbox=$box\" "
27 * This function computes the comapact paginator string.
29 * @param string $box mailbox name
30 * @param integer $iOffset offset in total number of messages
31 * @param integer $iTotal total number of messages
32 * @param integer $iLimit maximum number of messages to show on a page
33 * @param bool $bShowAll show all messages at once (non paginate mode)
34 * @return string $result paginate string with links to pages
36 function get_compact_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll, $javascript_on, $page_selector) {
38 sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER
);
40 /* Initialize paginator string chunks. */
46 $box = urlencode($box);
48 /* Create simple strings that will be creating the paginator. */
49 $spc = ' '; /* This will be used as a space. */
50 $sep = '|'; /* This will be used as a seperator. */
52 /* Make sure that our start message number is not too big. */
53 $iOffset = min($iOffset, $iTotal);
55 /* Compute the starting message of the previous and next page group. */
56 $next_grp = $iOffset +
$iLimit;
57 $prev_grp = $iOffset - $iLimit;
60 /* Compute the basic previous and next strings. */
61 if (($next_grp <= $iTotal) && ($prev_grp >= 0)) {
62 $prv_str = get_paginator_link($box, $prev_grp, '<');
63 $nxt_str = get_paginator_link($box, $next_grp, '>');
64 } else if (($next_grp > $iTotal) && ($prev_grp >= 0)) {
65 $prv_str = get_paginator_link($box, $prev_grp, '<');
67 } else if (($next_grp <= $iTotal) && ($prev_grp < 0)) {
69 $nxt_str = get_paginator_link($box, $next_grp, '>');
72 /* Page selector block. Following code computes page links. */
73 if ($iLimit != 0 && $page_selector && ($iTotal > $iLimit)) {
74 /* Most importantly, what is the current page!!! */
75 $cur_pg = intval($iOffset / $iLimit) +
1;
77 /* Compute total # of pages and # of paginator page links. */
78 $tot_pgs = ceil($iTotal / $iLimit); /* Total number of Pages */
80 $last_grp = (($tot_pgs - 1) * $iLimit) +
1;
83 $pg_str = "<a href=\"$php_self?showall=0"
84 . "&startMessage=1&mailbox=$box\" "
85 . ">" ._("Paginate") . '</a>';
88 /* Put all the pieces of the paginator string together. */
90 * Hairy code... But let's leave it like it is since I am not certain
91 * a different approach would be any easier to read. ;)
94 if ( $prv_str ||
$nxt_str ) {
96 /* Compute the 'show all' string. */
97 $all_str = "<a href=\"$php_self?showall=1"
98 . "&startMessage=1&mailbox=$box\" "
99 . ">" . _("Show All") . '</a>';
101 $result .= '[' . get_paginator_link($box, 1, '<<') . ']';
102 $result .= '[' . $prv_str . ']';
104 $pg_url = $php_self . '?mailbox=' . $box;
106 $result .= '[' . $nxt_str . ']';
107 $result .= '[' . get_paginator_link($box, $last_grp, '>>') . ']';
109 if ($page_selector) {
110 $result .= $spc . '<select name="startMessage"';
111 if ($javascript_on) {
112 $result .= ' onchange="JavaScript:SubmitOnSelect'
113 . '(this, \'' . $pg_url . '&startMessage=\')"';
117 for ($p = 0; $p < $tot_pgs; $p++
) {
118 $result .= '<option ';
119 if (($p+
1) == $cur_pg) $result .= 'selected ';
120 $result .= 'value="' . (($p*$iLimit)+
1) . '">'
121 . ($p+
1) . "/$tot_pgs" . '</option>';
124 $result .= '</select>';
126 if ($javascript_on) {
127 $result .= '<noscript language="JavaScript">'
131 $result .= addSubmit(_("Go"));
136 $result .= ($pg_str != '' ?
'['.$pg_str.']' . $spc : '');
137 $result .= ($all_str != '' ?
$spc . '['.$all_str.']' . $spc . $spc : '');
139 /* If the resulting string is blank, return a non-breaking space. */
143 /* Return our final magical paginator string. */
148 * This function computes the paginator string.
150 * @param string $box mailbox name
151 * @param integer $iOffset offset in total number of messages
152 * @param integer $iTotal total number of messages
153 * @param integer $iLimit maximum number of messages to show on a page
154 * @param bool $bShowAll show all messages at once (non paginate mode)
155 * @return string $result paginate string with links to pages
157 function get_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll,$page_selector, $page_selector_max) {
159 sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER
);
161 /* Initialize paginator string chunks. */
167 $box = urlencode($box);
169 /* Create simple strings that will be creating the paginator. */
170 $spc = ' '; /* This will be used as a space. */
171 $sep = '|'; /* This will be used as a seperator. */
173 /* Make sure that our start message number is not too big. */
174 $iOffset = min($iOffset, $iTotal);
176 /* Compute the starting message of the previous and next page group. */
177 $next_grp = $iOffset +
$iLimit;
178 $prev_grp = $iOffset - $iLimit;
181 /* Compute the basic previous and next strings. */
183 if (($next_grp <= $iTotal) && ($prev_grp >= 0)) {
184 $prv_str = get_paginator_link($box, $prev_grp, _("Previous"));
185 $nxt_str = get_paginator_link($box, $next_grp, _("Next"));
186 } else if (($next_grp > $iTotal) && ($prev_grp >= 0)) {
187 $prv_str = get_paginator_link($box, $prev_grp, _("Previous"));
188 $nxt_str = _("Next");
189 } else if (($next_grp <= $iTotal) && ($prev_grp < 0)) {
190 $prv_str = _("Previous");
191 $nxt_str = get_paginator_link($box, $next_grp, _("Next"));
194 /* Page selector block. Following code computes page links. */
195 if ($iLimit != 0 && $page_selector && ($iTotal > $iLimit)) {
196 /* Most importantly, what is the current page!!! */
197 $cur_pg = intval($iOffset / $iLimit) +
1;
199 /* Compute total # of pages and # of paginator page links. */
200 $tot_pgs = ceil($iTotal / $iLimit); /* Total number of Pages */
202 $vis_pgs = min($page_selector_max, $tot_pgs - 1); /* Visible Pages */
204 /* Compute the size of the four quarters of the page links. */
206 /* If we can, just show all the pages. */
207 if (($tot_pgs - 1) <= $page_selector_max) {
208 $q1_pgs = $cur_pg - 1;
209 $q2_pgs = $q3_pgs = 0;
210 $q4_pgs = $tot_pgs - $cur_pg;
212 /* Otherwise, compute some magic to choose the four quarters. */
215 * Compute the magic base values. Added together,
216 * these values will always equal to the $pag_pgs.
217 * NOTE: These are DEFAULT values and do not take
218 * the current page into account. That is below.
220 $q1_pgs = floor($vis_pgs/4);
221 $q2_pgs = round($vis_pgs/4, 0);
222 $q3_pgs = ceil($vis_pgs/4);
223 $q4_pgs = round(($vis_pgs - $q2_pgs)/3, 0);
225 /* Adjust if the first quarter contains the current page. */
226 if (($cur_pg - $q1_pgs) < 1) {
227 $extra_pgs = ($q1_pgs - ($cur_pg - 1)) +
$q2_pgs;
228 $q1_pgs = $cur_pg - 1;
230 $q3_pgs +
= ceil($extra_pgs / 2);
231 $q4_pgs +
= floor($extra_pgs / 2);
233 /* Adjust if the first and second quarters intersect. */
234 } else if (($cur_pg - $q2_pgs - ceil($q2_pgs/3)) <= $q1_pgs) {
235 $extra_pgs = $q2_pgs;
236 $extra_pgs -= ceil(($cur_pg - $q1_pgs - 1) * 3/4);
237 $q2_pgs = ceil(($cur_pg - $q1_pgs - 1) * 3/4);
238 $q3_pgs +
= ceil($extra_pgs / 2);
239 $q4_pgs +
= floor($extra_pgs / 2);
241 /* Adjust if the fourth quarter contains the current page. */
242 } else if (($cur_pg +
$q4_pgs) >= $tot_pgs) {
243 $extra_pgs = ($q4_pgs - ($tot_pgs - $cur_pg)) +
$q3_pgs;
245 $q4_pgs = $tot_pgs - $cur_pg;
246 $q1_pgs +
= floor($extra_pgs / 2);
247 $q2_pgs +
= ceil($extra_pgs / 2);
249 /* Adjust if the third and fourth quarter intersect. */
250 } else if (($cur_pg +
$q3_pgs +
1) >= ($tot_pgs - $q4_pgs +
1)) {
251 $extra_pgs = $q3_pgs;
252 $extra_pgs -= ceil(($tot_pgs - $cur_pg - $q4_pgs) * 3/4);
253 $q3_pgs = ceil(($tot_pgs - $cur_pg - $q4_pgs) * 3/4);
254 $q1_pgs +
= floor($extra_pgs / 2);
255 $q2_pgs +
= ceil($extra_pgs / 2);
260 * I am leaving this debug code here, commented out, because
261 * it is a really nice way to see what the above code is doing.
262 * echo "qts = $q1_pgs/$q2_pgs/$q3_pgs/$q4_pgs = "
263 * . ($q1_pgs + $q2_pgs + $q3_pgs + $q4_pgs) . '<br />';
266 /* Print out the page links from the compute page quarters. */
268 /* Start with the first quarter. */
269 if (($q1_pgs == 0) && ($cur_pg > 1)) {
270 $pg_str .= "...$spc";
272 for ($pg = 1; $pg <= $q1_pgs; ++
$pg) {
273 $start = (($pg-1) * $iLimit) +
1;
274 $pg_str .= get_paginator_link($box, $start, $pg) . $spc;
276 if ($cur_pg - $q2_pgs - $q1_pgs > 1) {
277 $pg_str .= "...$spc";
281 /* Continue with the second quarter. */
282 for ($pg = $cur_pg - $q2_pgs; $pg < $cur_pg; ++
$pg) {
283 $start = (($pg-1) * $iLimit) +
1;
284 $pg_str .= get_paginator_link($box, $start, $pg) . $spc;
287 /* Now print the current page. */
288 $pg_str .= $cur_pg . $spc;
290 /* Next comes the third quarter. */
291 for ($pg = $cur_pg +
1; $pg <= $cur_pg +
$q3_pgs; ++
$pg) {
292 $start = (($pg-1) * $iLimit) +
1;
293 $pg_str .= get_paginator_link($box, $start, $pg) . $spc;
296 /* And last, print the forth quarter page links. */
297 if (($q4_pgs == 0) && ($cur_pg < $tot_pgs)) {
298 $pg_str .= "...$spc";
300 if (($tot_pgs - $q4_pgs) > ($cur_pg +
$q3_pgs)) {
301 $pg_str .= "...$spc";
303 for ($pg = $tot_pgs - $q4_pgs +
1; $pg <= $tot_pgs; ++
$pg) {
304 $start = (($pg-1) * $iLimit) +
1;
305 $pg_str .= get_paginator_link($box, $start,$pg) . $spc;
309 $last_grp = (($tot_pgs - 1) * $iLimit) +
1;
312 $pg_str = "<a href=\"$php_self?showall=0"
313 . "&startMessage=1&mailbox=$box\" "
314 . ">" ._("Paginate") . '</a>';
317 /* Put all the pieces of the paginator string together. */
319 * Hairy code... But let's leave it like it is since I am not certain
320 * a different approach would be any easier to read. ;)
323 if ( $prv_str ||
$nxt_str ) {
325 /* Compute the 'show all' string. */
326 $all_str = "<a href=\"$php_self?showall=1"
327 . "&startMessage=1&mailbox=$box\" "
328 . ">" . _("Show All") . '</a>';
331 $result .= ($prv_str != '' ?
$prv_str . $spc . $sep . $spc : '');
332 $result .= ($nxt_str != '' ?
$nxt_str : '');
333 $result .= ']' . $spc ;
336 $result .= ($pg_str != '' ?
$spc . '['.$spc.$pg_str.']' . $spc : '');
337 $result .= ($all_str != '' ?
$spc . '['.$all_str.']' . $spc . $spc : '');
339 /* If the resulting string is blank, return a non-breaking space. */
343 /* Return our final magical compact paginator string. */