From 21f69fb1228a845838853a3229f94a27ecd5981c Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 18 May 2017 19:18:04 +0300 Subject: [PATCH] -Pagination noindex follow --- frontend/widgets/Seo.php | 849 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 1 file changed, 443 insertions(+), 406 deletions(-) diff --git a/frontend/widgets/Seo.php b/frontend/widgets/Seo.php index e35c629..fee892b 100755 --- a/frontend/widgets/Seo.php +++ b/frontend/widgets/Seo.php @@ -1,449 +1,486 @@ url = \Yii::$app->request->url; - $this->project_name = \Yii::$app->name; - if(empty(self::$optionsList)){ - self::$optionsList = ArrayHelper::getColumn(TaxGroup::find()->where(['is_filter' => 'TRUE'])->all(),'alias'); - } - - parent::init(); - - } - - - public function run() + namespace frontend\widgets; + + use common\models\SeoDynamic; + use Yii; + use common\modules\product\models\Brand; + use common\modules\rubrication\models\TaxGroup; + use common\modules\rubrication\models\TaxOption; + use yii\base\Widget; + use yii\helpers\ArrayHelper; + use yii\helpers\Html; + use yii\helpers\Url; + use yii\web\HttpException; + + class Seo extends Widget { - - $seoData = $this->getViewData(); - foreach ($seoData as $key => $value) { - $this->$key = $value; + private $url; + public $row; + public $own_attr; + public $fields; + public $category_name; + public $description; + public $title; + public $meta; + public $seo_text; + public $h1; + public $key; + public $name; + public $project_name; + public static $optionsList; + protected static $check_url; + protected static $check_url_bool; + + const SEO_TEXT = 'seo_text'; + const DESCRIPTION = 'description'; + const META = 'meta'; + const H1 = 'h1'; + const TITLE = 'title'; + + public function init() + { + $this->url = \Yii::$app->request->url; + $this->project_name = \Yii::$app->name; + if (empty(self::$optionsList)) { + self::$optionsList = ArrayHelper::getColumn( + TaxGroup::find() + ->where([ 'is_filter' => 'TRUE' ]) + ->all(), + 'alias' + ); + } + + parent::init(); + } - - - switch ($this->row) { - case self::SEO_TEXT: - - - $filter = \Yii::$app->request->get('filters', []); - $sort = \Yii::$app->request->get('sort', []); - $paginate = \Yii::$app->request->get('page', []); - - if(empty($filter) && empty($sort) && empty($paginate) ){ - - return $this->selectSeoData(self::SEO_TEXT); - - } else { - - $widgetData = static::findSeoByUrl($this->url); - - $result = ''; - - if ($widgetData instanceof \common\models\Seo) { - - $result = $widgetData->{self::SEO_TEXT}; - + + public function run() + { + + $seoData = $this->getViewData(); + foreach ($seoData as $key => $value) { + $this->$key = $value; + } + + switch ($this->row) { + case self::SEO_TEXT: + + $filter = \Yii::$app->request->get('filters', []); + $sort = \Yii::$app->request->get('sort', []); + $paginate = \Yii::$app->request->get('page', []); + + if (empty($filter) && empty($sort) && empty($paginate)) { + + return $this->selectSeoData(self::SEO_TEXT); + } else { - - $widgetData = $this->findSeoByDynamic(); - - if ($widgetData instanceof SeoDynamic) { - + + $widgetData = static::findSeoByUrl($this->url); + + $result = ''; + + if ($widgetData instanceof \common\models\Seo) { + $result = $widgetData->{self::SEO_TEXT}; - + + } else { + + $widgetData = $this->findSeoByDynamic(); + + if ($widgetData instanceof SeoDynamic) { + + $result = $widgetData->{self::SEO_TEXT}; + + } + } - + + return $this->replaceData($result); } - - return $this->replaceData($result); - } - - - break; - case self::H1: - - $filter = \Yii::$app->request->get('filters', []); - - $default = $this->selectSeoData(self::H1); - - if ($default != $this->{self::H1}) { - - return $default; - - - } else if(!empty($filter) && !$this->checkFilter($filter)){ - - $array = $this->arrayBuilder($filter); - return $this->getNameString($array); - } - else { - - return $default; - } + break; - case self::TITLE: - - $filter = \Yii::$app->request->get('filters', []); - - - $title = $this->selectSeoData(self::TITLE); - - - if(!empty($filter) && $title == $this->title || !empty($filter) && empty($title)) { - - $array = $this->arrayBuilder($filter); - - $title_string = $this->getTitleString($array); - - if($title_string){ - return $title_string; + case self::H1: + + $filter = \Yii::$app->request->get('filters', []); + + $default = $this->selectSeoData(self::H1); + + if ($default != $this->{self::H1}) { + + return $default; + + } else if (!empty($filter) && !$this->checkFilter($filter)) { + + $array = $this->arrayBuilder($filter); + return $this->getNameString($array); + } else { + + return $default; } - - } - - if (!empty($title)) { - - return $title; - } else { - return $this->project_name; - } - - break; - case self::DESCRIPTION: - $description = $this->selectSeoData(self::DESCRIPTION); - - if (!empty($description)) { - - $this->getView()->registerMetaTag([ - 'name' => 'description', - 'content' => $description - ]); - - } else { - + break; + case self::TITLE: + $filter = \Yii::$app->request->get('filters', []); - - if(!empty($filter)){ + + $title = $this->selectSeoData(self::TITLE); + + if (!empty($filter) && $title == $this->title || !empty($filter) && empty($title)) { + $array = $this->arrayBuilder($filter); - $this->getView()->registerMetaTag([ - 'name' => 'description', - 'content' => $this->getDescriptionString($array) - ]); + + $title_string = $this->getTitleString($array); + + if ($title_string) { + return $title_string; + } + } - - } - - break; - case self::META: - - $meta = $this->selectSeoData(self::META); - - $filter = \Yii::$app->request->get('filters', []); - $sort = \Yii::$app->request->get('sort', []); - $paginate = \Yii::$app->request->get('page', []); - - - if (!empty($meta) && empty($sort) && empty($paginate) && !isset($filter['prices']) ) { - - $this->getView()->registerMetaTag([ - 'name' => 'robots', - 'content' => $meta - ]); - - } else if(!empty($filter['special'])){ - - $this->getView()->registerMetaTag([ - 'name' => 'robots', - 'content' => 'noindex,follow' - ]); - - } else if ( - isset($filter['brands']) && count($filter['brands']) > 1 - || isset($filter) && $this->checkFilter($filter) - - ) { - - $this->getView()->registerMetaTag([ - 'name' => 'robots', - 'content' => 'noindex,nofollow' - ]); - - } else if ( - isset($filter['brands']) && count($filter['brands']) > 1 && isset($filter) && count($filter, COUNT_RECURSIVE) >= 4 - || isset($filter) && count($filter, COUNT_RECURSIVE) > 4 - || !empty($sort) || !empty($paginate) || isset($filter['prices']) - ) { - - $this->getView()->registerMetaTag([ - 'name' => 'robots', - 'content' => 'noindex,nofollow' - ]); - } else { - - $this->getView()->registerMetaTag([ - 'name' => 'robots', - 'content' => 'index,follow' - ]); + + if (!empty($title)) { + + return $title; + } else { + return $this->project_name; + } + + break; + case self::DESCRIPTION: + $description = $this->selectSeoData(self::DESCRIPTION); + + if (!empty($description)) { + + $this->getView() + ->registerMetaTag( + [ + 'name' => 'description', + 'content' => $description, + ] + ); + + } else { + + $filter = \Yii::$app->request->get('filters', []); + + if (!empty($filter)) { + $array = $this->arrayBuilder($filter); + $this->getView() + ->registerMetaTag( + [ + 'name' => 'description', + 'content' => $this->getDescriptionString($array), + ] + ); + } + + } + + break; + case self::META: + + $meta = $this->selectSeoData(self::META); + + $filter = \Yii::$app->request->get('filters', []); + $sort = \Yii::$app->request->get('sort', []); + $paginate = \Yii::$app->request->get('page', []); + + if (!empty($meta) && empty($sort) && empty($paginate) && !isset($filter[ 'prices' ])) { + $this->getView() + ->registerMetaTag( + [ + 'name' => 'robots', + 'content' => $meta, + ] + ); + + } else if (!empty($filter[ 'special' ])) { + + $this->getView() + ->registerMetaTag( + [ + 'name' => 'robots', + 'content' => 'noindex,follow', + ] + ); + + } else if (isset($filter[ 'brands' ]) && count( + $filter[ 'brands' ] + ) > 1 || isset($filter) && $this->checkFilter($filter) + + ) { + + $this->getView() + ->registerMetaTag( + [ + 'name' => 'robots', + 'content' => 'noindex,nofollow', + ] + ); + + } else if (isset($filter[ 'brands' ]) && count($filter[ 'brands' ]) > 1 && isset($filter) && count( + $filter, + COUNT_RECURSIVE + ) >= 4 || isset($filter) && count( + $filter, + COUNT_RECURSIVE + ) > 4 || !empty($sort) || isset($filter[ 'prices' ]) + ) { + + $this->getView() + ->registerMetaTag( + [ + 'name' => 'robots', + 'content' => 'noindex,nofollow', + ] + ); + } else if (!empty($paginate)) { + + $this->getView() + ->registerMetaTag( + [ + 'name' => 'robots', + 'content' => 'noindex,follow', + ] + ); + } else { + $this->getView() + ->registerMetaTag( + [ + 'name' => 'robots', + 'content' => 'index,follow', + ] + ); + } + + break; + } + + } + + protected function replaceData($str) + { + + if (!empty($this->fields)) { + foreach ($this->fields as $field_name => $field_value) { + $str = str_replace('{' . $field_name . '}', $field_value, $str); } - - - - - break; + } + $str = str_replace('{project_name}', $this->project_name, $str); + return $str; } - - - } - - protected function replaceData($str) - { - - if (!empty($this->fields)) { - foreach ($this->fields as $field_name => $field_value) { - $str = str_replace('{' . $field_name . '}', $field_value, $str); + + protected static function findSeoByUrl($url) + { + if (empty(self::$check_url_bool)) { + self::$check_url = \common\models\Seo::findOne([ 'url' => $url ]); + self::$check_url_bool = true; } + return self::$check_url; } - $str = str_replace('{project_name}', $this->project_name, $str); - return $str; - } - - protected static function findSeoByUrl($url) - { - if(empty(self::$check_url_bool)){ - self::$check_url = \common\models\Seo::findOne(['url' => $url]); - self::$check_url_bool = true; + + protected function findSeoByDynamic() + { + + if (!empty($this->key)) { + + $query = SeoDynamic::find() + ->joinWith('seoCategory') + ->where( + [ + 'controller' => \Yii::$app->controller->id, + 'action' => \Yii::$app->controller->action->id, + 'key' => $this->key, + ] + ); + } else { + + $query = SeoDynamic::find() + ->joinWith('seoCategory') + ->where( + [ + 'controller' => \Yii::$app->controller->id, + 'action' => \Yii::$app->controller->action->id, + ] + ); + } + + return $query->one(); } - return self::$check_url; - } - - protected function findSeoByDynamic() - { - - if(!empty($this->key)){ - - $query = SeoDynamic::find()->joinWith('seoCategory')->where(['controller' => \Yii::$app->controller->id, 'action' => \Yii::$app->controller->action->id, 'key' => $this->key]); - } else { - - - $query = SeoDynamic::find()->joinWith('seoCategory')->where(['controller' => \Yii::$app->controller->id, 'action' => \Yii::$app->controller->action->id]); + + protected function findSeoByDynamicForFilters() + { + return SeoDynamic::find() + ->joinWith('seoCategory') + ->where([ 'param' => 'filters' ]) + ->one(); } - - return $query->one(); - } - - - protected function findSeoByDynamicForFilters(){ - return SeoDynamic::find()->joinWith('seoCategory')->where(['param' =>'filters'])->one(); - } - - - protected function getViewData() - { - $params = $this->getView()->params; - if (isset($params['seo'])) { - return $params['seo']; - } else { - return []; + + protected function getViewData() + { + $params = $this->getView()->params; + if (isset($params[ 'seo' ])) { + return $params[ 'seo' ]; + } else { + return []; + } } - } - - protected function selectSeoData($param) - { - - $result = ''; - - $widgetData = static::findSeoByUrl($this->url); - - $seoDynamicData = $this->findSeoByDynamic(); - if ($widgetData instanceof \common\models\Seo) { - - $result = $widgetData->$param; - - } else if ($seoDynamicData instanceof SeoDynamic) { + protected function selectSeoData($param) + { - $result = $seoDynamicData->$param; - - } else if (!empty($this->$param)) { - - $result = $this->$param; - - } else { $result = ''; - } - - return $this->replaceData($result); - - } - - public function getTitleString($array){ - // "{Название раздела: Название блока фильтра | Фильтр 1 | Название блока фильтра: Фильтр 2 | Название блока фильтра: Фильтр 3} - купить в Киеве, Украине - интернет магазин Лінія Світла"; - $row = ''; - foreach($array as $name => $field){ - - if($name == 'category' ){ - $row = $field.' | '.$row; + + $widgetData = static::findSeoByUrl($this->url); + + $seoDynamicData = $this->findSeoByDynamic(); + + if ($widgetData instanceof \common\models\Seo) { + + $result = $widgetData->$param; + + } else if ($seoDynamicData instanceof SeoDynamic) { + + $result = $seoDynamicData->$param; + + } else if (!empty($this->$param)) { + + $result = $this->$param; + } else { - $row .= $field['name'] .' '.$field['value'].' | ' ; + $result = ''; } - - - + + return $this->replaceData($result); + } - $row = substr($row, 0,-2 ); - $row .= " - купить в Киеве, Украине - интернет магазин Лінія Світла"; - return $row; -// $template = SeoDynamic::find()->select('title')->where(['param' =>'filters'])->one(); -// if($template instanceof SeoDynamic){ -// foreach ($array as $field_name => $field_value) { -// $template->title = str_replace('{' . $field_name . '}', mb_strtolower($field_value), $template->title); -// } -// $template = preg_replace('/\{.[^\}]*\}\s/','',$template->title); -// return $template; -// } -// -// return false; - - } - - - public function getDescriptionString($array){ - // "Лучшие цены на {Название раздела | Название блока фильтра: Фильтр 1 | Название блока фильтра: Фильтр 2 | Название блока фильтра: Фильтр 3}. Лінія Світла"; - $row = 'Лучшие цены на '; - foreach($array as $name => $field){ - - if($name == 'category' ){ - $row = $field.' | '.$row; - } else { - $row .= $field['name'] .' '.$field['value'].' | ' ; + + public function getTitleString($array) + { + // "{Название раздела: Название блока фильтра | Фильтр 1 | Название блока фильтра: Фильтр 2 | Название блока фильтра: Фильтр 3} - купить в Киеве, Украине - интернет магазин Лінія Світла"; + $row = ''; + foreach ($array as $name => $field) { + + if ($name == 'category') { + $row = $field . ' | ' . $row; + } else { + $row .= $field[ 'name' ] . ' ' . $field[ 'value' ] . ' | '; + } + } - - - + $row = substr($row, 0, -2); + $row .= " - купить в Киеве, Украине - интернет магазин Лінія Світла"; + return $row; + // $template = SeoDynamic::find()->select('title')->where(['param' =>'filters'])->one(); + // if($template instanceof SeoDynamic){ + // foreach ($array as $field_name => $field_value) { + // $template->title = str_replace('{' . $field_name . '}', mb_strtolower($field_value), $template->title); + // } + // $template = preg_replace('/\{.[^\}]*\}\s/','',$template->title); + // return $template; + // } + // + // return false; + } - $row = substr($row, 0,-2 ); - $row .= ". Лінія Світла"; - return $row; - - } - - - public function getNameString($array){ - // "Лучшие цены на {Название раздела | Название блока фильтра: Фильтр 1 | Название блока фильтра: Фильтр 2 | Название блока фильтра: Фильтр 3}. Лінія Світла"; - $row = ''; - foreach($array as $name => $field){ - - if($name == 'category' ){ - $row = $field.' | '.$row; - } else { - $row .= $field['name'] .' '.$field['value'].' | ' ; + + public function getDescriptionString($array) + { + // "Лучшие цены на {Название раздела | Название блока фильтра: Фильтр 1 | Название блока фильтра: Фильтр 2 | Название блока фильтра: Фильтр 3}. Лінія Світла"; + $row = 'Лучшие цены на '; + foreach ($array as $name => $field) { + + if ($name == 'category') { + $row = $field . ' | ' . $row; + } else { + $row .= $field[ 'name' ] . ' ' . $field[ 'value' ] . ' | '; + } + } - - - + $row = substr($row, 0, -2); + $row .= ". Лінія Світла"; + return $row; + } - $row = substr($row, 0,-2 ); - return $row; - - } - - public function arrayBuilder($filter) - { - + + public function getNameString($array) + { + // "Лучшие цены на {Название раздела | Название блока фильтра: Фильтр 1 | Название блока фильтра: Фильтр 2 | Название блока фильтра: Фильтр 3}. Лінія Світла"; + $row = ''; + foreach ($array as $name => $field) { + + if ($name == 'category') { + $row = $field . ' | ' . $row; + } else { + $row .= $field[ 'name' ] . ' ' . $field[ 'value' ] . ' | '; + } + + } + $row = substr($row, 0, -2); + return $row; + + } + + public function arrayBuilder($filter) + { + $array = [ - 'category' => $this->category_name + 'category' => $this->category_name, ]; - - - if (isset($filter['brands']) && count($filter['brands']) == 1) { - $model = Brand::find()->where(['alias' => $filter['brands'][0]])->one(); + + if (isset($filter[ 'brands' ]) && count($filter[ 'brands' ]) == 1) { + $model = Brand::find() + ->where([ 'alias' => $filter[ 'brands' ][ 0 ] ]) + ->one(); if (!$model instanceof Brand) { - - \Yii::$app->response->redirect(['/site/error'], 404); + + \Yii::$app->response->redirect([ '/site/error' ], 404); } else { - $array['brand']['name'] = 'Бренд'; - $array['brand']['value'] = $model->name; + $array[ 'brand' ][ 'name' ] = 'Бренд'; + $array[ 'brand' ][ 'value' ] = $model->name; } - + } - - - $optionsList = ArrayHelper::map(TaxGroup::find()->where(['is_filter' => 'TRUE'])->all(), 'alias', 'name'); - - + + $optionsList = ArrayHelper::map( + TaxGroup::find() + ->where([ 'is_filter' => 'TRUE' ]) + ->all(), + 'alias', + 'name' + ); + foreach ($optionsList as $optionList => $name) { - - - if (isset($filter[$optionList]) && count($filter[$optionList]) == 1) { - - $model = TaxOption::find()->where(['alias' => $filter[$optionList]])->one(); + + if (isset($filter[ $optionList ]) && count($filter[ $optionList ]) == 1) { + + $model = TaxOption::find() + ->where([ 'alias' => $filter[ $optionList ] ]) + ->one(); if (!$model instanceof TaxOption) { - - \Yii::$app->response->redirect(['site/error'], 404); + + \Yii::$app->response->redirect([ 'site/error' ], 404); } else { - $array[$optionList]['value'] = $model->value; - $array[$optionList]['name'] = $name; + $array[ $optionList ][ 'value' ] = $model->value; + $array[ $optionList ][ 'name' ] = $name; } - - + } - - + } - - return $array; - - } - - protected function checkFilter($filter){ - foreach(self::$optionsList as $optionList){ - - if(isset($filter[$optionList]) && count($filter[$optionList]) > 1){ - return true; + + return $array; + + } + + protected function checkFilter($filter) + { + foreach (self::$optionsList as $optionList) { + + if (isset($filter[ $optionList ]) && count($filter[ $optionList ]) > 1) { + return true; + } + } - + return false; } - return false; - } - - -} \ No newline at end of file + + } \ No newline at end of file -- libgit2 0.21.4