Commit 69bdaf707e2ca531c1264e8e72b1d6c9a97bceb9
1 parent
d38bb7e7
почистил мусор в пагинации на blog/index
Showing
4 changed files
with
191 additions
and
17 deletions
Show diff stats
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +/** | ||
| 4 | + * ====================================================================================================================| | ||
| 5 | + * Компонент, который служит для обрезания строк по заданным параметрам | ||
| 6 | + * ====================================================================================================================| | ||
| 7 | + */ | ||
| 8 | + | ||
| 9 | +namespace common\components; | ||
| 10 | + | ||
| 11 | +use yii\base\Object; | ||
| 12 | + | ||
| 13 | +/** | ||
| 14 | + * Class Substringer | ||
| 15 | + * @package common\components | ||
| 16 | + * ====================================================================================================================| | ||
| 17 | + * @method @static simpleStringSubstring | ||
| 18 | + * @method @static changeStringByRegex | ||
| 19 | + * ====================================================================================================================| | ||
| 20 | + */ | ||
| 21 | +class Substringer extends Object | ||
| 22 | +{ | ||
| 23 | + | ||
| 24 | + | ||
| 25 | + /** | ||
| 26 | + * @param string $haystack | ||
| 27 | + * @param string $needle | ||
| 28 | + * @param bool $reverse | ||
| 29 | + * @return string | ||
| 30 | + *=================================================================================================================| | ||
| 31 | + * @static | ||
| 32 | + * Метод, который берет строку, и обрезает её до заданного момента | ||
| 33 | + *=================================================================================================================| | ||
| 34 | + * @todo | ||
| 35 | + * Пока что метод адекватно работает только при том условии, что нужный нам символ/комбинация символов является | ||
| 36 | + * первой при поисковом запросе, нужно допилить логику, если например: | ||
| 37 | + * $haystack='www.site.com?sort=title_act&sort=category-1&sort=test_val_desc | ||
| 38 | + * то, обрезать всё до нужной комбинации | ||
| 39 | + *=================================================================================================================| | ||
| 40 | + * @example 1 | ||
| 41 | + * Start Data: | ||
| 42 | + * $haystack='https://www.youtube.com?v=OBwS66EBUcY | ||
| 43 | + * $needle='?'; | ||
| 44 | + * Return result: | ||
| 45 | + * $res='https://www.youtube.com/watch'; | ||
| 46 | + * @example 2 | ||
| 47 | + * Start data | ||
| 48 | + * $haystack='https://www.youtube.com/watch?v=OBwS66EBUcY'; | ||
| 49 | + * $needle ='?'; | ||
| 50 | + * $res = '?v=OBwS66EBUcY' | ||
| 51 | + * | ||
| 52 | + */ | ||
| 53 | + public static function simpleStringSubstring(string $haystack, string $needle): string | ||
| 54 | + { | ||
| 55 | + $deletePosition = strpos($haystack, $needle); | ||
| 56 | + if ($deletePosition == 0) | ||
| 57 | + return $haystack; | ||
| 58 | + $result = substr($haystack, 0, $deletePosition); | ||
| 59 | + return $result; | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + | ||
| 63 | + | ||
| 64 | + | ||
| 65 | + | ||
| 66 | + | ||
| 67 | + | ||
| 68 | + /** | ||
| 69 | + * @param string $haystack | ||
| 70 | + * @param string $regex1 | ||
| 71 | + * @param string $regex2 | ||
| 72 | + * @param string $requiredDelimiter | ||
| 73 | + * @param string $firstConcatenateSymbol | ||
| 74 | + * @param string $secondConcatenateSymbol | ||
| 75 | + * @return string | ||
| 76 | + * ================================================================================================================| | ||
| 77 | + * Метод, который берет $haystack, а так же 2 Regex паттерна | ||
| 78 | + * 1) пытается найти regex совпадения | ||
| 79 | + * если их нет @return original $haystack | ||
| 80 | + * 2) вырезает всё лишнее со строки с помощью self::Substringer | ||
| 81 | + * 3) конкатенирует между собой то,что получилось за принципом | ||
| 82 | + * mainString+delimiter1+regex1Result+delimiter2+regex2Result | ||
| 83 | + * ================================================================================================================| | ||
| 84 | + * @example | ||
| 85 | + * $haystack='https://www.linija-svitla.ua/catalog/ulichnoe-osveshchenie?page_num=3&fcatlist=3268%2C&sort=test_test&3269%2C3270%2C3272%2C3273%2C3274%2C3275&page=50&per-page=18'; | ||
| 86 | + * $regex1='/(\??|&?)page=\d{1,5}&per-page=\d{1,5}/'; | ||
| 87 | + * $regex2='/(\?|&)sort=[a-zA-z_]+/'; | ||
| 88 | + * $requiredDelimiter='?'; | ||
| 89 | + * $firstConcatenateSymbol='?'; | ||
| 90 | + * $secondConcatenateSymbol='&'; | ||
| 91 | + * $res=$this->changeStringByRegex($haystack,$regex1,$regex2,$firstConcatenateSymbol,$secondConcatenateSymbol,$requiredDelimiter); | ||
| 92 | + * @example-return | ||
| 93 | + * https://www.linija-svitla.ua/catalog/ulichnoe-osveshchenie?page=50&per-page=18&sort=test_test | ||
| 94 | + * ================================================================================================================| | ||
| 95 | + * @todo | ||
| 96 | + * 1) Пока что метод работает только с 2 regex,надо будет поменять строго regex string => regex array | ||
| 97 | + * 2) Метод полюбому обрезает первый символ результирующей строки regex | ||
| 98 | + * 3) нужно сделать механизм замены строки только по 1 regex | ||
| 99 | + * ================================================================================================================| | ||
| 100 | + * | ||
| 101 | + */ | ||
| 102 | + /** | ||
| 103 | + * @param string $haystack | ||
| 104 | + * @param string $regex1 | ||
| 105 | + * @param string $regex2 | ||
| 106 | + * @param string $requiredDelimiter | ||
| 107 | + * @param string $firstConcatenateSymbol | ||
| 108 | + * @param string $secondConcatenateSymbol | ||
| 109 | + * | ||
| 110 | + * @return string | ||
| 111 | + */ | ||
| 112 | + public static function changeStringByRegex(string $haystack, string $regex1, string $regex2, string $requiredDelimiter = '', | ||
| 113 | + string $firstConcatenateSymbol = '', | ||
| 114 | + string $secondConcatenateSymbol = '' | ||
| 115 | + ): string | ||
| 116 | + { | ||
| 117 | + | ||
| 118 | + # 1 give rexe1/regex2 parts | ||
| 119 | + # IF we have no consilience with both Regex == > return $haystack | ||
| 120 | + if (preg_match($regex1, $haystack) !== 0 || preg_match($regex2, $haystack) !== 0) { | ||
| 121 | + preg_match($regex1, $haystack, $matches[0]); | ||
| 122 | + preg_match($regex2, $haystack, $matches[1]); | ||
| 123 | + } else return $haystack; | ||
| 124 | + | ||
| 125 | + # 2 give must part of string | ||
| 126 | + $mustPartOfstring = self::SimpleStringSubstring($haystack, $requiredDelimiter); | ||
| 127 | + | ||
| 128 | + # 3 if regex1/regex2 !empty concatenate they with $mustPartOfString | ||
| 129 | + if (isset($matches[0][0]) && isset($matches[1][0])) { | ||
| 130 | + # удаляем первый символ ( прим; $matches[0][0]='&sort=test_desc') | ||
| 131 | + # нам надо только текст без первого спецсимвола | ||
| 132 | + $matches[0][0] = substr($matches[0][0], 1); | ||
| 133 | + $mustPartOfstring = (isset($matches[0][0])) ? $mustPartOfstring . $firstConcatenateSymbol . $matches[0][0] : $mustPartOfstring; | ||
| 134 | + $matches[1][0] = substr($matches[1][0], 1); | ||
| 135 | + $mustPartOfstring = (isset($matches[1][0])) ? $mustPartOfstring . $secondConcatenateSymbol . $matches[1][0] : $mustPartOfstring; | ||
| 136 | + } # если найден только 1й regex | ||
| 137 | + elseif (isset($matches[0][0]) && !isset($matches[1][0])) { | ||
| 138 | + $matches[0][0] = substr($matches[0][0], 1); | ||
| 139 | + $mustPartOfstring = (isset($matches[0][0])) ? $mustPartOfstring . $firstConcatenateSymbol . $matches[0][0] : $mustPartOfstring; | ||
| 140 | + } # если найден 2й regex | ||
| 141 | + elseif (!isset($matches[0][0]) && isset($matches[1][0])) { | ||
| 142 | + $matches[1][0] = substr($matches[1][0], 1); | ||
| 143 | + $mustPartOfstring = (isset($matches[1][0])) ? $mustPartOfstring . $firstConcatenateSymbol . $matches[1][0] : $mustPartOfstring; | ||
| 144 | + } | ||
| 145 | + | ||
| 146 | + return $mustPartOfstring; | ||
| 147 | + | ||
| 148 | + | ||
| 149 | + } | ||
| 150 | + | ||
| 151 | + | ||
| 152 | +} | ||
| 0 | \ No newline at end of file | 153 | \ No newline at end of file |
frontend/controllers/BlogController.php
| @@ -10,7 +10,8 @@ | @@ -10,7 +10,8 @@ | ||
| 10 | use yii\helpers\ArrayHelper; | 10 | use yii\helpers\ArrayHelper; |
| 11 | use yii\web\Controller; | 11 | use yii\web\Controller; |
| 12 | use yii\web\NotFoundHttpException; | 12 | use yii\web\NotFoundHttpException; |
| 13 | - | 13 | + |
| 14 | + | ||
| 14 | /** | 15 | /** |
| 15 | * Class BlogController | 16 | * Class BlogController |
| 16 | * | 17 | * |
| @@ -52,15 +53,17 @@ | @@ -52,15 +53,17 @@ | ||
| 52 | ->distinct(), | 53 | ->distinct(), |
| 53 | 'pagination' => [ | 54 | 'pagination' => [ |
| 54 | 'pageSize' => 6, | 55 | 'pageSize' => 6, |
| 56 | + | ||
| 55 | ], | 57 | ], |
| 56 | ] | 58 | ] |
| 57 | ); | 59 | ); |
| 58 | - | ||
| 59 | - return $this->render( | 60 | + |
| 61 | + return $this->render( | ||
| 60 | 'index', | 62 | 'index', |
| 61 | [ | 63 | [ |
| 62 | 'categories' => $data, | 64 | 'categories' => $data, |
| 63 | 'dataProvider' => $dataProvider, | 65 | 'dataProvider' => $dataProvider, |
| 66 | + | ||
| 64 | ] | 67 | ] |
| 65 | ); | 68 | ); |
| 66 | } | 69 | } |
frontend/views/blog/index.php
| @@ -61,29 +61,35 @@ | @@ -61,29 +61,35 @@ | ||
| 61 | </div> | 61 | </div> |
| 62 | </div> | 62 | </div> |
| 63 | <div class="row blog-list-row"> | 63 | <div class="row blog-list-row"> |
| 64 | - <?= ListView::widget( | 64 | + <?= ListView::widget( |
| 65 | [ | 65 | [ |
| 66 | 'dataProvider' => $dataProvider, | 66 | 'dataProvider' => $dataProvider, |
| 67 | 'itemView' => '_article', | 67 | 'itemView' => '_article', |
| 68 | 'itemOptions' => [ | 68 | 'itemOptions' => [ |
| 69 | 'class' => 'col-xs-12 col-sm-4 col-md-4 blog-list-col', | 69 | 'class' => 'col-xs-12 col-sm-4 col-md-4 blog-list-col', |
| 70 | ], | 70 | ], |
| 71 | - 'layout' => '{items}{pager}', | 71 | + #'layout' => '{items}{pager}', |
| 72 | + 'layout' => '{items}', | ||
| 72 | ] | 73 | ] |
| 73 | ); ?> | 74 | ); ?> |
| 75 | + <?php | ||
| 76 | + #die(var_dump($dataProvider2)); | ||
| 77 | + | ||
| 78 | + ?> | ||
| 79 | + | ||
| 74 | 80 | ||
| 75 | <div class="col-xs-12 col-sm-12"> | 81 | <div class="col-xs-12 col-sm-12"> |
| 76 | <div class="style navi-c-a"> | 82 | <div class="style navi-c-a"> |
| 77 | 83 | ||
| 78 | - <!-- --><?php // echo \frontend\widgets\FrontendPager::widget( | ||
| 79 | - // [ | ||
| 80 | - // 'pagination' => $dataProvider->pagination, | ||
| 81 | - // 'prevPageLabel' => 'previous', | ||
| 82 | - // 'nextPageLabel' => 'next', | ||
| 83 | - // 'maxButtonCount' => 5, | ||
| 84 | - // 'lastPageLabel' => 'last_number', | ||
| 85 | - // ] | ||
| 86 | - // );?> | 84 | + <?php echo \frontend\widgets\FrontendPager::widget( |
| 85 | + [ | ||
| 86 | + 'pagination' => $dataProvider->pagination, | ||
| 87 | + 'prevPageLabel' => 'previous', | ||
| 88 | + 'nextPageLabel' => 'next', | ||
| 89 | + 'maxButtonCount' => 5, | ||
| 90 | + 'lastPageLabel' => 'last_number', | ||
| 91 | + ] | ||
| 92 | + ); ?> | ||
| 87 | </div> | 93 | </div> |
| 88 | </div> | 94 | </div> |
| 89 | </div> | 95 | </div> |
frontend/widgets/FrontendPager.php
| @@ -5,7 +5,7 @@ | @@ -5,7 +5,7 @@ | ||
| 5 | use function key_exists; | 5 | use function key_exists; |
| 6 | use yii\helpers\ArrayHelper; | 6 | use yii\helpers\ArrayHelper; |
| 7 | use yii\helpers\Html; | 7 | use yii\helpers\Html; |
| 8 | - | 8 | + use common\components\Substringer; |
| 9 | /** | 9 | /** |
| 10 | * Class FrontendPager | 10 | * Class FrontendPager |
| 11 | * | 11 | * |
| @@ -138,10 +138,23 @@ | @@ -138,10 +138,23 @@ | ||
| 138 | $options | 138 | $options |
| 139 | ); | 139 | ); |
| 140 | } | 140 | } |
| 141 | - if ($active) { | 141 | + |
| 142 | + | ||
| 143 | + if ($active) { | ||
| 142 | return Html::tag('li', Html::a($label, null, $linkOptions), $options); | 144 | return Html::tag('li', Html::a($label, null, $linkOptions), $options); |
| 143 | } else { | 145 | } else { |
| 144 | - return Html::tag('li', Html::a($label, $this->pagination->createUrl($page), $linkOptions), $options); | 146 | + # убираю весь мусор кроме прямой пагинации с ссылок |
| 147 | + $haystack = $this->pagination->createUrl($page); | ||
| 148 | + | ||
| 149 | + $regex1 = '/(\??|&?)page=\d{1,5}&per-page=\d{1,5}/'; | ||
| 150 | + $regex2 = '/(\?|&)sort=[a-zA-z_]+/'; | ||
| 151 | + $requiredDelimiter = '?'; | ||
| 152 | + $firstConcatenateSymbol = '?'; | ||
| 153 | + $secondConcatenateSymbol = '?'; | ||
| 154 | + $res = Substringer::changeStringByRegex($haystack, $regex1, $regex2, $firstConcatenateSymbol, $secondConcatenateSymbol, $requiredDelimiter); | ||
| 155 | + | ||
| 156 | + return Html::tag('li', Html::a($label, $res, $linkOptions), $options); | ||
| 157 | + #return Html::tag('li', Html::a($label, $this->pagination->createUrl($page), $linkOptions), $options); | ||
| 145 | } | 158 | } |
| 146 | 159 | ||
| 147 | } | 160 | } |