Pagination.class.php
14.1 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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
<?php
/**
* 分页插件
*
* @version 0.1
* @author xiaoma
* @name Component_Scripts_Common_Pagination
*/
class Component_Scripts_Common_Pagination extends Framework_YComponent
{
/**
* 默认分页模式
*
*/
const Model_Default = 'default' ;
/**
* 自动加载分页模式
* 自动加载N页后出现页码(当前页码)如:三页之后显示页码,则显示页码时,当前页在第3页
*
*/
const Model_AutoLoad = 'autoload' ;
/**
* 自动加载分页模式2
* 自动加载N页后出现页码(按比例分割页码)如:三页之后显示页码,则显示页码时,当前页在第1页
*/
const Model_AutoLoad2 = 'autoload2' ;
/**
* 点击异步翻页模式
*
*/
const Model_Ajax = 'ajax' ;
/**
* 分页信息
* 信息包含:
* options: 配置项
* udi 路径UDI
* model 分页模式
* parames 条件参数
* record_count: 总记录数
* page_count: 按照页大小计算出来的总页数
* first: 第一页的索引
* last: 最后一页的索引
* current: 当前页的索引
* next: 下一页的索引
* prev: 上一页的索引
* page_size: 页大小
*
* @var Array
*/
private $_pagination = null ;
private $_jsPath = '' ;
private $_id ;
/**
* 重新格式化插件配置
*
* @param array $pagination 外部传入的分页信息
* @return array
*/
public function formatPagination($pagination)
{
//JS目录
$this->_jsPath = SITE_JS . '/plugins/pagination' ;
//确认配置项
$options = $pagination['options'] ;
$udi = isset( $options['udi'] ) ? $options['udi'] : $this->_request->requestUDI();
$model = isset( $options['model'] ) ? $options['model']:self::Model_Default ;
$id = $this->get('id','');
// 设置默认的ID
if ($id === '')
{
if ($options['model'] != self::Model_AutoLoad2)
{
$this->_id = 'page_'.time().rand(1,20);
}
else
{
$this->_id = $this->_request->namespace.'_'.$this->_request->controller_name.'_'.$this->_request->action_name ;
}
}
// 使用指定的ID
else
{
$this->_id = $id;
}
$pagination['options']['model'] = $model ;
$pagination['options']['udi'] = $udi ;
$pagination['options']['loadPageCount'] = ($pagination['options']['loadPageCount'])?$pagination['options']['loadPageCount']:3 ;
$this->_pagination = $pagination ;
return $pagination ;
}
/**
* 主方法
*
* @return unknown
*/
public function render()
{
//分页基本参数
$pagination = $this->get('pagination');
if (!is_array($pagination) || !isset($pagination['options']))
{
return false ;
}
//格式化信息
$pagination = $this->formatPagination($pagination);
//确认配置项
$options = $pagination['options'] ;
$model = isset( $options['model'] ) ? $options['model']:'default' ;
//确认分页模式
if ( $model == self::Model_Ajax )
{
$this->modelAjax();
}elseif ( $model == self::Model_AutoLoad )
{
$this->modelAutoLoad();
}elseif ( $model == self::Model_AutoLoad2)
{
$this->modelAutoLoad2();
}else
{
$this->modelDefault();
}
}
/**
* 默认分页模式
*
*/
private function modelDefault()
{
$html = $this->buildHtml();
echo $html ;
}
/**
* 自动加载分页模式
*
*/
private function modelAutoLoad()
{
$html = $this->buildHtml();
$jsPath = $this->_jsPath ;
$id = $this->_id ;
$htmlTarget = $this->_pagination['options']['htmlTarget'];
$pageTarget = isset($this->_pagination['options']['pageTarget'])?$this->_pagination['options']['pageTarget']:'';
$beforeAppend = isset($this->_pagination['options']['beforeAppend'])?$this->_pagination['options']['beforeAppend']:'function(){}';
$afterAppend = isset($this->_pagination['options']['afterAppend'])?$this->_pagination['options']['afterAppend']:'function(){}';
$loadPage = $this->_pagination['options']['loadPageCount'];
$onload_begin = $onload_end = '' ;
if (!$this->_request->isAjax())
{
$onload_begin = 'window.onload=function(){' ;
$onload_end = '}';
}
$html .= <<<EOT
<script type="text/javascript">
$onload_begin
seajs.use('$jsPath',function(page){
page.initAutoLoad('$id','$htmlTarget',$loadPage,'$pageTarget',$beforeAppend,$afterAppend);
});
$onload_end
</script>
EOT;
echo $html ;
}
/**
* 自动加载分页模式2 (按每页自动加载N页数据进行切分页码 )
* 如:一共10页,现在每次加载3页后再显示页码,则显示的为 1 2 3 4 四页,而不是1-10页
*
*/
private function modelAutoLoad2()
{
$nextPage = '' ;
if ($this->_pagination['current'] < $this->_pagination['page_count'])
{
$nextPage = ($this->_pagination['page_count'] <= 1)?'':$this->getUrl($this->_pagination['next']);
}
$split = $this->_pagination['options']['loadPageCount'];
//重置页码
$this->resetPagination($split);
$html = $this->buildHtml();
$html = substr($html,0,-8).'<span rel="nextpage" style="display:none;">'.$nextPage.'</span>'.substr($html,-8);
$jsPath = $this->_jsPath ;
$id = $this->_id ;
$htmlTarget = $this->_pagination['options']['htmlTarget'];
$pageTarget = isset($this->_pagination['options']['pageTarget'])?$this->_pagination['options']['pageTarget']:'';
$beforeAppend = isset($this->_pagination['options']['beforeAppend'])?$this->_pagination['options']['beforeAppend']:'function(){}';
$afterAppend = isset($this->_pagination['options']['afterAppend'])?$this->_pagination['options']['afterAppend']:'function(){}';
$loadPage = $this->_pagination['options']['loadPageCount'];
$onload_begin = $onload_end = '' ;
if (!$this->_request->isAjax())
{
$onload_begin = 'window.onload=function(){' ;
$onload_end = '}';
}
$html .= <<<EOT
<script type="text/javascript">
$onload_begin
seajs.use('$jsPath',function(page){
page.initAutoLoad2('$id','$htmlTarget',$loadPage,'$pageTarget',$beforeAppend,$afterAppend);
});
$onload_end
</script>
EOT;
echo $html ;
}
/**
* 划分重置页码
*
* @param int $split 设置将多少页合为一页
*/
private function resetPagination ($split)
{
$pagination = $this->_pagination ;
$pagination['first'] = 1 ;
$pagination['page_count'] = ceil($pagination['page_count'] / $split);
$pagination['last'] = $pagination['page_count'] ;
$current_page = ceil($pagination['current'] / $split) ;
$pagination['current'] = $current_page ;
$pagination['next'] = ($current_page < $pagination['last'] - 1) ? $current_page + 1 : $pagination['last'];
$pagination['prev'] = ($current_page > 1)?$current_page - 1 : 1 ;
$pagination['page_size'] = $split * $pagination['page_size'] ;
$this->_pagination = array_merge($this->_pagination,$pagination);
}
/**
* AJAX分页模式
*
*/
private function modelAjax()
{
$html = $this->buildHtml();
$jsPath = $this->_jsPath ;
$id = $this->_id ;
$htmlTarget = $this->_pagination['options']['htmlTarget'];
$onload_begin = $onload_end = '' ;
if (!$this->_request->isAjax())
{
$onload_begin = 'window.onload=function(){' ;
$onload_end = '}';
}
$beforeAppend = isset($this->_pagination['options']['beforeAppend'])?$this->_pagination['options']['beforeAppend']:'function(){}';
$afterAppend = isset($this->_pagination['options']['afterAppend'])?$this->_pagination['options']['afterAppend']:'function(){}';
$html .= <<<EOT
<script type="text/javascript">
$onload_begin
seajs.use('$jsPath',function(page){
page.initAjax('$id','$htmlTarget',$beforeAppend,$afterAppend);
});
$onload_end
</script>
EOT;
echo $html ;
}
/**
* 生成HTML
*
* @return string
*/
private function buildHtml()
{
$pagination = $this->_pagination;
$options = $pagination['options'];
$udi = $options['udi'];
$url_args = $pagination['parames'];
$length = $this->get('length', 5);
$slider = $this->get('slider', 2);
$prev_label = $this->get('prev_label', '<');
$next_label = $this->get('next_label', '>');
$current_class = $this->get('current_class','link1');
$link_class = $this->get('link_class','link2');
$page_class = $this->get('page_class','y_news_bolg1_link');
$isMobile = $this->get('is_mobile',false);
// 2014/05/14 hf: 根据业务需要,增加上一页和下一页的样式
$prev_class = $this->get('prev_class', $link_class);
$next_class = $this->get('next_class', $link_class);
$id = $this->_id ;
$model = $options['model'] ;
$htmlTarget = $this->_pagination['options']['htmlTarget'];
$out = "<div class=\"{$page_class}\" id='{$id}' model=\"{$model}\" rel=\"{$htmlTarget}\">\n";
//如果是移动端
if($isMobile)
{
if ($pagination['current'] != $pagination['first'])
{
$url = $this->getUrl($pagination['prev']);
$out .= "<a href=\"{$url}\" class=\"$prev_class\" p='".$pagination['prev']."'>{$prev_label}</a>\n";
}
if ($pagination['current'] != $pagination['last'])
{
$url = $this->getUrl( $pagination['next']);
$out .= "<a href=\"{$url}\" class=\"$next_class\" p='".$pagination['next']."'>{$next_label}</a>\n";
}
return $out."</div>\n";
}
//如果小于2页
if ($pagination['page_count']< 2)
{
return $out."</div>\n";
}
if ($this->get('show_count'))
{
$out .= "<span class='countinfo'>共 {$pagination['record_count']} 个条目</span>\n";
}
// 2012/09/08 hf: 修改 bug #12470 第一页不显示"上一页"
if ($pagination['current'] != $pagination['first'])
{
/*$url_args['page'] = $pagination['prev'];
$url = url($udi, $url_args);*/
$url = $this->getUrl($pagination['prev']);
$out .= "<a href=\"{$url}\" class=\"$prev_class\" p='".$pagination['prev']."' >{$prev_label}</a>\n";
}
$base = $pagination['first'];
$current = $pagination['current'];
$mid = intval($length / 2);
if ($current < $pagination['first'])
{
$current = $pagination['first'];
}
if ($current > $pagination['last'])
{
$current = $pagination['last'];
}
$begin = $current - $mid;
if ($begin < $pagination['first'])
{
$begin = $pagination['first'];
}
$end = $begin + $length - 1;
if ($end >= $pagination['last'])
{
$end = $pagination['last'];
$begin = $end - $length + 1;
if ($begin < $pagination['first'])
{
$begin = $pagination['first'];
}
}
if ($begin > $pagination['first'])
{
for ($i = $pagination['first']; $i < $pagination['first'] + $slider && $i < $begin; $i ++)
{
//$url_args['page'] = $i;
$in = $i + 1 - $base;
//$url = url($udi, $url_args);
$url = $this->getUrl( $i);
$out .= "<a href=\"{$url}\" class=\"$link_class\" p='{$i}'>{$in}</a>\n";
}
if ($i < $begin)
{
$out .= "<a class=\"$link_class\" >...</a>\n";
}
}
for ($i = $begin; $i <= $end; $i ++)
{
$url_args['page'] = $i;
$in = $i + 1 - $base;
if ($i == $pagination['current'])
{
$out .= "<a class=\"{$current_class}\" p='$i'>{$in}</a>\n";
}
else
{
//$url = url($udi, $url_args);
$url = $this->getUrl( $i);
$out .= "<a href=\"{$url}\" class=\"$link_class\" p='$i'>{$in}</a>\n";
}
}
if ($pagination['last'] - $end > $slider)
{
$out .= "<a class=\"$link_class\" >...</a>\n";
$end = $pagination['last'] - $slider;
}
for ($i = $end + 1; $i <= $pagination['last']; $i ++)
{
$url_args['page'] = $i;
$in = $i + 1 - $base;
//$url = url($udi, $url_args);
$url = $this->getUrl( $i);
$out .= "<a href=\"{$url}\" class=\"$link_class\" p='$i'>{$in}</a>\n";
}
// 2012/09/08 hf: 修改 bug #12470 最后一页不显示"下一页"
if ($pagination['current'] != $pagination['last'])
{
//$url_args['page'] = $pagination['next'];
//$url = url($udi, $url_args);
$url = $this->getUrl( $pagination['next']);
$out .= "<a href=\"{$url}\" class=\"$next_class\" p='".$pagination['next']."' >{$next_label}</a>\n";
}
//检测是否需要增加
if ($this->get('form') && $pagination['options']['model'] == self::Model_Default)
{
if (isset($url_args['page']))
{
unset($url_args['page']);
$formurl = $this->getUrl($url_args);
}
$out .= <<<EOT
<div class='pagination-form'>
<form action="{$formurl}" method="GET">
<span class="btn38_page">到</span>
<input type="text" class="btn39_inp" name="page" maxLength="10"
onKeypress="if (event.keyCode < 48 || event.keyCode > 57) event.returnValue = false;"
onblur="if(isNaN(this.value)) this.value='1'">
<span class="btn38_page1">页</span>
<input type="submit" value="GO" class="btn41" style="">
</form>
</div>
EOT;
}
$out .= "</div>\n";
return $out;
}
/**
* 获取链接
*
* @param unknown_type $page 页码
* @param unknown_type $args 额外链接参数
* @return string
*/
private function getUrl($page,$args=array())
{
$pagination = $this->_pagination;
$options = $pagination['options'];
$udi = $options['udi'];
$url_args = array_merge($pagination['parames'],array('page' => $page ),$args);
return SITE_MAIN.url($udi,$url_args);
}
}