1 <?php
2 // +----------------------------------------------------------------------
3 // | ThinkPHP [ WE CAN DO IT JUST THINK ]
4 // +----------------------------------------------------------------------
5 // | Copyright (c) 2006~2016 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 use think\Config;
16 class Bootstrap extends Paginator
17 {
18
19 /**
20 * 上一页按钮
21 * @param string $text
22 * @return string
23 */
24 protected function getPreviousButton($text = "«",$style=false)
25 {
26
27 if ($this->currentPage() <= 1) {
28 return $this->getDisabledTextWrapper($text,$style);
29 }
30
31 $url = $this->url(
32 $this->currentPage() - 1
33 );
34
35 return $this->getPageLinkWrapper($url, $text,$style);
36 }
37
38 /**
39 * 下一页按钮
40 * @param string $text
41 * @return string
42 */
43 protected function getNextButton($text = '»',$style=false)
44 {
45 if (!$this->hasMore) {
46 return $this->getDisabledTextWrapper($text,$style);
47 }
48
49 $url = $this->url($this->currentPage() + 1);
50
51 return $this->getPageLinkWrapper($url, $text,$style);
52 }
53
54 /**
55 * 页码按钮
56 * @return string
57 */
58 protected function getLinks($style=false)
59 {
60 if ($this->simple)
61 return '';
62
63 $block = [
64 'first' => null,
65 'slider' => null,
66 'last' => null
67 ];
68
69 $side = 3;
70 $window = $side * 2;
71
72 if ($this->lastPage < $window + 6) {
73 $block['first'] = $this->getUrlRange(1, $this->lastPage);
74 } elseif ($this->currentPage <= $window) {
75 $block['first'] = $this->getUrlRange(1, $window + 2);
76 $block['last'] = $this->getUrlRange($this->lastPage - 1, $this->lastPage);
77 } elseif ($this->currentPage > ($this->lastPage - $window)) {
78 $block['first'] = $this->getUrlRange(1, 2);
79 $block['last'] = $this->getUrlRange($this->lastPage - ($window + 2), $this->lastPage);
80 } else {
81 $block['first'] = $this->getUrlRange(1, 2);
82 $block['slider'] = $this->getUrlRange($this->currentPage - $side, $this->currentPage + $side);
83 $block['last'] = $this->getUrlRange($this->lastPage - 1, $this->lastPage);
84 }
85
86 $html = '';
87
88 if (is_array($block['first'])) {
89 $html .= $this->getUrlLinks($block['first'],$style);
90 }
91
92 if (is_array($block['slider'])) {
93 $html .= $this->getDots();
94 $html .= $this->getUrlLinks($block['slider'],$style);
95 }
96
97 if (is_array($block['last'])) {
98 $html .= $this->getDots();
99 $html .= $this->getUrlLinks($block['last'],$style);
100 }
101
102 return $html;
103 }
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 if(empty(Config::get('paginate.newstyle')))
121 {
122 return sprintf(
123 '<ul class="pagination">%s %s %s</ul>',
124 $this->getPreviousButton(),
125 $this->getLinks(),
126 $this->getNextButton()
127 );
128 }
129 else
130 {
131 return sprintf(
132 '%s %s %s',
133 $this->getPreviousButton("«",true),
134 $this->getLinks(true),
135 $this->getNextButton("»",true)
136 );
137 }
138 }
139 }
140 }
141
142
143 /**
144 * 生成一个可点击的按钮
145 *
146 * @param string $url
147 * @param int $page
148 * @return string
149 */
150 protected function getAvailablePageWrapper($url, $page,$style)
151 {
152 if($style)
153 return '<a href="' . htmlentities($url) . '">' . $page . '</a>';
154 else
155 return '<li><a href="' . htmlentities($url) . '">' . $page . '</a></li>';
156 }
157
158 /**
159 * 生成一个禁用的按钮
160 *
161 * @param string $text
162 * @return string
163 */
164 protected function getDisabledTextWrapper($text,$style)
165 {
166 if($style)
167 return '<a>' . $text . '</a>';
168 else
169 return '<li class="disabled"><span>' . $text . '</span></li>';
170 }
171
172 /**
173 * 生成一个激活的按钮
174 *
175 * @param string $text
176 * @return string
177 */
178 protected function getActivePageWrapper($text,$style)
179 {
180 if($style)
181 return '<span class="current">' . $text . '</span>';
182 else
183 return '<li class="active"><span>' . $text . '</span></li>';
184 }
185
186 /**
187 * 生成省略号按钮
188 *
189 * @return string
190 */
191 protected function getDots()
192 {
193 return $this->getDisabledTextWrapper('...',$style);
194 }
195
196 /**
197 * 批量生成页码按钮.
198 *
199 * @param array $urls
200 * @return string
201 */
202 protected function getUrlLinks(array $urls,$style)
203 {
204 $html = '';
205
206 foreach ($urls as $page => $url) {
207 $html .= $this->getPageLinkWrapper($url, $page,$style);
208 }
209
210 return $html;
211 }
212
213 /**
214 * 生成普通页码按钮
215 *
216 * @param string $url
217 * @param int $page
218 * @return string
219 */
220 protected function getPageLinkWrapper($url, $page,$style)
221 {
222 if ($page == $this->currentPage()) {
223 return $this->getActivePageWrapper($page,$style);
224 }
225
226 return $this->getAvailablePageWrapper($url, $page,$style);
227 }
228 }