Happy New Year
[squirrelmail.git] / functions / template / paginator_util.php
1 <?php
2
3 /**
4 * paginator_util.php
5 *
6 * The following functions are utility functions for templates. Do not
7 * echo output in these functions.
8 *
9 * @copyright 2005-2018 The SquirrelMail Project Team
10 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
11 * @version $Id$
12 * @package squirrelmail
13 */
14
15
16 /** Load forms functions, needed for addsubmit(). */
17 include_once(SM_PATH . 'functions/forms.php');
18
19
20 /**
21 * Generate a paginator link.
22 *
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
27 * @return string
28 */
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&amp;mailbox=$box"
32 . (strpos($php_self, 'src/search.php') ? '&amp;smtoken=' . sm_generate_security_token() : ''),
33 $text, '', '', '', '', '',
34 ($accesskey == 'NONE'
35 ? array()
36 : array('accesskey' => $accesskey)));
37 }
38
39
40 /**
41 * This function computes the comapact paginator string.
42 *
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
51 *
52 * @return string $result paginate string with links to pages
53 *
54 */
55 function get_compact_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll, $javascript_on, $page_selector) {
56
57 static $accesskeys_constructed = FALSE;
58
59 /* This will be used as a space. */
60 global $oTemplate, $nbsp;
61
62 // keeps count of how many times
63 // the paginator is used, avoids
64 // duplicate naming of <select>
65 // and GO button
66 static $display_iterations = 0;
67 $display_iterations++;
68
69 sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER);
70
71 /* Initialize paginator string chunks. */
72 $prv_str = '';
73 $nxt_str = '';
74 $pg_str = '';
75 $all_str = '';
76
77 $box = urlencode($box);
78
79 /* Create simple strings that will be creating the paginator. */
80 /* This will be used as a seperator. */
81 $sep = '|';
82
83 /* Make sure that our start message number is not too big. */
84 $iOffset = min($iOffset, $iTotal);
85
86 /* Compute the starting message of the previous and next page group. */
87 $next_grp = $iOffset + $iLimit;
88 $prev_grp = $iOffset - $iLimit;
89
90 if (!$bShowAll) {
91
92 /* Compute the basic previous and next strings. */
93
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));
106 $nxt_str = '>';
107 } else if (($next_grp <= $iTotal) && ($prev_grp < 0)) {
108 $prv_str = '<';
109 $nxt_str = get_paginator_link($box, $next_grp, '>',
110 ($accesskeys_constructed
111 ? 'NONE' : $accesskey_mailbox_next));
112 }
113
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;
118
119 /* Compute total # of pages and # of paginator page links. */
120 $tot_pgs = ceil($iTotal / $iLimit); /* Total number of Pages */
121
122 $last_grp = (($tot_pgs - 1) * $iLimit) + 1;
123 }
124 } else {
125 global $accesskey_mailbox_all_paginate;
126 $pg_str = create_hyperlink("$php_self?showall=0&amp;startMessage=1&amp;mailbox=$box" . (strpos($php_self, 'src/search.php') ? '&amp;smtoken=' . sm_generate_security_token() : ''), _("Paginate"), '', '', '', '', '', ($accesskeys_constructed ? array() : array('accesskey' => $accesskey_mailbox_all_paginate)));
127 }
128
129 /* Put all the pieces of the paginator string together. */
130 /**
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. ;)
133 */
134 $result = '';
135 if ( $prv_str || $nxt_str ) {
136
137 /* Compute the 'show all' string. */
138 global $accesskey_mailbox_all_paginate;
139 $all_str = create_hyperlink("$php_self?showall=1&amp;startMessage=1&amp;mailbox=$box" . (strpos($php_self, 'src/search.php') ? '&amp;smtoken=' . sm_generate_security_token() : ''), _("Show All"), '', '', '', '', '', ($accesskeys_constructed ? array() : array('accesskey' => $accesskey_mailbox_all_paginate)));
140
141 $result .= '[' . get_paginator_link($box, 1, '<<') . ']';
142 $result .= '[' . $prv_str . ']';
143
144 $pg_url = $php_self . '?mailbox=' . $box . (strpos($php_self, 'src/search.php') ? '&smtoken=' . sm_generate_security_token() : '');
145
146 $result .= '[' . $nxt_str . ']';
147 $result .= '[' . get_paginator_link($box, $last_grp, '>>') . ']';
148
149 if ($page_selector) {
150 $options = array();
151 for ($p = 0; $p < $tot_pgs; $p++) {
152 $options[(($p*$iLimit)+1) . '_' . $box] = ($p+1) . "/$tot_pgs";
153 }
154 $result .= $nbsp . addSelect('startMessage_' . $display_iterations,
155 $options,
156 ((($cur_pg-1)*$iLimit)+1),
157 TRUE,
158 ($javascript_on ? array('onchange' => 'JavaScript:SubmitOnSelect(this, \'' . $pg_url . '&startMessage=\')') : array()));
159
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)
168 // . '</noscript>';
169 } else {
170 $result .= addSubmit(_("Go"), 'paginator_submit_' . $display_iterations);
171 }
172 }
173 }
174
175 $result .= ($pg_str != '' ? '['.$pg_str.']' . $nbsp : '');
176 $result .= ($all_str != '' ? $nbsp . '['.$all_str.']' . $nbsp . $nbsp : '');
177
178 /* If the resulting string is blank, return a non-breaking space. */
179 if ($result == '') {
180 $result = '&nbsp;';
181 }
182
183 $accesskeys_constructed = TRUE;
184
185 /* Return our final magical paginator string. */
186 return ($result);
187 }
188
189
190 /**
191 * This function computes the paginator string.
192 *
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
201 *
202 * @return string $result paginate string with links to pages
203 *
204 */
205 function get_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll,$page_selector, $page_selector_max) {
206
207 static $accesskeys_constructed = FALSE;
208
209 /* This will be used as a space. */
210 global $oTemplate, $nbsp;
211 sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER);
212
213 /* Initialize paginator string chunks. */
214 $prv_str = '';
215 $nxt_str = '';
216 $pg_str = '';
217 $all_str = '';
218
219 $box = urlencode($box);
220
221 /* Create simple strings that will be creating the paginator. */
222 /* This will be used as a seperator. */
223 $sep = '|';
224
225 /* Make sure that our start message number is not too big. */
226 $iOffset = min($iOffset, $iTotal);
227
228 /* Compute the starting message of the previous and next page group. */
229 $next_grp = $iOffset + $iLimit;
230 $prev_grp = $iOffset - $iLimit;
231
232 if (!$bShowAll) {
233
234 /* Compute the basic previous and next strings. */
235
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));
254 }
255
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;
260
261 /* Compute total # of pages and # of paginator page links. */
262 $tot_pgs = ceil($iTotal / $iLimit); /* Total number of Pages */
263
264 $vis_pgs = min($page_selector_max, $tot_pgs - 1); /* Visible Pages */
265
266 /* Compute the size of the four quarters of the page links. */
267
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;
273
274 /* Otherwise, compute some magic to choose the four quarters. */
275 } else {
276 /*
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.
281 */
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);
286
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;
291 $q2_pgs = 0;
292 $q3_pgs += ceil($extra_pgs / 2);
293 $q4_pgs += floor($extra_pgs / 2);
294
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);
302
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;
306 $q3_pgs = 0;
307 $q4_pgs = $tot_pgs - $cur_pg;
308 $q1_pgs += floor($extra_pgs / 2);
309 $q2_pgs += ceil($extra_pgs / 2);
310
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);
318 }
319 }
320
321 /*
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 />';
326 */
327
328 /* Print out the page links from the compute page quarters. */
329
330 /* Start with the first quarter. */
331 if (($q1_pgs == 0) && ($cur_pg > 1)) {
332 $pg_str .= "...$nbsp";
333 } else {
334 for ($pg = 1; $pg <= $q1_pgs; ++$pg) {
335 $start = (($pg-1) * $iLimit) + 1;
336 $pg_str .= get_paginator_link($box, $start, $pg) . $nbsp;
337 }
338 if ($cur_pg - $q2_pgs - $q1_pgs > 1) {
339 $pg_str .= "...$nbsp";
340 }
341 }
342
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;
347 }
348
349 /* Now print the current page. */
350 $pg_str .= $cur_pg . $nbsp;
351
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;
356 }
357
358 /* And last, print the forth quarter page links. */
359 if (($q4_pgs == 0) && ($cur_pg < $tot_pgs)) {
360 $pg_str .= "...$nbsp";
361 } else {
362 if (($tot_pgs - $q4_pgs) > ($cur_pg + $q3_pgs)) {
363 $pg_str .= "...$nbsp";
364 }
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;
368 }
369 }
370
371 $last_grp = (($tot_pgs - 1) * $iLimit) + 1;
372 }
373 } else {
374 global $accesskey_mailbox_all_paginate;
375 $pg_str = create_hyperlink("$php_self?showall=0&amp;startMessage=1&amp;mailbox=$box" . (strpos($php_self, 'src/search.php') ? '&amp;smtoken=' . sm_generate_security_token() : ''), _("Paginate"), '', '', '', '', '', ($accesskeys_constructed ? array() : array('accesskey' =>
376 $accesskey_mailbox_all_paginate)));
377 }
378
379 /* Put all the pieces of the paginator string together. */
380 /**
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. ;)
383 */
384 $result = '';
385 if ( $prv_str || $nxt_str ) {
386
387 /* Compute the 'show all' string. */
388 global $accesskey_mailbox_all_paginate;
389 $all_str = create_hyperlink("$php_self?showall=1&amp;startMessage=1&amp;mailbox=$box" . (strpos($php_self, 'src/search.php') ? '&amp;smtoken=' . sm_generate_security_token() : ''), _("Show All"), '', '', '', '', '', ($accesskeys_constructed ? array() : array('accesskey' =>
390 $accesskey_mailbox_all_paginate)));
391
392 $result .= '[';
393 $result .= ($prv_str != '' ? $prv_str . $nbsp . $sep . $nbsp : '');
394 $result .= ($nxt_str != '' ? $nxt_str : '');
395 $result .= ']' . $nbsp ;
396 }
397
398 $result .= ($pg_str != '' ? $nbsp . '['.$nbsp.$pg_str.']' . $nbsp : '');
399 $result .= ($all_str != '' ? $nbsp . '['.$all_str.']' . $nbsp . $nbsp : '');
400
401 /* If the resulting string is blank, return a non-breaking space. */
402 if ($result == '') {
403 $result = $nbsp;
404 }
405
406 $accesskeys_constructed = TRUE;
407
408 /* Return our final magical compact paginator string. */
409 return ($result);
410 }
411
412