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