url = \Yii::$app->request->url; $this->project_name = \Yii::$app->name; parent::init(); } public function run() { $seoData = $this->getViewData(); foreach ($seoData as $key => $value) { $this->$key = $value; } switch ($this->row) { case self::SEO_TEXT: return $this->selectSeoData(self::SEO_TEXT); break; case self::H1: return $this->selectSeoData(self::H1); break; case self::TITLE: $filter = \Yii::$app->request->get('filter', []); // var_dump($filter );die(); // if(!empty($filter)){ // $filter_row = ''; // foreach($filter as $sub_filter_name => $sub_filter_array){ // if($sub_filter_name=='options'){ // foreach($sub_filter_array as $f_name=>$f_values){ // $filter_row .= $f_name.':'.implode(',',$f_values).'|'; // } // } // // } // $this->fields['name'] = $filter_row; // } $title = $this->selectSeoData(self::TITLE); // if(!empty($filter_row)){ // return $filter_row; // } 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 ]); } break; case self::META: $meta = $this->selectSeoData(self::META); $filter = \Yii::$app->request->get('filter', []); $sort = \Yii::$app->request->get('sort', []); if (!empty($meta)) { $this->getView()->registerMetaTag([ 'name' => 'robots', 'content' => $meta ]); } else if ( isset($filter['brands']) && count($filter['brands']) > 1 || isset($filter['options']["pol"]) && count($filter['options']["pol"]) > 1 || isset($filter['options']["naznacenie"]) && count($filter['options']["naznacenie"]) > 1 || isset($filter['options']["god"]) && count($filter['options']["god"]) > 1 ) { $this->getView()->registerMetaTag([ 'name' => 'robots', 'content' => 'noindex,nofollow' ]); } else if ( isset($filter['brands']) && count($filter['brands']) <= 1 && isset($filter['options']) && count($filter['options'], COUNT_RECURSIVE) >= 4 || isset($filter['options']) && count($filter['options'], COUNT_RECURSIVE) > 4 || !empty($sort) || isset($filter['prices']) ) { $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); } } $str = str_replace('{project_name}', $this->project_name, $str); return $str; } protected function findSeoByUrl() { return \common\models\Seo::findOne(['url' => $this->url]); } protected function findSeoByDynamic() { $query = SeoDynamic::find()->joinWith('seoCategory')->where(['controller' => \Yii::$app->controller->id, 'action' => \Yii::$app->controller->action->id]); if (!empty($this->key)) { $query->andWhere(['key' => $this->key]); } return $query->one(); } protected function getViewData() { $params = $this->getView()->params; if (isset($params['seo'])) { return $params['seo']; } else { return []; } } protected function selectSeoData($param) { $result = ''; $widgetData = $this->findSeoByUrl(); if ($widgetData instanceof \common\models\Seo) { $result = $widgetData->$param; } else if (!empty($this->$param)) { $result = $this->$param; } else { $widgetData = $this->findSeoByDynamic(); if ($widgetData instanceof SeoDynamic) { $result = $widgetData->$param; } } return $this->replaceData($result); } }