6 * The following functions are utility functions for this template. Do not
7 * echo output in those functions. Output is generated above this comment block
9 * @copyright © 2005-2006 The SquirrelMail Project Team
10 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
12 * @package squirrelmail
16 * Generate a paginator link.
18 * @param string $box Mailbox name
19 * @param integer $start_msg Message Offset
20 * @param string $text text used for paginator link
23 function get_paginator_link($box, $start_msg, $text) {
24 sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER
);
25 $result = "<a href=\"$php_self?startMessage=$start_msg&mailbox=$box\" "
32 * This function computes the comapact paginator string.
34 * @param string $box mailbox name
35 * @param integer $iOffset offset in total number of messages
36 * @param integer $iTotal total number of messages
37 * @param integer $iLimit maximum number of messages to show on a page
38 * @param bool $bShowAll show all messages at once (non paginate mode)
39 * @return string $result paginate string with links to pages
41 function get_compact_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll, $javascript_on, $page_selector) {
43 sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER
);
45 /* Initialize paginator string chunks. */
51 $box = urlencode($box);
53 /* Create simple strings that will be creating the paginator. */
54 $spc = ' '; /* This will be used as a space. */
55 $sep = '|'; /* This will be used as a seperator. */
57 /* Make sure that our start message number is not too big. */
58 $iOffset = min($iOffset, $iTotal);
60 /* Compute the starting message of the previous and next page group. */
61 $next_grp = $iOffset +
$iLimit;
62 $prev_grp = $iOffset - $iLimit;
65 /* Compute the basic previous and next strings. */
66 if (($next_grp <= $iTotal) && ($prev_grp >= 0)) {
67 $prv_str = get_paginator_link($box, $prev_grp, '<');
68 $nxt_str = get_paginator_link($box, $next_grp, '>');
69 } else if (($next_grp > $iTotal) && ($prev_grp >= 0)) {
70 $prv_str = get_paginator_link($box, $prev_grp, '<');
72 } else if (($next_grp <= $iTotal) && ($prev_grp < 0)) {
74 $nxt_str = get_paginator_link($box, $next_grp, '>');
77 /* Page selector block. Following code computes page links. */
78 if ($iLimit != 0 && $page_selector && ($iTotal > $iLimit)) {
79 /* Most importantly, what is the current page!!! */
80 $cur_pg = intval($iOffset / $iLimit) +
1;
82 /* Compute total # of pages and # of paginator page links. */
83 $tot_pgs = ceil($iTotal / $iLimit); /* Total number of Pages */
85 $last_grp = (($tot_pgs - 1) * $iLimit) +
1;
88 $pg_str = "<a href=\"$php_self?showall=0"
89 . "&startMessage=1&mailbox=$box\" "
90 . ">" ._("Paginate") . '</a>';
93 /* Put all the pieces of the paginator string together. */
95 * Hairy code... But let's leave it like it is since I am not certain
96 * a different approach would be any easier to read. ;)
99 if ( $prv_str ||
$nxt_str ) {
101 /* Compute the 'show all' string. */
102 $all_str = "<a href=\"$php_self?showall=1"
103 . "&startMessage=1&mailbox=$box\" "
104 . ">" . _("Show All") . '</a>';
106 $result .= '[' . get_paginator_link($box, 1, '<<') . ']';
107 $result .= '[' . $prv_str . ']';
109 $pg_url = $php_self . '?mailbox=' . $box;
111 $result .= '[' . $nxt_str . ']';
112 $result .= '[' . get_paginator_link($box, $last_grp, '>>') . ']';
114 if ($page_selector) {
115 $result .= $spc . '<select name="startMessage"';
116 if ($javascript_on) {
117 $result .= ' onchange="JavaScript:SubmitOnSelect'
118 . '(this, \'' . $pg_url . '&startMessage=\')"';
122 for ($p = 0; $p < $tot_pgs; $p++
) {
123 $result .= '<option ';
124 if (($p+
1) == $cur_pg) $result .= 'selected ';
125 $result .= 'value="' . (($p*$iLimit)+
1) . '">'
126 . ($p+
1) . "/$tot_pgs" . '</option>';
129 $result .= '</select>';
131 if ($javascript_on) {
132 $result .= '<noscript language="JavaScript">'
136 $result .= addSubmit(_("Go"));
141 $result .= ($pg_str != '' ?
'['.$pg_str.']' . $spc : '');
142 $result .= ($all_str != '' ?
$spc . '['.$all_str.']' . $spc . $spc : '');
144 /* If the resulting string is blank, return a non-breaking space. */
148 /* Return our final magical paginator string. */
153 * This function computes the paginator string.
155 * @param string $box mailbox name
156 * @param integer $iOffset offset in total number of messages
157 * @param integer $iTotal total number of messages
158 * @param integer $iLimit maximum number of messages to show on a page
159 * @param bool $bShowAll show all messages at once (non paginate mode)
160 * @return string $result paginate string with links to pages
162 function get_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll,$page_selector, $page_selector_max) {
164 sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER
);
166 /* Initialize paginator string chunks. */
172 $box = urlencode($box);
174 /* Create simple strings that will be creating the paginator. */
175 $spc = ' '; /* This will be used as a space. */
176 $sep = '|'; /* This will be used as a seperator. */
178 /* Make sure that our start message number is not too big. */
179 $iOffset = min($iOffset, $iTotal);
181 /* Compute the starting message of the previous and next page group. */
182 $next_grp = $iOffset +
$iLimit;
183 $prev_grp = $iOffset - $iLimit;
186 /* Compute the basic previous and next strings. */
188 if (($next_grp <= $iTotal) && ($prev_grp >= 0)) {
189 $prv_str = get_paginator_link($box, $prev_grp, _("Previous"));
190 $nxt_str = get_paginator_link($box, $next_grp, _("Next"));
191 } else if (($next_grp > $iTotal) && ($prev_grp >= 0)) {
192 $prv_str = get_paginator_link($box, $prev_grp, _("Previous"));
193 $nxt_str = _("Next");
194 } else if (($next_grp <= $iTotal) && ($prev_grp < 0)) {
195 $prv_str = _("Previous");
196 $nxt_str = get_paginator_link($box, $next_grp, _("Next"));
199 /* Page selector block. Following code computes page links. */
200 if ($iLimit != 0 && $page_selector && ($iTotal > $iLimit)) {
201 /* Most importantly, what is the current page!!! */
202 $cur_pg = intval($iOffset / $iLimit) +
1;
204 /* Compute total # of pages and # of paginator page links. */
205 $tot_pgs = ceil($iTotal / $iLimit); /* Total number of Pages */
207 $vis_pgs = min($page_selector_max, $tot_pgs - 1); /* Visible Pages */
209 /* Compute the size of the four quarters of the page links. */
211 /* If we can, just show all the pages. */
212 if (($tot_pgs - 1) <= $page_selector_max) {
213 $q1_pgs = $cur_pg - 1;
214 $q2_pgs = $q3_pgs = 0;
215 $q4_pgs = $tot_pgs - $cur_pg;
217 /* Otherwise, compute some magic to choose the four quarters. */
220 * Compute the magic base values. Added together,
221 * these values will always equal to the $pag_pgs.
222 * NOTE: These are DEFAULT values and do not take
223 * the current page into account. That is below.
225 $q1_pgs = floor($vis_pgs/4);
226 $q2_pgs = round($vis_pgs/4, 0);
227 $q3_pgs = ceil($vis_pgs/4);
228 $q4_pgs = round(($vis_pgs - $q2_pgs)/3, 0);
230 /* Adjust if the first quarter contains the current page. */
231 if (($cur_pg - $q1_pgs) < 1) {
232 $extra_pgs = ($q1_pgs - ($cur_pg - 1)) +
$q2_pgs;
233 $q1_pgs = $cur_pg - 1;
235 $q3_pgs +
= ceil($extra_pgs / 2);
236 $q4_pgs +
= floor($extra_pgs / 2);
238 /* Adjust if the first and second quarters intersect. */
239 } else if (($cur_pg - $q2_pgs - ceil($q2_pgs/3)) <= $q1_pgs) {
240 $extra_pgs = $q2_pgs;
241 $extra_pgs -= ceil(($cur_pg - $q1_pgs - 1) * 3/4);
242 $q2_pgs = ceil(($cur_pg - $q1_pgs - 1) * 3/4);
243 $q3_pgs +
= ceil($extra_pgs / 2);
244 $q4_pgs +
= floor($extra_pgs / 2);
246 /* Adjust if the fourth quarter contains the current page. */
247 } else if (($cur_pg +
$q4_pgs) >= $tot_pgs) {
248 $extra_pgs = ($q4_pgs - ($tot_pgs - $cur_pg)) +
$q3_pgs;
250 $q4_pgs = $tot_pgs - $cur_pg;
251 $q1_pgs +
= floor($extra_pgs / 2);
252 $q2_pgs +
= ceil($extra_pgs / 2);
254 /* Adjust if the third and fourth quarter intersect. */
255 } else if (($cur_pg +
$q3_pgs +
1) >= ($tot_pgs - $q4_pgs +
1)) {
256 $extra_pgs = $q3_pgs;
257 $extra_pgs -= ceil(($tot_pgs - $cur_pg - $q4_pgs) * 3/4);
258 $q3_pgs = ceil(($tot_pgs - $cur_pg - $q4_pgs) * 3/4);
259 $q1_pgs +
= floor($extra_pgs / 2);
260 $q2_pgs +
= ceil($extra_pgs / 2);
265 * I am leaving this debug code here, commented out, because
266 * it is a really nice way to see what the above code is doing.
267 * echo "qts = $q1_pgs/$q2_pgs/$q3_pgs/$q4_pgs = "
268 * . ($q1_pgs + $q2_pgs + $q3_pgs + $q4_pgs) . '<br />';
271 /* Print out the page links from the compute page quarters. */
273 /* Start with the first quarter. */
274 if (($q1_pgs == 0) && ($cur_pg > 1)) {
275 $pg_str .= "...$spc";
277 for ($pg = 1; $pg <= $q1_pgs; ++
$pg) {
278 $start = (($pg-1) * $iLimit) +
1;
279 $pg_str .= get_paginator_link($box, $start, $pg) . $spc;
281 if ($cur_pg - $q2_pgs - $q1_pgs > 1) {
282 $pg_str .= "...$spc";
286 /* Continue with the second quarter. */
287 for ($pg = $cur_pg - $q2_pgs; $pg < $cur_pg; ++
$pg) {
288 $start = (($pg-1) * $iLimit) +
1;
289 $pg_str .= get_paginator_link($box, $start, $pg) . $spc;
292 /* Now print the current page. */
293 $pg_str .= $cur_pg . $spc;
295 /* Next comes the third quarter. */
296 for ($pg = $cur_pg +
1; $pg <= $cur_pg +
$q3_pgs; ++
$pg) {
297 $start = (($pg-1) * $iLimit) +
1;
298 $pg_str .= get_paginator_link($box, $start, $pg) . $spc;
301 /* And last, print the forth quarter page links. */
302 if (($q4_pgs == 0) && ($cur_pg < $tot_pgs)) {
303 $pg_str .= "...$spc";
305 if (($tot_pgs - $q4_pgs) > ($cur_pg +
$q3_pgs)) {
306 $pg_str .= "...$spc";
308 for ($pg = $tot_pgs - $q4_pgs +
1; $pg <= $tot_pgs; ++
$pg) {
309 $start = (($pg-1) * $iLimit) +
1;
310 $pg_str .= get_paginator_link($box, $start,$pg) . $spc;
314 $last_grp = (($tot_pgs - 1) * $iLimit) +
1;
317 $pg_str = "<a href=\"$php_self?showall=0"
318 . "&startMessage=1&mailbox=$box\" "
319 . ">" ._("Paginate") . '</a>';
322 /* Put all the pieces of the paginator string together. */
324 * Hairy code... But let's leave it like it is since I am not certain
325 * a different approach would be any easier to read. ;)
328 if ( $prv_str ||
$nxt_str ) {
330 /* Compute the 'show all' string. */
331 $all_str = "<a href=\"$php_self?showall=1"
332 . "&startMessage=1&mailbox=$box\" "
333 . ">" . _("Show All") . '</a>';
336 $result .= ($prv_str != '' ?
$prv_str . $spc . $sep . $spc : '');
337 $result .= ($nxt_str != '' ?
$nxt_str : '');
338 $result .= ']' . $spc ;
341 $result .= ($pg_str != '' ?
$spc . '['.$spc.$pg_str.']' . $spc : '');
342 $result .= ($all_str != '' ?
$spc . '['.$all_str.']' . $spc . $spc : '');
344 /* If the resulting string is blank, return a non-breaking space. */
348 /* Return our final magical compact paginator string. */