FrontendPager.php 5.03 KB
<?php
    
    namespace frontend\widgets;

    use function key_exists;
    use yii\helpers\ArrayHelper;
    use yii\helpers\Html;

    /**
     * Class FrontendPager
     *
     * @package frontend\widgets
     */
    class FrontendPager extends \yii\widgets\LinkPager
    {
        public $dots = false;
        public $dotsClass = null;
    
        public $params = null;
    
        public $pageParam = 'page';
    
        public $defaultPageSize = 20;
    
        public $pageSizeParam = 'per-page';
    
        public $route;
    
        public $urlManager;
    
        protected function renderPageButtons()
        {
            $pageCount = $this->pagination->getPageCount();
            if ($pageCount < 2 && $this->hideOnSinglePage) {
                return '';
            }
    
            $buttons = [];
            $currentPage = $this->pagination->getPage();
    
            // first page
    
            // prev page
            if ($this->prevPageLabel !== false) {
                if (( $page = $currentPage - 1 ) < 0) {
                    $page = 0;
                }
                $buttons[] = $this->renderPageButton(
                    $this->prevPageLabel,
                    $page,
                    $this->prevPageCssClass,
                    $currentPage <= 0,
                    false
                );
            }
            $firstPageLabel = '1';
            list($beginPage, $endPage) = $this->getPageRange();
            if ($beginPage !== 0) {
                $buttons[] = $this->renderPageButton(
                    $firstPageLabel,
                    0,
                    $this->firstPageCssClass,
                    $currentPage <= 0,
                    false
                );
            }
    
            // internal pages
    
            for ($i = $beginPage; $i <= $endPage; ++$i) {
                $buttons[] = $this->renderPageButton(
                    $i + 1,
                    $i,
                    null,
                    $this->disableCurrentPageButton && $i == $currentPage,
                    $i == $currentPage
                );
            }
            // last page
            $lastPageLabel = $this->lastPageLabel === true ? $pageCount : $this->lastPageLabel;
            if ($lastPageLabel !== false and $endPage !== $pageCount - 1) {
                if ($endPage + 1 < $pageCount - 1 and $this->dots) {
                    $buttons[] = $this->renderPageButton('...', $endPage + 1, $this->dotsClass, true, false);
                }
                if ($lastPageLabel == 'last_number') {
                    $lastPageLabel = $pageCount;
                }
                $buttons[] = $this->renderPageButton(
                    $lastPageLabel,
                    $pageCount - 1,
                    $this->lastPageCssClass,
                    $currentPage >= $pageCount - 1,
                    false
                );
            }
            // next page
            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
                );
            }
            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);
                $tag = ArrayHelper::remove($this->disabledListItemSubTagOptions, 'tag', 'span');
    
                return Html::tag('li', Html::tag($tag, $label, $this->disabledListItemSubTagOptions), $options);
            }
            $linkOptions = $this->linkOptions;
            $linkOptions[ 'data-page' ] = $page;
            if ($page == 0 and !$active) {
                $link = stristr($this->pagination->createUrl(0), '?', true);
                /**
                 * @var \artbox\catalog\models\Filter $filter
                 */
                $filter = \Yii::$app->get('filter')->filterObj;
                if (key_exists($link, $filter->getReplacedFilters())) {
                    $link = $filter->getReplacedFilters()[ $link ];
                }
                return Html::tag(
                    'li',
                    Html::a($label, $link, $linkOptions),
                    $options
                );
            }
            if ($active) {
                return Html::tag('li', Html::a($label, null, $linkOptions), $options);
            } else {
                return Html::tag('li', Html::a($label, $this->pagination->createUrl($page), $linkOptions), $options);
            }
    
        }
    
    }