PageRenderTime 54ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/squirrelmail/functions/template/paginator_util.php

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