Move another template util file, strip HTML out of paginator utility fxns.
[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 global $oTemplate;
52 sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER);
53
54 /* Initialize paginator string chunks. */
55 $prv_str = '';
56 $nxt_str = '';
57 $pg_str = '';
58 $all_str = '';
59
60 $box = urlencode($box);
61
62 /* Create simple strings that will be creating the paginator. */
63 /* This will be used as a space. */
64 $spc = $oTemplate->fetch('non_breaking_space.tpl');
65 /* This will be used as a seperator. */
66 $sep = '|';
67
68 /* Make sure that our start message number is not too big. */
69 $iOffset = min($iOffset, $iTotal);
70
71 /* Compute the starting message of the previous and next page group. */
72 $next_grp = $iOffset + $iLimit;
73 $prev_grp = $iOffset - $iLimit;
74
75 if (!$bShowAll) {
76 /* Compute the basic previous and next strings. */
77 if (($next_grp <= $iTotal) && ($prev_grp >= 0)) {
78 $prv_str = get_paginator_link($box, $prev_grp, '<');
79 $nxt_str = get_paginator_link($box, $next_grp, '>');
80 } else if (($next_grp > $iTotal) && ($prev_grp >= 0)) {
81 $prv_str = get_paginator_link($box, $prev_grp, '<');
82 $nxt_str = '>';
83 } else if (($next_grp <= $iTotal) && ($prev_grp < 0)) {
84 $prv_str = '<';
85 $nxt_str = get_paginator_link($box, $next_grp, '>');
86 }
87
88 /* Page selector block. Following code computes page links. */
89 if ($iLimit != 0 && $page_selector && ($iTotal > $iLimit)) {
90 /* Most importantly, what is the current page!!! */
91 $cur_pg = intval($iOffset / $iLimit) + 1;
92
93 /* Compute total # of pages and # of paginator page links. */
94 $tot_pgs = ceil($iTotal / $iLimit); /* Total number of Pages */
95
96 $last_grp = (($tot_pgs - 1) * $iLimit) + 1;
97 }
98 } else {
99 $pg_str = create_hyperlink("$php_self?showall=0&amp;startMessage=1&amp;mailbox=$box", _("Paginate"));
100 }
101
102 /* Put all the pieces of the paginator string together. */
103 /**
104 * Hairy code... But let's leave it like it is since I am not certain
105 * a different approach would be any easier to read. ;)
106 */
107 $result = '';
108 if ( $prv_str || $nxt_str ) {
109
110 /* Compute the 'show all' string. */
111 $all_str = create_hyperlink("$php_self?showall=1&amp;startMessage=1&amp;mailbox=$box", _("Show All"));
112
113 $result .= '[' . get_paginator_link($box, 1, '<<') . ']';
114 $result .= '[' . $prv_str . ']';
115
116 $pg_url = $php_self . '?mailbox=' . $box;
117
118 $result .= '[' . $nxt_str . ']';
119 $result .= '[' . get_paginator_link($box, $last_grp, '>>') . ']';
120
121 if ($page_selector) {
122 $options = array();
123 for ($p = 0; $p < $tot_pgs; $p++) {
124 $options[(($p*$iLimit)+1)] = ($p+1) . "/$tot_pgs";
125 }
126 $result .= $spc . addSelect('startMessage', $options,
127 ((($cur_pg-1)*$iLimit)+1),
128 TRUE,
129 ($javascript_on ? array('onchange' => 'JavaScript:SubmitOnSelect(this, \'' . $pg_url . '&startMessage=\')') : array()));
130
131 if ($javascript_on) {
132 //FIXME: What in the world? Two issues here: for one, $javascript_on is supposed
133 // to have already detected whether or not JavaScript is available and enabled.
134 // Secondly, we need to rid ourselves of any HTML output in the core. This
135 // is being removed (but left in case the original author points out why it
136 // should not be) and we'll trust $javascript_on to do the right thing.
137 // $result .= '<noscript language="JavaScript">'
138 // . addSubmit(_("Go"))
139 // . '</noscript>';
140 } else {
141 $result .= addSubmit(_("Go"));
142 }
143 }
144 }
145
146 $result .= ($pg_str != '' ? '['.$pg_str.']' . $spc : '');
147 $result .= ($all_str != '' ? $spc . '['.$all_str.']' . $spc . $spc : '');
148
149 /* If the resulting string is blank, return a non-breaking space. */
150 if ($result == '') {
151 $result = '&nbsp;';
152 }
153 /* Return our final magical paginator string. */
154 return ($result);
155 }
156
157
158 /**
159 * This function computes the paginator string.
160 *
161 * @param string $box mailbox name
162 * @param integer $iOffset offset in total number of messages
163 * @param integer $iTotal total number of messages
164 * @param integer $iLimit maximum number of messages to show on a page
165 * @param bool $bShowAll whether or not to show all messages at once
166 * ("show all" == non paginate mode)
167 * @param bool $page_selector whether or not to show the page selection widget
168 * @param integer $page_selector_max maximum number of pages to show on the screen
169 *
170 * @return string $result paginate string with links to pages
171 *
172 */
173 function get_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll,$page_selector, $page_selector_max) {
174
175 global $oTemplate;
176 sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER);
177
178 /* Initialize paginator string chunks. */
179 $prv_str = '';
180 $nxt_str = '';
181 $pg_str = '';
182 $all_str = '';
183
184 $box = urlencode($box);
185
186 /* Create simple strings that will be creating the paginator. */
187 /* This will be used as a space. */
188 $spc = $oTemplate->fetch('non_breaking_space.tpl');
189 /* This will be used as a seperator. */
190 $sep = '|';
191
192 /* Make sure that our start message number is not too big. */
193 $iOffset = min($iOffset, $iTotal);
194
195 /* Compute the starting message of the previous and next page group. */
196 $next_grp = $iOffset + $iLimit;
197 $prev_grp = $iOffset - $iLimit;
198
199 if (!$bShowAll) {
200 /* Compute the basic previous and next strings. */
201
202 if (($next_grp <= $iTotal) && ($prev_grp >= 0)) {
203 $prv_str = get_paginator_link($box, $prev_grp, _("Previous"));
204 $nxt_str = get_paginator_link($box, $next_grp, _("Next"));
205 } else if (($next_grp > $iTotal) && ($prev_grp >= 0)) {
206 $prv_str = get_paginator_link($box, $prev_grp, _("Previous"));
207 $nxt_str = _("Next");
208 } else if (($next_grp <= $iTotal) && ($prev_grp < 0)) {
209 $prv_str = _("Previous");
210 $nxt_str = get_paginator_link($box, $next_grp, _("Next"));
211 }
212
213 /* Page selector block. Following code computes page links. */
214 if ($iLimit != 0 && $page_selector && ($iTotal > $iLimit)) {
215 /* Most importantly, what is the current page!!! */
216 $cur_pg = intval($iOffset / $iLimit) + 1;
217
218 /* Compute total # of pages and # of paginator page links. */
219 $tot_pgs = ceil($iTotal / $iLimit); /* Total number of Pages */
220
221 $vis_pgs = min($page_selector_max, $tot_pgs - 1); /* Visible Pages */
222
223 /* Compute the size of the four quarters of the page links. */
224
225 /* If we can, just show all the pages. */
226 if (($tot_pgs - 1) <= $page_selector_max) {
227 $q1_pgs = $cur_pg - 1;
228 $q2_pgs = $q3_pgs = 0;
229 $q4_pgs = $tot_pgs - $cur_pg;
230
231 /* Otherwise, compute some magic to choose the four quarters. */
232 } else {
233 /*
234 * Compute the magic base values. Added together,
235 * these values will always equal to the $pag_pgs.
236 * NOTE: These are DEFAULT values and do not take
237 * the current page into account. That is below.
238 */
239 $q1_pgs = floor($vis_pgs/4);
240 $q2_pgs = round($vis_pgs/4, 0);
241 $q3_pgs = ceil($vis_pgs/4);
242 $q4_pgs = round(($vis_pgs - $q2_pgs)/3, 0);
243
244 /* Adjust if the first quarter contains the current page. */
245 if (($cur_pg - $q1_pgs) < 1) {
246 $extra_pgs = ($q1_pgs - ($cur_pg - 1)) + $q2_pgs;
247 $q1_pgs = $cur_pg - 1;
248 $q2_pgs = 0;
249 $q3_pgs += ceil($extra_pgs / 2);
250 $q4_pgs += floor($extra_pgs / 2);
251
252 /* Adjust if the first and second quarters intersect. */
253 } else if (($cur_pg - $q2_pgs - ceil($q2_pgs/3)) <= $q1_pgs) {
254 $extra_pgs = $q2_pgs;
255 $extra_pgs -= ceil(($cur_pg - $q1_pgs - 1) * 3/4);
256 $q2_pgs = ceil(($cur_pg - $q1_pgs - 1) * 3/4);
257 $q3_pgs += ceil($extra_pgs / 2);
258 $q4_pgs += floor($extra_pgs / 2);
259
260 /* Adjust if the fourth quarter contains the current page. */
261 } else if (($cur_pg + $q4_pgs) >= $tot_pgs) {
262 $extra_pgs = ($q4_pgs - ($tot_pgs - $cur_pg)) + $q3_pgs;
263 $q3_pgs = 0;
264 $q4_pgs = $tot_pgs - $cur_pg;
265 $q1_pgs += floor($extra_pgs / 2);
266 $q2_pgs += ceil($extra_pgs / 2);
267
268 /* Adjust if the third and fourth quarter intersect. */
269 } else if (($cur_pg + $q3_pgs + 1) >= ($tot_pgs - $q4_pgs + 1)) {
270 $extra_pgs = $q3_pgs;
271 $extra_pgs -= ceil(($tot_pgs - $cur_pg - $q4_pgs) * 3/4);
272 $q3_pgs = ceil(($tot_pgs - $cur_pg - $q4_pgs) * 3/4);
273 $q1_pgs += floor($extra_pgs / 2);
274 $q2_pgs += ceil($extra_pgs / 2);
275 }
276 }
277
278 /*
279 * I am leaving this debug code here, commented out, because
280 * it is a really nice way to see what the above code is doing.
281 * echo "qts = $q1_pgs/$q2_pgs/$q3_pgs/$q4_pgs = "
282 * . ($q1_pgs + $q2_pgs + $q3_pgs + $q4_pgs) . '<br />';
283 */
284
285 /* Print out the page links from the compute page quarters. */
286
287 /* Start with the first quarter. */
288 if (($q1_pgs == 0) && ($cur_pg > 1)) {
289 $pg_str .= "...$spc";
290 } else {
291 for ($pg = 1; $pg <= $q1_pgs; ++$pg) {
292 $start = (($pg-1) * $iLimit) + 1;
293 $pg_str .= get_paginator_link($box, $start, $pg) . $spc;
294 }
295 if ($cur_pg - $q2_pgs - $q1_pgs > 1) {
296 $pg_str .= "...$spc";
297 }
298 }
299
300 /* Continue with the second quarter. */
301 for ($pg = $cur_pg - $q2_pgs; $pg < $cur_pg; ++$pg) {
302 $start = (($pg-1) * $iLimit) + 1;
303 $pg_str .= get_paginator_link($box, $start, $pg) . $spc;
304 }
305
306 /* Now print the current page. */
307 $pg_str .= $cur_pg . $spc;
308
309 /* Next comes the third quarter. */
310 for ($pg = $cur_pg + 1; $pg <= $cur_pg + $q3_pgs; ++$pg) {
311 $start = (($pg-1) * $iLimit) + 1;
312 $pg_str .= get_paginator_link($box, $start, $pg) . $spc;
313 }
314
315 /* And last, print the forth quarter page links. */
316 if (($q4_pgs == 0) && ($cur_pg < $tot_pgs)) {
317 $pg_str .= "...$spc";
318 } else {
319 if (($tot_pgs - $q4_pgs) > ($cur_pg + $q3_pgs)) {
320 $pg_str .= "...$spc";
321 }
322 for ($pg = $tot_pgs - $q4_pgs + 1; $pg <= $tot_pgs; ++$pg) {
323 $start = (($pg-1) * $iLimit) + 1;
324 $pg_str .= get_paginator_link($box, $start,$pg) . $spc;
325 }
326 }
327
328 $last_grp = (($tot_pgs - 1) * $iLimit) + 1;
329 }
330 } else {
331 $pg_str = create_hyperlink("$php_self?showall=0&amp;startMessage=1&amp;mailbox=$box", _("Paginate"));
332 }
333
334 /* Put all the pieces of the paginator string together. */
335 /**
336 * Hairy code... But let's leave it like it is since I am not certain
337 * a different approach would be any easier to read. ;)
338 */
339 $result = '';
340 if ( $prv_str || $nxt_str ) {
341
342 /* Compute the 'show all' string. */
343 $all_str = create_hyperlink("$php_self?showall=1&amp;startMessage=1&amp;mailbox=$box", _("Show All"));
344
345 $result .= '[';
346 $result .= ($prv_str != '' ? $prv_str . $spc . $sep . $spc : '');
347 $result .= ($nxt_str != '' ? $nxt_str : '');
348 $result .= ']' . $spc ;
349 }
350
351 $result .= ($pg_str != '' ? $spc . '['.$spc.$pg_str.']' . $spc : '');
352 $result .= ($all_str != '' ? $spc . '['.$all_str.']' . $spc . $spc : '');
353
354 /* If the resulting string is blank, return a non-breaking space. */
355 if ($result == '') {
356 $result = $oTemplate->fetch('non_breaking_space.tpl');
357 }
358 /* Return our final magical compact paginator string. */
359 return ($result);
360 }
361
362