Completed
Push — master ( 7c7d75...134283 )
by Dmitry
10:06
created

LinkPager::getPageRange()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.1481

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
ccs 6
cts 9
cp 0.6667
cc 2
eloc 8
nc 2
nop 0
crap 2.1481
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
16
/**
17
 * LinkPager displays a list of hyperlinks that lead to different pages of target.
18
 *
19
 * LinkPager works with a [[Pagination]] object which specifies the total number
20
 * of pages and the current page number.
21
 *
22
 * Note that LinkPager only generates the necessary HTML markups. In order for it
23
 * to look like a real pager, you should provide some CSS styles for it.
24
 * With the default configuration, LinkPager should look good using Twitter Bootstrap CSS framework.
25
 *
26
 * For more details and usage information on LinkPager, see the [guide article on pagination](guide:output-pagination).
27
 *
28
 * @author Qiang Xue <[email protected]>
29
 * @since 2.0
30
 */
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 = '&raquo;';
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 = '&laquo;';
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()
120
    {
121 8
        if ($this->pagination === null) {
122
            throw new InvalidConfigException('The "pagination" property must be set.');
123
        }
124 8
    }
125
126
    /**
127
     * Executes the widget.
128
     * This overrides the parent implementation by displaying the generated page buttons.
129
     */
130 8
    public function run()
131
    {
132 8
        if ($this->registerLinkTags) {
133
            $this->registerLinkTags();
134
        }
135 8
        echo $this->renderPageButtons();
136 8
    }
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()
144
    {
145
        $view = $this->getView();
146
        foreach ($this->pagination->getLinks() as $rel => $href) {
147
            $view->registerLinkTag(['rel' => $rel, 'href' => $href], $rel);
148
        }
149
    }
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);
0 ignored issues
show
Bug introduced by
It seems like $firstPageLabel defined by $this->firstPageLabel ==...: $this->firstPageLabel on line 166 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...
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)
213
    {
214 1
        $options = ['class' => empty($class) ? $this->pageCssClass : $class];
215 1
        if ($active) {
216 1
            Html::addCssClass($options, $this->activePageCssClass);
217 1
        }
218 1
        if ($disabled) {
219
            Html::addCssClass($options, $this->disabledPageCssClass);
220
221
            return Html::tag('li', Html::tag('span', $label), $options);
222
        }
223 1
        $linkOptions = $this->linkOptions;
224 1
        $linkOptions['data-page'] = $page;
225
226 1
        return Html::tag('li', Html::a($label, $this->pagination->createUrl($page), $linkOptions), $options);
227
    }
228
229
    /**
230
     * @return array the begin and end pages that need to be displayed.
231
     */
232 1
    protected function getPageRange()
233
    {
234 1
        $currentPage = $this->pagination->getPage();
235 1
        $pageCount = $this->pagination->getPageCount();
236
237 1
        $beginPage = max(0, $currentPage - (int) ($this->maxButtonCount / 2));
238 1
        if (($endPage = $beginPage + $this->maxButtonCount - 1) >= $pageCount) {
239
            $endPage = $pageCount - 1;
240
            $beginPage = max(0, $endPage - $this->maxButtonCount + 1);
241
        }
242
243 1
        return [$beginPage, $endPage];
244
    }
245
}
246