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 | * |
47ccfad4 |
9 | * @copyright © 2005-2006 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 | * |
23 | * @param string $box Mailbox name |
24 | * @param integer $start_msg Message Offset |
25 | * @param string $text text used for paginator link |
26 | * @return string |
27 | */ |
28 | function get_paginator_link($box, $start_msg, $text) { |
29 | sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER); |
8409c786 |
30 | return create_hyperlink("$php_self?startMessage=$start_msg&mailbox=$box", $text); |
49db257d |
31 | } |
32 | |
8409c786 |
33 | |
49db257d |
34 | /** |
35 | * This function computes the comapact paginator string. |
36 | * |
8409c786 |
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 |
45 | * |
49db257d |
46 | * @return string $result paginate string with links to pages |
8409c786 |
47 | * |
49db257d |
48 | */ |
49 | function get_compact_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll, $javascript_on, $page_selector) { |
50 | |
551c7b53 |
51 | /* This will be used as a space. */ |
52 | global $oTemplate, $nbsp; |
54ce41dd |
53 | |
54 | // keeps count of how many times |
55 | // the paginator is used, avoids |
56 | // duplicate naming of <select> |
57 | // and GO button |
58 | static $display_iterations = 0; |
59 | $display_iterations++; |
60 | |
49db257d |
61 | sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER); |
62 | |
63 | /* Initialize paginator string chunks. */ |
64 | $prv_str = ''; |
65 | $nxt_str = ''; |
66 | $pg_str = ''; |
67 | $all_str = ''; |
68 | |
69 | $box = urlencode($box); |
70 | |
71 | /* Create simple strings that will be creating the paginator. */ |
8409c786 |
72 | /* This will be used as a seperator. */ |
73 | $sep = '|'; |
49db257d |
74 | |
75 | /* Make sure that our start message number is not too big. */ |
76 | $iOffset = min($iOffset, $iTotal); |
77 | |
78 | /* Compute the starting message of the previous and next page group. */ |
79 | $next_grp = $iOffset + $iLimit; |
80 | $prev_grp = $iOffset - $iLimit; |
81 | |
82 | if (!$bShowAll) { |
83 | /* Compute the basic previous and next strings. */ |
84 | if (($next_grp <= $iTotal) && ($prev_grp >= 0)) { |
85 | $prv_str = get_paginator_link($box, $prev_grp, '<'); |
86 | $nxt_str = get_paginator_link($box, $next_grp, '>'); |
87 | } else if (($next_grp > $iTotal) && ($prev_grp >= 0)) { |
88 | $prv_str = get_paginator_link($box, $prev_grp, '<'); |
89 | $nxt_str = '>'; |
90 | } else if (($next_grp <= $iTotal) && ($prev_grp < 0)) { |
91 | $prv_str = '<'; |
92 | $nxt_str = get_paginator_link($box, $next_grp, '>'); |
93 | } |
94 | |
95 | /* Page selector block. Following code computes page links. */ |
96 | if ($iLimit != 0 && $page_selector && ($iTotal > $iLimit)) { |
97 | /* Most importantly, what is the current page!!! */ |
98 | $cur_pg = intval($iOffset / $iLimit) + 1; |
99 | |
100 | /* Compute total # of pages and # of paginator page links. */ |
101 | $tot_pgs = ceil($iTotal / $iLimit); /* Total number of Pages */ |
102 | |
103 | $last_grp = (($tot_pgs - 1) * $iLimit) + 1; |
104 | } |
105 | } else { |
8409c786 |
106 | $pg_str = create_hyperlink("$php_self?showall=0&startMessage=1&mailbox=$box", _("Paginate")); |
49db257d |
107 | } |
108 | |
109 | /* Put all the pieces of the paginator string together. */ |
110 | /** |
111 | * Hairy code... But let's leave it like it is since I am not certain |
112 | * a different approach would be any easier to read. ;) |
113 | */ |
114 | $result = ''; |
115 | if ( $prv_str || $nxt_str ) { |
116 | |
117 | /* Compute the 'show all' string. */ |
8409c786 |
118 | $all_str = create_hyperlink("$php_self?showall=1&startMessage=1&mailbox=$box", _("Show All")); |
49db257d |
119 | |
120 | $result .= '[' . get_paginator_link($box, 1, '<<') . ']'; |
121 | $result .= '[' . $prv_str . ']'; |
122 | |
123 | $pg_url = $php_self . '?mailbox=' . $box; |
124 | |
125 | $result .= '[' . $nxt_str . ']'; |
126 | $result .= '[' . get_paginator_link($box, $last_grp, '>>') . ']'; |
127 | |
128 | if ($page_selector) { |
8409c786 |
129 | $options = array(); |
49db257d |
130 | for ($p = 0; $p < $tot_pgs; $p++) { |
7dc61587 |
131 | $options[(($p*$iLimit)+1) . '_' . $box] = ($p+1) . "/$tot_pgs"; |
49db257d |
132 | } |
551c7b53 |
133 | $result .= $nbsp . addSelect('startMessage_' . $display_iterations, |
54ce41dd |
134 | $options, |
8409c786 |
135 | ((($cur_pg-1)*$iLimit)+1), |
136 | TRUE, |
137 | ($javascript_on ? array('onchange' => 'JavaScript:SubmitOnSelect(this, \'' . $pg_url . '&startMessage=\')') : array())); |
49db257d |
138 | |
139 | if ($javascript_on) { |
8409c786 |
140 | //FIXME: What in the world? Two issues here: for one, $javascript_on is supposed |
141 | // to have already detected whether or not JavaScript is available and enabled. |
142 | // Secondly, we need to rid ourselves of any HTML output in the core. This |
143 | // is being removed (but left in case the original author points out why it |
144 | // should not be) and we'll trust $javascript_on to do the right thing. |
145 | // $result .= '<noscript language="JavaScript">' |
54ce41dd |
146 | // . addSubmit(_("Go"), 'paginator_submit_' . $display_iterations) |
8409c786 |
147 | // . '</noscript>'; |
49db257d |
148 | } else { |
54ce41dd |
149 | $result .= addSubmit(_("Go"), 'paginator_submit_' . $display_iterations); |
49db257d |
150 | } |
151 | } |
152 | } |
153 | |
551c7b53 |
154 | $result .= ($pg_str != '' ? '['.$pg_str.']' . $nbsp : ''); |
155 | $result .= ($all_str != '' ? $nbsp . '['.$all_str.']' . $nbsp . $nbsp : ''); |
49db257d |
156 | |
157 | /* If the resulting string is blank, return a non-breaking space. */ |
158 | if ($result == '') { |
159 | $result = ' '; |
160 | } |
161 | /* Return our final magical paginator string. */ |
162 | return ($result); |
163 | } |
164 | |
8409c786 |
165 | |
49db257d |
166 | /** |
167 | * This function computes the paginator string. |
168 | * |
8409c786 |
169 | * @param string $box mailbox name |
170 | * @param integer $iOffset offset in total number of messages |
171 | * @param integer $iTotal total number of messages |
172 | * @param integer $iLimit maximum number of messages to show on a page |
173 | * @param bool $bShowAll whether or not to show all messages at once |
174 | * ("show all" == non paginate mode) |
175 | * @param bool $page_selector whether or not to show the page selection widget |
176 | * @param integer $page_selector_max maximum number of pages to show on the screen |
177 | * |
49db257d |
178 | * @return string $result paginate string with links to pages |
8409c786 |
179 | * |
49db257d |
180 | */ |
181 | function get_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll,$page_selector, $page_selector_max) { |
182 | |
551c7b53 |
183 | /* This will be used as a space. */ |
184 | global $oTemplate, $nbsp; |
49db257d |
185 | sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER); |
186 | |
187 | /* Initialize paginator string chunks. */ |
188 | $prv_str = ''; |
189 | $nxt_str = ''; |
190 | $pg_str = ''; |
191 | $all_str = ''; |
192 | |
193 | $box = urlencode($box); |
194 | |
195 | /* Create simple strings that will be creating the paginator. */ |
8409c786 |
196 | /* This will be used as a seperator. */ |
197 | $sep = '|'; |
49db257d |
198 | |
199 | /* Make sure that our start message number is not too big. */ |
200 | $iOffset = min($iOffset, $iTotal); |
201 | |
202 | /* Compute the starting message of the previous and next page group. */ |
203 | $next_grp = $iOffset + $iLimit; |
204 | $prev_grp = $iOffset - $iLimit; |
205 | |
206 | if (!$bShowAll) { |
207 | /* Compute the basic previous and next strings. */ |
208 | |
209 | if (($next_grp <= $iTotal) && ($prev_grp >= 0)) { |
210 | $prv_str = get_paginator_link($box, $prev_grp, _("Previous")); |
211 | $nxt_str = get_paginator_link($box, $next_grp, _("Next")); |
212 | } else if (($next_grp > $iTotal) && ($prev_grp >= 0)) { |
213 | $prv_str = get_paginator_link($box, $prev_grp, _("Previous")); |
214 | $nxt_str = _("Next"); |
215 | } else if (($next_grp <= $iTotal) && ($prev_grp < 0)) { |
216 | $prv_str = _("Previous"); |
217 | $nxt_str = get_paginator_link($box, $next_grp, _("Next")); |
218 | } |
219 | |
220 | /* Page selector block. Following code computes page links. */ |
221 | if ($iLimit != 0 && $page_selector && ($iTotal > $iLimit)) { |
222 | /* Most importantly, what is the current page!!! */ |
223 | $cur_pg = intval($iOffset / $iLimit) + 1; |
224 | |
225 | /* Compute total # of pages and # of paginator page links. */ |
226 | $tot_pgs = ceil($iTotal / $iLimit); /* Total number of Pages */ |
227 | |
228 | $vis_pgs = min($page_selector_max, $tot_pgs - 1); /* Visible Pages */ |
229 | |
230 | /* Compute the size of the four quarters of the page links. */ |
231 | |
232 | /* If we can, just show all the pages. */ |
233 | if (($tot_pgs - 1) <= $page_selector_max) { |
234 | $q1_pgs = $cur_pg - 1; |
235 | $q2_pgs = $q3_pgs = 0; |
236 | $q4_pgs = $tot_pgs - $cur_pg; |
237 | |
238 | /* Otherwise, compute some magic to choose the four quarters. */ |
239 | } else { |
240 | /* |
241 | * Compute the magic base values. Added together, |
242 | * these values will always equal to the $pag_pgs. |
243 | * NOTE: These are DEFAULT values and do not take |
244 | * the current page into account. That is below. |
245 | */ |
246 | $q1_pgs = floor($vis_pgs/4); |
247 | $q2_pgs = round($vis_pgs/4, 0); |
248 | $q3_pgs = ceil($vis_pgs/4); |
249 | $q4_pgs = round(($vis_pgs - $q2_pgs)/3, 0); |
250 | |
251 | /* Adjust if the first quarter contains the current page. */ |
252 | if (($cur_pg - $q1_pgs) < 1) { |
253 | $extra_pgs = ($q1_pgs - ($cur_pg - 1)) + $q2_pgs; |
254 | $q1_pgs = $cur_pg - 1; |
255 | $q2_pgs = 0; |
256 | $q3_pgs += ceil($extra_pgs / 2); |
257 | $q4_pgs += floor($extra_pgs / 2); |
258 | |
259 | /* Adjust if the first and second quarters intersect. */ |
260 | } else if (($cur_pg - $q2_pgs - ceil($q2_pgs/3)) <= $q1_pgs) { |
261 | $extra_pgs = $q2_pgs; |
262 | $extra_pgs -= ceil(($cur_pg - $q1_pgs - 1) * 3/4); |
263 | $q2_pgs = ceil(($cur_pg - $q1_pgs - 1) * 3/4); |
264 | $q3_pgs += ceil($extra_pgs / 2); |
265 | $q4_pgs += floor($extra_pgs / 2); |
266 | |
267 | /* Adjust if the fourth quarter contains the current page. */ |
268 | } else if (($cur_pg + $q4_pgs) >= $tot_pgs) { |
269 | $extra_pgs = ($q4_pgs - ($tot_pgs - $cur_pg)) + $q3_pgs; |
270 | $q3_pgs = 0; |
271 | $q4_pgs = $tot_pgs - $cur_pg; |
272 | $q1_pgs += floor($extra_pgs / 2); |
273 | $q2_pgs += ceil($extra_pgs / 2); |
274 | |
275 | /* Adjust if the third and fourth quarter intersect. */ |
276 | } else if (($cur_pg + $q3_pgs + 1) >= ($tot_pgs - $q4_pgs + 1)) { |
277 | $extra_pgs = $q3_pgs; |
278 | $extra_pgs -= ceil(($tot_pgs - $cur_pg - $q4_pgs) * 3/4); |
279 | $q3_pgs = ceil(($tot_pgs - $cur_pg - $q4_pgs) * 3/4); |
280 | $q1_pgs += floor($extra_pgs / 2); |
281 | $q2_pgs += ceil($extra_pgs / 2); |
282 | } |
283 | } |
284 | |
285 | /* |
286 | * I am leaving this debug code here, commented out, because |
287 | * it is a really nice way to see what the above code is doing. |
288 | * echo "qts = $q1_pgs/$q2_pgs/$q3_pgs/$q4_pgs = " |
289 | * . ($q1_pgs + $q2_pgs + $q3_pgs + $q4_pgs) . '<br />'; |
290 | */ |
291 | |
292 | /* Print out the page links from the compute page quarters. */ |
293 | |
294 | /* Start with the first quarter. */ |
295 | if (($q1_pgs == 0) && ($cur_pg > 1)) { |
551c7b53 |
296 | $pg_str .= "...$nbsp"; |
49db257d |
297 | } else { |
298 | for ($pg = 1; $pg <= $q1_pgs; ++$pg) { |
299 | $start = (($pg-1) * $iLimit) + 1; |
551c7b53 |
300 | $pg_str .= get_paginator_link($box, $start, $pg) . $nbsp; |
49db257d |
301 | } |
302 | if ($cur_pg - $q2_pgs - $q1_pgs > 1) { |
551c7b53 |
303 | $pg_str .= "...$nbsp"; |
49db257d |
304 | } |
305 | } |
306 | |
307 | /* Continue with the second quarter. */ |
308 | for ($pg = $cur_pg - $q2_pgs; $pg < $cur_pg; ++$pg) { |
309 | $start = (($pg-1) * $iLimit) + 1; |
551c7b53 |
310 | $pg_str .= get_paginator_link($box, $start, $pg) . $nbsp; |
49db257d |
311 | } |
312 | |
313 | /* Now print the current page. */ |
551c7b53 |
314 | $pg_str .= $cur_pg . $nbsp; |
49db257d |
315 | |
316 | /* Next comes the third quarter. */ |
317 | for ($pg = $cur_pg + 1; $pg <= $cur_pg + $q3_pgs; ++$pg) { |
318 | $start = (($pg-1) * $iLimit) + 1; |
551c7b53 |
319 | $pg_str .= get_paginator_link($box, $start, $pg) . $nbsp; |
49db257d |
320 | } |
321 | |
322 | /* And last, print the forth quarter page links. */ |
323 | if (($q4_pgs == 0) && ($cur_pg < $tot_pgs)) { |
551c7b53 |
324 | $pg_str .= "...$nbsp"; |
49db257d |
325 | } else { |
326 | if (($tot_pgs - $q4_pgs) > ($cur_pg + $q3_pgs)) { |
551c7b53 |
327 | $pg_str .= "...$nbsp"; |
49db257d |
328 | } |
329 | for ($pg = $tot_pgs - $q4_pgs + 1; $pg <= $tot_pgs; ++$pg) { |
330 | $start = (($pg-1) * $iLimit) + 1; |
551c7b53 |
331 | $pg_str .= get_paginator_link($box, $start,$pg) . $nbsp; |
49db257d |
332 | } |
333 | } |
334 | |
335 | $last_grp = (($tot_pgs - 1) * $iLimit) + 1; |
336 | } |
337 | } else { |
8409c786 |
338 | $pg_str = create_hyperlink("$php_self?showall=0&startMessage=1&mailbox=$box", _("Paginate")); |
49db257d |
339 | } |
340 | |
341 | /* Put all the pieces of the paginator string together. */ |
342 | /** |
343 | * Hairy code... But let's leave it like it is since I am not certain |
344 | * a different approach would be any easier to read. ;) |
345 | */ |
346 | $result = ''; |
347 | if ( $prv_str || $nxt_str ) { |
348 | |
349 | /* Compute the 'show all' string. */ |
8409c786 |
350 | $all_str = create_hyperlink("$php_self?showall=1&startMessage=1&mailbox=$box", _("Show All")); |
49db257d |
351 | |
352 | $result .= '['; |
551c7b53 |
353 | $result .= ($prv_str != '' ? $prv_str . $nbsp . $sep . $nbsp : ''); |
49db257d |
354 | $result .= ($nxt_str != '' ? $nxt_str : ''); |
551c7b53 |
355 | $result .= ']' . $nbsp ; |
49db257d |
356 | } |
357 | |
551c7b53 |
358 | $result .= ($pg_str != '' ? $nbsp . '['.$nbsp.$pg_str.']' . $nbsp : ''); |
359 | $result .= ($all_str != '' ? $nbsp . '['.$all_str.']' . $nbsp . $nbsp : ''); |
49db257d |
360 | |
361 | /* If the resulting string is blank, return a non-breaking space. */ |
362 | if ($result == '') { |
551c7b53 |
363 | $result = $nbsp; |
49db257d |
364 | } |
365 | /* Return our final magical compact paginator string. */ |
366 | return ($result); |
367 | } |
8409c786 |
368 | |
369 | |