Page.php 5.3 KB

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