paginator_util.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. <?php
  2. /**
  3. * paginator_util.php
  4. *
  5. * The following functions are utility functions for templates. Do not
  6. * echo output in these functions.
  7. *
  8. * @copyright &copy; 2005-2006 The SquirrelMail Project Team
  9. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  10. * @version $Id$
  11. * @package squirrelmail
  12. */
  13. /** Load forms functions, needed for addsubmit(). */
  14. include_once(SM_PATH . 'functions/forms.php');
  15. /**
  16. * Generate a paginator link.
  17. *
  18. * @param string $box Mailbox name
  19. * @param integer $start_msg Message Offset
  20. * @param string $text The text used for paginator link
  21. * @param string $accesskey The access key for the link, if any
  22. * @return string
  23. */
  24. function get_paginator_link($box, $start_msg, $text, $accesskey='NONE') {
  25. sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER);
  26. return create_hyperlink("$php_self?startMessage=$start_msg&amp;mailbox=$box",
  27. $text, '', '', '', '', '',
  28. array('accesskey' => $accesskey));
  29. }
  30. /**
  31. * This function computes the comapact paginator string.
  32. *
  33. * @param string $box mailbox name
  34. * @param integer $iOffset offset in total number of messages
  35. * @param integer $iTotal total number of messages
  36. * @param integer $iLimit maximum number of messages to show on a page
  37. * @param bool $bShowAll whether or not to show all messages at once
  38. * ("show all" == non paginate mode)
  39. * @param bool $javascript_on whether or not javascript is currently enabled
  40. * @param bool $page_selector whether or not to show the page selection widget
  41. *
  42. * @return string $result paginate string with links to pages
  43. *
  44. */
  45. function get_compact_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll, $javascript_on, $page_selector) {
  46. static $accesskeys_constructed = FALSE;
  47. /* This will be used as a space. */
  48. global $oTemplate, $nbsp;
  49. // keeps count of how many times
  50. // the paginator is used, avoids
  51. // duplicate naming of <select>
  52. // and GO button
  53. static $display_iterations = 0;
  54. $display_iterations++;
  55. sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER);
  56. /* Initialize paginator string chunks. */
  57. $prv_str = '';
  58. $nxt_str = '';
  59. $pg_str = '';
  60. $all_str = '';
  61. $box = urlencode($box);
  62. /* Create simple strings that will be creating the paginator. */
  63. /* This will be used as a seperator. */
  64. $sep = '|';
  65. /* Make sure that our start message number is not too big. */
  66. $iOffset = min($iOffset, $iTotal);
  67. /* Compute the starting message of the previous and next page group. */
  68. $next_grp = $iOffset + $iLimit;
  69. $prev_grp = $iOffset - $iLimit;
  70. if (!$bShowAll) {
  71. /* Compute the basic previous and next strings. */
  72. global $accesskey_mailbox_previous, $accesskey_mailbox_next;
  73. if (($next_grp <= $iTotal) && ($prev_grp >= 0)) {
  74. $prv_str = get_paginator_link($box, $prev_grp, '<',
  75. ($accesskeys_constructed
  76. ? '' : $accesskey_mailbox_previous));
  77. $nxt_str = get_paginator_link($box, $next_grp, '>',
  78. ($accesskeys_constructed
  79. ? '' : $accesskey_mailbox_next));
  80. } else if (($next_grp > $iTotal) && ($prev_grp >= 0)) {
  81. $prv_str = get_paginator_link($box, $prev_grp, '<',
  82. ($accesskeys_constructed
  83. ? '' : $accesskey_mailbox_previous));
  84. $nxt_str = '>';
  85. } else if (($next_grp <= $iTotal) && ($prev_grp < 0)) {
  86. $prv_str = '<';
  87. $nxt_str = get_paginator_link($box, $next_grp, '>',
  88. ($accesskeys_constructed
  89. ? '' : $accesskey_mailbox_next));
  90. }
  91. /* Page selector block. Following code computes page links. */
  92. if ($iLimit != 0 && $page_selector && ($iTotal > $iLimit)) {
  93. /* Most importantly, what is the current page!!! */
  94. $cur_pg = intval($iOffset / $iLimit) + 1;
  95. /* Compute total # of pages and # of paginator page links. */
  96. $tot_pgs = ceil($iTotal / $iLimit); /* Total number of Pages */
  97. $last_grp = (($tot_pgs - 1) * $iLimit) + 1;
  98. }
  99. } else {
  100. global $accesskey_mailbox_all_paginate;
  101. $pg_str = create_hyperlink("$php_self?showall=0&amp;startMessage=1&amp;mailbox=$box", _("Paginate"), '', '', '', '', '', array('accesskey' => ($accesskeys_constructed ? 'NONE' : $accesskey_mailbox_all_paginate)));
  102. }
  103. /* Put all the pieces of the paginator string together. */
  104. /**
  105. * Hairy code... But let's leave it like it is since I am not certain
  106. * a different approach would be any easier to read. ;)
  107. */
  108. $result = '';
  109. if ( $prv_str || $nxt_str ) {
  110. /* Compute the 'show all' string. */
  111. global $accesskey_mailbox_all_paginate;
  112. $all_str = create_hyperlink("$php_self?showall=1&amp;startMessage=1&amp;mailbox=$box", _("Show All"), '', '', '', '', '', array('accesskey' => ($accesskeys_constructed ? 'NONE' : $accesskey_mailbox_all_paginate)));
  113. $result .= '[' . get_paginator_link($box, 1, '<<') . ']';
  114. $result .= '[' . $prv_str . ']';
  115. $pg_url = $php_self . '?mailbox=' . $box;
  116. $result .= '[' . $nxt_str . ']';
  117. $result .= '[' . get_paginator_link($box, $last_grp, '>>') . ']';
  118. if ($page_selector) {
  119. $options = array();
  120. for ($p = 0; $p < $tot_pgs; $p++) {
  121. $options[(($p*$iLimit)+1) . '_' . $box] = ($p+1) . "/$tot_pgs";
  122. }
  123. $result .= $nbsp . addSelect('startMessage_' . $display_iterations,
  124. $options,
  125. ((($cur_pg-1)*$iLimit)+1),
  126. TRUE,
  127. ($javascript_on ? array('onchange' => 'JavaScript:SubmitOnSelect(this, \'' . $pg_url . '&startMessage=\')') : array()));
  128. if ($javascript_on) {
  129. //FIXME: What in the world? Two issues here: for one, $javascript_on is supposed
  130. // to have already detected whether or not JavaScript is available and enabled.
  131. // Secondly, we need to rid ourselves of any HTML output in the core. This
  132. // is being removed (but left in case the original author points out why it
  133. // should not be) and we'll trust $javascript_on to do the right thing.
  134. // $result .= '<noscript language="JavaScript">'
  135. // . addSubmit(_("Go"), 'paginator_submit_' . $display_iterations)
  136. // . '</noscript>';
  137. } else {
  138. $result .= addSubmit(_("Go"), 'paginator_submit_' . $display_iterations);
  139. }
  140. }
  141. }
  142. $result .= ($pg_str != '' ? '['.$pg_str.']' . $nbsp : '');
  143. $result .= ($all_str != '' ? $nbsp . '['.$all_str.']' . $nbsp . $nbsp : '');
  144. /* If the resulting string is blank, return a non-breaking space. */
  145. if ($result == '') {
  146. $result = '&nbsp;';
  147. }
  148. $accesskeys_constructed = TRUE;
  149. /* Return our final magical paginator string. */
  150. return ($result);
  151. }
  152. /**
  153. * This function computes the paginator string.
  154. *
  155. * @param string $box mailbox name
  156. * @param integer $iOffset offset in total number of messages
  157. * @param integer $iTotal total number of messages
  158. * @param integer $iLimit maximum number of messages to show on a page
  159. * @param bool $bShowAll whether or not to show all messages at once
  160. * ("show all" == non paginate mode)
  161. * @param bool $page_selector whether or not to show the page selection widget
  162. * @param integer $page_selector_max maximum number of pages to show on the screen
  163. *
  164. * @return string $result paginate string with links to pages
  165. *
  166. */
  167. function get_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll,$page_selector, $page_selector_max) {
  168. static $accesskeys_constructed = FALSE;
  169. /* This will be used as a space. */
  170. global $oTemplate, $nbsp;
  171. sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER);
  172. /* Initialize paginator string chunks. */
  173. $prv_str = '';
  174. $nxt_str = '';
  175. $pg_str = '';
  176. $all_str = '';
  177. $box = urlencode($box);
  178. /* Create simple strings that will be creating the paginator. */
  179. /* This will be used as a seperator. */
  180. $sep = '|';
  181. /* Make sure that our start message number is not too big. */
  182. $iOffset = min($iOffset, $iTotal);
  183. /* Compute the starting message of the previous and next page group. */
  184. $next_grp = $iOffset + $iLimit;
  185. $prev_grp = $iOffset - $iLimit;
  186. if (!$bShowAll) {
  187. /* Compute the basic previous and next strings. */
  188. global $accesskey_mailbox_previous, $accesskey_mailbox_next;
  189. if (($next_grp <= $iTotal) && ($prev_grp >= 0)) {
  190. $prv_str = get_paginator_link($box, $prev_grp, _("Previous"),
  191. ($accesskeys_constructed
  192. ? '' : $accesskey_mailbox_previous));
  193. $nxt_str = get_paginator_link($box, $next_grp, _("Next"),
  194. ($accesskeys_constructed
  195. ? '' : $accesskey_mailbox_next));
  196. } else if (($next_grp > $iTotal) && ($prev_grp >= 0)) {
  197. $prv_str = get_paginator_link($box, $prev_grp, _("Previous"),
  198. ($accesskeys_constructed
  199. ? '' : $accesskey_mailbox_previous));
  200. $nxt_str = _("Next");
  201. } else if (($next_grp <= $iTotal) && ($prev_grp < 0)) {
  202. $prv_str = _("Previous");
  203. $nxt_str = get_paginator_link($box, $next_grp, _("Next"),
  204. ($accesskeys_constructed
  205. ? '' : $accesskey_mailbox_next));
  206. }
  207. /* Page selector block. Following code computes page links. */
  208. if ($iLimit != 0 && $page_selector && ($iTotal > $iLimit)) {
  209. /* Most importantly, what is the current page!!! */
  210. $cur_pg = intval($iOffset / $iLimit) + 1;
  211. /* Compute total # of pages and # of paginator page links. */
  212. $tot_pgs = ceil($iTotal / $iLimit); /* Total number of Pages */
  213. $vis_pgs = min($page_selector_max, $tot_pgs - 1); /* Visible Pages */
  214. /* Compute the size of the four quarters of the page links. */
  215. /* If we can, just show all the pages. */
  216. if (($tot_pgs - 1) <= $page_selector_max) {
  217. $q1_pgs = $cur_pg - 1;
  218. $q2_pgs = $q3_pgs = 0;
  219. $q4_pgs = $tot_pgs - $cur_pg;
  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. /* 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. /* Adjust if the first and second quarters intersect. */
  240. } else if (($cur_pg - $q2_pgs - ceil($q2_pgs/3)) <= $q1_pgs) {
  241. $extra_pgs = $q2_pgs;
  242. $extra_pgs -= ceil(($cur_pg - $q1_pgs - 1) * 3/4);
  243. $q2_pgs = ceil(($cur_pg - $q1_pgs - 1) * 3/4);
  244. $q3_pgs += ceil($extra_pgs / 2);
  245. $q4_pgs += floor($extra_pgs / 2);
  246. /* Adjust if the fourth quarter contains the current page. */
  247. } else if (($cur_pg + $q4_pgs) >= $tot_pgs) {
  248. $extra_pgs = ($q4_pgs - ($tot_pgs - $cur_pg)) + $q3_pgs;
  249. $q3_pgs = 0;
  250. $q4_pgs = $tot_pgs - $cur_pg;
  251. $q1_pgs += floor($extra_pgs / 2);
  252. $q2_pgs += ceil($extra_pgs / 2);
  253. /* Adjust if the third and fourth quarter intersect. */
  254. } else if (($cur_pg + $q3_pgs + 1) >= ($tot_pgs - $q4_pgs + 1)) {
  255. $extra_pgs = $q3_pgs;
  256. $extra_pgs -= ceil(($tot_pgs - $cur_pg - $q4_pgs) * 3/4);
  257. $q3_pgs = ceil(($tot_pgs - $cur_pg - $q4_pgs) * 3/4);
  258. $q1_pgs += floor($extra_pgs / 2);
  259. $q2_pgs += ceil($extra_pgs / 2);
  260. }
  261. }
  262. /*
  263. * I am leaving this debug code here, commented out, because
  264. * it is a really nice way to see what the above code is doing.
  265. * echo "qts = $q1_pgs/$q2_pgs/$q3_pgs/$q4_pgs = "
  266. * . ($q1_pgs + $q2_pgs + $q3_pgs + $q4_pgs) . '<br />';
  267. */
  268. /* Print out the page links from the compute page quarters. */
  269. /* Start with the first quarter. */
  270. if (($q1_pgs == 0) && ($cur_pg > 1)) {
  271. $pg_str .= "...$nbsp";
  272. } else {
  273. for ($pg = 1; $pg <= $q1_pgs; ++$pg) {
  274. $start = (($pg-1) * $iLimit) + 1;
  275. $pg_str .= get_paginator_link($box, $start, $pg) . $nbsp;
  276. }
  277. if ($cur_pg - $q2_pgs - $q1_pgs > 1) {
  278. $pg_str .= "...$nbsp";
  279. }
  280. }
  281. /* Continue with the second quarter. */
  282. for ($pg = $cur_pg - $q2_pgs; $pg < $cur_pg; ++$pg) {
  283. $start = (($pg-1) * $iLimit) + 1;
  284. $pg_str .= get_paginator_link($box, $start, $pg) . $nbsp;
  285. }
  286. /* Now print the current page. */
  287. $pg_str .= $cur_pg . $nbsp;
  288. /* Next comes the third quarter. */
  289. for ($pg = $cur_pg + 1; $pg <= $cur_pg + $q3_pgs; ++$pg) {
  290. $start = (($pg-1) * $iLimit) + 1;
  291. $pg_str .= get_paginator_link($box, $start, $pg) . $nbsp;
  292. }
  293. /* And last, print the forth quarter page links. */
  294. if (($q4_pgs == 0) && ($cur_pg < $tot_pgs)) {
  295. $pg_str .= "...$nbsp";
  296. } else {
  297. if (($tot_pgs - $q4_pgs) > ($cur_pg + $q3_pgs)) {
  298. $pg_str .= "...$nbsp";
  299. }
  300. for ($pg = $tot_pgs - $q4_pgs + 1; $pg <= $tot_pgs; ++$pg) {
  301. $start = (($pg-1) * $iLimit) + 1;
  302. $pg_str .= get_paginator_link($box, $start,$pg) . $nbsp;
  303. }
  304. }
  305. $last_grp = (($tot_pgs - 1) * $iLimit) + 1;
  306. }
  307. } else {
  308. global $accesskey_mailbox_all_paginate;
  309. $pg_str = create_hyperlink("$php_self?showall=0&amp;startMessage=1&amp;mailbox=$box", _("Paginate"), '', '', '', '', '', array('accesskey' => ($accesskeys_constructed ? 'NONE' : $accesskey_mailbox_all_paginate)));
  310. }
  311. /* Put all the pieces of the paginator string together. */
  312. /**
  313. * Hairy code... But let's leave it like it is since I am not certain
  314. * a different approach would be any easier to read. ;)
  315. */
  316. $result = '';
  317. if ( $prv_str || $nxt_str ) {
  318. /* Compute the 'show all' string. */
  319. global $accesskey_mailbox_all_paginate;
  320. $all_str = create_hyperlink("$php_self?showall=1&amp;startMessage=1&amp;mailbox=$box", _("Show All"), '', '', '', '', '', array('accesskey' => ($accesskeys_constructed ? 'NONE' : $accesskey_mailbox_all_paginate)));
  321. $result .= '[';
  322. $result .= ($prv_str != '' ? $prv_str . $nbsp . $sep . $nbsp : '');
  323. $result .= ($nxt_str != '' ? $nxt_str : '');
  324. $result .= ']' . $nbsp ;
  325. }
  326. $result .= ($pg_str != '' ? $nbsp . '['.$nbsp.$pg_str.']' . $nbsp : '');
  327. $result .= ($all_str != '' ? $nbsp . '['.$all_str.']' . $nbsp . $nbsp : '');
  328. /* If the resulting string is blank, return a non-breaking space. */
  329. if ($result == '') {
  330. $result = $nbsp;
  331. }
  332. $accesskeys_constructed = TRUE;
  333. /* Return our final magical compact paginator string. */
  334. return ($result);
  335. }