1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TPager class file. |
5
|
|
|
* |
6
|
|
|
* @author Qiang Xue <[email protected]> |
7
|
|
|
* @link https://github.com/pradosoft/prado |
8
|
|
|
* @license https://github.com/pradosoft/prado/blob/master/LICENSE |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Prado\Web\UI\WebControls; |
12
|
|
|
|
13
|
|
|
use Prado\Exceptions\TConfigurationException; |
14
|
|
|
use Prado\TPropertyValue; |
15
|
|
|
use Prado\Exceptions\TInvalidDataValueException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* TPager class. |
19
|
|
|
* |
20
|
|
|
* TPager creates a pager that provides UI for end-users to interactively |
21
|
|
|
* specify which page of data to be rendered in a {@see \Prado\Web\UI\WebControls\TDataBoundControl}-derived control, |
22
|
|
|
* such as {@see \Prado\Web\UI\WebControls\TDataList}, {@see \Prado\Web\UI\WebControls\TRepeater}, {@see \Prado\Web\UI\WebControls\TCheckBoxList}, etc. |
23
|
|
|
* The target data-bound control is specified by {@see setControlToPaginate ControlToPaginate}, |
24
|
|
|
* which must be the ID path of the target control reaching from the pager's |
25
|
|
|
* naming container. Note, the target control must have its {@see \Prado\Web\UI\WebControls\TDataBoundControl::setAllowPaging AllowPaging} |
26
|
|
|
* set to true. |
27
|
|
|
* |
28
|
|
|
* TPager can display three different UIs, specified via {@see setMode Mode}: |
29
|
|
|
* - NextPrev: a next page and a previous page button are rendered. |
30
|
|
|
* - Numeric: a list of page index buttons are rendered. |
31
|
|
|
* - List: a dropdown list of page indices are rendered. |
32
|
|
|
* |
33
|
|
|
* When the pager mode is either NextPrev or Numeric, the paging buttons may be displayed |
34
|
|
|
* in three types by setting {@see setButtonType ButtonType}: |
35
|
|
|
* - LinkButton: a hyperlink button |
36
|
|
|
* - PushButton: a normal button |
37
|
|
|
* - ImageButton: an image button (please set XXXPageImageUrl properties accordingly to specify the button images.) |
38
|
|
|
* |
39
|
|
|
* Since Prado 3.2.1, you can use the {@see setButtonCssClass ButtonCssClass} property to specify a css class |
40
|
|
|
* that will be applied to each button created by the pager in NextPrev or Numeric mode. |
41
|
|
|
* |
42
|
|
|
* TPager raises an {@see onPageIndexChanged OnPageIndexChanged} event when |
43
|
|
|
* the end-user interacts with it and specifies a new page (e.g. clicking |
44
|
|
|
* on a page button that leads to a new page.) The new page index may be obtained |
45
|
|
|
* from the event parameter's property {@see \Prado\Web\UI\WebControls\TPagerPageChangedEventParameter::getNewPageIndex NewPageIndex}. |
46
|
|
|
* Normally, in the event handler, one can set the {@see \Prado\Web\UI\WebControls\TDataBoundControl::getCurrentPageIndex CurrentPageIndex} |
47
|
|
|
* to this new page index so that the new page of data is rendered. |
48
|
|
|
* |
49
|
|
|
* Multiple pagers can be associated with the same data-bound control. |
50
|
|
|
* |
51
|
|
|
* @author Qiang Xue <[email protected]> |
52
|
|
|
* @since 3.0.2 |
53
|
|
|
*/ |
54
|
|
|
class TPager extends \Prado\Web\UI\WebControls\TWebControl implements \Prado\Web\UI\INamingContainer |
55
|
|
|
{ |
56
|
|
|
/** |
57
|
|
|
* Command name that TPager understands. |
58
|
|
|
*/ |
59
|
|
|
public const CMD_PAGE = 'Page'; |
60
|
|
|
public const CMD_PAGE_NEXT = 'Next'; |
61
|
|
|
public const CMD_PAGE_PREV = 'Previous'; |
62
|
|
|
public const CMD_PAGE_FIRST = 'First'; |
63
|
|
|
public const CMD_PAGE_LAST = 'Last'; |
64
|
|
|
|
65
|
|
|
private $_pageCount = 0; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Restores the pager state. |
69
|
|
|
* This method overrides the parent implementation and is invoked when |
70
|
|
|
* the control is loading persistent state. |
71
|
|
|
*/ |
72
|
|
|
public function loadState() |
73
|
|
|
{ |
74
|
|
|
parent::loadState(); |
75
|
|
|
if ($this->getEnableViewState(true)) { |
76
|
|
|
$this->getControls()->clear(); |
77
|
|
|
$this->buildPager(); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return string the ID path of the control whose content would be paginated. |
83
|
|
|
*/ |
84
|
|
|
public function getControlToPaginate() |
85
|
|
|
{ |
86
|
|
|
return $this->getViewState('ControlToPaginate', ''); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Sets the ID path of the control whose content would be paginated. |
91
|
|
|
* The ID path is the dot-connected IDs of the controls reaching from |
92
|
|
|
* the pager's naming container to the target control. |
93
|
|
|
* @param string $value the ID path |
94
|
|
|
*/ |
95
|
|
|
public function setControlToPaginate($value) |
96
|
|
|
{ |
97
|
|
|
$this->setViewState('ControlToPaginate', $value, ''); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return string the css class of the buttons. |
102
|
|
|
* @since 3.2.1 |
103
|
|
|
*/ |
104
|
|
|
public function getButtonCssClass() |
105
|
|
|
{ |
106
|
|
|
return $this->getViewState('ButtonCssClass', ''); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $value Sets the css class of the buttons that will be rendered by this pager to $value. |
111
|
|
|
* @since 3.2.1 |
112
|
|
|
*/ |
113
|
|
|
public function setButtonCssClass($value) |
114
|
|
|
{ |
115
|
|
|
$this->setViewState('ButtonCssClass', TPropertyValue::ensureString($value), ''); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return TPagerMode pager mode. Defaults to TPagerMode::NextPrev. |
120
|
|
|
*/ |
121
|
|
|
public function getMode() |
122
|
|
|
{ |
123
|
|
|
return $this->getViewState('Mode', TPagerMode::NextPrev); |
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param TPagerMode $value pager mode. |
128
|
|
|
*/ |
129
|
|
|
public function setMode($value) |
130
|
|
|
{ |
131
|
|
|
$this->setViewState('Mode', TPropertyValue::ensureEnum($value, TPagerMode::class), TPagerMode::NextPrev); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return TPagerButtonType the type of command button for paging. Defaults to TPagerButtonType::LinkButton. |
136
|
|
|
*/ |
137
|
|
|
public function getButtonType() |
138
|
|
|
{ |
139
|
|
|
return $this->getViewState('ButtonType', TPagerButtonType::LinkButton); |
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param TPagerButtonType $value the type of command button for paging. |
144
|
|
|
*/ |
145
|
|
|
public function setButtonType($value) |
146
|
|
|
{ |
147
|
|
|
$this->setViewState('ButtonType', TPropertyValue::ensureEnum($value, TPagerButtonType::class), TPagerButtonType::LinkButton); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return string text for the next page button. Defaults to '>'. |
152
|
|
|
*/ |
153
|
|
|
public function getNextPageText() |
154
|
|
|
{ |
155
|
|
|
return $this->getViewState('NextPageText', '>'); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param string $value text for the next page button. |
160
|
|
|
*/ |
161
|
|
|
public function setNextPageText($value) |
162
|
|
|
{ |
163
|
|
|
$this->setViewState('NextPageText', $value, '>'); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return string text for the previous page button. Defaults to '<'. |
168
|
|
|
*/ |
169
|
|
|
public function getPrevPageText() |
170
|
|
|
{ |
171
|
|
|
return $this->getViewState('PrevPageText', '<'); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param string $value text for the next page button. |
176
|
|
|
*/ |
177
|
|
|
public function setPrevPageText($value) |
178
|
|
|
{ |
179
|
|
|
$this->setViewState('PrevPageText', $value, '<'); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @return string text for the first page button. Defaults to '<<'. |
184
|
|
|
*/ |
185
|
|
|
public function getFirstPageText() |
186
|
|
|
{ |
187
|
|
|
return $this->getViewState('FirstPageText', '<<'); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @param string $value text for the first page button. If empty, the first page button will not be rendered. |
192
|
|
|
*/ |
193
|
|
|
public function setFirstPageText($value) |
194
|
|
|
{ |
195
|
|
|
$this->setViewState('FirstPageText', $value, '<<'); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @return string text for the last page button. Defaults to '>>'. |
200
|
|
|
*/ |
201
|
|
|
public function getLastPageText() |
202
|
|
|
{ |
203
|
|
|
return $this->getViewState('LastPageText', '>>'); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @param string $value text for the last page button. If empty, the last page button will not be rendered. |
208
|
|
|
*/ |
209
|
|
|
public function setLastPageText($value) |
210
|
|
|
{ |
211
|
|
|
$this->setViewState('LastPageText', $value, '>>'); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @return string the image URL for the first page button. This is only used when {@see getButtonType ButtonType} is 'ImageButton'. |
216
|
|
|
* @since 3.1.1 |
217
|
|
|
*/ |
218
|
|
|
public function getFirstPageImageUrl() |
219
|
|
|
{ |
220
|
|
|
return $this->getViewState('FirstPageImageUrl', ''); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @param string $value the image URL for the first page button. This is only used when {@see getButtonType ButtonType} is 'ImageButton'. |
225
|
|
|
* @since 3.1.1 |
226
|
|
|
*/ |
227
|
|
|
public function setFirstPageImageUrl($value) |
228
|
|
|
{ |
229
|
|
|
$this->setViewState('FirstPageImageUrl', $value); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @return string the image URL for the last page button. This is only used when {@see getButtonType ButtonType} is 'ImageButton'. |
234
|
|
|
* @since 3.1.1 |
235
|
|
|
*/ |
236
|
|
|
public function getLastPageImageUrl() |
237
|
|
|
{ |
238
|
|
|
return $this->getViewState('LastPageImageUrl', ''); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @param string $value the image URL for the last page button. This is only used when {@see getButtonType ButtonType} is 'ImageButton'. |
243
|
|
|
* @since 3.1.1 |
244
|
|
|
*/ |
245
|
|
|
public function setLastPageImageUrl($value) |
246
|
|
|
{ |
247
|
|
|
$this->setViewState('LastPageImageUrl', $value); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @return string the image URL for the next page button. This is only used when {@see getButtonType ButtonType} is 'ImageButton'. |
252
|
|
|
* @since 3.1.1 |
253
|
|
|
*/ |
254
|
|
|
public function getNextPageImageUrl() |
255
|
|
|
{ |
256
|
|
|
return $this->getViewState('NextPageImageUrl', ''); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* @param string $value the image URL for the next page button. This is only used when {@see getButtonType ButtonType} is 'ImageButton'. |
261
|
|
|
* @since 3.1.1 |
262
|
|
|
*/ |
263
|
|
|
public function setNextPageImageUrl($value) |
264
|
|
|
{ |
265
|
|
|
$this->setViewState('NextPageImageUrl', $value); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* @return string the image URL for the previous page button. This is only used when {@see getButtonType ButtonType} is 'ImageButton'. |
270
|
|
|
* @since 3.1.1 |
271
|
|
|
*/ |
272
|
|
|
public function getPrevPageImageUrl() |
273
|
|
|
{ |
274
|
|
|
return $this->getViewState('PrevPageImageUrl', ''); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @param string $value the image URL for the previous page button. This is only used when {@see getButtonType ButtonType} is 'ImageButton'. |
279
|
|
|
* @since 3.1.1 |
280
|
|
|
*/ |
281
|
|
|
public function setPrevPageImageUrl($value) |
282
|
|
|
{ |
283
|
|
|
$this->setViewState('PrevPageImageUrl', $value); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* @return string the image URL for the numeric page buttons. This is only used when {@see getButtonType ButtonType} is 'ImageButton' and {@see getMode Mode} is 'Numeric'. |
288
|
|
|
* @see setNumericPageImageUrl |
289
|
|
|
* @since 3.1.1 |
290
|
|
|
*/ |
291
|
|
|
public function getNumericPageImageUrl() |
292
|
|
|
{ |
293
|
|
|
return $this->getViewState('NumericPageImageUrl', ''); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Sets the image URL for the numeric page buttons. |
298
|
|
|
* This is actually a template for generating a set of URLs corresponding to numeric button 1, 2, 3, .., etc. |
299
|
|
|
* Use {0} as the placeholder for the numbers. |
300
|
|
|
* For example, the image URL http://example.com/images/button{0}.gif |
301
|
|
|
* will be replaced as http://example.com/images/button1.gif, http://example.com/images/button2.gif, etc. |
302
|
|
|
* @param string $value the image URL for the numeric page buttons. This is only used when {@see getButtonType ButtonType} is 'ImageButton' and {@see getMode Mode} is 'Numeric'. |
303
|
|
|
* @since 3.1.1 |
304
|
|
|
*/ |
305
|
|
|
public function setNumericPageImageUrl($value) |
306
|
|
|
{ |
307
|
|
|
$this->setViewState('NumericPageImageUrl', $value); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* @return int maximum number of pager buttons to be displayed. Defaults to 10. |
312
|
|
|
*/ |
313
|
|
|
public function getPageButtonCount() |
314
|
|
|
{ |
315
|
|
|
return $this->getViewState('PageButtonCount', 10); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* @param int $value maximum number of pager buttons to be displayed |
320
|
|
|
* @throws TInvalidDataValueException if the value is less than 1. |
321
|
|
|
*/ |
322
|
|
|
public function setPageButtonCount($value) |
323
|
|
|
{ |
324
|
|
|
if (($value = TPropertyValue::ensureInteger($value)) < 1) { |
325
|
|
|
throw new TInvalidDataValueException('pager_pagebuttoncount_invalid'); |
326
|
|
|
} |
327
|
|
|
$this->setViewState('PageButtonCount', $value, 10); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* @return int the zero-based index of the current page. Defaults to 0. |
332
|
|
|
*/ |
333
|
|
|
public function getCurrentPageIndex() |
334
|
|
|
{ |
335
|
|
|
return $this->getViewState('CurrentPageIndex', 0); |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* @param int $value the zero-based index of the current page |
340
|
|
|
* @throws TInvalidDataValueException if the value is less than 0 |
341
|
|
|
*/ |
342
|
|
|
protected function setCurrentPageIndex($value) |
343
|
|
|
{ |
344
|
|
|
if (($value = TPropertyValue::ensureInteger($value)) < 0) { |
345
|
|
|
throw new TInvalidDataValueException('pager_currentpageindex_invalid'); |
346
|
|
|
} |
347
|
|
|
$this->setViewState('CurrentPageIndex', $value, 0); |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* @return int number of pages of data items available |
352
|
|
|
*/ |
353
|
|
|
public function getPageCount() |
354
|
|
|
{ |
355
|
|
|
return $this->getViewState('PageCount', 0); |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* @param int $value number of pages of data items available |
360
|
|
|
* @throws TInvalidDataValueException if the value is less than 0 |
361
|
|
|
*/ |
362
|
|
|
protected function setPageCount($value) |
363
|
|
|
{ |
364
|
|
|
if (($value = TPropertyValue::ensureInteger($value)) < 0) { |
365
|
|
|
throw new TInvalidDataValueException('pager_pagecount_invalid'); |
366
|
|
|
} |
367
|
|
|
$this->setViewState('PageCount', $value, 0); |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* @return bool whether the current page is the first page Defaults to false. |
372
|
|
|
*/ |
373
|
|
|
public function getIsFirstPage() |
374
|
|
|
{ |
375
|
|
|
return $this->getCurrentPageIndex() === 0; |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
/** |
379
|
|
|
* @return bool whether the current page is the last page |
380
|
|
|
*/ |
381
|
|
|
public function getIsLastPage() |
382
|
|
|
{ |
383
|
|
|
return $this->getCurrentPageIndex() === $this->getPageCount() - 1; |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* Performs databinding to populate data items from data source. |
388
|
|
|
* This method is invoked by {@see dataBind()}. |
389
|
|
|
* You may override this function to provide your own way of data population. |
390
|
|
|
* @param \Traversable $param the bound data |
391
|
|
|
*/ |
392
|
|
|
public function onPreRender($param) |
393
|
|
|
{ |
394
|
|
|
parent::onPreRender($param); |
|
|
|
|
395
|
|
|
|
396
|
|
|
$controlID = $this->getControlToPaginate(); |
397
|
|
|
if (($targetControl = $this->getNamingContainer()->findControl($controlID)) === null || !($targetControl instanceof TDataBoundControl)) { |
398
|
|
|
throw new TConfigurationException('pager_controltopaginate_invalid', $controlID); |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
if ($targetControl->getAllowPaging()) { |
402
|
|
|
$this->_pageCount = $targetControl->getPageCount(); |
403
|
|
|
$this->getControls()->clear(); |
404
|
|
|
$this->setPageCount($targetControl->getPageCount()); |
405
|
|
|
$this->setCurrentPageIndex($targetControl->getCurrentPageIndex()); |
406
|
|
|
$this->buildPager(); |
407
|
|
|
} else { |
408
|
|
|
$this->_pageCount = 0; |
409
|
|
|
} |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
/** |
413
|
|
|
* Renders the control. |
414
|
|
|
* The method overrides the parent implementation by rendering |
415
|
|
|
* the pager only when there are two or more pages. |
416
|
|
|
* @param \Prado\Web\UI\THtmlWriter $writer the writer |
417
|
|
|
*/ |
418
|
|
|
public function render($writer) |
419
|
|
|
{ |
420
|
|
|
if ($this->_pageCount > 1) { |
421
|
|
|
parent::render($writer); |
422
|
|
|
} |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
/** |
426
|
|
|
* Builds the pager content based on the pager mode. |
427
|
|
|
* Current implementation includes building 'NextPrev', 'Numeric' and 'DropDownList' pagers. |
428
|
|
|
* Derived classes may override this method to provide additional pagers. |
429
|
|
|
*/ |
430
|
|
|
protected function buildPager() |
431
|
|
|
{ |
432
|
|
|
switch ($this->getMode()) { |
433
|
|
|
case TPagerMode::NextPrev: |
434
|
|
|
$this->buildNextPrevPager(); |
435
|
|
|
break; |
436
|
|
|
case TPagerMode::Numeric: |
437
|
|
|
$this->buildNumericPager(); |
438
|
|
|
break; |
439
|
|
|
case TPagerMode::DropDownList: |
440
|
|
|
$this->buildListPager(); |
441
|
|
|
break; |
442
|
|
|
} |
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
/** |
446
|
|
|
* Creates a pager button. |
447
|
|
|
* Depending on the button type, a TLinkButton or a TButton may be created. |
448
|
|
|
* If it is enabled (clickable), its command name and parameter will also be set. |
449
|
|
|
* Derived classes may override this method to create additional types of buttons, such as TImageButton. |
450
|
|
|
* @param string $buttonType button type, either LinkButton or PushButton |
451
|
|
|
* @param bool $enabled whether the button should be enabled |
452
|
|
|
* @param string $text caption of the button. |
453
|
|
|
* @param string $commandName CommandName corresponding to the OnCommand event of the button. |
454
|
|
|
* @param string $commandParameter CommandParameter corresponding to the OnCommand event of the button |
455
|
|
|
* @return mixed the button instance |
456
|
|
|
*/ |
457
|
|
|
protected function createPagerButton($buttonType, $enabled, $text, $commandName, $commandParameter) |
458
|
|
|
{ |
459
|
|
|
if ($buttonType === TPagerButtonType::LinkButton) { |
460
|
|
|
if ($enabled) { |
461
|
|
|
$button = new TLinkButton(); |
462
|
|
|
} else { |
463
|
|
|
$button = new TLabel(); |
464
|
|
|
$button->setText($text); |
465
|
|
|
$button->setCssClass($this->getButtonCssClass()); |
466
|
|
|
return $button; |
467
|
|
|
} |
468
|
|
|
} else { |
469
|
|
|
if ($buttonType === TPagerButtonType::ImageButton) { |
470
|
|
|
$button = new TImageButton(); |
471
|
|
|
$button->setImageUrl($this->getPageImageUrl($text, $commandName)); |
472
|
|
|
} else { |
473
|
|
|
$button = new TButton(); |
474
|
|
|
} |
475
|
|
|
if (!$enabled) { |
476
|
|
|
$button->setEnabled(false); |
477
|
|
|
} |
478
|
|
|
} |
479
|
|
|
$button->setText($text); |
480
|
|
|
$button->setCommandName($commandName); |
481
|
|
|
$button->setCommandParameter($commandParameter); |
482
|
|
|
$button->setCausesValidation(false); |
483
|
|
|
$button->setCssClass($this->getButtonCssClass()); |
484
|
|
|
return $button; |
485
|
|
|
} |
486
|
|
|
|
487
|
|
|
/** |
488
|
|
|
* @param string $text the caption of the image button |
489
|
|
|
* @param string $commandName the command name associated with the image button |
490
|
|
|
* @since 3.1.1 |
491
|
|
|
*/ |
492
|
|
|
protected function getPageImageUrl($text, $commandName) |
493
|
|
|
{ |
494
|
|
|
switch ($commandName) { |
495
|
|
|
case self::CMD_PAGE: |
496
|
|
|
$url = $this->getNumericPageImageUrl(); |
497
|
|
|
return str_replace('{0}', $text, $url); |
498
|
|
|
case self::CMD_PAGE_NEXT: |
499
|
|
|
return $this->getNextPageImageUrl(); |
500
|
|
|
case self::CMD_PAGE_PREV: |
501
|
|
|
return $this->getPrevPageImageUrl(); |
502
|
|
|
case self::CMD_PAGE_FIRST: |
503
|
|
|
return $this->getFirstPageImageUrl(); |
504
|
|
|
case self::CMD_PAGE_LAST: |
505
|
|
|
return $this->getLastPageImageUrl(); |
506
|
|
|
default: |
507
|
|
|
return ''; |
508
|
|
|
} |
509
|
|
|
} |
510
|
|
|
|
511
|
|
|
/** |
512
|
|
|
* Builds a next-prev pager |
513
|
|
|
*/ |
514
|
|
|
protected function buildNextPrevPager() |
515
|
|
|
{ |
516
|
|
|
$buttonType = $this->getButtonType(); |
517
|
|
|
$controls = $this->getControls(); |
518
|
|
|
if ($this->getIsFirstPage()) { |
519
|
|
|
if (($text = $this->getFirstPageText()) !== '') { |
520
|
|
|
$label = $this->createPagerButton($buttonType, false, $text, self::CMD_PAGE_FIRST, ''); |
|
|
|
|
521
|
|
|
$controls->add($label); |
522
|
|
|
$controls->add("\n"); |
523
|
|
|
} |
524
|
|
|
$label = $this->createPagerButton($buttonType, false, $this->getPrevPageText(), self::CMD_PAGE_PREV, ''); |
525
|
|
|
$controls->add($label); |
526
|
|
|
} else { |
527
|
|
|
if (($text = $this->getFirstPageText()) !== '') { |
528
|
|
|
$button = $this->createPagerButton($buttonType, true, $text, self::CMD_PAGE_FIRST, ''); |
529
|
|
|
$controls->add($button); |
530
|
|
|
$controls->add("\n"); |
531
|
|
|
} |
532
|
|
|
$button = $this->createPagerButton($buttonType, true, $this->getPrevPageText(), self::CMD_PAGE_PREV, ''); |
533
|
|
|
$controls->add($button); |
534
|
|
|
} |
535
|
|
|
$controls->add("\n"); |
536
|
|
|
if ($this->getIsLastPage()) { |
537
|
|
|
$label = $this->createPagerButton($buttonType, false, $this->getNextPageText(), self::CMD_PAGE_NEXT, ''); |
538
|
|
|
$controls->add($label); |
539
|
|
|
if (($text = $this->getLastPageText()) !== '') { |
540
|
|
|
$controls->add("\n"); |
541
|
|
|
$label = $this->createPagerButton($buttonType, false, $text, self::CMD_PAGE_LAST, ''); |
542
|
|
|
$controls->add($label); |
543
|
|
|
} |
544
|
|
|
} else { |
545
|
|
|
$button = $this->createPagerButton($buttonType, true, $this->getNextPageText(), self::CMD_PAGE_NEXT, ''); |
546
|
|
|
$controls->add($button); |
547
|
|
|
if (($text = $this->getLastPageText()) !== '') { |
548
|
|
|
$controls->add("\n"); |
549
|
|
|
$button = $this->createPagerButton($buttonType, true, $text, self::CMD_PAGE_LAST, ''); |
550
|
|
|
$controls->add($button); |
551
|
|
|
} |
552
|
|
|
} |
553
|
|
|
} |
554
|
|
|
|
555
|
|
|
/** |
556
|
|
|
* Builds a numeric pager |
557
|
|
|
*/ |
558
|
|
|
protected function buildNumericPager() |
559
|
|
|
{ |
560
|
|
|
$buttonType = $this->getButtonType(); |
561
|
|
|
$controls = $this->getControls(); |
562
|
|
|
$pageCount = $this->getPageCount(); |
563
|
|
|
$pageIndex = $this->getCurrentPageIndex() + 1; |
564
|
|
|
$maxButtonCount = $this->getPageButtonCount(); |
565
|
|
|
$buttonCount = $maxButtonCount > $pageCount ? $pageCount : $maxButtonCount; |
566
|
|
|
$startPageIndex = 1; |
567
|
|
|
$endPageIndex = $buttonCount; |
568
|
|
|
if ($pageIndex > $endPageIndex) { |
569
|
|
|
$startPageIndex = ((int) (($pageIndex - 1) / $maxButtonCount)) * $maxButtonCount + 1; |
570
|
|
|
if (($endPageIndex = $startPageIndex + $maxButtonCount - 1) > $pageCount) { |
571
|
|
|
$endPageIndex = $pageCount; |
572
|
|
|
} |
573
|
|
|
if ($endPageIndex - $startPageIndex + 1 < $maxButtonCount) { |
574
|
|
|
if (($startPageIndex = $endPageIndex - $maxButtonCount + 1) < 1) { |
575
|
|
|
$startPageIndex = 1; |
576
|
|
|
} |
577
|
|
|
} |
578
|
|
|
} |
579
|
|
|
|
580
|
|
|
if ($startPageIndex > 1) { |
581
|
|
|
if (($text = $this->getFirstPageText()) !== '') { |
582
|
|
|
$button = $this->createPagerButton($buttonType, true, $text, self::CMD_PAGE_FIRST, ''); |
|
|
|
|
583
|
|
|
$controls->add($button); |
584
|
|
|
$controls->add("\n"); |
585
|
|
|
} |
586
|
|
|
$prevPageIndex = $startPageIndex - 1; |
587
|
|
|
$button = $this->createPagerButton($buttonType, true, $this->getPrevPageText(), self::CMD_PAGE, "$prevPageIndex"); |
588
|
|
|
$controls->add($button); |
589
|
|
|
$controls->add("\n"); |
590
|
|
|
} |
591
|
|
|
|
592
|
|
|
for ($i = $startPageIndex; $i <= $endPageIndex; ++$i) { |
593
|
|
|
if ($i === $pageIndex) { |
594
|
|
|
$label = $this->createPagerButton($buttonType, false, "$i", self::CMD_PAGE, ''); |
595
|
|
|
$controls->add($label); |
596
|
|
|
} else { |
597
|
|
|
$button = $this->createPagerButton($buttonType, true, "$i", self::CMD_PAGE, "$i"); |
598
|
|
|
$controls->add($button); |
599
|
|
|
} |
600
|
|
|
if ($i < $endPageIndex) { |
601
|
|
|
$controls->add("\n"); |
602
|
|
|
} |
603
|
|
|
} |
604
|
|
|
|
605
|
|
|
if ($pageCount > $endPageIndex) { |
606
|
|
|
$controls->add("\n"); |
607
|
|
|
$nextPageIndex = $endPageIndex + 1; |
608
|
|
|
$button = $this->createPagerButton($buttonType, true, $this->getNextPageText(), self::CMD_PAGE, "$nextPageIndex"); |
609
|
|
|
$controls->add($button); |
610
|
|
|
if (($text = $this->getLastPageText()) !== '') { |
611
|
|
|
$controls->add("\n"); |
612
|
|
|
$button = $this->createPagerButton($buttonType, true, $text, self::CMD_PAGE_LAST, ''); |
613
|
|
|
$controls->add($button); |
614
|
|
|
} |
615
|
|
|
} |
616
|
|
|
} |
617
|
|
|
|
618
|
|
|
/** |
619
|
|
|
* Builds a dropdown list pager |
620
|
|
|
*/ |
621
|
|
|
protected function buildListPager() |
622
|
|
|
{ |
623
|
|
|
$list = new TDropDownList(); |
624
|
|
|
$this->getControls()->add($list); |
625
|
|
|
$list->setDataSource(range(1, $this->getPageCount())); |
626
|
|
|
$list->dataBind(); |
627
|
|
|
$list->setSelectedIndex($this->getCurrentPageIndex()); |
628
|
|
|
$list->setAutoPostBack(true); |
629
|
|
|
$list->attachEventHandler('OnSelectedIndexChanged', [$this, 'listIndexChanged']); |
630
|
|
|
} |
631
|
|
|
|
632
|
|
|
/** |
633
|
|
|
* Event handler to the OnSelectedIndexChanged event of the dropdown list. |
634
|
|
|
* This handler will raise {@see onPageIndexChanged OnPageIndexChanged} event. |
635
|
|
|
* @param TDropDownList $sender the dropdown list control raising the event |
636
|
|
|
* @param \Prado\TEventParameter $param event parameter |
637
|
|
|
*/ |
638
|
|
|
public function listIndexChanged($sender, $param) |
|
|
|
|
639
|
|
|
{ |
640
|
|
|
$pageIndex = $sender->getSelectedIndex(); |
641
|
|
|
$this->onPageIndexChanged(new TPagerPageChangedEventParameter($sender, $pageIndex)); |
642
|
|
|
} |
643
|
|
|
|
644
|
|
|
/** |
645
|
|
|
* This event is raised when page index is changed due to a page button click. |
646
|
|
|
* @param TPagerPageChangedEventParameter $param event parameter |
647
|
|
|
*/ |
648
|
|
|
public function onPageIndexChanged($param) |
649
|
|
|
{ |
650
|
|
|
$this->raiseEvent('OnPageIndexChanged', $this, $param); |
651
|
|
|
} |
652
|
|
|
|
653
|
|
|
/** |
654
|
|
|
* Processes a bubbled event. |
655
|
|
|
* This method overrides parent's implementation by wrapping event parameter |
656
|
|
|
* for <b>OnCommand</b> event with item information. |
657
|
|
|
* @param \Prado\Web\UI\TControl $sender the sender of the event |
658
|
|
|
* @param \Prado\TEventParameter $param event parameter |
659
|
|
|
* @return bool whether the event bubbling should stop here. |
660
|
|
|
*/ |
661
|
|
|
public function bubbleEvent($sender, $param) |
662
|
|
|
{ |
663
|
|
|
if ($param instanceof \Prado\Web\UI\TCommandEventParameter) { |
664
|
|
|
$command = $param->getCommandName(); |
665
|
|
|
if (strcasecmp($command, self::CMD_PAGE) === 0) { |
666
|
|
|
$pageIndex = TPropertyValue::ensureInteger($param->getCommandParameter()) - 1; |
667
|
|
|
$this->onPageIndexChanged(new TPagerPageChangedEventParameter($sender, $pageIndex)); |
668
|
|
|
return true; |
669
|
|
|
} elseif (strcasecmp($command, self::CMD_PAGE_NEXT) === 0) { |
670
|
|
|
$pageIndex = $this->getCurrentPageIndex() + 1; |
671
|
|
|
$this->onPageIndexChanged(new TPagerPageChangedEventParameter($sender, $pageIndex)); |
672
|
|
|
return true; |
673
|
|
|
} elseif (strcasecmp($command, self::CMD_PAGE_PREV) === 0) { |
674
|
|
|
$pageIndex = $this->getCurrentPageIndex() - 1; |
675
|
|
|
$this->onPageIndexChanged(new TPagerPageChangedEventParameter($sender, $pageIndex)); |
676
|
|
|
return true; |
677
|
|
|
} elseif (strcasecmp($command, self::CMD_PAGE_FIRST) === 0) { |
678
|
|
|
$this->onPageIndexChanged(new TPagerPageChangedEventParameter($sender, 0)); |
679
|
|
|
return true; |
680
|
|
|
} elseif (strcasecmp($command, self::CMD_PAGE_LAST) === 0) { |
681
|
|
|
$this->onPageIndexChanged(new TPagerPageChangedEventParameter($sender, $this->getPageCount() - 1)); |
682
|
|
|
return true; |
683
|
|
|
} |
684
|
|
|
return false; |
685
|
|
|
} else { |
686
|
|
|
return false; |
687
|
|
|
} |
688
|
|
|
} |
689
|
|
|
} |
690
|
|
|
|