paginator_util.php 14 KB

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