From 69bdaf707e2ca531c1264e8e72b1d6c9a97bceb9 Mon Sep 17 00:00:00 2001 From: dozer111 Date: Thu, 21 Jun 2018 10:43:33 +0300 Subject: [PATCH] почистил мусор в пагинации на blog/index --- common/components/Substringer.php | 152 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ frontend/controllers/BlogController.php | 9 ++++++--- frontend/views/blog/index.php | 28 +++++++++++++++++----------- frontend/widgets/FrontendPager.php | 19 ++++++++++++++++--- 4 files changed, 191 insertions(+), 17 deletions(-) create mode 100644 common/components/Substringer.php diff --git a/common/components/Substringer.php b/common/components/Substringer.php new file mode 100644 index 0000000..9c0bf80 --- /dev/null +++ b/common/components/Substringer.php @@ -0,0 +1,152 @@ +changeStringByRegex($haystack,$regex1,$regex2,$firstConcatenateSymbol,$secondConcatenateSymbol,$requiredDelimiter); + * @example-return + * https://www.linija-svitla.ua/catalog/ulichnoe-osveshchenie?page=50&per-page=18&sort=test_test + * ================================================================================================================| + * @todo + * 1) Пока что метод работает только с 2 regex,надо будет поменять строго regex string => regex array + * 2) Метод полюбому обрезает первый символ результирующей строки regex + * 3) нужно сделать механизм замены строки только по 1 regex + * ================================================================================================================| + * + */ + /** + * @param string $haystack + * @param string $regex1 + * @param string $regex2 + * @param string $requiredDelimiter + * @param string $firstConcatenateSymbol + * @param string $secondConcatenateSymbol + * + * @return string + */ + public static function changeStringByRegex(string $haystack, string $regex1, string $regex2, string $requiredDelimiter = '', + string $firstConcatenateSymbol = '', + string $secondConcatenateSymbol = '' + ): string + { + + # 1 give rexe1/regex2 parts + # IF we have no consilience with both Regex == > return $haystack + if (preg_match($regex1, $haystack) !== 0 || preg_match($regex2, $haystack) !== 0) { + preg_match($regex1, $haystack, $matches[0]); + preg_match($regex2, $haystack, $matches[1]); + } else return $haystack; + + # 2 give must part of string + $mustPartOfstring = self::SimpleStringSubstring($haystack, $requiredDelimiter); + + # 3 if regex1/regex2 !empty concatenate they with $mustPartOfString + if (isset($matches[0][0]) && isset($matches[1][0])) { + # удаляем первый символ ( прим; $matches[0][0]='&sort=test_desc') + # нам надо только текст без первого спецсимвола + $matches[0][0] = substr($matches[0][0], 1); + $mustPartOfstring = (isset($matches[0][0])) ? $mustPartOfstring . $firstConcatenateSymbol . $matches[0][0] : $mustPartOfstring; + $matches[1][0] = substr($matches[1][0], 1); + $mustPartOfstring = (isset($matches[1][0])) ? $mustPartOfstring . $secondConcatenateSymbol . $matches[1][0] : $mustPartOfstring; + } # если найден только 1й regex + elseif (isset($matches[0][0]) && !isset($matches[1][0])) { + $matches[0][0] = substr($matches[0][0], 1); + $mustPartOfstring = (isset($matches[0][0])) ? $mustPartOfstring . $firstConcatenateSymbol . $matches[0][0] : $mustPartOfstring; + } # если найден 2й regex + elseif (!isset($matches[0][0]) && isset($matches[1][0])) { + $matches[1][0] = substr($matches[1][0], 1); + $mustPartOfstring = (isset($matches[1][0])) ? $mustPartOfstring . $firstConcatenateSymbol . $matches[1][0] : $mustPartOfstring; + } + + return $mustPartOfstring; + + + } + + +} \ No newline at end of file diff --git a/frontend/controllers/BlogController.php b/frontend/controllers/BlogController.php index 720748f..b18ce43 100755 --- a/frontend/controllers/BlogController.php +++ b/frontend/controllers/BlogController.php @@ -10,7 +10,8 @@ use yii\helpers\ArrayHelper; use yii\web\Controller; use yii\web\NotFoundHttpException; - + + /** * Class BlogController * @@ -52,15 +53,17 @@ ->distinct(), 'pagination' => [ 'pageSize' => 6, + ], ] ); - - return $this->render( + + return $this->render( 'index', [ 'categories' => $data, 'dataProvider' => $dataProvider, + ] ); } diff --git a/frontend/views/blog/index.php b/frontend/views/blog/index.php index ee68142..3a7a86c 100755 --- a/frontend/views/blog/index.php +++ b/frontend/views/blog/index.php @@ -61,29 +61,35 @@
- $dataProvider, 'itemView' => '_article', 'itemOptions' => [ 'class' => 'col-xs-12 col-sm-4 col-md-4 blog-list-col', ], - 'layout' => '{items}{pager}', + #'layout' => '{items}{pager}', + 'layout' => '{items}', ] ); ?> + +
diff --git a/frontend/widgets/FrontendPager.php b/frontend/widgets/FrontendPager.php index 6b658f0..c067838 100644 --- a/frontend/widgets/FrontendPager.php +++ b/frontend/widgets/FrontendPager.php @@ -5,7 +5,7 @@ use function key_exists; use yii\helpers\ArrayHelper; use yii\helpers\Html; - + use common\components\Substringer; /** * Class FrontendPager * @@ -138,10 +138,23 @@ $options ); } - if ($active) { + + + 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); + # убираю весь мусор кроме прямой пагинации с ссылок + $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); } } -- libgit2 0.21.4