paginator_util.php 14 KB

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