Blame view

frontend/widgets/FrontendPager.php 5.68 KB
cc2da9cc   Anastasia   - question page
1
2
3
4
5
6
7
  <?php
      
      namespace frontend\widgets;
  
      use function key_exists;
      use yii\helpers\ArrayHelper;
      use yii\helpers\Html;
69bdaf70   alex   почистил мусор в ...
8
      use common\components\Substringer;
cc2da9cc   Anastasia   - question page
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
      /**
       * 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
                   */
9de39b6f   alex   забыл закоментить
131
132
133
134
  //                $filter = \Yii::$app->get('filter')->filterObj;
  //                if (key_exists($link, $filter->getReplacedFilters())) {
  //                    $link = $filter->getReplacedFilters()[ $link ];
  //                }
cc2da9cc   Anastasia   - question page
135
136
137
138
139
140
                  return Html::tag(
                      'li',
                      Html::a($label, $link, $linkOptions),
                      $options
                  );
              }
69bdaf70   alex   почистил мусор в ...
141
142
143
  
  
  	        if ($active) {
cc2da9cc   Anastasia   - question page
144
145
                  return Html::tag('li', Html::a($label, null, $linkOptions), $options);
              } else {
69bdaf70   alex   почистил мусор в ...
146
147
148
149
150
151
152
153
154
155
156
157
  		        # убираю весь мусор кроме прямой пагинации с ссылок
  		        $haystack = $this->pagination->createUrl($page);
  
  		        $regex1 = '/(\??|&?)page=\d{1,5}&per-page=\d{1,5}/';
  		        $regex2 = '/(\?|&)sort=[a-zA-z_]+/';
  		        $requiredDelimiter = '?';
  		        $firstConcatenateSymbol = '?';
  		        $secondConcatenateSymbol = '?';
  		        $res = Substringer::changeStringByRegex($haystack, $regex1, $regex2, $firstConcatenateSymbol, $secondConcatenateSymbol, $requiredDelimiter);
  
  		        return Html::tag('li', Html::a($label, $res, $linkOptions), $options);
  		        #return Html::tag('li', Html::a($label, $this->pagination->createUrl($page), $linkOptions), $options);
cc2da9cc   Anastasia   - question page
158
159
160
161
162
              }
      
          }
      
      }