PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/rel-1_5_1/squirrelmail/templates/util_paginator.php

#
PHP | 351 lines | 198 code | 57 blank | 96 comment | 72 complexity | c202f8fed484cbb1e7f4293adf1b7deb MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0
  1. <?php
  2. /**
  3. * Template logic
  4. *
  5. * The following functions are utility functions for this template. Do not
  6. * echo output in those functions. Output is generated above this comment block
  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: util_paginator.php 10559 2006-01-23 18:46:41Z tokul $
  11. * @package squirrelmail
  12. */
  13. /**
  14. * Generate a paginator link.
  15. *
  16. * @param string $box Mailbox name
  17. * @param integer $start_msg Message Offset
  18. * @param string $text text used for paginator link
  19. * @return string
  20. */
  21. function get_paginator_link($box, $start_msg, $text) {
  22. sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER);
  23. $result = "<a href=\"$php_self?startMessage=$start_msg&amp;mailbox=$box\" "
  24. . ">$text</a>";
  25. return ($result);
  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 show all messages at once (non paginate mode)
  35. * @return string $result paginate string with links to pages
  36. */
  37. function get_compact_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll, $javascript_on, $page_selector) {
  38. sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER);
  39. /* Initialize paginator string chunks. */
  40. $prv_str = '';
  41. $nxt_str = '';
  42. $pg_str = '';
  43. $all_str = '';
  44. $box = urlencode($box);
  45. /* Create simple strings that will be creating the paginator. */
  46. $spc = '&nbsp;'; /* This will be used as a space. */
  47. $sep = '|'; /* This will be used as a seperator. */
  48. /* Make sure that our start message number is not too big. */
  49. $iOffset = min($iOffset, $iTotal);
  50. /* Compute the starting message of the previous and next page group. */
  51. $next_grp = $iOffset + $iLimit;
  52. $prev_grp = $iOffset - $iLimit;
  53. if (!$bShowAll) {
  54. /* Compute the basic previous and next strings. */
  55. if (($next_grp <= $iTotal) && ($prev_grp >= 0)) {
  56. $prv_str = get_paginator_link($box, $prev_grp, '<');
  57. $nxt_str = get_paginator_link($box, $next_grp, '>');
  58. } else if (($next_grp > $iTotal) && ($prev_grp >= 0)) {
  59. $prv_str = get_paginator_link($box, $prev_grp, '<');
  60. $nxt_str = '>';
  61. } else if (($next_grp <= $iTotal) && ($prev_grp < 0)) {
  62. $prv_str = '<';
  63. $nxt_str = get_paginator_link($box, $next_grp, '>');
  64. }
  65. /* Page selector block. Following code computes page links. */
  66. if ($iLimit != 0 && $page_selector && ($iTotal > $iLimit)) {
  67. /* Most importantly, what is the current page!!! */
  68. $cur_pg = intval($iOffset / $iLimit) + 1;
  69. /* Compute total # of pages and # of paginator page links. */
  70. $tot_pgs = ceil($iTotal / $iLimit); /* Total number of Pages */
  71. $last_grp = (($tot_pgs - 1) * $iLimit) + 1;
  72. }
  73. } else {
  74. $pg_str = "<a href=\"$php_self?showall=0"
  75. . "&amp;startMessage=1&amp;mailbox=$box\" "
  76. . ">" ._("Paginate") . '</a>';
  77. }
  78. /* Put all the pieces of the paginator string together. */
  79. /**
  80. * Hairy code... But let's leave it like it is since I am not certain
  81. * a different approach would be any easier to read. ;)
  82. */
  83. $result = '';
  84. if ( $prv_str || $nxt_str ) {
  85. /* Compute the 'show all' string. */
  86. $all_str = "<a href=\"$php_self?showall=1"
  87. . "&amp;startMessage=1&amp;mailbox=$box\" "
  88. . ">" . _("Show All") . '</a>';
  89. $result .= '[' . get_paginator_link($box, 1, '<<') . ']';
  90. $result .= '[' . $prv_str . ']';
  91. $pg_url = $php_self . '?mailbox=' . $box;
  92. $result .= '[' . $nxt_str . ']';
  93. $result .= '[' . get_paginator_link($box, $last_grp, '>>') . ']';
  94. if ($page_selector) {
  95. $result .= $spc . '<select name="startMessage"';
  96. if ($javascript_on) {
  97. $result .= ' onchange="JavaScript:SubmitOnSelect'
  98. . '(this, \'' . $pg_url . '&startMessage=\')"';
  99. }
  100. $result .='>';
  101. for ($p = 0; $p < $tot_pgs; $p++) {
  102. $result .= '<option ';
  103. if (($p+1) == $cur_pg) $result .= 'selected ';
  104. $result .= 'value="' . (($p*$iLimit)+1) . '">'
  105. . ($p+1) . "/$tot_pgs" . '</option>';
  106. }
  107. $result .= '</select>';
  108. if ($javascript_on) {
  109. $result .= '<noscript language="JavaScript">'
  110. . addSubmit(_("Go"))
  111. . '</noscript>';
  112. } else {
  113. $result .= addSubmit(_("Go"));
  114. }
  115. }
  116. }
  117. $result .= ($pg_str != '' ? '['.$pg_str.']' . $spc : '');
  118. $result .= ($all_str != '' ? $spc . '['.$all_str.']' . $spc . $spc : '');
  119. /* If the resulting string is blank, return a non-breaking space. */
  120. if ($result == '') {
  121. $result = '&nbsp;';
  122. }
  123. /* Return our final magical paginator string. */
  124. return ($result);
  125. }
  126. /**
  127. * This function computes the paginator string.
  128. *
  129. * @param string $box mailbox name
  130. * @param integer $iOffset offset in total number of messages
  131. * @param integer $iTotal total number of messages
  132. * @param integer $iLimit maximum number of messages to show on a page
  133. * @param bool $bShowAll show all messages at once (non paginate mode)
  134. * @return string $result paginate string with links to pages
  135. */
  136. function get_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll,$page_selector, $page_selector_max) {
  137. sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER);
  138. /* Initialize paginator string chunks. */
  139. $prv_str = '';
  140. $nxt_str = '';
  141. $pg_str = '';
  142. $all_str = '';
  143. $box = urlencode($box);
  144. /* Create simple strings that will be creating the paginator. */
  145. $spc = '&nbsp;'; /* This will be used as a space. */
  146. $sep = '|'; /* This will be used as a seperator. */
  147. /* Make sure that our start message number is not too big. */
  148. $iOffset = min($iOffset, $iTotal);
  149. /* Compute the starting message of the previous and next page group. */
  150. $next_grp = $iOffset + $iLimit;
  151. $prev_grp = $iOffset - $iLimit;
  152. if (!$bShowAll) {
  153. /* Compute the basic previous and next strings. */
  154. if (($next_grp <= $iTotal) && ($prev_grp >= 0)) {
  155. $prv_str = get_paginator_link($box, $prev_grp, _("Previous"));
  156. $nxt_str = get_paginator_link($box, $next_grp, _("Next"));
  157. } else if (($next_grp > $iTotal) && ($prev_grp >= 0)) {
  158. $prv_str = get_paginator_link($box, $prev_grp, _("Previous"));
  159. $nxt_str = _("Next");
  160. } else if (($next_grp <= $iTotal) && ($prev_grp < 0)) {
  161. $prv_str = _("Previous");
  162. $nxt_str = get_paginator_link($box, $next_grp, _("Next"));
  163. }
  164. /* Page selector block. Following code computes page links. */
  165. if ($iLimit != 0 && $page_selector && ($iTotal > $iLimit)) {
  166. /* Most importantly, what is the current page!!! */
  167. $cur_pg = intval($iOffset / $iLimit) + 1;
  168. /* Compute total # of pages and # of paginator page links. */
  169. $tot_pgs = ceil($iTotal / $iLimit); /* Total number of Pages */
  170. $vis_pgs = min($page_selector_max, $tot_pgs - 1); /* Visible Pages */
  171. /* Compute the size of the four quarters of the page links. */
  172. /* If we can, just show all the pages. */
  173. if (($tot_pgs - 1) <= $page_selector_max) {
  174. $q1_pgs = $cur_pg - 1;
  175. $q2_pgs = $q3_pgs = 0;
  176. $q4_pgs = $tot_pgs - $cur_pg;
  177. /* Otherwise, compute some magic to choose the four quarters. */
  178. } else {
  179. /*
  180. * Compute the magic base values. Added together,
  181. * these values will always equal to the $pag_pgs.
  182. * NOTE: These are DEFAULT values and do not take
  183. * the current page into account. That is below.
  184. */
  185. $q1_pgs = floor($vis_pgs/4);
  186. $q2_pgs = round($vis_pgs/4, 0);
  187. $q3_pgs = ceil($vis_pgs/4);
  188. $q4_pgs = round(($vis_pgs - $q2_pgs)/3, 0);
  189. /* Adjust if the first quarter contains the current page. */
  190. if (($cur_pg - $q1_pgs) < 1) {
  191. $extra_pgs = ($q1_pgs - ($cur_pg - 1)) + $q2_pgs;
  192. $q1_pgs = $cur_pg - 1;
  193. $q2_pgs = 0;
  194. $q3_pgs += ceil($extra_pgs / 2);
  195. $q4_pgs += floor($extra_pgs / 2);
  196. /* Adjust if the first and second quarters intersect. */
  197. } else if (($cur_pg - $q2_pgs - ceil($q2_pgs/3)) <= $q1_pgs) {
  198. $extra_pgs = $q2_pgs;
  199. $extra_pgs -= ceil(($cur_pg - $q1_pgs - 1) * 3/4);
  200. $q2_pgs = ceil(($cur_pg - $q1_pgs - 1) * 3/4);
  201. $q3_pgs += ceil($extra_pgs / 2);
  202. $q4_pgs += floor($extra_pgs / 2);
  203. /* Adjust if the fourth quarter contains the current page. */
  204. } else if (($cur_pg + $q4_pgs) >= $tot_pgs) {
  205. $extra_pgs = ($q4_pgs - ($tot_pgs - $cur_pg)) + $q3_pgs;
  206. $q3_pgs = 0;
  207. $q4_pgs = $tot_pgs - $cur_pg;
  208. $q1_pgs += floor($extra_pgs / 2);
  209. $q2_pgs += ceil($extra_pgs / 2);
  210. /* Adjust if the third and fourth quarter intersect. */
  211. } else if (($cur_pg + $q3_pgs + 1) >= ($tot_pgs - $q4_pgs + 1)) {
  212. $extra_pgs = $q3_pgs;
  213. $extra_pgs -= ceil(($tot_pgs - $cur_pg - $q4_pgs) * 3/4);
  214. $q3_pgs = ceil(($tot_pgs - $cur_pg - $q4_pgs) * 3/4);
  215. $q1_pgs += floor($extra_pgs / 2);
  216. $q2_pgs += ceil($extra_pgs / 2);
  217. }
  218. }
  219. /*
  220. * I am leaving this debug code here, commented out, because
  221. * it is a really nice way to see what the above code is doing.
  222. * echo "qts = $q1_pgs/$q2_pgs/$q3_pgs/$q4_pgs = "
  223. * . ($q1_pgs + $q2_pgs + $q3_pgs + $q4_pgs) . '<br />';
  224. */
  225. /* Print out the page links from the compute page quarters. */
  226. /* Start with the first quarter. */
  227. if (($q1_pgs == 0) && ($cur_pg > 1)) {
  228. $pg_str .= "...$spc";
  229. } else {
  230. for ($pg = 1; $pg <= $q1_pgs; ++$pg) {
  231. $start = (($pg-1) * $iLimit) + 1;
  232. $pg_str .= get_paginator_link($box, $start, $pg) . $spc;
  233. }
  234. if ($cur_pg - $q2_pgs - $q1_pgs > 1) {
  235. $pg_str .= "...$spc";
  236. }
  237. }
  238. /* Continue with the second quarter. */
  239. for ($pg = $cur_pg - $q2_pgs; $pg < $cur_pg; ++$pg) {
  240. $start = (($pg-1) * $iLimit) + 1;
  241. $pg_str .= get_paginator_link($box, $start, $pg) . $spc;
  242. }
  243. /* Now print the current page. */
  244. $pg_str .= $cur_pg . $spc;
  245. /* Next comes the third quarter. */
  246. for ($pg = $cur_pg + 1; $pg <= $cur_pg + $q3_pgs; ++$pg) {
  247. $start = (($pg-1) * $iLimit) + 1;
  248. $pg_str .= get_paginator_link($box, $start, $pg) . $spc;
  249. }
  250. /* And last, print the forth quarter page links. */
  251. if (($q4_pgs == 0) && ($cur_pg < $tot_pgs)) {
  252. $pg_str .= "...$spc";
  253. } else {
  254. if (($tot_pgs - $q4_pgs) > ($cur_pg + $q3_pgs)) {
  255. $pg_str .= "...$spc";
  256. }
  257. for ($pg = $tot_pgs - $q4_pgs + 1; $pg <= $tot_pgs; ++$pg) {
  258. $start = (($pg-1) * $iLimit) + 1;
  259. $pg_str .= get_paginator_link($box, $start,$pg) . $spc;
  260. }
  261. }
  262. $last_grp = (($tot_pgs - 1) * $iLimit) + 1;
  263. }
  264. } else {
  265. $pg_str = "<a href=\"$php_self?showall=0"
  266. . "&amp;startMessage=1&amp;mailbox=$box\" "
  267. . ">" ._("Paginate") . '</a>';
  268. }
  269. /* Put all the pieces of the paginator string together. */
  270. /**
  271. * Hairy code... But let's leave it like it is since I am not certain
  272. * a different approach would be any easier to read. ;)
  273. */
  274. $result = '';
  275. if ( $prv_str || $nxt_str ) {
  276. /* Compute the 'show all' string. */
  277. $all_str = "<a href=\"$php_self?showall=1"
  278. . "&amp;startMessage=1&amp;mailbox=$box\" "
  279. . ">" . _("Show All") . '</a>';
  280. $result .= '[';
  281. $result .= ($prv_str != '' ? $prv_str . $spc . $sep . $spc : '');
  282. $result .= ($nxt_str != '' ? $nxt_str : '');
  283. $result .= ']' . $spc ;
  284. }
  285. $result .= ($pg_str != '' ? $spc . '['.$spc.$pg_str.']' . $spc : '');
  286. $result .= ($all_str != '' ? $spc . '['.$all_str.']' . $spc . $spc : '');
  287. /* If the resulting string is blank, return a non-breaking space. */
  288. if ($result == '') {
  289. $result = '&nbsp;';
  290. }
  291. /* Return our final magical compact paginator string. */
  292. return ($result);
  293. }
  294. ?>