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