You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

206 lines
5.2 KiB

  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2018 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 think\paginator\driver;
  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. $block = [
  52. 'first' => null,
  53. 'slider' => null,
  54. 'last' => null
  55. ];
  56. $side = 3;
  57. $window = $side * 2;
  58. if ($this->lastPage < $window + 6) {
  59. $block['first'] = $this->getUrlRange(1, $this->lastPage);
  60. } elseif ($this->currentPage <= $window) {
  61. $block['first'] = $this->getUrlRange(1, $window + 2);
  62. $block['last'] = $this->getUrlRange($this->lastPage - 1, $this->lastPage);
  63. } elseif ($this->currentPage > ($this->lastPage - $window)) {
  64. $block['first'] = $this->getUrlRange(1, 2);
  65. $block['last'] = $this->getUrlRange($this->lastPage - ($window + 2), $this->lastPage);
  66. } else {
  67. $block['first'] = $this->getUrlRange(1, 2);
  68. $block['slider'] = $this->getUrlRange($this->currentPage - $side, $this->currentPage + $side);
  69. $block['last'] = $this->getUrlRange($this->lastPage - 1, $this->lastPage);
  70. }
  71. $html = '';
  72. if (is_array($block['first'])) {
  73. $html .= $this->getUrlLinks($block['first']);
  74. }
  75. if (is_array($block['slider'])) {
  76. $html .= $this->getDots();
  77. $html .= $this->getUrlLinks($block['slider']);
  78. }
  79. if (is_array($block['last'])) {
  80. $html .= $this->getDots();
  81. $html .= $this->getUrlLinks($block['last']);
  82. }
  83. return $html;
  84. }
  85. /**
  86. * 渲染分页html
  87. * @return mixed
  88. */
  89. public function render()
  90. {
  91. if ($this->hasPages()) {
  92. if ($this->simple) {
  93. return sprintf(
  94. '<ul class="pager">%s %s</ul>',
  95. $this->getPreviousButton(),
  96. $this->getNextButton()
  97. );
  98. } else {
  99. return sprintf(
  100. '<ul class="pagination">%s %s %s</ul>',
  101. $this->getPreviousButton(),
  102. $this->getLinks(),
  103. $this->getNextButton()
  104. );
  105. }
  106. }
  107. }
  108. /**
  109. * 生成一个可点击的按钮
  110. *
  111. * @param string $url
  112. * @param int $page
  113. * @return string
  114. */
  115. protected function getAvailablePageWrapper($url, $page)
  116. {
  117. return '<li><a href="' . htmlentities($url) . '">' . $page . '</a></li>';
  118. }
  119. /**
  120. * 生成一个禁用的按钮
  121. *
  122. * @param string $text
  123. * @return string
  124. */
  125. protected function getDisabledTextWrapper($text)
  126. {
  127. return '<li class="disabled"><span>' . $text . '</span></li>';
  128. }
  129. /**
  130. * 生成一个激活的按钮
  131. *
  132. * @param string $text
  133. * @return string
  134. */
  135. protected function getActivePageWrapper($text)
  136. {
  137. return '<li class="active"><span>' . $text . '</span></li>';
  138. }
  139. /**
  140. * 生成省略号按钮
  141. *
  142. * @return string
  143. */
  144. protected function getDots()
  145. {
  146. return $this->getDisabledTextWrapper('...');
  147. }
  148. /**
  149. * 批量生成页码按钮.
  150. *
  151. * @param array $urls
  152. * @return string
  153. */
  154. protected function getUrlLinks(array $urls)
  155. {
  156. $html = '';
  157. foreach ($urls as $page => $url) {
  158. $html .= $this->getPageLinkWrapper($url, $page);
  159. }
  160. return $html;
  161. }
  162. /**
  163. * 生成普通页码按钮
  164. *
  165. * @param string $url
  166. * @param int $page
  167. * @return string
  168. */
  169. protected function getPageLinkWrapper($url, $page)
  170. {
  171. if ($page == $this->currentPage()) {
  172. return $this->getActivePageWrapper($page);
  173. }
  174. return $this->getAvailablePageWrapper($url, $page);
  175. }
  176. }