This repository has been archived by the owner on Jul 25, 2022. It is now read-only.
/
Basic.php
227 lines (195 loc) · 7.78 KB
/
Basic.php
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
<?php // vim:ts=4:sw=4:et:fdm=marker
/*
==ATK4===================================================
This file is part of Agile Toolkit 4
http://agiletoolkit.org/
(c) 2008-2013 Agile Toolkit Limited <info@agiletoolkit.org>
Distributed under Affero General Public License v3 and
commercial license.
See LICENSE or LICENSE_COM for more information
=====================================================ATK4=*/
/**
* Paginator needs to have source set (which can be either Model,
* DSQL or Array). It will render itself into parent and will
* limit the source to display limited number of records per page
* with ability to travel back and forth
*/
class Paginator_Basic extends CompleteLister {
public $ipp=30; // By default, show 30 records per page
public $skip=0; // By default, do not skip anything
public $range=4; // Display 4 adjacent pages from current one
public $ajax_reload=true; // Reload parent with AJAX
public $memorize=true; // Remember page, when user comes back
public $skip_var=null; // Argument to use to specify page
public $source=null; // Set with setSource()
public $base_page=null; // let's redefine page nicely
function init(){
parent::init();
if(!$this->skip_var)$this->skip_var=$this->name.'_skip';
}
/** Set number of items displayed per page */
function setRowsPerPage($rows) {
$this->ipp = $rows;
return $this;
}
// obsolete, should be removed in 4.4
function ipp($rows){
return $this->setRowsPerPage($rows);
}
/** Set a custom source. Must be an object with foundRows() method */
function setSource($source){
if($this->memorize){
if (isset($_GET[$this->skip_var])){
$this->skip=$this->memorize('skip', (int)$_GET[$this->skip_var]);
} else {
$this->skip=(int)$this->recall('skip');
}
}else{
$this->skip=@$_GET[$this->skip_var]+0;
}
// Start iterating early ($source = DSQL of model)
if($source instanceof SQL_Model){
$source = $source->_preexec();
}
if($source instanceof DB_dsql){
$source->limit($this->ipp, $this->skip);
$source->calcFoundRows();
$this->source = $source;
}elseif($source instanceof Model){
$this->source = $source->setLimit($this->ipp,$this->skip);
}else{
// NOTE: no limiting enabled for unknown data source
$this->source =& $source;
}
}
function recursiveRender(){
// get data source
if (! $this->source) {
// force grid sorting implemented in Grid_Advanced
if($this->owner instanceof Grid_Advanced) {
$this->owner->getIterator();
}
// set data source for Paginator
if ($this->owner->model) {
$this->setSource($this->owner->model);
} elseif ($this->owner->dq) {
$this->setSource($this->owner->dq);
} else {
throw $this->exception('Unable to find source for Paginator');
}
}
// calculate found rows
if($this->source instanceof DB_dsql){
$this->source->preexec();
$this->found_rows=$this->source->foundRows();
}elseif($this->source instanceof Model){
$this->found_rows=$this->source->count();
}else{
$this->found_rows=count($this->source);
}
// calculate current page and total pages
$this->cur_page=floor($this->skip / $this->ipp) +1;
$this->total_pages = ceil($this->found_rows / $this->ipp);
// no need for paginator if there is only one page
if($this->total_pages<=1)return $this->destroy();
if($this->cur_page>$this->total_pages || ($this->cur_page==1 && $this->skip!=0)){
$this->cur_page=1;
if($this->memorize){
$this->memorize('skip',$this->skip=0);
}
if($this->source instanceof DB_dsql){
$this->source->limit($this->ipp,$this->skip);
$this->source->rewind(); // re-execute the query
}elseif($this->source instanceof Model){
$this->source->setLimit($this->ipp,$this->skip);
}else{
// Imants: not sure if this is correct, but it was like this before
$this->source->setLimit($this->ipp,$this->skip);
}
}
// generate source for Paginator Lister (pages, links, labels etc.)
$data=array();
/*
if($this->cur_page>1){
$data[]=array(
'href'=>$this->api->url($this->base_page,array($this->skip_var=>$pn=($p-1)*$this->ipp)),
'pn'=>$pn,
'cur'=>$p==$this->cur_page?$this->template->get('cur'):'',
'label'=>'«'
);
$this->add('View',null,'prev')
->setElement('a')
->setAttr('href',$this->api->url($this->base_page,$u=array($this->skip_var=>
$pn=max(0,$this->skip-$this->ipp)
)))
->setAttr('data-skip',$pn)
->set('<')
;
}else $first=null;
if($this->cur_page<$this->total_pages){
$this->add('View',null,'next')
->setElement('a')
->setAttr('href',$this->api->url($this->base_page,$u=array($this->skip_var=>
$pn=$this->skip+$this->ipp
)))
->setAttr('data-skip',$pn)
->set('>')
;
}
// First page
if($this->cur_page>$this->range+1){
$this->add('View',null,'first')
->setElement('a')
->setAttr('href',$this->api->url($this->base_page,$u=array($this->skip_var=>
$pn=max(0,0)
)))
->setAttr('data-skip',$pn)
->set('1')
;
if($this->cur_page>$this->range+2){
$this->add('View',null,'points_left')
->setElement('span')
->set('...')
;
}
}
// Last page
if($this->cur_page<$this->total_pages-$this->range){
$this->add('View',null,'last')
->setElement('a')
->setAttr('href',$this->api->url($this->base_page,$u=array($this->skip_var=>
$pn=max(0,($this->total_pages-1)*$this->ipp)
)))
->setAttr('data-skip',$pn)
->set($this->total_pages)
;
if($this->cur_page<$this->total_pages-$this->range-1){
$this->add('View',null,'points_right')
->setElement('span')
->set('...')
;
}
}
*/
foreach(range(max(1,$this->cur_page-$this->range), min($this->total_pages, $this->cur_page+$this->range)) as $p){
$data[]=array(
'href'=>$this->api->url($this->base_page,array($this->skip_var=>$pn=($p-1)*$this->ipp)),
'pn'=>$pn,
'cur'=>$p==$this->cur_page?$this->template->get('cur'):'',
'label'=>$p
);
}
if($this->ajax_reload){
$this->js('click',$this->owner->js()->reload(array($this->skip_var=>$this->js()->_selectorThis()->attr('data-skip'))))
->_selector('#'.$this->name.' a');
}
parent::setSource($data);
return parent::recursiveRender();
}
function defaultTemplate(){
return array('paginator42','paginator');
}
function defaultSpot(){
return 'Paginator';
}
}