5 * The following functions are utility functions for this template. Do not
6 * echo output in those functions. Output is generated abbove this comment block
10 * Generate a paginator link.
12 * @param string $box Mailbox name
13 * @param integer $start_msg Message Offset
14 * @param string $text text used for paginator link
17 function get_paginator_link($box, $start_msg, $text) {
18 sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER
);
19 $result = "<a href=\"$php_self?startMessage=$start_msg&mailbox=$box\" "
26 * This function computes the comapact paginator string.
28 * @param string $box mailbox name
29 * @param integer $iOffset offset in total number of messages
30 * @param integer $iTotal total number of messages
31 * @param integer $iLimit maximum number of messages to show on a page
32 * @param bool $bShowAll show all messages at once (non paginate mode)
33 * @return string $result paginate string with links to pages
35 function get_compact_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll, $javascript_on, $page_selector) {
37 sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER
);
39 /* Initialize paginator string chunks. */
45 $box = urlencode($box);
47 /* Create simple strings that will be creating the paginator. */
48 $spc = ' '; /* This will be used as a space. */
49 $sep = '|'; /* This will be used as a seperator. */
51 /* Make sure that our start message number is not too big. */
52 $iOffset = min($iOffset, $iTotal);
54 /* Compute the starting message of the previous and next page group. */
55 $next_grp = $iOffset +
$iLimit;
56 $prev_grp = $iOffset - $iLimit;
59 /* Compute the basic previous and next strings. */
60 if (($next_grp <= $iTotal) && ($prev_grp >= 0)) {
61 $prv_str = get_paginator_link($box, $prev_grp, '<');
62 $nxt_str = get_paginator_link($box, $next_grp, '>');
63 } else if (($next_grp > $iTotal) && ($prev_grp >= 0)) {
64 $prv_str = get_paginator_link($box, $prev_grp, '<');
66 } else if (($next_grp <= $iTotal) && ($prev_grp < 0)) {
68 $nxt_str = get_paginator_link($box, $next_grp, '>');
71 /* Page selector block. Following code computes page links. */
72 if ($iLimit != 0 && $page_selector && ($iTotal > $iLimit)) {
73 /* Most importantly, what is the current page!!! */
74 $cur_pg = intval($iOffset / $iLimit) +
1;
76 /* Compute total # of pages and # of paginator page links. */
77 $tot_pgs = ceil($iTotal / $iLimit); /* Total number of Pages */
79 $last_grp = (($tot_pgs - 1) * $iLimit) +
1;
82 $pg_str = "<a href=\"$php_self?showall=0"
83 . "&startMessage=1&mailbox=$box\" "
84 . ">" ._("Paginate") . '</a>';
87 /* Put all the pieces of the paginator string together. */
89 * Hairy code... But let's leave it like it is since I am not certain
90 * a different approach would be any easier to read. ;)
93 if ( $prv_str ||
$nxt_str ) {
95 /* Compute the 'show all' string. */
96 $all_str = "<a href=\"$php_self?showall=1"
97 . "&startMessage=1&mailbox=$box\" "
98 . ">" . _("Show All") . '</a>';
100 $result .= '[' . get_paginator_link($box, 1, '<<') . ']';
101 $result .= '[' . $prv_str . ']';
103 $pg_url = $php_self . '?mailbox=' . $box;
105 $result .= '[' . $nxt_str . ']';
106 $result .= '[' . get_paginator_link($box, $last_grp, '>>') . ']';
108 if ($page_selector) {
109 $result .= $spc . '<select name="startMessage"';
110 if ($javascript_on) {
111 $result .= ' onchange="JavaScript:SubmitOnSelect'
112 . '(this, \'' . $pg_url . '&startMessage=\')"';
116 for ($p = 0; $p < $tot_pgs; $p++
) {
117 $result .= '<option ';
118 if (($p+
1) == $cur_pg) $result .= 'selected ';
119 $result .= 'value="' . (($p*$iLimit)+
1) . '">'
120 . ($p+
1) . "/$tot_pgs" . '</option>';
123 $result .= '</select>';
125 if ($javascript_on) {
126 $result .= '<noscript language="JavaScript">'
130 $result .= addSubmit(_("Go"));
135 $result .= ($pg_str != '' ?
'['.$pg_str.']' . $spc : '');
136 $result .= ($all_str != '' ?
$spc . '['.$all_str.']' . $spc . $spc : '');
138 /* If the resulting string is blank, return a non-breaking space. */
142 /* Return our final magical paginator string. */
147 * This function computes the paginator string.
149 * @param string $box mailbox name
150 * @param integer $iOffset offset in total number of messages
151 * @param integer $iTotal total number of messages
152 * @param integer $iLimit maximum number of messages to show on a page
153 * @param bool $bShowAll show all messages at once (non paginate mode)
154 * @return string $result paginate string with links to pages
156 function get_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll,$page_selector, $page_selector_max) {
158 sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER
);
160 /* Initialize paginator string chunks. */
166 $box = urlencode($box);
168 /* Create simple strings that will be creating the paginator. */
169 $spc = ' '; /* This will be used as a space. */
170 $sep = '|'; /* This will be used as a seperator. */
172 /* Make sure that our start message number is not too big. */
173 $iOffset = min($iOffset, $iTotal);
175 /* Compute the starting message of the previous and next page group. */
176 $next_grp = $iOffset +
$iLimit;
177 $prev_grp = $iOffset - $iLimit;
180 /* Compute the basic previous and next strings. */
182 if (($next_grp <= $iTotal) && ($prev_grp >= 0)) {
183 $prv_str = get_paginator_link($box, $prev_grp, _("Previous"));
184 $nxt_str = get_paginator_link($box, $next_grp, _("Next"));
185 } else if (($next_grp > $iTotal) && ($prev_grp >= 0)) {
186 $prv_str = get_paginator_link($box, $prev_grp, _("Previous"));
187 $nxt_str = _("Next");
188 } else if (($next_grp <= $iTotal) && ($prev_grp < 0)) {
189 $prv_str = _("Previous");
190 $nxt_str = get_paginator_link($box, $next_grp, _("Next"));
193 /* Page selector block. Following code computes page links. */
194 if ($iLimit != 0 && $page_selector && ($iTotal > $iLimit)) {
195 /* Most importantly, what is the current page!!! */
196 $cur_pg = intval($iOffset / $iLimit) +
1;
198 /* Compute total # of pages and # of paginator page links. */
199 $tot_pgs = ceil($iTotal / $iLimit); /* Total number of Pages */
201 $vis_pgs = min($page_selector_max, $tot_pgs - 1); /* Visible Pages */
203 /* Compute the size of the four quarters of the page links. */
205 /* If we can, just show all the pages. */
206 if (($tot_pgs - 1) <= $page_selector_max) {
207 $q1_pgs = $cur_pg - 1;
208 $q2_pgs = $q3_pgs = 0;
209 $q4_pgs = $tot_pgs - $cur_pg;
211 /* Otherwise, compute some magic to choose the four quarters. */
214 * Compute the magic base values. Added together,
215 * these values will always equal to the $pag_pgs.
216 * NOTE: These are DEFAULT values and do not take
217 * the current page into account. That is below.
219 $q1_pgs = floor($vis_pgs/4);
220 $q2_pgs = round($vis_pgs/4, 0);
221 $q3_pgs = ceil($vis_pgs/4);
222 $q4_pgs = round(($vis_pgs - $q2_pgs)/3, 0);
224 /* Adjust if the first quarter contains the current page. */
225 if (($cur_pg - $q1_pgs) < 1) {
226 $extra_pgs = ($q1_pgs - ($cur_pg - 1)) +
$q2_pgs;
227 $q1_pgs = $cur_pg - 1;
229 $q3_pgs +
= ceil($extra_pgs / 2);
230 $q4_pgs +
= floor($extra_pgs / 2);
232 /* Adjust if the first and second quarters intersect. */
233 } else if (($cur_pg - $q2_pgs - ceil($q2_pgs/3)) <= $q1_pgs) {
234 $extra_pgs = $q2_pgs;
235 $extra_pgs -= ceil(($cur_pg - $q1_pgs - 1) * 3/4);
236 $q2_pgs = ceil(($cur_pg - $q1_pgs - 1) * 3/4);
237 $q3_pgs +
= ceil($extra_pgs / 2);
238 $q4_pgs +
= floor($extra_pgs / 2);
240 /* Adjust if the fourth quarter contains the current page. */
241 } else if (($cur_pg +
$q4_pgs) >= $tot_pgs) {
242 $extra_pgs = ($q4_pgs - ($tot_pgs - $cur_pg)) +
$q3_pgs;
244 $q4_pgs = $tot_pgs - $cur_pg;
245 $q1_pgs +
= floor($extra_pgs / 2);
246 $q2_pgs +
= ceil($extra_pgs / 2);
248 /* Adjust if the third and fourth quarter intersect. */
249 } else if (($cur_pg +
$q3_pgs +
1) >= ($tot_pgs - $q4_pgs +
1)) {
250 $extra_pgs = $q3_pgs;
251 $extra_pgs -= ceil(($tot_pgs - $cur_pg - $q4_pgs) * 3/4);
252 $q3_pgs = ceil(($tot_pgs - $cur_pg - $q4_pgs) * 3/4);
253 $q1_pgs +
= floor($extra_pgs / 2);
254 $q2_pgs +
= ceil($extra_pgs / 2);
259 * I am leaving this debug code here, commented out, because
260 * it is a really nice way to see what the above code is doing.
261 * echo "qts = $q1_pgs/$q2_pgs/$q3_pgs/$q4_pgs = "
262 * . ($q1_pgs + $q2_pgs + $q3_pgs + $q4_pgs) . '<br />';
265 /* Print out the page links from the compute page quarters. */
267 /* Start with the first quarter. */
268 if (($q1_pgs == 0) && ($cur_pg > 1)) {
269 $pg_str .= "...$spc";
271 for ($pg = 1; $pg <= $q1_pgs; ++
$pg) {
272 $start = (($pg-1) * $iLimit) +
1;
273 $pg_str .= get_paginator_link($box, $start, $pg) . $spc;
275 if ($cur_pg - $q2_pgs - $q1_pgs > 1) {
276 $pg_str .= "...$spc";
280 /* Continue with the second quarter. */
281 for ($pg = $cur_pg - $q2_pgs; $pg < $cur_pg; ++
$pg) {
282 $start = (($pg-1) * $iLimit) +
1;
283 $pg_str .= get_paginator_link($box, $start, $pg) . $spc;
286 /* Now print the current page. */
287 $pg_str .= $cur_pg . $spc;
289 /* Next comes the third quarter. */
290 for ($pg = $cur_pg +
1; $pg <= $cur_pg +
$q3_pgs; ++
$pg) {
291 $start = (($pg-1) * $iLimit) +
1;
292 $pg_str .= get_paginator_link($box, $start, $pg) . $spc;
295 /* And last, print the forth quarter page links. */
296 if (($q4_pgs == 0) && ($cur_pg < $tot_pgs)) {
297 $pg_str .= "...$spc";
299 if (($tot_pgs - $q4_pgs) > ($cur_pg +
$q3_pgs)) {
300 $pg_str .= "...$spc";
302 for ($pg = $tot_pgs - $q4_pgs +
1; $pg <= $tot_pgs; ++
$pg) {
303 $start = (($pg-1) * $iLimit) +
1;
304 $pg_str .= get_paginator_link($box, $start,$pg) . $spc;
308 $last_grp = (($tot_pgs - 1) * $iLimit) +
1;
311 $pg_str = "<a href=\"$php_self?showall=0"
312 . "&startMessage=1&mailbox=$box\" "
313 . ">" ._("Paginate") . '</a>';
316 /* Put all the pieces of the paginator string together. */
318 * Hairy code... But let's leave it like it is since I am not certain
319 * a different approach would be any easier to read. ;)
322 if ( $prv_str ||
$nxt_str ) {
324 /* Compute the 'show all' string. */
325 $all_str = "<a href=\"$php_self?showall=1"
326 . "&startMessage=1&mailbox=$box\" "
327 . ">" . _("Show All") . '</a>';
330 $result .= ($prv_str != '' ?
$prv_str . $spc . $sep . $spc : '');
331 $result .= ($nxt_str != '' ?
$nxt_str : '');
332 $result .= ']' . $spc ;
335 $result .= ($pg_str != '' ?
$spc . '['.$spc.$pg_str.']' . $spc : '');
336 $result .= ($all_str != '' ?
$spc . '['.$all_str.']' . $spc . $spc : '');
338 /* If the resulting string is blank, return a non-breaking space. */
342 /* Return our final magical compact paginator string. */