Paging.php
8.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
<?php
/**
* Enter description here...
*
* example:
* <pre>
*
* </pre>
*
* @name QLib_Paging
* @version
* @package
* @author peter.zyliu peter.zyliu@gmail.com
* @since 1.0
*/
// // require_once 'Q/Paging.php';
class QLib_Paging extends Q_Paging
{
/**
* 路径
*
* @var String
*/
private $path = '';
/**
* 查询参数
* String or Array
* @var mixed
*/
private $query;
/**
* 分页集尺寸
*
* @var Integer
*/
private $pageSetSize = 11;
/**
* 子串
*
* @var String
*/
private $substring = '?';
/**
* 连接符号
*
* @var String
*/
private $sign = '=';
/**
* 对符
*
* @var String
*/
private $pairs = '&';
/**
* 模板路径
*
* @var String
*/
private $templatePath = '';
/**
* jsName
* @var String
*/
private $jsFName = '';
/**
* 废弃参数
* @var String
*/
private $disuse = null;
/**
* 设置分页链接中的关键字
*
* @var String
*/
private $keyword = 'page';
/**
* 是否开启rewrite url
*
* @var bool
*/
private $rewrite = false;
/**
* 附加参数
* @var string
*/
private $appendParam = '';
/**
* 分页中代码当前页码的常量
*
*/
const PAGER_VARIABLE_STRING = "%{PAGE_NO}";
public function __construct($file)
{
$this->templatePath = __DIR__ . '/Template/Paging/' . ucfirst($file) . '.php';
}
/**
* 新的获取当前偏移
* @return Integer
*/
public function getNewCurrent()
{
if ($this->currentPage > 1) {
return min($this->currentPage, $this->getPageNum());
}
$pageNo = !isset($_GET[$this->keyword]) ? 0 : (int)(intval($_GET[$this->keyword]) * $this->getSize()) - $this->getSize();
return (int)($pageNo < 0 ? 0 : $pageNo);
}
/**
* 重载当前页方法
* @return Integer
*/
public function getCurrent()
{
if ($this->currentPage > 1) {
return min($this->currentPage, $this->getPageNum());
}
$pageNo = isset($_GET[$this->keyword]) ? (int)intval($_GET[$this->keyword]) : 1;
if ($pageNo <= 0) {
$pageNo = 1;
}
$this->currentPage = min($pageNo, $this->getPageNum());
return $this->currentPage;
}
/**
* 获取关键词
* @return String
*/
public function getKeyword()
{
return $this->keyword;
}
/**
* 设置关键词
*
* @param String $keyword
* @return QLib_Paging
*/
public function setKeyword($keyword)
{
if (!empty($keyword)) {
$this->keyword = $keyword;
}
return $this;
}
/**
* 设置js名字
* @param String $name
* @return Q_Page_Abstract
*/
public function setFJs($name)
{
if (!empty($name)) {
$this->jsFName = $name;
}
return $this;
}
/**
* 设置附加参数
* @param string $param
* @return Q_Page_Abstract
*/
public function setAppendParam($param)
{
$this->appendParam = $param;
return $this;
}
/**
* 获取附加参数
* @return string
*/
public function getAppendParam()
{
return $this->appendParam;
}
/**
* 获取分页js名字
* @return String
*
*/
public function getFJs()
{
return $this->jsFName;
}
/**
*
* 设置链接的路径
*
* @param String $path
* @return QLib_Paging
*/
public function setPath($path)
{
if (!empty($path)) {
$this->path = trim($path);
}
return $this;
}
/**
* 设置连续Url
*
* @param bool $seo
* @return QLib_Paging
*/
public function rewrite($rw = true)
{
if ($rw == true) {
$this->substring = '';
$this->sign = '/';
$this->pairs = '/';
$this->rewrite = true;
}
return $this;
}
/**
* 取得程序路径
* @return String
*/
public function getPath()
{
return $this->path;
}
/**
* 设置分页集尺寸
*
* @param integer $num 大于1
* @return QLib_Paging
*/
public function setPageSetSize($num)
{
$this->pageSetSize = (int)intval($num);
return $this;
}
/**
* 取得分页集尺寸
*
* @return integer
*/
public function getPageSetSize()
{
return (int)$this->pageSetSize;
}
/**
* 获取查询参数
* @return String
*/
public function getQuery()
{
if (empty($this->query)) {
$this->query = $this->autoUrl();
}
if (is_array($this->query) && count($this->query) > 0) {
$_query = array();
foreach ($this->query as $key => $value) {
if ($key == $this->getKeyword()) {
continue;
}
if (is_array($value)) {
foreach ($value as $k => $val) {
$_query[] = "{$key}[]" . $this->sign . $val;
}
} else {
$_query[] = "{$key}" . $this->sign . $value;
}
}
$this->query = $this->pairs . implode($this->pairs, $_query);
}
return $this->query ? $this->query : '';
}
/**
* 获取URL
*
* @param Integer $pageNo
* @return String
*/
public function getUrl($pageNo)
{
$query = $this->getQuery();
if (strstr($query, self::PAGER_VARIABLE_STRING) && is_string($query)) {
$query = str_replace(self::PAGER_VARIABLE_STRING, $pageNo, $query);
} else {
if (empty($query)) {
$query = $this->getKeyword() . $this->sign . $pageNo;
} else {
$query = $this->getKeyword() . $this->sign . $pageNo . $query;
}
}
$url = $this->getPath() . $this->substring . $query;
if ($this->getFJs()) {
$url = $query;
}
return $url;
}
/**
* 设置查询参数
*
* @param mixed $query String or Array
* @return QLib_Paging
*/
public function setQuery($query)
{
$this->query = $query;
return $this;
}
/**
* 设置模板路径
*
* @param String $path
* @return QLib_Paging
*/
public function setTemplate($path)
{
$this->templatePath = $path;
return $this;
}
/**
* 输出模板
*/
public function view()
{
if ($this->getTotal() > 0) {
include($this->templatePath);
}
}
/**
* 自动组织 URL
* @return Array
*/
private function autoUrl()
{
$queryOpt = $_SERVER['REQUEST_URI'];
$queryArg = parse_url($queryOpt);
$queryOpt = isset($queryArg['query']) ? explode('&', $queryArg['query']) : array();
$query = array();
foreach ($queryOpt as $key => $val) {
$strTmp = explode('=', $val);
if (count($strTmp) < 2 || empty($strTmp[0]) || $strTmp[1] == '' || $strTmp[0] == $this->getKeyword()) {
continue;
}
if (is_array($this->disuse)) {
if (in_array($strTmp[1], $this->disuse) || in_array($strTmp[0], $this->disuse)) {
continue;
}
} else {
if ($strTmp[1] == $this->disuse || $strTmp[0] == $this->disuse) {
continue;
}
}
$query[$strTmp[0]] = $strTmp[1];
}
return $query;
}
/**
* set废弃参数
* @param mixed $disuse
* @return QLib_Paging
*/
public function setDisuse($disuse = null)
{
$this->disuse = $disuse;
return $this;
}
}