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
15 /** Load forms functions, needed for addsubmit(). */
16 include_once(SM_PATH
. 'functions/forms.php');
19 * Generate a paginator link.
21 * @param string $box Mailbox name
22 * @param integer $start_msg Message Offset
23 * @param string $text text used for paginator link
26 function get_paginator_link($box, $start_msg, $text) {
27 sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER
);
28 $result = "<a href=\"$php_self?startMessage=$start_msg&mailbox=$box\" "
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 show all messages at once (non paginate mode)
42 * @return string $result paginate string with links to pages
44 function get_compact_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll, $javascript_on, $page_selector) {
46 sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER
);
48 /* Initialize paginator string chunks. */
54 $box = urlencode($box);
56 /* Create simple strings that will be creating the paginator. */
57 $spc = ' '; /* This will be used as a space. */
58 $sep = '|'; /* This will be used as a seperator. */
60 /* Make sure that our start message number is not too big. */
61 $iOffset = min($iOffset, $iTotal);
63 /* Compute the starting message of the previous and next page group. */
64 $next_grp = $iOffset +
$iLimit;
65 $prev_grp = $iOffset - $iLimit;
68 /* Compute the basic previous and next strings. */
69 if (($next_grp <= $iTotal) && ($prev_grp >= 0)) {
70 $prv_str = get_paginator_link($box, $prev_grp, '<');
71 $nxt_str = get_paginator_link($box, $next_grp, '>');
72 } else if (($next_grp > $iTotal) && ($prev_grp >= 0)) {
73 $prv_str = get_paginator_link($box, $prev_grp, '<');
75 } else if (($next_grp <= $iTotal) && ($prev_grp < 0)) {
77 $nxt_str = get_paginator_link($box, $next_grp, '>');
80 /* Page selector block. Following code computes page links. */
81 if ($iLimit != 0 && $page_selector && ($iTotal > $iLimit)) {
82 /* Most importantly, what is the current page!!! */
83 $cur_pg = intval($iOffset / $iLimit) +
1;
85 /* Compute total # of pages and # of paginator page links. */
86 $tot_pgs = ceil($iTotal / $iLimit); /* Total number of Pages */
88 $last_grp = (($tot_pgs - 1) * $iLimit) +
1;
91 $pg_str = "<a href=\"$php_self?showall=0"
92 . "&startMessage=1&mailbox=$box\" "
93 . ">" ._("Paginate") . '</a>';
96 /* Put all the pieces of the paginator string together. */
98 * Hairy code... But let's leave it like it is since I am not certain
99 * a different approach would be any easier to read. ;)
102 if ( $prv_str ||
$nxt_str ) {
104 /* Compute the 'show all' string. */
105 $all_str = "<a href=\"$php_self?showall=1"
106 . "&startMessage=1&mailbox=$box\" "
107 . ">" . _("Show All") . '</a>';
109 $result .= '[' . get_paginator_link($box, 1, '<<') . ']';
110 $result .= '[' . $prv_str . ']';
112 $pg_url = $php_self . '?mailbox=' . $box;
114 $result .= '[' . $nxt_str . ']';
115 $result .= '[' . get_paginator_link($box, $last_grp, '>>') . ']';
117 if ($page_selector) {
118 $result .= $spc . '<select name="startMessage"';
119 if ($javascript_on) {
120 $result .= ' onchange="JavaScript:SubmitOnSelect'
121 . '(this, \'' . $pg_url . '&startMessage=\')"';
125 for ($p = 0; $p < $tot_pgs; $p++
) {
126 $result .= '<option ';
127 if (($p+
1) == $cur_pg) $result .= 'selected ';
128 $result .= 'value="' . (($p*$iLimit)+
1) . '">'
129 . ($p+
1) . "/$tot_pgs" . '</option>';
132 $result .= '</select>';
134 if ($javascript_on) {
135 $result .= '<noscript language="JavaScript">'
139 $result .= addSubmit(_("Go"));
144 $result .= ($pg_str != '' ?
'['.$pg_str.']' . $spc : '');
145 $result .= ($all_str != '' ?
$spc . '['.$all_str.']' . $spc . $spc : '');
147 /* If the resulting string is blank, return a non-breaking space. */
151 /* Return our final magical paginator string. */
156 * This function computes the paginator string.
158 * @param string $box mailbox name
159 * @param integer $iOffset offset in total number of messages
160 * @param integer $iTotal total number of messages
161 * @param integer $iLimit maximum number of messages to show on a page
162 * @param bool $bShowAll show all messages at once (non paginate mode)
163 * @return string $result paginate string with links to pages
165 function get_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll,$page_selector, $page_selector_max) {
167 sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER
);
169 /* Initialize paginator string chunks. */
175 $box = urlencode($box);
177 /* Create simple strings that will be creating the paginator. */
178 $spc = ' '; /* This will be used as a space. */
179 $sep = '|'; /* This will be used as a seperator. */
181 /* Make sure that our start message number is not too big. */
182 $iOffset = min($iOffset, $iTotal);
184 /* Compute the starting message of the previous and next page group. */
185 $next_grp = $iOffset +
$iLimit;
186 $prev_grp = $iOffset - $iLimit;
189 /* Compute the basic previous and next strings. */
191 if (($next_grp <= $iTotal) && ($prev_grp >= 0)) {
192 $prv_str = get_paginator_link($box, $prev_grp, _("Previous"));
193 $nxt_str = get_paginator_link($box, $next_grp, _("Next"));
194 } else if (($next_grp > $iTotal) && ($prev_grp >= 0)) {
195 $prv_str = get_paginator_link($box, $prev_grp, _("Previous"));
196 $nxt_str = _("Next");
197 } else if (($next_grp <= $iTotal) && ($prev_grp < 0)) {
198 $prv_str = _("Previous");
199 $nxt_str = get_paginator_link($box, $next_grp, _("Next"));
202 /* Page selector block. Following code computes page links. */
203 if ($iLimit != 0 && $page_selector && ($iTotal > $iLimit)) {
204 /* Most importantly, what is the current page!!! */
205 $cur_pg = intval($iOffset / $iLimit) +
1;
207 /* Compute total # of pages and # of paginator page links. */
208 $tot_pgs = ceil($iTotal / $iLimit); /* Total number of Pages */
210 $vis_pgs = min($page_selector_max, $tot_pgs - 1); /* Visible Pages */
212 /* Compute the size of the four quarters of the page links. */
214 /* If we can, just show all the pages. */
215 if (($tot_pgs - 1) <= $page_selector_max) {
216 $q1_pgs = $cur_pg - 1;
217 $q2_pgs = $q3_pgs = 0;
218 $q4_pgs = $tot_pgs - $cur_pg;
220 /* Otherwise, compute some magic to choose the four quarters. */
223 * Compute the magic base values. Added together,
224 * these values will always equal to the $pag_pgs.
225 * NOTE: These are DEFAULT values and do not take
226 * the current page into account. That is below.
228 $q1_pgs = floor($vis_pgs/4);
229 $q2_pgs = round($vis_pgs/4, 0);
230 $q3_pgs = ceil($vis_pgs/4);
231 $q4_pgs = round(($vis_pgs - $q2_pgs)/3, 0);
233 /* Adjust if the first quarter contains the current page. */
234 if (($cur_pg - $q1_pgs) < 1) {
235 $extra_pgs = ($q1_pgs - ($cur_pg - 1)) +
$q2_pgs;
236 $q1_pgs = $cur_pg - 1;
238 $q3_pgs +
= ceil($extra_pgs / 2);
239 $q4_pgs +
= floor($extra_pgs / 2);
241 /* Adjust if the first and second quarters intersect. */
242 } else if (($cur_pg - $q2_pgs - ceil($q2_pgs/3)) <= $q1_pgs) {
243 $extra_pgs = $q2_pgs;
244 $extra_pgs -= ceil(($cur_pg - $q1_pgs - 1) * 3/4);
245 $q2_pgs = ceil(($cur_pg - $q1_pgs - 1) * 3/4);
246 $q3_pgs +
= ceil($extra_pgs / 2);
247 $q4_pgs +
= floor($extra_pgs / 2);
249 /* Adjust if the fourth quarter contains the current page. */
250 } else if (($cur_pg +
$q4_pgs) >= $tot_pgs) {
251 $extra_pgs = ($q4_pgs - ($tot_pgs - $cur_pg)) +
$q3_pgs;
253 $q4_pgs = $tot_pgs - $cur_pg;
254 $q1_pgs +
= floor($extra_pgs / 2);
255 $q2_pgs +
= ceil($extra_pgs / 2);
257 /* Adjust if the third and fourth quarter intersect. */
258 } else if (($cur_pg +
$q3_pgs +
1) >= ($tot_pgs - $q4_pgs +
1)) {
259 $extra_pgs = $q3_pgs;
260 $extra_pgs -= ceil(($tot_pgs - $cur_pg - $q4_pgs) * 3/4);
261 $q3_pgs = ceil(($tot_pgs - $cur_pg - $q4_pgs) * 3/4);
262 $q1_pgs +
= floor($extra_pgs / 2);
263 $q2_pgs +
= ceil($extra_pgs / 2);
268 * I am leaving this debug code here, commented out, because
269 * it is a really nice way to see what the above code is doing.
270 * echo "qts = $q1_pgs/$q2_pgs/$q3_pgs/$q4_pgs = "
271 * . ($q1_pgs + $q2_pgs + $q3_pgs + $q4_pgs) . '<br />';
274 /* Print out the page links from the compute page quarters. */
276 /* Start with the first quarter. */
277 if (($q1_pgs == 0) && ($cur_pg > 1)) {
278 $pg_str .= "...$spc";
280 for ($pg = 1; $pg <= $q1_pgs; ++
$pg) {
281 $start = (($pg-1) * $iLimit) +
1;
282 $pg_str .= get_paginator_link($box, $start, $pg) . $spc;
284 if ($cur_pg - $q2_pgs - $q1_pgs > 1) {
285 $pg_str .= "...$spc";
289 /* Continue with the second quarter. */
290 for ($pg = $cur_pg - $q2_pgs; $pg < $cur_pg; ++
$pg) {
291 $start = (($pg-1) * $iLimit) +
1;
292 $pg_str .= get_paginator_link($box, $start, $pg) . $spc;
295 /* Now print the current page. */
296 $pg_str .= $cur_pg . $spc;
298 /* Next comes the third quarter. */
299 for ($pg = $cur_pg +
1; $pg <= $cur_pg +
$q3_pgs; ++
$pg) {
300 $start = (($pg-1) * $iLimit) +
1;
301 $pg_str .= get_paginator_link($box, $start, $pg) . $spc;
304 /* And last, print the forth quarter page links. */
305 if (($q4_pgs == 0) && ($cur_pg < $tot_pgs)) {
306 $pg_str .= "...$spc";
308 if (($tot_pgs - $q4_pgs) > ($cur_pg +
$q3_pgs)) {
309 $pg_str .= "...$spc";
311 for ($pg = $tot_pgs - $q4_pgs +
1; $pg <= $tot_pgs; ++
$pg) {
312 $start = (($pg-1) * $iLimit) +
1;
313 $pg_str .= get_paginator_link($box, $start,$pg) . $spc;
317 $last_grp = (($tot_pgs - 1) * $iLimit) +
1;
320 $pg_str = "<a href=\"$php_self?showall=0"
321 . "&startMessage=1&mailbox=$box\" "
322 . ">" ._("Paginate") . '</a>';
325 /* Put all the pieces of the paginator string together. */
327 * Hairy code... But let's leave it like it is since I am not certain
328 * a different approach would be any easier to read. ;)
331 if ( $prv_str ||
$nxt_str ) {
333 /* Compute the 'show all' string. */
334 $all_str = "<a href=\"$php_self?showall=1"
335 . "&startMessage=1&mailbox=$box\" "
336 . ">" . _("Show All") . '</a>';
339 $result .= ($prv_str != '' ?
$prv_str . $spc . $sep . $spc : '');
340 $result .= ($nxt_str != '' ?
$nxt_str : '');
341 $result .= ']' . $spc ;
344 $result .= ($pg_str != '' ?
$spc . '['.$spc.$pg_str.']' . $spc : '');
345 $result .= ($all_str != '' ?
$spc . '['.$all_str.']' . $spc . $spc : '');
347 /* If the resulting string is blank, return a non-breaking space. */
351 /* Return our final magical compact paginator string. */