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