976723c3ae0238bd093529479f109a18133e67fb
6 * The following functions are utility functions for templates. Do not
7 * echo output in these functions.
9 * @copyright 2005-2019 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 The text used for paginator link
26 * @param string $accesskey The access key for the link, if any
29 function get_paginator_link($box, $start_msg, $text, $accesskey='NONE') {
30 sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER
);
31 return create_hyperlink("$php_self?startMessage=$start_msg&mailbox=$box"
32 . (strpos($php_self, 'src/search.php') ?
'&smtoken=' . sm_generate_security_token() : ''),
33 $text, '', '', '', '', '',
36 : array('accesskey' => $accesskey)));
41 * This function computes the comapact paginator string.
43 * @param string $box mailbox name
44 * @param integer $iOffset offset in total number of messages
45 * @param integer $iTotal total number of messages
46 * @param integer $iLimit maximum number of messages to show on a page
47 * @param bool $bShowAll whether or not to show all messages at once
48 * ("show all" == non paginate mode)
49 * @param bool $javascript_on whether or not javascript is currently enabled
50 * @param bool $page_selector whether or not to show the page selection widget
52 * @return string $result paginate string with links to pages
55 function get_compact_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll, $javascript_on, $page_selector) {
57 static $accesskeys_constructed = FALSE;
59 /* This will be used as a space. */
60 global $oTemplate, $nbsp;
62 // keeps count of how many times
63 // the paginator is used, avoids
64 // duplicate naming of <select>
66 static $display_iterations = 0;
67 $display_iterations++
;
69 sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER
);
71 /* Initialize paginator string chunks. */
77 $box = urlencode($box);
79 /* Create simple strings that will be creating the paginator. */
80 /* This will be used as a seperator. */
83 /* Make sure that our start message number is not too big. */
84 $iOffset = min($iOffset, $iTotal);
86 /* Compute the starting message of the previous and next page group. */
87 $next_grp = $iOffset +
$iLimit;
88 $prev_grp = $iOffset - $iLimit;
92 /* Compute the basic previous and next strings. */
94 global $accesskey_mailbox_previous, $accesskey_mailbox_next;
95 if (($next_grp <= $iTotal) && ($prev_grp >= 0)) {
96 $prv_str = get_paginator_link($box, $prev_grp, '<',
97 ($accesskeys_constructed
98 ?
'NONE' : $accesskey_mailbox_previous));
99 $nxt_str = get_paginator_link($box, $next_grp, '>',
100 ($accesskeys_constructed
101 ?
'NONE' : $accesskey_mailbox_next));
102 } else if (($next_grp > $iTotal) && ($prev_grp >= 0)) {
103 $prv_str = get_paginator_link($box, $prev_grp, '<',
104 ($accesskeys_constructed
105 ?
'NONE' : $accesskey_mailbox_previous));
107 } else if (($next_grp <= $iTotal) && ($prev_grp < 0)) {
109 $nxt_str = get_paginator_link($box, $next_grp, '>',
110 ($accesskeys_constructed
111 ?
'NONE' : $accesskey_mailbox_next));
114 /* Page selector block. Following code computes page links. */
115 if ($iLimit != 0 && $page_selector && ($iTotal > $iLimit)) {
116 /* Most importantly, what is the current page!!! */
117 $cur_pg = intval($iOffset / $iLimit) +
1;
119 /* Compute total # of pages and # of paginator page links. */
120 $tot_pgs = ceil($iTotal / $iLimit); /* Total number of Pages */
122 $last_grp = (($tot_pgs - 1) * $iLimit) +
1;
125 global $accesskey_mailbox_all_paginate;
126 $pg_str = create_hyperlink("$php_self?showall=0&startMessage=1&mailbox=$box" . (strpos($php_self, 'src/search.php') ?
'&smtoken=' . sm_generate_security_token() : ''), _("Paginate"), '', '', '', '', '', ($accesskeys_constructed ?
array() : array('accesskey' => $accesskey_mailbox_all_paginate)));
129 /* Put all the pieces of the paginator string together. */
131 * Hairy code... But let's leave it like it is since I am not certain
132 * a different approach would be any easier to read. ;)
135 if ( $prv_str ||
$nxt_str ) {
137 /* Compute the 'show all' string. */
138 global $accesskey_mailbox_all_paginate;
139 $all_str = create_hyperlink("$php_self?showall=1&startMessage=1&mailbox=$box" . (strpos($php_self, 'src/search.php') ?
'&smtoken=' . sm_generate_security_token() : ''), _("Show All"), '', '', '', '', '', ($accesskeys_constructed ?
array() : array('accesskey' => $accesskey_mailbox_all_paginate)));
141 $result .= '[' . get_paginator_link($box, 1, '<<') . ']';
142 $result .= '[' . $prv_str . ']';
144 $pg_url = $php_self . '?mailbox=' . $box . (strpos($php_self, 'src/search.php') ?
'&smtoken=' . sm_generate_security_token() : '');
146 $result .= '[' . $nxt_str . ']';
147 $result .= '[' . get_paginator_link($box, $last_grp, '>>') . ']';
149 if ($page_selector) {
151 for ($p = 0; $p < $tot_pgs; $p++
) {
152 $options[(($p*$iLimit)+
1) . '_' . $box] = ($p+
1) . "/$tot_pgs";
154 $result .= $nbsp . addSelect('startMessage_' . $display_iterations,
156 ((($cur_pg-1)*$iLimit)+
1),
158 ($javascript_on ?
array('onchange' => 'JavaScript:SubmitOnSelect(this, \'' . $pg_url . '&startMessage=\')') : array()));
160 if ($javascript_on) {
161 //FIXME: What in the world? Two issues here: for one, $javascript_on is supposed
162 // to have already detected whether or not JavaScript is available and enabled.
163 // Secondly, we need to rid ourselves of any HTML output in the core. This
164 // is being removed (but left in case the original author points out why it
165 // should not be) and we'll trust $javascript_on to do the right thing.
166 // $result .= '<noscript language="JavaScript">'
167 // . addSubmit(_("Go"), 'paginator_submit_' . $display_iterations)
170 $result .= addSubmit(_("Go"), 'paginator_submit_' . $display_iterations);
175 $result .= ($pg_str != '' ?
'['.$pg_str.']' . $nbsp : '');
176 $result .= ($all_str != '' ?
$nbsp . '['.$all_str.']' . $nbsp . $nbsp : '');
178 /* If the resulting string is blank, return a non-breaking space. */
183 $accesskeys_constructed = TRUE;
185 /* Return our final magical paginator string. */
191 * This function computes the paginator string.
193 * @param string $box mailbox name
194 * @param integer $iOffset offset in total number of messages
195 * @param integer $iTotal total number of messages
196 * @param integer $iLimit maximum number of messages to show on a page
197 * @param bool $bShowAll whether or not to show all messages at once
198 * ("show all" == non paginate mode)
199 * @param bool $page_selector whether or not to show the page selection widget
200 * @param integer $page_selector_max maximum number of pages to show on the screen
202 * @return string $result paginate string with links to pages
205 function get_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll,$page_selector, $page_selector_max) {
207 static $accesskeys_constructed = FALSE;
209 /* This will be used as a space. */
210 global $oTemplate, $nbsp;
211 sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER
);
213 /* Initialize paginator string chunks. */
219 $box = urlencode($box);
221 /* Create simple strings that will be creating the paginator. */
222 /* This will be used as a seperator. */
225 /* Make sure that our start message number is not too big. */
226 $iOffset = min($iOffset, $iTotal);
228 /* Compute the starting message of the previous and next page group. */
229 $next_grp = $iOffset +
$iLimit;
230 $prev_grp = $iOffset - $iLimit;
234 /* Compute the basic previous and next strings. */
236 global $accesskey_mailbox_previous, $accesskey_mailbox_next;
237 if (($next_grp <= $iTotal) && ($prev_grp >= 0)) {
238 $prv_str = get_paginator_link($box, $prev_grp, _("Previous"),
239 ($accesskeys_constructed
240 ?
'NONE' : $accesskey_mailbox_previous));
241 $nxt_str = get_paginator_link($box, $next_grp, _("Next"),
242 ($accesskeys_constructed
243 ?
'NONE' : $accesskey_mailbox_next));
244 } else if (($next_grp > $iTotal) && ($prev_grp >= 0)) {
245 $prv_str = get_paginator_link($box, $prev_grp, _("Previous"),
246 ($accesskeys_constructed
247 ?
'NONE' : $accesskey_mailbox_previous));
248 $nxt_str = _("Next");
249 } else if (($next_grp <= $iTotal) && ($prev_grp < 0)) {
250 $prv_str = _("Previous");
251 $nxt_str = get_paginator_link($box, $next_grp, _("Next"),
252 ($accesskeys_constructed
253 ?
'NONE' : $accesskey_mailbox_next));
256 /* Page selector block. Following code computes page links. */
257 if ($iLimit != 0 && $page_selector && ($iTotal > $iLimit)) {
258 /* Most importantly, what is the current page!!! */
259 $cur_pg = intval($iOffset / $iLimit) +
1;
261 /* Compute total # of pages and # of paginator page links. */
262 $tot_pgs = ceil($iTotal / $iLimit); /* Total number of Pages */
264 $vis_pgs = min($page_selector_max, $tot_pgs - 1); /* Visible Pages */
266 /* Compute the size of the four quarters of the page links. */
268 /* If we can, just show all the pages. */
269 if (($tot_pgs - 1) <= $page_selector_max) {
270 $q1_pgs = $cur_pg - 1;
271 $q2_pgs = $q3_pgs = 0;
272 $q4_pgs = $tot_pgs - $cur_pg;
274 /* Otherwise, compute some magic to choose the four quarters. */
277 * Compute the magic base values. Added together,
278 * these values will always equal to the $pag_pgs.
279 * NOTE: These are DEFAULT values and do not take
280 * the current page into account. That is below.
282 $q1_pgs = floor($vis_pgs/4);
283 $q2_pgs = round($vis_pgs/4, 0);
284 $q3_pgs = ceil($vis_pgs/4);
285 $q4_pgs = round(($vis_pgs - $q2_pgs)/3, 0);
287 /* Adjust if the first quarter contains the current page. */
288 if (($cur_pg - $q1_pgs) < 1) {
289 $extra_pgs = ($q1_pgs - ($cur_pg - 1)) +
$q2_pgs;
290 $q1_pgs = $cur_pg - 1;
292 $q3_pgs +
= ceil($extra_pgs / 2);
293 $q4_pgs +
= floor($extra_pgs / 2);
295 /* Adjust if the first and second quarters intersect. */
296 } else if (($cur_pg - $q2_pgs - ceil($q2_pgs/3)) <= $q1_pgs) {
297 $extra_pgs = $q2_pgs;
298 $extra_pgs -= ceil(($cur_pg - $q1_pgs - 1) * 3/4);
299 $q2_pgs = ceil(($cur_pg - $q1_pgs - 1) * 3/4);
300 $q3_pgs +
= ceil($extra_pgs / 2);
301 $q4_pgs +
= floor($extra_pgs / 2);
303 /* Adjust if the fourth quarter contains the current page. */
304 } else if (($cur_pg +
$q4_pgs) >= $tot_pgs) {
305 $extra_pgs = ($q4_pgs - ($tot_pgs - $cur_pg)) +
$q3_pgs;
307 $q4_pgs = $tot_pgs - $cur_pg;
308 $q1_pgs +
= floor($extra_pgs / 2);
309 $q2_pgs +
= ceil($extra_pgs / 2);
311 /* Adjust if the third and fourth quarter intersect. */
312 } else if (($cur_pg +
$q3_pgs +
1) >= ($tot_pgs - $q4_pgs +
1)) {
313 $extra_pgs = $q3_pgs;
314 $extra_pgs -= ceil(($tot_pgs - $cur_pg - $q4_pgs) * 3/4);
315 $q3_pgs = ceil(($tot_pgs - $cur_pg - $q4_pgs) * 3/4);
316 $q1_pgs +
= floor($extra_pgs / 2);
317 $q2_pgs +
= ceil($extra_pgs / 2);
322 * I am leaving this debug code here, commented out, because
323 * it is a really nice way to see what the above code is doing.
324 * echo "qts = $q1_pgs/$q2_pgs/$q3_pgs/$q4_pgs = "
325 * . ($q1_pgs + $q2_pgs + $q3_pgs + $q4_pgs) . '<br />';
328 /* Print out the page links from the compute page quarters. */
330 /* Start with the first quarter. */
331 if (($q1_pgs == 0) && ($cur_pg > 1)) {
332 $pg_str .= "...$nbsp";
334 for ($pg = 1; $pg <= $q1_pgs; ++
$pg) {
335 $start = (($pg-1) * $iLimit) +
1;
336 $pg_str .= get_paginator_link($box, $start, $pg) . $nbsp;
338 if ($cur_pg - $q2_pgs - $q1_pgs > 1) {
339 $pg_str .= "...$nbsp";
343 /* Continue with the second quarter. */
344 for ($pg = $cur_pg - $q2_pgs; $pg < $cur_pg; ++
$pg) {
345 $start = (($pg-1) * $iLimit) +
1;
346 $pg_str .= get_paginator_link($box, $start, $pg) . $nbsp;
349 /* Now print the current page. */
350 $pg_str .= $cur_pg . $nbsp;
352 /* Next comes the third quarter. */
353 for ($pg = $cur_pg +
1; $pg <= $cur_pg +
$q3_pgs; ++
$pg) {
354 $start = (($pg-1) * $iLimit) +
1;
355 $pg_str .= get_paginator_link($box, $start, $pg) . $nbsp;
358 /* And last, print the forth quarter page links. */
359 if (($q4_pgs == 0) && ($cur_pg < $tot_pgs)) {
360 $pg_str .= "...$nbsp";
362 if (($tot_pgs - $q4_pgs) > ($cur_pg +
$q3_pgs)) {
363 $pg_str .= "...$nbsp";
365 for ($pg = $tot_pgs - $q4_pgs +
1; $pg <= $tot_pgs; ++
$pg) {
366 $start = (($pg-1) * $iLimit) +
1;
367 $pg_str .= get_paginator_link($box, $start,$pg) . $nbsp;
371 $last_grp = (($tot_pgs - 1) * $iLimit) +
1;
374 global $accesskey_mailbox_all_paginate;
375 $pg_str = create_hyperlink("$php_self?showall=0&startMessage=1&mailbox=$box" . (strpos($php_self, 'src/search.php') ?
'&smtoken=' . sm_generate_security_token() : ''), _("Paginate"), '', '', '', '', '', ($accesskeys_constructed ?
array() : array('accesskey' =>
376 $accesskey_mailbox_all_paginate)));
379 /* Put all the pieces of the paginator string together. */
381 * Hairy code... But let's leave it like it is since I am not certain
382 * a different approach would be any easier to read. ;)
385 if ( $prv_str ||
$nxt_str ) {
387 /* Compute the 'show all' string. */
388 global $accesskey_mailbox_all_paginate;
389 $all_str = create_hyperlink("$php_self?showall=1&startMessage=1&mailbox=$box" . (strpos($php_self, 'src/search.php') ?
'&smtoken=' . sm_generate_security_token() : ''), _("Show All"), '', '', '', '', '', ($accesskeys_constructed ?
array() : array('accesskey' =>
390 $accesskey_mailbox_all_paginate)));
393 $result .= ($prv_str != '' ?
$prv_str . $nbsp . $sep . $nbsp : '');
394 $result .= ($nxt_str != '' ?
$nxt_str : '');
395 $result .= ']' . $nbsp ;
398 $result .= ($pg_str != '' ?
$nbsp . '['.$nbsp.$pg_str.']' . $nbsp : '');
399 $result .= ($all_str != '' ?
$nbsp . '['.$all_str.']' . $nbsp . $nbsp : '');
401 /* If the resulting string is blank, return a non-breaking space. */
406 $accesskeys_constructed = TRUE;
408 /* Return our final magical compact paginator string. */