|
- <?php
-
-
- namespace yii\widgets;
-
- use Yii;
- use yii\base\InvalidConfigException;
- use yii\helpers\Html;
- use yii\base\Widget;
- use yii\data\Pagination;
-
-
- class LinkPager extends Widget
- {
-
-
- public $pagination;
-
-
- public $options = ['class' => 'pagination'];
-
-
- public $linkOptions = [];
-
-
- public $pageCssClass;
-
-
- public $firstPageCssClass = 'first';
-
-
- public $lastPageCssClass = 'last';
-
-
- public $prevPageCssClass = 'prev';
-
-
- public $nextPageCssClass = 'next';
-
-
- public $activePageCssClass = 'active';
-
-
- public $disabledPageCssClass = 'disabled';
-
-
- public $maxButtonCount = 10;
-
-
- public $nextPageLabel = '»';
-
-
- public $prevPageLabel = '«';
-
-
- public $firstPageLabel = false;
-
-
- public $lastPageLabel = false;
-
-
- public $registerLinkTags = false;
-
-
- public $hideOnSinglePage = true;
-
-
-
-
- public function init()
- {
- if ($this->pagination === null) {
- throw new InvalidConfigException('The "pagination" property must be set.');
- }
- }
-
-
-
- public function run()
- {
- if ($this->registerLinkTags) {
- $this->registerLinkTags();
- }
- echo $this->renderPageButtons();
- }
-
-
-
- protected function registerLinkTags()
- {
- $view = $this->getView();
- foreach ($this->pagination->getLinks() as $rel => $href) {
- $view->registerLinkTag(['rel' => $rel, 'href' => $href], $rel);
- }
- }
-
-
-
- protected function renderPageButtons()
- {
- $pageCount = $this->pagination->getPageCount();
- if ($pageCount < 2 && $this->hideOnSinglePage) {
- return '';
- }
-
- $buttons = [];
- $currentPage = $this->pagination->getPage();
-
-
- $firstPageLabel = $this->firstPageLabel === true ? '1' : $this->firstPageLabel;
- if ($firstPageLabel !== false) {
- $buttons[] = $this->renderPageButton($firstPageLabel, 0, $this->firstPageCssClass, $currentPage <= 0, false);
- }
-
-
- if ($this->prevPageLabel !== false) {
- if (($page = $currentPage - 1) < 0) {
- $page = 0;
- }
- $buttons[] = $this->renderPageButton($this->prevPageLabel, $page, $this->prevPageCssClass, $currentPage <= 0, false);
- }
-
-
- list($beginPage, $endPage) = $this->getPageRange();
- for ($i = $beginPage; $i <= $endPage; ++$i) {
- $buttons[] = $this->renderPageButton($i + 1, $i, null, false, $i == $currentPage);
- }
-
-
- if ($this->nextPageLabel !== false) {
- if (($page = $currentPage + 1) >= $pageCount - 1) {
- $page = $pageCount - 1;
- }
- $buttons[] = $this->renderPageButton($this->nextPageLabel, $page, $this->nextPageCssClass, $currentPage >= $pageCount - 1, false);
- }
-
-
- $lastPageLabel = $this->lastPageLabel === true ? $pageCount : $this->lastPageLabel;
- if ($lastPageLabel !== false) {
- $buttons[] = $this->renderPageButton($lastPageLabel, $pageCount - 1, $this->lastPageCssClass, $currentPage >= $pageCount - 1, false);
- }
-
- return Html::tag('ul', implode("\n", $buttons), $this->options);
- }
-
-
-
- protected function renderPageButton($label, $page, $class, $disabled, $active)
- {
- $options = ['class' => empty($class) ? $this->pageCssClass : $class];
- if ($active) {
- Html::addCssClass($options, $this->activePageCssClass);
- }
- if ($disabled) {
- Html::addCssClass($options, $this->disabledPageCssClass);
-
- return Html::tag('li', Html::tag('span', $label), $options);
- }
- $linkOptions = $this->linkOptions;
- $linkOptions['data-page'] = $page;
-
- return Html::tag('li', Html::a($label, $this->pagination->createUrl($page), $linkOptions), $options);
- }
-
-
-
- protected function getPageRange()
- {
- $currentPage = $this->pagination->getPage();
- $pageCount = $this->pagination->getPageCount();
-
- $beginPage = max(0, $currentPage - (int) ($this->maxButtonCount / 2));
- if (($endPage = $beginPage + $this->maxButtonCount - 1) >= $pageCount) {
- $endPage = $pageCount - 1;
- $beginPage = max(0, $endPage - $this->maxButtonCount + 1);
- }
-
- return [$beginPage, $endPage];
- }
- }
|