pagination.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /**
  3. * Elgg pagination
  4. *
  5. * @package Elgg
  6. * @subpackage Core
  7. *
  8. * @uses int $vars['offset'] The offset in the list
  9. * @uses int $vars['limit'] Number of items per page
  10. * @uses int $vars['count'] Number of items in list
  11. * @uses string $vars['base_url'] Base URL to use in links
  12. * @uses string $vars['url_fragment'] URL fragment to add to links if not present in base_url (optional)
  13. * @uses string $vars['offset_key'] The string to use for offet in the URL
  14. */
  15. if (elgg_in_context('widget')) {
  16. // widgets do not show pagination
  17. return;
  18. }
  19. $count = (int) elgg_extract('count', $vars, 0);
  20. if (!$count) {
  21. return;
  22. }
  23. $offset = abs((int) elgg_extract('offset', $vars, 0));
  24. // because you can say $vars['limit'] = 0
  25. if (!$limit = (int) elgg_extract('limit', $vars, elgg_get_config('default_limit'))) {
  26. $limit = 10;
  27. }
  28. $offset_key = elgg_extract('offset_key', $vars, 'offset');
  29. $url_fragment = elgg_extract('url_fragment', $vars, '');
  30. // some views pass an empty string for base_url
  31. if (isset($vars['base_url']) && $vars['base_url']) {
  32. $base_url = $vars['base_url'];
  33. } else if (isset($vars['baseurl']) && $vars['baseurl']) {
  34. elgg_deprecated_notice("Use 'base_url' instead of 'baseurl' for the navigation/pagination view", 1.8);
  35. $base_url = $vars['baseurl'];
  36. } elseif (elgg_is_xhr() && !empty($_SERVER['HTTP_REFERER'])) {
  37. $base_url = $_SERVER['HTTP_REFERER'];
  38. } else {
  39. $base_url = current_page_url();
  40. }
  41. $base_url_has_fragment = preg_match('~#.~', $base_url);
  42. $get_href = function ($offset) use ($base_url, $base_url_has_fragment, $offset_key, $url_fragment) {
  43. $link = elgg_http_add_url_query_elements($base_url, array($offset_key => $offset));
  44. if (!$base_url_has_fragment && $offset) {
  45. $link .= "#$url_fragment";
  46. }
  47. return $link;
  48. };
  49. if ($count <= $limit && $offset == 0) {
  50. // no need for pagination
  51. return;
  52. }
  53. $total_pages = (int) ceil($count / $limit);
  54. $current_page = (int) ceil($offset / $limit) + 1;
  55. $pages = array();
  56. // determine starting page
  57. $start_page = max(min([$current_page - 2, $total_pages - 4]), 1);
  58. // add previous
  59. $prev_offset = $offset - $limit;
  60. if ($prev_offset < 1) {
  61. // don't include offset=0
  62. $prev_offset = null;
  63. }
  64. $pages['prev'] = [
  65. 'text' => elgg_echo('previous'),
  66. 'href' => $get_href($prev_offset),
  67. ];
  68. if ($current_page == 1) {
  69. $pages['prev']['disabled'] = true;
  70. }
  71. // add first page to be listed
  72. if (1 < $start_page) {
  73. $pages[1] = [];
  74. }
  75. // added dotted spacer
  76. if (1 < ($start_page - 2)) {
  77. $pages[] = ['text' => '...', 'disabled' => true];
  78. } elseif ($start_page == 3) {
  79. $pages[2] = [];
  80. }
  81. $max = 1;
  82. for ($page = $start_page; $page <= $total_pages; $page++) {
  83. if ($max > 5) {
  84. break;
  85. }
  86. $pages[$page] = [];
  87. $max++;
  88. }
  89. // added dotted spacer
  90. if ($total_pages > ($start_page + 6)) {
  91. $pages[] = ['text' => '...', 'disabled' => true];
  92. } elseif (($start_page + 5) == ($total_pages - 1)) {
  93. $pages[$total_pages - 1] = [];
  94. }
  95. // add last page to be listed
  96. if ($total_pages >= ($start_page + 5)) {
  97. $pages[$total_pages] = [];
  98. }
  99. // add next
  100. $next_offset = $offset + $limit;
  101. if ($next_offset >= $count) {
  102. $next_offset--;
  103. }
  104. $pages['next'] = [
  105. 'text' => elgg_echo('next'),
  106. 'href' => $get_href($next_offset),
  107. ];
  108. if ($current_page == $total_pages) {
  109. $pages['next']['disabled'] = true;
  110. }
  111. $list ="";
  112. foreach ($pages as $page_num => $page) {
  113. if ($page_num == $current_page) {
  114. $list .= elgg_format_element('li', ['class' => 'elgg-state-selected'], "<span>$page_num</span>");
  115. } else {
  116. $href = elgg_extract('href', $page);
  117. $text = elgg_extract('text', $page, $page_num);
  118. $disabled = elgg_extract('disabled', $page, false);
  119. if (!$href && !$disabled) {
  120. $page_offset = (($page_num - $current_page) * $limit) + $offset;
  121. if ($page_offset <= 0) {
  122. // don't include offset=0
  123. $page_offset = null;
  124. }
  125. $href = $get_href($page_offset);
  126. }
  127. if ($href && !$disabled) {
  128. $link = elgg_view('output/url', array(
  129. 'href' => $href,
  130. 'text' => $text,
  131. 'is_trusted' => true,
  132. ));
  133. } else {
  134. $link = elgg_format_element('span', [], $page['text']);
  135. }
  136. $element_options = array();
  137. if ($disabled) {
  138. $element_options['class'] = 'elgg-state-disabled';
  139. }
  140. $list .= elgg_format_element('li', $element_options, $link);
  141. }
  142. }
  143. echo elgg_format_element('ul', ['class' => 'elgg-pagination'], $list);