Bootstrap.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: zhangyajun <448901948@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace addons\cms\library;
  12. use think\Paginator;
  13. class Bootstrap extends Paginator
  14. {
  15. /**
  16. * 上一页按钮
  17. * @param string $text
  18. * @return string
  19. */
  20. protected function getPreviousButton($text = "&laquo;")
  21. {
  22. if ($this->currentPage() <= 1) {
  23. return $this->getDisabledTextWrapper($text);
  24. }
  25. $url = $this->url(
  26. $this->currentPage() - 1
  27. );
  28. return $this->getPageLinkWrapper($url, $text);
  29. }
  30. /**
  31. * 下一页按钮
  32. * @param string $text
  33. * @return string
  34. */
  35. protected function getNextButton($text = '&raquo;')
  36. {
  37. if (!$this->hasMore) {
  38. return $this->getDisabledTextWrapper($text);
  39. }
  40. $url = $this->url($this->currentPage() + 1);
  41. return $this->getPageLinkWrapper($url, $text);
  42. }
  43. /**
  44. * 页码按钮
  45. * @return string
  46. */
  47. protected function getLinks()
  48. {
  49. if ($this->simple) {
  50. return '';
  51. }
  52. $block = [
  53. 'first' => null,
  54. 'slider' => null,
  55. 'last' => null
  56. ];
  57. $side = 3;
  58. $window = $side * 2;
  59. if ($this->lastPage < $window + 6) {
  60. $block['first'] = $this->getUrlRange(1, $this->lastPage);
  61. } elseif ($this->currentPage <= $window) {
  62. $block['first'] = $this->getUrlRange(1, $window + 2);
  63. $block['last'] = $this->getUrlRange($this->lastPage - 1, $this->lastPage);
  64. } elseif ($this->currentPage > ($this->lastPage - $window)) {
  65. $block['first'] = $this->getUrlRange(1, 2);
  66. $block['last'] = $this->getUrlRange($this->lastPage - ($window + 2), $this->lastPage);
  67. } else {
  68. $block['first'] = $this->getUrlRange(1, 2);
  69. $block['slider'] = $this->getUrlRange($this->currentPage - $side, $this->currentPage + $side);
  70. $block['last'] = $this->getUrlRange($this->lastPage - 1, $this->lastPage);
  71. }
  72. $html = '';
  73. if (is_array($block['first'])) {
  74. $html .= $this->getUrlLinks($block['first']);
  75. }
  76. if (is_array($block['slider'])) {
  77. $html .= $this->getDots();
  78. $html .= $this->getUrlLinks($block['slider']);
  79. }
  80. if (is_array($block['last'])) {
  81. $html .= $this->getDots();
  82. $html .= $this->getUrlLinks($block['last']);
  83. }
  84. return $html;
  85. }
  86. /**
  87. * 渲染分页html
  88. * @return mixed
  89. */
  90. public function render($params = null)
  91. {
  92. if (is_array($params)) {
  93. if (isset($params['type'])) {
  94. $this->simple = $params['type'] === 'simple';
  95. }
  96. }
  97. if ($this->hasPages()) {
  98. if ($this->simple) {
  99. return sprintf(
  100. '<ul class="pager">%s %s</ul>',
  101. $this->getPreviousButton(),
  102. $this->getNextButton()
  103. );
  104. } else {
  105. return sprintf(
  106. '<ul class="pagination">%s %s %s</ul>',
  107. $this->getPreviousButton(),
  108. $this->getLinks(),
  109. $this->getNextButton()
  110. );
  111. }
  112. }
  113. }
  114. public function getNextPage()
  115. {
  116. return $this->currentPage + 1;
  117. }
  118. /**
  119. * 生成一个可点击的按钮
  120. *
  121. * @param string $url
  122. * @param int $page
  123. * @return string
  124. */
  125. protected function getAvailablePageWrapper($url, $page)
  126. {
  127. return '<li><a href="' . htmlentities($url) . '">' . $page . '</a></li>';
  128. }
  129. /**
  130. * 生成一个禁用的按钮
  131. *
  132. * @param string $text
  133. * @return string
  134. */
  135. protected function getDisabledTextWrapper($text)
  136. {
  137. return '<li class="disabled"><span>' . $text . '</span></li>';
  138. }
  139. /**
  140. * 生成一个激活的按钮
  141. *
  142. * @param string $text
  143. * @return string
  144. */
  145. protected function getActivePageWrapper($text)
  146. {
  147. return '<li class="active"><span>' . $text . '</span></li>';
  148. }
  149. /**
  150. * 生成省略号按钮
  151. *
  152. * @return string
  153. */
  154. protected function getDots()
  155. {
  156. return $this->getDisabledTextWrapper('...');
  157. }
  158. /**
  159. * 批量生成页码按钮.
  160. *
  161. * @param array $urls
  162. * @return string
  163. */
  164. protected function getUrlLinks(array $urls)
  165. {
  166. $html = '';
  167. foreach ($urls as $page => $url) {
  168. $html .= $this->getPageLinkWrapper($url, $page);
  169. }
  170. return $html;
  171. }
  172. /**
  173. * 生成普通页码按钮
  174. *
  175. * @param string $url
  176. * @param int $page
  177. * @return string
  178. */
  179. protected function getPageLinkWrapper($url, $page)
  180. {
  181. if ($page == $this->currentPage()) {
  182. return $this->getActivePageWrapper($page);
  183. }
  184. return $this->getAvailablePageWrapper($url, $page);
  185. }
  186. }