add space to improve display
[squirrelmail.git] / functions / template / paginator_util.php
1 <?php
2
3 /**
4 * paginator_util.php
5 *
6 * The following functions are utility functions for templates. Do not
7 * echo output in these functions.
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
16 /** Load forms functions, needed for addsubmit(). */
17 include_once(SM_PATH . 'functions/forms.php');
18
19
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);
30 return create_hyperlink("$php_self?startMessage=$start_msg&amp;mailbox=$box", $text);
31 }
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 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 *
46 * @return string $result paginate string with links to pages
47 *
48 */
49 function get_compact_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll, $javascript_on, $page_selector) {
50
51 /* This will be used as a space. */
52 global $oTemplate, $nbsp;
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
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. */
72 /* This will be used as a seperator. */
73 $sep = '|';
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 {
106 $pg_str = create_hyperlink("$php_self?showall=0&amp;startMessage=1&amp;mailbox=$box", _("Paginate"));
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. */
118 $all_str = create_hyperlink("$php_self?showall=1&amp;startMessage=1&amp;mailbox=$box", _("Show All"));
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) {
129 $options = array();
130 for ($p = 0; $p < $tot_pgs; $p++) {
131 $options[(($p*$iLimit)+1) . '_' . $box] = ($p+1) . "/$tot_pgs";
132 }
133 $result .= $nbsp . addSelect('startMessage_' . $display_iterations,
134 $options,
135 ((($cur_pg-1)*$iLimit)+1),
136 TRUE,
137 ($javascript_on ? array('onchange' => 'JavaScript:SubmitOnSelect(this, \'' . $pg_url . '&startMessage=\')') : array()));
138
139 if ($javascript_on) {
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">'
146 // . addSubmit(_("Go"), 'paginator_submit_' . $display_iterations)
147 // . '</noscript>';
148 } else {
149 $result .= addSubmit(_("Go"), 'paginator_submit_' . $display_iterations);
150 }
151 }
152 }
153
154 $result .= ($pg_str != '' ? '['.$pg_str.']' . $nbsp : '');
155 $result .= ($all_str != '' ? $nbsp . '['.$all_str.']' . $nbsp . $nbsp : '');
156
157 /* If the resulting string is blank, return a non-breaking space. */
158 if ($result == '') {
159 $result = '&nbsp;';
160 }
161 /* Return our final magical paginator string. */
162 return ($result);
163 }
164
165
166 /**
167 * This function computes the paginator string.
168 *
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 *
178 * @return string $result paginate string with links to pages
179 *
180 */
181 function get_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll,$page_selector, $page_selector_max) {
182
183 /* This will be used as a space. */
184 global $oTemplate, $nbsp;
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. */
196 /* This will be used as a seperator. */
197 $sep = '|';
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)) {
296 $pg_str .= "...$nbsp";
297 } else {
298 for ($pg = 1; $pg <= $q1_pgs; ++$pg) {
299 $start = (($pg-1) * $iLimit) + 1;
300 $pg_str .= get_paginator_link($box, $start, $pg) . $nbsp;
301 }
302 if ($cur_pg - $q2_pgs - $q1_pgs > 1) {
303 $pg_str .= "...$nbsp";
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;
310 $pg_str .= get_paginator_link($box, $start, $pg) . $nbsp;
311 }
312
313 /* Now print the current page. */
314 $pg_str .= $cur_pg . $nbsp;
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;
319 $pg_str .= get_paginator_link($box, $start, $pg) . $nbsp;
320 }
321
322 /* And last, print the forth quarter page links. */
323 if (($q4_pgs == 0) && ($cur_pg < $tot_pgs)) {
324 $pg_str .= "...$nbsp";
325 } else {
326 if (($tot_pgs - $q4_pgs) > ($cur_pg + $q3_pgs)) {
327 $pg_str .= "...$nbsp";
328 }
329 for ($pg = $tot_pgs - $q4_pgs + 1; $pg <= $tot_pgs; ++$pg) {
330 $start = (($pg-1) * $iLimit) + 1;
331 $pg_str .= get_paginator_link($box, $start,$pg) . $nbsp;
332 }
333 }
334
335 $last_grp = (($tot_pgs - 1) * $iLimit) + 1;
336 }
337 } else {
338 $pg_str = create_hyperlink("$php_self?showall=0&amp;startMessage=1&amp;mailbox=$box", _("Paginate"));
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. */
350 $all_str = create_hyperlink("$php_self?showall=1&amp;startMessage=1&amp;mailbox=$box", _("Show All"));
351
352 $result .= '[';
353 $result .= ($prv_str != '' ? $prv_str . $nbsp . $sep . $nbsp : '');
354 $result .= ($nxt_str != '' ? $nxt_str : '');
355 $result .= ']' . $nbsp ;
356 }
357
358 $result .= ($pg_str != '' ? $nbsp . '['.$nbsp.$pg_str.']' . $nbsp : '');
359 $result .= ($all_str != '' ? $nbsp . '['.$all_str.']' . $nbsp . $nbsp : '');
360
361 /* If the resulting string is blank, return a non-breaking space. */
362 if ($result == '') {
363 $result = $nbsp;
364 }
365 /* Return our final magical compact paginator string. */
366 return ($result);
367 }
368
369