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) {
52 sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER
);
54 /* Initialize paginator string chunks. */
60 $box = urlencode($box);
62 /* Create simple strings that will be creating the paginator. */
63 /* This will be used as a space. */
64 $spc = $oTemplate->fetch('non_breaking_space.tpl');
65 /* This will be used as a seperator. */
68 /* Make sure that our start message number is not too big. */
69 $iOffset = min($iOffset, $iTotal);
71 /* Compute the starting message of the previous and next page group. */
72 $next_grp = $iOffset +
$iLimit;
73 $prev_grp = $iOffset - $iLimit;
76 /* Compute the basic previous and next strings. */
77 if (($next_grp <= $iTotal) && ($prev_grp >= 0)) {
78 $prv_str = get_paginator_link($box, $prev_grp, '<');
79 $nxt_str = get_paginator_link($box, $next_grp, '>');
80 } else if (($next_grp > $iTotal) && ($prev_grp >= 0)) {
81 $prv_str = get_paginator_link($box, $prev_grp, '<');
83 } else if (($next_grp <= $iTotal) && ($prev_grp < 0)) {
85 $nxt_str = get_paginator_link($box, $next_grp, '>');
88 /* Page selector block. Following code computes page links. */
89 if ($iLimit != 0 && $page_selector && ($iTotal > $iLimit)) {
90 /* Most importantly, what is the current page!!! */
91 $cur_pg = intval($iOffset / $iLimit) +
1;
93 /* Compute total # of pages and # of paginator page links. */
94 $tot_pgs = ceil($iTotal / $iLimit); /* Total number of Pages */
96 $last_grp = (($tot_pgs - 1) * $iLimit) +
1;
99 $pg_str = create_hyperlink("$php_self?showall=0&startMessage=1&mailbox=$box", _("Paginate"));
102 /* Put all the pieces of the paginator string together. */
104 * Hairy code... But let's leave it like it is since I am not certain
105 * a different approach would be any easier to read. ;)
108 if ( $prv_str ||
$nxt_str ) {
110 /* Compute the 'show all' string. */
111 $all_str = create_hyperlink("$php_self?showall=1&startMessage=1&mailbox=$box", _("Show All"));
113 $result .= '[' . get_paginator_link($box, 1, '<<') . ']';
114 $result .= '[' . $prv_str . ']';
116 $pg_url = $php_self . '?mailbox=' . $box;
118 $result .= '[' . $nxt_str . ']';
119 $result .= '[' . get_paginator_link($box, $last_grp, '>>') . ']';
121 if ($page_selector) {
123 for ($p = 0; $p < $tot_pgs; $p++
) {
124 $options[(($p*$iLimit)+
1)] = ($p+
1) . "/$tot_pgs";
126 $result .= $spc . addSelect('startMessage', $options,
127 ((($cur_pg-1)*$iLimit)+
1),
129 ($javascript_on ?
array('onchange' => 'JavaScript:SubmitOnSelect(this, \'' . $pg_url . '&startMessage=\')') : array()));
131 if ($javascript_on) {
132 //FIXME: What in the world? Two issues here: for one, $javascript_on is supposed
133 // to have already detected whether or not JavaScript is available and enabled.
134 // Secondly, we need to rid ourselves of any HTML output in the core. This
135 // is being removed (but left in case the original author points out why it
136 // should not be) and we'll trust $javascript_on to do the right thing.
137 // $result .= '<noscript language="JavaScript">'
138 // . addSubmit(_("Go"))
141 $result .= addSubmit(_("Go"));
146 $result .= ($pg_str != '' ?
'['.$pg_str.']' . $spc : '');
147 $result .= ($all_str != '' ?
$spc . '['.$all_str.']' . $spc . $spc : '');
149 /* If the resulting string is blank, return a non-breaking space. */
153 /* Return our final magical paginator string. */
159 * This function computes the paginator string.
161 * @param string $box mailbox name
162 * @param integer $iOffset offset in total number of messages
163 * @param integer $iTotal total number of messages
164 * @param integer $iLimit maximum number of messages to show on a page
165 * @param bool $bShowAll whether or not to show all messages at once
166 * ("show all" == non paginate mode)
167 * @param bool $page_selector whether or not to show the page selection widget
168 * @param integer $page_selector_max maximum number of pages to show on the screen
170 * @return string $result paginate string with links to pages
173 function get_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll,$page_selector, $page_selector_max) {
176 sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER
);
178 /* Initialize paginator string chunks. */
184 $box = urlencode($box);
186 /* Create simple strings that will be creating the paginator. */
187 /* This will be used as a space. */
188 $spc = $oTemplate->fetch('non_breaking_space.tpl');
189 /* This will be used as a seperator. */
192 /* Make sure that our start message number is not too big. */
193 $iOffset = min($iOffset, $iTotal);
195 /* Compute the starting message of the previous and next page group. */
196 $next_grp = $iOffset +
$iLimit;
197 $prev_grp = $iOffset - $iLimit;
200 /* Compute the basic previous and next strings. */
202 if (($next_grp <= $iTotal) && ($prev_grp >= 0)) {
203 $prv_str = get_paginator_link($box, $prev_grp, _("Previous"));
204 $nxt_str = get_paginator_link($box, $next_grp, _("Next"));
205 } else if (($next_grp > $iTotal) && ($prev_grp >= 0)) {
206 $prv_str = get_paginator_link($box, $prev_grp, _("Previous"));
207 $nxt_str = _("Next");
208 } else if (($next_grp <= $iTotal) && ($prev_grp < 0)) {
209 $prv_str = _("Previous");
210 $nxt_str = get_paginator_link($box, $next_grp, _("Next"));
213 /* Page selector block. Following code computes page links. */
214 if ($iLimit != 0 && $page_selector && ($iTotal > $iLimit)) {
215 /* Most importantly, what is the current page!!! */
216 $cur_pg = intval($iOffset / $iLimit) +
1;
218 /* Compute total # of pages and # of paginator page links. */
219 $tot_pgs = ceil($iTotal / $iLimit); /* Total number of Pages */
221 $vis_pgs = min($page_selector_max, $tot_pgs - 1); /* Visible Pages */
223 /* Compute the size of the four quarters of the page links. */
225 /* If we can, just show all the pages. */
226 if (($tot_pgs - 1) <= $page_selector_max) {
227 $q1_pgs = $cur_pg - 1;
228 $q2_pgs = $q3_pgs = 0;
229 $q4_pgs = $tot_pgs - $cur_pg;
231 /* Otherwise, compute some magic to choose the four quarters. */
234 * Compute the magic base values. Added together,
235 * these values will always equal to the $pag_pgs.
236 * NOTE: These are DEFAULT values and do not take
237 * the current page into account. That is below.
239 $q1_pgs = floor($vis_pgs/4);
240 $q2_pgs = round($vis_pgs/4, 0);
241 $q3_pgs = ceil($vis_pgs/4);
242 $q4_pgs = round(($vis_pgs - $q2_pgs)/3, 0);
244 /* Adjust if the first quarter contains the current page. */
245 if (($cur_pg - $q1_pgs) < 1) {
246 $extra_pgs = ($q1_pgs - ($cur_pg - 1)) +
$q2_pgs;
247 $q1_pgs = $cur_pg - 1;
249 $q3_pgs +
= ceil($extra_pgs / 2);
250 $q4_pgs +
= floor($extra_pgs / 2);
252 /* Adjust if the first and second quarters intersect. */
253 } else if (($cur_pg - $q2_pgs - ceil($q2_pgs/3)) <= $q1_pgs) {
254 $extra_pgs = $q2_pgs;
255 $extra_pgs -= ceil(($cur_pg - $q1_pgs - 1) * 3/4);
256 $q2_pgs = ceil(($cur_pg - $q1_pgs - 1) * 3/4);
257 $q3_pgs +
= ceil($extra_pgs / 2);
258 $q4_pgs +
= floor($extra_pgs / 2);
260 /* Adjust if the fourth quarter contains the current page. */
261 } else if (($cur_pg +
$q4_pgs) >= $tot_pgs) {
262 $extra_pgs = ($q4_pgs - ($tot_pgs - $cur_pg)) +
$q3_pgs;
264 $q4_pgs = $tot_pgs - $cur_pg;
265 $q1_pgs +
= floor($extra_pgs / 2);
266 $q2_pgs +
= ceil($extra_pgs / 2);
268 /* Adjust if the third and fourth quarter intersect. */
269 } else if (($cur_pg +
$q3_pgs +
1) >= ($tot_pgs - $q4_pgs +
1)) {
270 $extra_pgs = $q3_pgs;
271 $extra_pgs -= ceil(($tot_pgs - $cur_pg - $q4_pgs) * 3/4);
272 $q3_pgs = ceil(($tot_pgs - $cur_pg - $q4_pgs) * 3/4);
273 $q1_pgs +
= floor($extra_pgs / 2);
274 $q2_pgs +
= ceil($extra_pgs / 2);
279 * I am leaving this debug code here, commented out, because
280 * it is a really nice way to see what the above code is doing.
281 * echo "qts = $q1_pgs/$q2_pgs/$q3_pgs/$q4_pgs = "
282 * . ($q1_pgs + $q2_pgs + $q3_pgs + $q4_pgs) . '<br />';
285 /* Print out the page links from the compute page quarters. */
287 /* Start with the first quarter. */
288 if (($q1_pgs == 0) && ($cur_pg > 1)) {
289 $pg_str .= "...$spc";
291 for ($pg = 1; $pg <= $q1_pgs; ++
$pg) {
292 $start = (($pg-1) * $iLimit) +
1;
293 $pg_str .= get_paginator_link($box, $start, $pg) . $spc;
295 if ($cur_pg - $q2_pgs - $q1_pgs > 1) {
296 $pg_str .= "...$spc";
300 /* Continue with the second quarter. */
301 for ($pg = $cur_pg - $q2_pgs; $pg < $cur_pg; ++
$pg) {
302 $start = (($pg-1) * $iLimit) +
1;
303 $pg_str .= get_paginator_link($box, $start, $pg) . $spc;
306 /* Now print the current page. */
307 $pg_str .= $cur_pg . $spc;
309 /* Next comes the third quarter. */
310 for ($pg = $cur_pg +
1; $pg <= $cur_pg +
$q3_pgs; ++
$pg) {
311 $start = (($pg-1) * $iLimit) +
1;
312 $pg_str .= get_paginator_link($box, $start, $pg) . $spc;
315 /* And last, print the forth quarter page links. */
316 if (($q4_pgs == 0) && ($cur_pg < $tot_pgs)) {
317 $pg_str .= "...$spc";
319 if (($tot_pgs - $q4_pgs) > ($cur_pg +
$q3_pgs)) {
320 $pg_str .= "...$spc";
322 for ($pg = $tot_pgs - $q4_pgs +
1; $pg <= $tot_pgs; ++
$pg) {
323 $start = (($pg-1) * $iLimit) +
1;
324 $pg_str .= get_paginator_link($box, $start,$pg) . $spc;
328 $last_grp = (($tot_pgs - 1) * $iLimit) +
1;
331 $pg_str = create_hyperlink("$php_self?showall=0&startMessage=1&mailbox=$box", _("Paginate"));
334 /* Put all the pieces of the paginator string together. */
336 * Hairy code... But let's leave it like it is since I am not certain
337 * a different approach would be any easier to read. ;)
340 if ( $prv_str ||
$nxt_str ) {
342 /* Compute the 'show all' string. */
343 $all_str = create_hyperlink("$php_self?showall=1&startMessage=1&mailbox=$box", _("Show All"));
346 $result .= ($prv_str != '' ?
$prv_str . $spc . $sep . $spc : '');
347 $result .= ($nxt_str != '' ?
$nxt_str : '');
348 $result .= ']' . $spc ;
351 $result .= ($pg_str != '' ?
$spc . '['.$spc.$pg_str.']' . $spc : '');
352 $result .= ($all_str != '' ?
$spc . '['.$all_str.']' . $spc . $spc : '');
354 /* If the resulting string is blank, return a non-breaking space. */
356 $result = $oTemplate->fetch('non_breaking_space.tpl');
358 /* Return our final magical compact paginator string. */