class.pagenav.php
3.43 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
<?php
/**
* @author: Bunzia Alexander <nifus@mail.ru> <http://www.weblancer.net/users/nifus/>
* @copyright: Copyright (c) 2010, Bunzia Alexander
* @version: 1.0
* @license: http://www.gnu.org/copyleft/gpl.html GNU/GPL
* @project: HiLo
*/
class page_nav {
public $short = true; // укорачивать пагинацию
private $count_rows;
private $url_tmpl;
private $page;
private $limit;
private $tmpl = 'admin_page_nav';
public function set_tmpl($v){
$this -> tmpl = $v;
}
public function __construct($count){
$this -> count_rows = intval($count);
}
public function set_limit($limit){
$this -> limit = intval($limit);
}
public function set_url_tmpl($tmpl){
$this -> url_tmpl = $tmpl;
}
public function get($page){
$page = !empty($page) ? intval($page) : 1;
if ( $this->count_rows < $this->limit ) {
return false;
}
$total_pages = ceil($this->count_rows / $this->limit);
// если страниц больше 1
if ( $total_pages <= 1 ) {
return false;
}
$t = new PHPTAL();
$t -> setSnippet('admin',$this -> tmpl);
$ret = '';
$prev = $page - 1;
$rows = array();
if ($prev >= 1) {
$t -> prev = sprintf($this->url_tmpl,$prev);
}else{
$t -> prev = false;
}
$counter = 1;
$current_page = $page ;
// перебираем страницы
while ($counter <= $total_pages)
{
// всего 10 страниц
// стоим на 5 странице
// 1,...3,4,[5],6,7..10
if ($counter == $current_page)
{
// если текущая страницы, то ссылки нет
$rows[]=array('counter'=>$counter,'url'=> false,'now'=> true,'page'=>false);
}
elseif ( ( (false===$this -> short) || ($counter > $current_page-3 && $counter < $current_page + 3) ) || $counter == 1 || $counter == $total_pages )
{
// если счётчик от текущей страницы ушёл больше чем на 3 позиции и счётчик
// если счётчик стоит на первой странице
// если счётчик стоит на последней странице
if ( (true===$this -> short) && ($counter == $total_pages) && ($current_page < $total_pages - 3) )
{
// если счётчик на последней странице
// если текущая странице до конца больше 3-х страниц
// то пропускаем цифру
$rows[]=array('counter'=>'...','url'=> false,'now'=> false,'page'=>false);
}
$rows[]=array('counter'=>$counter,'url'=> sprintf($this->url_tmpl,$counter),'page'=>$counter);
if ( (true===$this -> short) && ($counter == 1) && ($current_page > 4 ) )
{
// если счётчик в начале
// если текущая страница ушла от начала дальше чем на 4 позици
// то пропускаем
$rows[]=array('counter'=>'...','url'=> false,'now'=> false,'page'=>false);
}
}
$counter++;
}
$next = ($current_page+1);
if ($next <= $total_pages) {
$t -> next = sprintf($this->url_tmpl,$next);
}else{
$t -> next = false;
}
$t -> rows = $rows;
$t -> all = $total_pages;
$t -> current_page = $current_page;
$t -> first = sprintf($this->url_tmpl,1);
$t -> last = sprintf($this->url_tmpl,$total_pages);
return $t -> execute();
}
}
?>