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