Completed
Push — pr/13020 ( fbd44c )
by
unknown
11:53
created

LinkPager::renderPageButton()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 17
ccs 12
cts 12
cp 1
rs 9.2
cc 4
eloc 11
nc 8
nop 5
crap 4
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\widgets;
9
10
use Yii;
11
use yii\base\InvalidConfigException;
12
use yii\helpers\Html;
13
use yii\base\Widget;
14
use yii\data\Pagination;
15
use yii\helpers\ArrayHelper;
16
17
/**
18
 * LinkPager displays a list of hyperlinks that lead to different pages of target.
19
 *
20
 * LinkPager works with a [[Pagination]] object which specifies the total number
21
 * of pages and the current page number.
22
 *
23
 * Note that LinkPager only generates the necessary HTML markups. In order for it
24
 * to look like a real pager, you should provide some CSS styles for it.
25
 * With the default configuration, LinkPager should look good using Twitter Bootstrap CSS framework.
26
 *
27
 * For more details and usage information on LinkPager, see the [guide article on pagination](guide:output-pagination).
28
 *
29
 * @author Qiang Xue <[email protected]>
30
 * @since 2.0
31
 */
32
class LinkPager extends Widget
33
{
34
    /**
35
     * @var Pagination the pagination object that this pager is associated with.
36
     * You must set this property in order to make LinkPager work.
37
     */
38
    public $pagination;
39
    /**
40
     * @var array HTML attributes for the pager container tag.
41
     * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
42
     */
43
    public $options = ['class' => 'pagination'];
44
    /**
45
     * @var array HTML attributes for the link in a pager container tag.
46
     * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
47
     */
48
    public $linkOptions = [];
49
    /**
50
     * @var string the CSS class for the each page button.
51
     * @since 2.0.7
52
     */
53
    public $pageCssClass;
54
    /**
55
     * @var string the CSS class for the "first" page button.
56
     */
57
    public $firstPageCssClass = 'first';
58
    /**
59
     * @var string the CSS class for the "last" page button.
60
     */
61
    public $lastPageCssClass = 'last';
62
    /**
63
     * @var string the CSS class for the "previous" page button.
64
     */
65
    public $prevPageCssClass = 'prev';
66
    /**
67
     * @var string the CSS class for the "next" page button.
68
     */
69
    public $nextPageCssClass = 'next';
70
    /**
71
     * @var string the CSS class for the active (currently selected) page button.
72
     */
73
    public $activePageCssClass = 'active';
74
    /**
75
     * @var string the CSS class for the disabled page buttons.
76
     */
77
    public $disabledPageCssClass = 'disabled';
78
    
79
    /**
80
     * @var array the options for the disabled tag to be generated inside the disabled list element.
81
     * In order to customize the html tag, please use the tag key.
82
     * 
83
     * ```php
84
     * $disabledListItemSubTagOptions = ['tag' => 'div', 'class' => 'disabled-div'];
85
     * ```
86
     * @since 2.0.11
87
     */
88
    public $disabledListItemSubTagOptions = [];
89
    
90
    /**
91
     * @var int maximum number of page buttons that can be displayed. Defaults to 10.
92
     */
93
    public $maxButtonCount = 10;
94
    /**
95
     * @var string|bool the label for the "next" page button. Note that this will NOT be HTML-encoded.
96
     * If this property is false, the "next" page button will not be displayed.
97
     */
98
    public $nextPageLabel = '&raquo;';
99
    /**
100
     * @var string|bool the text label for the previous page button. Note that this will NOT be HTML-encoded.
101
     * If this property is false, the "previous" page button will not be displayed.
102
     */
103
    public $prevPageLabel = '&laquo;';
104
    /**
105
     * @var string|bool the text label for the "first" page button. Note that this will NOT be HTML-encoded.
106
     * If it's specified as true, page number will be used as label.
107
     * Default is false that means the "first" page button will not be displayed.
108
     */
109
    public $firstPageLabel = false;
110
    /**
111
     * @var string|bool the text label for the "last" page button. Note that this will NOT be HTML-encoded.
112
     * If it's specified as true, page number will be used as label.
113
     * Default is false that means the "last" page button will not be displayed.
114
     */
115
    public $lastPageLabel = false;
116
    /**
117
     * @var bool whether to register link tags in the HTML header for prev, next, first and last page.
118
     * Defaults to `false` to avoid conflicts when multiple pagers are used on one page.
119
     * @see http://www.w3.org/TR/html401/struct/links.html#h-12.1.2
120
     * @see registerLinkTags()
121
     */
122
    public $registerLinkTags = false;
123
    /**
124
     * @var bool Hide widget when only one page exist.
125
     */
126
    public $hideOnSinglePage = true;
127
128
129
    /**
130
     * Initializes the pager.
131
     */
132 10
    public function init()
133
    {
134 10
        if ($this->pagination === null) {
135
            throw new InvalidConfigException('The "pagination" property must be set.');
136
        }
137 10
    }
138
139
    /**
140
     * Executes the widget.
141
     * This overrides the parent implementation by displaying the generated page buttons.
142
     */
143 10
    public function run()
144
    {
145 10
        if ($this->registerLinkTags) {
146
            $this->registerLinkTags();
147
        }
148 10
        echo $this->renderPageButtons();
149 10
    }
150
151
    /**
152
     * Registers relational link tags in the html header for prev, next, first and last page.
153
     * These links are generated using [[\yii\data\Pagination::getLinks()]].
154
     * @see http://www.w3.org/TR/html401/struct/links.html#h-12.1.2
155
     */
156
    protected function registerLinkTags()
157
    {
158
        $view = $this->getView();
159
        foreach ($this->pagination->getLinks() as $rel => $href) {
160
            $view->registerLinkTag(['rel' => $rel, 'href' => $href], $rel);
161
        }
162
    }
163
164
    /**
165
     * Renders the page buttons.
166
     * @return string the rendering result
167
     */
168 10
    protected function renderPageButtons()
169
    {
170 10
        $pageCount = $this->pagination->getPageCount();
171 10
        if ($pageCount < 2 && $this->hideOnSinglePage) {
172 7
            return '';
173
        }
174
175 3
        $buttons = [];
176 3
        $currentPage = $this->pagination->getPage();
177
178
        // first page
179 3
        $firstPageLabel = $this->firstPageLabel === true ? '1' : $this->firstPageLabel;
180 3
        if ($firstPageLabel !== false) {
181 1
            $buttons[] = $this->renderPageButton($firstPageLabel, 0, $this->firstPageCssClass, $currentPage <= 0, false);
0 ignored issues
show
Bug introduced by
It seems like $firstPageLabel defined by $this->firstPageLabel ==...: $this->firstPageLabel on line 179 can also be of type boolean; however, yii\widgets\LinkPager::renderPageButton() does only seem to accept string, maybe add an additional type check?

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:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
182 1
        }
183
184
        // prev page
185 3
        if ($this->prevPageLabel !== false) {
186 3
            if (($page = $currentPage - 1) < 0) {
187 2
                $page = 0;
188 2
            }
189 3
            $buttons[] = $this->renderPageButton($this->prevPageLabel, $page, $this->prevPageCssClass, $currentPage <= 0, false);
190 3
        }
191
192
        // internal pages
193 3
        list($beginPage, $endPage) = $this->getPageRange();
194 3
        for ($i = $beginPage; $i <= $endPage; ++$i) {
195 3
            $buttons[] = $this->renderPageButton($i + 1, $i, null, false, $i == $currentPage);
196 3
        }
197
198
        // next page
199 3
        if ($this->nextPageLabel !== false) {
200 3
            if (($page = $currentPage + 1) >= $pageCount - 1) {
201
                $page = $pageCount - 1;
202
            }
203 3
            $buttons[] = $this->renderPageButton($this->nextPageLabel, $page, $this->nextPageCssClass, $currentPage >= $pageCount - 1, false);
204 3
        }
205
206
        // last page
207 3
        $lastPageLabel = $this->lastPageLabel === true ? $pageCount : $this->lastPageLabel;
208 3
        if ($lastPageLabel !== false) {
209 1
            $buttons[] = $this->renderPageButton($lastPageLabel, $pageCount - 1, $this->lastPageCssClass, $currentPage >= $pageCount - 1, false);
210 1
        }
211
212 3
        return Html::tag('ul', implode("\n", $buttons), $this->options);
213
    }
214
215
    /**
216
     * Renders a page button.
217
     * You may override this method to customize the generation of page buttons.
218
     * @param string $label the text label for the button
219
     * @param int $page the page number
220
     * @param string $class the CSS class for the page button.
221
     * @param bool $disabled whether this page button is disabled
222
     * @param bool $active whether this page button is active
223
     * @return string the rendering result
224
     */
225 3
    protected function renderPageButton($label, $page, $class, $disabled, $active)
226
    {
227 3
        $options = ['class' => empty($class) ? $this->pageCssClass : $class];
228 3
        if ($active) {
229 3
            Html::addCssClass($options, $this->activePageCssClass);
230 3
        }
231 3
        if ($disabled) {
232 2
            Html::addCssClass($options, $this->disabledPageCssClass);
233 2
            $tag = ArrayHelper::remove($this->disabledListItemSubTagOptions, 'tag', 'span');
234
            
235 2
            return Html::tag('li', Html::tag($tag, $label, $this->disabledListItemSubTagOptions), $options);
236
        }
237 3
        $linkOptions = $this->linkOptions;
238 3
        $linkOptions['data-page'] = $page;
239
240 3
        return Html::tag('li', Html::a($label, $this->pagination->createUrl($page), $linkOptions), $options);
241
    }
242
243
    /**
244
     * @return array the begin and end pages that need to be displayed.
245
     */
246 3
    protected function getPageRange()
247
    {
248 3
        $currentPage = $this->pagination->getPage();
249 3
        $pageCount = $this->pagination->getPageCount();
250
251 3
        $beginPage = max(0, $currentPage - (int) ($this->maxButtonCount / 2));
252 3
        if (($endPage = $beginPage + $this->maxButtonCount - 1) >= $pageCount) {
253 2
            $endPage = $pageCount - 1;
254 2
            $beginPage = max(0, $endPage - $this->maxButtonCount + 1);
255 2
        }
256
257 3
        return [$beginPage, $endPage];
258
    }
259
}
260