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