|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\widgets; |
|
4
|
|
|
|
|
5
|
|
|
use yii\helpers\Html; |
|
6
|
|
|
|
|
7
|
|
|
class LinkPager extends \yii\widgets\LinkPager |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Renders the page buttons. |
|
11
|
|
|
* @return string the rendering result |
|
12
|
|
|
*/ |
|
13
|
|
|
protected function renderPageButtons() |
|
14
|
|
|
{ |
|
15
|
|
|
$pageCount = $this->pagination->getPageCount(); |
|
16
|
|
|
if ($pageCount < 2 && $this->hideOnSinglePage) { |
|
17
|
|
|
return ''; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
$buttons = []; |
|
21
|
|
|
$currentPage = $this->pagination->getPage(); |
|
22
|
|
|
|
|
23
|
|
|
// first page |
|
24
|
|
|
if ($this->firstPageLabel !== false) { |
|
25
|
|
|
$buttons[] = $this->renderPageButton($this->firstPageLabel, 0, $this->firstPageCssClass, $currentPage <= 0, false); |
|
|
|
|
|
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
// prev page |
|
29
|
|
|
if ($this->prevPageLabel !== false) { |
|
30
|
|
|
if (($page = $currentPage - 1) < 0) { |
|
31
|
|
|
$page = 0; |
|
32
|
|
|
} |
|
33
|
|
|
$buttons[] = $this->renderPageButton($this->prevPageLabel, $page, $this->prevPageCssClass, $currentPage <= 0, false, 'prev'); |
|
|
|
|
|
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
// internal pages |
|
37
|
|
|
list($beginPage, $endPage) = $this->getPageRange(); |
|
38
|
|
|
for ($i = $beginPage; $i <= $endPage; ++$i) { |
|
39
|
|
|
$buttons[] = $this->renderPageButton($i + 1, $i, null, false, $i == $currentPage); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
// next page |
|
43
|
|
|
if ($this->nextPageLabel !== false) { |
|
44
|
|
|
if (($page = $currentPage + 1) >= $pageCount - 1) { |
|
45
|
|
|
$page = $pageCount - 1; |
|
46
|
|
|
} |
|
47
|
|
|
$buttons[] = $this->renderPageButton($this->nextPageLabel, $page, $this->nextPageCssClass, $currentPage >= $pageCount - 1, false, 'next'); |
|
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
// last page |
|
51
|
|
|
if ($this->lastPageLabel !== false) { |
|
52
|
|
|
$buttons[] = $this->renderPageButton($this->lastPageLabel, $pageCount - 1, $this->lastPageCssClass, $currentPage >= $pageCount - 1, false); |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return Html::tag('ul', implode("\n", $buttons), $this->options); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Renders a page button. |
|
60
|
|
|
* You may override this method to customize the generation of page buttons. |
|
61
|
|
|
* @param string $label the text label for the button |
|
62
|
|
|
* @param integer $page the page number |
|
63
|
|
|
* @param string $class the CSS class for the page button. |
|
64
|
|
|
* @param boolean $disabled whether this page button is disabled |
|
65
|
|
|
* @param boolean $active whether this page button is active |
|
66
|
|
|
* @param string $rel the rel attribute for the button |
|
|
|
|
|
|
67
|
|
|
* @return string the rendering result |
|
68
|
|
|
*/ |
|
69
|
|
|
protected function renderPageButton($label, $page, $class, $disabled, $active, $rel = null) |
|
70
|
|
|
{ |
|
71
|
|
|
$options = ['class' => $class === '' ? null : $class]; |
|
72
|
|
|
if (!is_null($rel)) { |
|
73
|
|
|
$options['rel'] = $rel; |
|
74
|
|
|
} |
|
75
|
|
|
if ($active) { |
|
76
|
|
|
Html::addCssClass($options, $this->activePageCssClass); |
|
77
|
|
|
} |
|
78
|
|
|
if ($disabled) { |
|
79
|
|
|
Html::addCssClass($options, $this->disabledPageCssClass); |
|
80
|
|
|
|
|
81
|
|
|
return Html::tag('li', Html::tag('span', $label), $options); |
|
82
|
|
|
} |
|
83
|
|
|
$linkOptions = $this->linkOptions; |
|
84
|
|
|
$linkOptions['data-page'] = $page; |
|
85
|
|
|
|
|
86
|
|
|
return Html::tag('li', Html::a($label, $this->pagination->createUrl($page), $linkOptions), $options); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.