49db257d |
1 | <?php |
4b4abf93 |
2 | |
49db257d |
3 | /** |
8409c786 |
4 | * paginator_util.php |
49db257d |
5 | * |
8409c786 |
6 | * The following functions are utility functions for templates. Do not |
7 | * echo output in these functions. |
4b4abf93 |
8 | * |
1977ab55 |
9 | * @copyright 2005-2010 The SquirrelMail Project Team |
4b4abf93 |
10 | * @license http://opensource.org/licenses/gpl-license.php GNU Public License |
11 | * @version $Id$ |
57893ef8 |
12 | * @package squirrelmail |
49db257d |
13 | */ |
14 | |
8409c786 |
15 | |
9e68aa93 |
16 | /** Load forms functions, needed for addsubmit(). */ |
6341b325 |
17 | include_once(SM_PATH . 'functions/forms.php'); |
18 | |
8409c786 |
19 | |
49db257d |
20 | /** |
21 | * Generate a paginator link. |
22 | * |
47485591 |
23 | * @param string $box Mailbox name |
49db257d |
24 | * @param integer $start_msg Message Offset |
47485591 |
25 | * @param string $text The text used for paginator link |
26 | * @param string $accesskey The access key for the link, if any |
49db257d |
27 | * @return string |
28 | */ |
47485591 |
29 | function get_paginator_link($box, $start_msg, $text, $accesskey='NONE') { |
49db257d |
30 | sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER); |
9969411a |
31 | return create_hyperlink("$php_self?startMessage=$start_msg&mailbox=$box" |
32 | . (strpos($php_self, 'src/search.php') ? '&smtoken=' . sm_generate_security_token() : ''), |
47485591 |
33 | $text, '', '', '', '', '', |
c12535f6 |
34 | ($accesskey == 'NONE' |
35 | ? array() |
36 | : array('accesskey' => $accesskey))); |
49db257d |
37 | } |
38 | |
8409c786 |
39 | |
49db257d |
40 | /** |
41 | * This function computes the comapact paginator string. |
42 | * |
8409c786 |
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 | * |
49db257d |
52 | * @return string $result paginate string with links to pages |
8409c786 |
53 | * |
49db257d |
54 | */ |
55 | function get_compact_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll, $javascript_on, $page_selector) { |
56 | |
47485591 |
57 | static $accesskeys_constructed = FALSE; |
58 | |
551c7b53 |
59 | /* This will be used as a space. */ |
60 | global $oTemplate, $nbsp; |
54ce41dd |
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 | |
49db257d |
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. */ |
8409c786 |
80 | /* This will be used as a seperator. */ |
81 | $sep = '|'; |
49db257d |
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) { |
47485591 |
91 | |
49db257d |
92 | /* Compute the basic previous and next strings. */ |
47485591 |
93 | |
94 | global $accesskey_mailbox_previous, $accesskey_mailbox_next; |
49db257d |
95 | if (($next_grp <= $iTotal) && ($prev_grp >= 0)) { |
47485591 |
96 | $prv_str = get_paginator_link($box, $prev_grp, '<', |
97 | ($accesskeys_constructed |
c12535f6 |
98 | ? 'NONE' : $accesskey_mailbox_previous)); |
47485591 |
99 | $nxt_str = get_paginator_link($box, $next_grp, '>', |
100 | ($accesskeys_constructed |
c12535f6 |
101 | ? 'NONE' : $accesskey_mailbox_next)); |
49db257d |
102 | } else if (($next_grp > $iTotal) && ($prev_grp >= 0)) { |
47485591 |
103 | $prv_str = get_paginator_link($box, $prev_grp, '<', |
104 | ($accesskeys_constructed |
c12535f6 |
105 | ? 'NONE' : $accesskey_mailbox_previous)); |
49db257d |
106 | $nxt_str = '>'; |
107 | } else if (($next_grp <= $iTotal) && ($prev_grp < 0)) { |
108 | $prv_str = '<'; |
47485591 |
109 | $nxt_str = get_paginator_link($box, $next_grp, '>', |
110 | ($accesskeys_constructed |
c12535f6 |
111 | ? 'NONE' : $accesskey_mailbox_next)); |
49db257d |
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 { |
47485591 |
125 | global $accesskey_mailbox_all_paginate; |
9969411a |
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))); |
49db257d |
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. */ |
47485591 |
138 | global $accesskey_mailbox_all_paginate; |
9969411a |
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))); |
49db257d |
140 | |
141 | $result .= '[' . get_paginator_link($box, 1, '<<') . ']'; |
142 | $result .= '[' . $prv_str . ']'; |
143 | |
9969411a |
144 | $pg_url = $php_self . '?mailbox=' . $box . (strpos($php_self, 'src/search.php') ? '&smtoken=' . sm_generate_security_token() : ''); |
49db257d |
145 | |
146 | $result .= '[' . $nxt_str . ']'; |
147 | $result .= '[' . get_paginator_link($box, $last_grp, '>>') . ']'; |
148 | |
149 | if ($page_selector) { |
8409c786 |
150 | $options = array(); |
49db257d |
151 | for ($p = 0; $p < $tot_pgs; $p++) { |
7dc61587 |
152 | $options[(($p*$iLimit)+1) . '_' . $box] = ($p+1) . "/$tot_pgs"; |
49db257d |
153 | } |
551c7b53 |
154 | $result .= $nbsp . addSelect('startMessage_' . $display_iterations, |
54ce41dd |
155 | $options, |
8409c786 |
156 | ((($cur_pg-1)*$iLimit)+1), |
157 | TRUE, |
158 | ($javascript_on ? array('onchange' => 'JavaScript:SubmitOnSelect(this, \'' . $pg_url . '&startMessage=\')') : array())); |
49db257d |
159 | |
160 | if ($javascript_on) { |
8409c786 |
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">' |
54ce41dd |
167 | // . addSubmit(_("Go"), 'paginator_submit_' . $display_iterations) |
8409c786 |
168 | // . '</noscript>'; |
49db257d |
169 | } else { |
54ce41dd |
170 | $result .= addSubmit(_("Go"), 'paginator_submit_' . $display_iterations); |
49db257d |
171 | } |
172 | } |
173 | } |
174 | |
551c7b53 |
175 | $result .= ($pg_str != '' ? '['.$pg_str.']' . $nbsp : ''); |
176 | $result .= ($all_str != '' ? $nbsp . '['.$all_str.']' . $nbsp . $nbsp : ''); |
49db257d |
177 | |
178 | /* If the resulting string is blank, return a non-breaking space. */ |
179 | if ($result == '') { |
180 | $result = ' '; |
181 | } |
47485591 |
182 | |
183 | $accesskeys_constructed = TRUE; |
184 | |
49db257d |
185 | /* Return our final magical paginator string. */ |
186 | return ($result); |
187 | } |
188 | |
8409c786 |
189 | |
49db257d |
190 | /** |
191 | * This function computes the paginator string. |
192 | * |
8409c786 |
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 | * |
49db257d |
202 | * @return string $result paginate string with links to pages |
8409c786 |
203 | * |
49db257d |
204 | */ |
205 | function get_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll,$page_selector, $page_selector_max) { |
206 | |
47485591 |
207 | static $accesskeys_constructed = FALSE; |
208 | |
551c7b53 |
209 | /* This will be used as a space. */ |
210 | global $oTemplate, $nbsp; |
49db257d |
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. */ |
8409c786 |
222 | /* This will be used as a seperator. */ |
223 | $sep = '|'; |
49db257d |
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) { |
47485591 |
233 | |
49db257d |
234 | /* Compute the basic previous and next strings. */ |
235 | |
47485591 |
236 | global $accesskey_mailbox_previous, $accesskey_mailbox_next; |
49db257d |
237 | if (($next_grp <= $iTotal) && ($prev_grp >= 0)) { |
47485591 |
238 | $prv_str = get_paginator_link($box, $prev_grp, _("Previous"), |
239 | ($accesskeys_constructed |
c12535f6 |
240 | ? 'NONE' : $accesskey_mailbox_previous)); |
47485591 |
241 | $nxt_str = get_paginator_link($box, $next_grp, _("Next"), |
242 | ($accesskeys_constructed |
c12535f6 |
243 | ? 'NONE' : $accesskey_mailbox_next)); |
49db257d |
244 | } else if (($next_grp > $iTotal) && ($prev_grp >= 0)) { |
47485591 |
245 | $prv_str = get_paginator_link($box, $prev_grp, _("Previous"), |
246 | ($accesskeys_constructed |
c12535f6 |
247 | ? 'NONE' : $accesskey_mailbox_previous)); |
49db257d |
248 | $nxt_str = _("Next"); |
249 | } else if (($next_grp <= $iTotal) && ($prev_grp < 0)) { |
250 | $prv_str = _("Previous"); |
47485591 |
251 | $nxt_str = get_paginator_link($box, $next_grp, _("Next"), |
252 | ($accesskeys_constructed |
c12535f6 |
253 | ? 'NONE' : $accesskey_mailbox_next)); |
49db257d |
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)) { |
551c7b53 |
332 | $pg_str .= "...$nbsp"; |
49db257d |
333 | } else { |
334 | for ($pg = 1; $pg <= $q1_pgs; ++$pg) { |
335 | $start = (($pg-1) * $iLimit) + 1; |
551c7b53 |
336 | $pg_str .= get_paginator_link($box, $start, $pg) . $nbsp; |
49db257d |
337 | } |
338 | if ($cur_pg - $q2_pgs - $q1_pgs > 1) { |
551c7b53 |
339 | $pg_str .= "...$nbsp"; |
49db257d |
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; |
551c7b53 |
346 | $pg_str .= get_paginator_link($box, $start, $pg) . $nbsp; |
49db257d |
347 | } |
348 | |
349 | /* Now print the current page. */ |
551c7b53 |
350 | $pg_str .= $cur_pg . $nbsp; |
49db257d |
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; |
551c7b53 |
355 | $pg_str .= get_paginator_link($box, $start, $pg) . $nbsp; |
49db257d |
356 | } |
357 | |
358 | /* And last, print the forth quarter page links. */ |
359 | if (($q4_pgs == 0) && ($cur_pg < $tot_pgs)) { |
551c7b53 |
360 | $pg_str .= "...$nbsp"; |
49db257d |
361 | } else { |
362 | if (($tot_pgs - $q4_pgs) > ($cur_pg + $q3_pgs)) { |
551c7b53 |
363 | $pg_str .= "...$nbsp"; |
49db257d |
364 | } |
365 | for ($pg = $tot_pgs - $q4_pgs + 1; $pg <= $tot_pgs; ++$pg) { |
366 | $start = (($pg-1) * $iLimit) + 1; |
551c7b53 |
367 | $pg_str .= get_paginator_link($box, $start,$pg) . $nbsp; |
49db257d |
368 | } |
369 | } |
370 | |
371 | $last_grp = (($tot_pgs - 1) * $iLimit) + 1; |
372 | } |
373 | } else { |
47485591 |
374 | global $accesskey_mailbox_all_paginate; |
9969411a |
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' => |
c12535f6 |
376 | $accesskey_mailbox_all_paginate))); |
49db257d |
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. */ |
47485591 |
388 | global $accesskey_mailbox_all_paginate; |
9969411a |
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' => |
c12535f6 |
390 | $accesskey_mailbox_all_paginate))); |
49db257d |
391 | |
392 | $result .= '['; |
551c7b53 |
393 | $result .= ($prv_str != '' ? $prv_str . $nbsp . $sep . $nbsp : ''); |
49db257d |
394 | $result .= ($nxt_str != '' ? $nxt_str : ''); |
551c7b53 |
395 | $result .= ']' . $nbsp ; |
49db257d |
396 | } |
397 | |
551c7b53 |
398 | $result .= ($pg_str != '' ? $nbsp . '['.$nbsp.$pg_str.']' . $nbsp : ''); |
399 | $result .= ($all_str != '' ? $nbsp . '['.$all_str.']' . $nbsp . $nbsp : ''); |
49db257d |
400 | |
401 | /* If the resulting string is blank, return a non-breaking space. */ |
402 | if ($result == '') { |
551c7b53 |
403 | $result = $nbsp; |
49db257d |
404 | } |
47485591 |
405 | |
406 | $accesskeys_constructed = TRUE; |
407 | |
49db257d |
408 | /* Return our final magical compact paginator string. */ |
409 | return ($result); |
410 | } |
8409c786 |
411 | |
412 | |