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