1 | <?php |
||
31 | class LinkPager extends Widget |
||
32 | { |
||
33 | /** |
||
34 | * @var Pagination the pagination object that this pager is associated with. |
||
35 | * You must set this property in order to make LinkPager work. |
||
36 | */ |
||
37 | public $pagination; |
||
38 | /** |
||
39 | * @var array HTML attributes for the pager container tag. |
||
40 | * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
||
41 | */ |
||
42 | public $options = ['class' => 'pagination']; |
||
43 | /** |
||
44 | * @var array HTML attributes for the link in a pager container tag. |
||
45 | * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
||
46 | */ |
||
47 | public $linkOptions = []; |
||
48 | /** |
||
49 | * @var string the CSS class for the each page button. |
||
50 | * @since 2.0.7 |
||
51 | */ |
||
52 | public $pageCssClass; |
||
53 | /** |
||
54 | * @var string the CSS class for the "first" page button. |
||
55 | */ |
||
56 | public $firstPageCssClass = 'first'; |
||
57 | /** |
||
58 | * @var string the CSS class for the "last" page button. |
||
59 | */ |
||
60 | public $lastPageCssClass = 'last'; |
||
61 | /** |
||
62 | * @var string the CSS class for the "previous" page button. |
||
63 | */ |
||
64 | public $prevPageCssClass = 'prev'; |
||
65 | /** |
||
66 | * @var string the CSS class for the "next" page button. |
||
67 | */ |
||
68 | public $nextPageCssClass = 'next'; |
||
69 | /** |
||
70 | * @var string the CSS class for the active (currently selected) page button. |
||
71 | */ |
||
72 | public $activePageCssClass = 'active'; |
||
73 | /** |
||
74 | * @var string the CSS class for the disabled page buttons. |
||
75 | */ |
||
76 | public $disabledPageCssClass = 'disabled'; |
||
77 | /** |
||
78 | * @var int maximum number of page buttons that can be displayed. Defaults to 10. |
||
79 | */ |
||
80 | public $maxButtonCount = 10; |
||
81 | /** |
||
82 | * @var string|bool the label for the "next" page button. Note that this will NOT be HTML-encoded. |
||
83 | * If this property is false, the "next" page button will not be displayed. |
||
84 | */ |
||
85 | public $nextPageLabel = '»'; |
||
86 | /** |
||
87 | * @var string|bool the text label for the previous page button. Note that this will NOT be HTML-encoded. |
||
88 | * If this property is false, the "previous" page button will not be displayed. |
||
89 | */ |
||
90 | public $prevPageLabel = '«'; |
||
91 | /** |
||
92 | * @var string|bool the text label for the "first" page button. Note that this will NOT be HTML-encoded. |
||
93 | * If it's specified as true, page number will be used as label. |
||
94 | * Default is false that means the "first" page button will not be displayed. |
||
95 | */ |
||
96 | public $firstPageLabel = false; |
||
97 | /** |
||
98 | * @var string|bool the text label for the "last" page button. Note that this will NOT be HTML-encoded. |
||
99 | * If it's specified as true, page number will be used as label. |
||
100 | * Default is false that means the "last" page button will not be displayed. |
||
101 | */ |
||
102 | public $lastPageLabel = false; |
||
103 | /** |
||
104 | * @var bool whether to register link tags in the HTML header for prev, next, first and last page. |
||
105 | * Defaults to `false` to avoid conflicts when multiple pagers are used on one page. |
||
106 | * @see http://www.w3.org/TR/html401/struct/links.html#h-12.1.2 |
||
107 | * @see registerLinkTags() |
||
108 | */ |
||
109 | public $registerLinkTags = false; |
||
110 | /** |
||
111 | * @var bool Hide widget when only one page exist. |
||
112 | */ |
||
113 | public $hideOnSinglePage = true; |
||
114 | |||
115 | |||
116 | /** |
||
117 | * Initializes the pager. |
||
118 | */ |
||
119 | 8 | public function init() |
|
125 | |||
126 | /** |
||
127 | * Executes the widget. |
||
128 | * This overrides the parent implementation by displaying the generated page buttons. |
||
129 | */ |
||
130 | 8 | public function run() |
|
137 | |||
138 | /** |
||
139 | * Registers relational link tags in the html header for prev, next, first and last page. |
||
140 | * These links are generated using [[\yii\data\Pagination::getLinks()]]. |
||
141 | * @see http://www.w3.org/TR/html401/struct/links.html#h-12.1.2 |
||
142 | */ |
||
143 | protected function registerLinkTags() |
||
150 | |||
151 | /** |
||
152 | * Renders the page buttons. |
||
153 | * @return string the rendering result |
||
154 | */ |
||
155 | 8 | protected function renderPageButtons() |
|
156 | { |
||
157 | 8 | $pageCount = $this->pagination->getPageCount(); |
|
158 | 8 | if ($pageCount < 2 && $this->hideOnSinglePage) { |
|
159 | 7 | return ''; |
|
160 | } |
||
161 | |||
162 | 1 | $buttons = []; |
|
163 | 1 | $currentPage = $this->pagination->getPage(); |
|
164 | |||
165 | // first page |
||
166 | 1 | $firstPageLabel = $this->firstPageLabel === true ? '1' : $this->firstPageLabel; |
|
167 | 1 | if ($firstPageLabel !== false) { |
|
168 | 1 | $buttons[] = $this->renderPageButton($firstPageLabel, 0, $this->firstPageCssClass, $currentPage <= 0, false); |
|
|
|||
169 | 1 | } |
|
170 | |||
171 | // prev page |
||
172 | 1 | if ($this->prevPageLabel !== false) { |
|
173 | 1 | if (($page = $currentPage - 1) < 0) { |
|
174 | $page = 0; |
||
175 | } |
||
176 | 1 | $buttons[] = $this->renderPageButton($this->prevPageLabel, $page, $this->prevPageCssClass, $currentPage <= 0, false); |
|
177 | 1 | } |
|
178 | |||
179 | // internal pages |
||
180 | 1 | list($beginPage, $endPage) = $this->getPageRange(); |
|
181 | 1 | for ($i = $beginPage; $i <= $endPage; ++$i) { |
|
182 | 1 | $buttons[] = $this->renderPageButton($i + 1, $i, null, false, $i == $currentPage); |
|
183 | 1 | } |
|
184 | |||
185 | // next page |
||
186 | 1 | if ($this->nextPageLabel !== false) { |
|
187 | 1 | if (($page = $currentPage + 1) >= $pageCount - 1) { |
|
188 | $page = $pageCount - 1; |
||
189 | } |
||
190 | 1 | $buttons[] = $this->renderPageButton($this->nextPageLabel, $page, $this->nextPageCssClass, $currentPage >= $pageCount - 1, false); |
|
191 | 1 | } |
|
192 | |||
193 | // last page |
||
194 | 1 | $lastPageLabel = $this->lastPageLabel === true ? $pageCount : $this->lastPageLabel; |
|
195 | 1 | if ($lastPageLabel !== false) { |
|
196 | 1 | $buttons[] = $this->renderPageButton($lastPageLabel, $pageCount - 1, $this->lastPageCssClass, $currentPage >= $pageCount - 1, false); |
|
197 | 1 | } |
|
198 | |||
199 | 1 | return Html::tag('ul', implode("\n", $buttons), $this->options); |
|
200 | } |
||
201 | |||
202 | /** |
||
203 | * Renders a page button. |
||
204 | * You may override this method to customize the generation of page buttons. |
||
205 | * @param string $label the text label for the button |
||
206 | * @param int $page the page number |
||
207 | * @param string $class the CSS class for the page button. |
||
208 | * @param bool $disabled whether this page button is disabled |
||
209 | * @param bool $active whether this page button is active |
||
210 | * @return string the rendering result |
||
211 | */ |
||
212 | 1 | protected function renderPageButton($label, $page, $class, $disabled, $active) |
|
228 | |||
229 | /** |
||
230 | * @return array the begin and end pages that need to be displayed. |
||
231 | */ |
||
232 | 1 | protected function getPageRange() |
|
245 | } |
||
246 |
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.