PageRenderTime 42ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/GSoC-config/squirrelmail/functions/template/paginator_util.php

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