url = \Yii::$app->request->url; $this->project_name = \Yii::$app->name; $this->page = \Yii::$app->request->get('page', []); if(empty(self::$optionsList)){ self::$optionsList = ArrayHelper::getColumn(TaxGroup::find()->where(['is_filter' => 'TRUE'])->all(),'alias'); } parent::init(); } public function run() { $seoData = $this->getViewData(); foreach ($seoData as $key => $value) { $this->$key = $value; } $priority = 0; switch ($this->row) { case self::SEO_TEXT: $filter = \Yii::$app->request->get('filters', []); $sort = \Yii::$app->request->get('sort', []); if(empty($filter) && empty($sort) && empty($this->page) ){ return $this->selectSeoData(self::SEO_TEXT, $priority); } else { $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); } break; case self::H1: $filter = \Yii::$app->request->get('filters', []); $default = $this->selectSeoData(self::H1, $priority); if (($default != $this->{self::H1}) || $priority == 1) { 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, $priority); if(!empty($filter) && isset($this->fields['meta-title']) && $title == $this->fields['meta-title']) { $array = [ 'category' => $this->category_name ]; 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); } else { $array['brand'] = $model->name; } } foreach(self::$optionsList as $optionList){ 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); } else { $array[$optionList] = $model->value->value; } } } $title_string = $this->getTitleString($array); if($title_string){ return $this->addPage($this->my_mb_ucfirst(trim($title_string))); } } if (!empty($title)) { return $this->addPage($title); } else { return $this->addPage($this->project_name); } break; case self::DESCRIPTION: $description = $this->selectSeoData(self::DESCRIPTION, $priority); $filter = \Yii::$app->request->get('filters', []); if(!empty($filter) && $description == $this->{self::DESCRIPTION}){ $array = $this->arrayBuilder($filter); $this->getView()->registerMetaTag([ 'name' => 'description', 'content' => $this->addPage($this->getDescriptionString($array)) ]); }elseif (!empty($description)) { $this->getView()->registerMetaTag([ 'name' => 'description', 'content' => $this->addPage($description) ]); } break; case self::META: $meta = $this->selectSeoData(self::META, $priority); $filter = \Yii::$app->request->get('filters', []); $sort = \Yii::$app->request->get('sort', []); $paginate = \Yii::$app->request->get('page', []); $language = \Yii::$app->request->get('language', []); if (!empty($meta) && empty($sort) && !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)|| !empty($sort) || isset($filter['prices']) ) { $this->getView()->registerMetaTag([ 'name' => 'robots', 'content' => 'noindex,nofollow' ]); } else if ( isset($filter) && count($filter, COUNT_RECURSIVE) > 4) { $this->getView()->registerMetaTag([ 'name' => 'robots', 'content' => 'noindex,follow' ]); } else if (isset($language) && $language == 'ua') { $this->getView()->registerMetaTag([ 'name' => 'robots', 'content' => 'noindex,nofollow' ]); } else { $this->getView()->registerMetaTag([ 'name' => 'robots', 'content' => 'index,follow' ]); } break; } } protected function checkFilter($filter){ foreach(self::$optionsList as $optionList){ if(isset($filter[$optionList]) && count($filter[$optionList]) > 1){ return true; } } return false; } protected function replaceData($str) { if (!empty($this->fields)) { foreach ($this->fields as $field_name => $field_value) { $str = str_replace('{' . $field_name . '}', $field_value, $str); } } $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; } 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]); } 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 selectSeoData($param, &$priority) { $result = ''; $widgetData = static::findSeoByUrl($this->url); if ($widgetData instanceof \common\models\Seo) { $result = $widgetData->$param; $priority = 1; } else if (!empty($this->$param)) { $result = $this->$param; $priority = 2; } else { $widgetData = $this->findSeoByDynamic(); if ($widgetData instanceof SeoDynamic) { $result = $widgetData->$param; $priority = 3; } } return $this->replaceData($result); } public function getTitleString($array){ $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 my_mb_ucfirst($str) { $str = strtolower($str); $fc = mb_strtoupper(mb_substr($str, 0, 1)); return $fc.mb_substr($str, 1); } public function getDescriptionString($array){ // "{Название раздела | Название блока фильтра: Фильтр 1 | Название блока фильтра: Фильтр 2 | Название блока фильтра: Фильтр 3}. по самой лучшей цене с гарантией от производителя - Ruzkachok.com.ua"; $row = ''; foreach($array as $name => $field){ if($name == 'category' ){ $row = $field.' '.$row; } else { $row .= $field['value'].' ' ; } } $row .= " по самой лучшей цене с гарантией от производителя - Ruzkachok.com.ua"; return $row; } public function getNameString($array){ $row = ''; foreach($array as $name => $field){ if($name == 'category' ){ $row = $field.' '.$row; } else { $row .= $field['value'].' ' ; } } return $row; } public function arrayBuilder($filter) { $array = [ 'category' => $this->category_name ]; 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); } else { $array['brand']['name'] = 'Бренд'; $array['brand']['value'] = $model->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 (!$model instanceof TaxOption) { \Yii::$app->response->redirect(['site/error'], 404); } else { $array[$optionList]['value'] = $model->value->value; $array[$optionList]['name'] = $name; } } } return $array; } public function addPage($string){ if($this->page){ return $string." - страница {$this->page}"; } return $string; } }