/**
  * @param ActiveRecord $model
  * @param null|string $language
  * @param array|null $metaTagsToFetch
  */
 public static function register(ActiveRecord $model, $language = null, $metaTagsToFetch = null)
 {
     $metaTags = MetaTagContent::find()->where([MetaTagContent::tableName() . '.model_id' => $model->id])->andWhere([MetaTagContent::tableName() . '.model_name' => (new \ReflectionClass($model))->getShortName()])->joinWith(['metaTag']);
     if (is_array($metaTagsToFetch)) {
         $metaTags->andWhere([MetaTag::tableName() . '.name' => $metaTagsToFetch]);
     } else {
         $metaTags->andWhere([MetaTag::tableName() . '.is_active' => 1]);
     }
     if (!is_string($language)) {
         $language = Yii::$app->language;
     }
     $metaTags->andWhere([MetaTagContent::tableName() . '.language' => $language]);
     /** @var MetaTagContent[] $metaTags */
     $metaTags = $metaTags->all();
     foreach ($metaTags as $metaTag) {
         $content = $metaTag->content;
         if (!empty($content)) {
             if (strtolower($metaTag->metaTag->name) === MetaTag::META_TITLE_NAME) {
                 Yii::$app->getView()->title = $content;
             } else {
                 if ($metaTag->metaTag->name) {
                     Yii::$app->view->registerMetaTag([$metaTag->metaTag->is_http_equiv ? 'http-equiv' : 'name' => $metaTag->metaTag->name, 'content' => $content], $metaTag->metaTag->name);
                 }
             }
         }
     }
 }
 /**
  * @param ActiveRecord $model
  * @param null|string $language
  * @param array|null $metaTagsToFetch
  */
 public static function register(ActiveRecord $model, $language = null, $metaTagsToFetch = null)
 {
     $metaTags = MetaTagContent::find()->where([MetaTagContent::tableName() . '.model_id' => $model->id])->andWhere([MetaTagContent::tableName() . '.model_name' => $model->formName()])->joinWith(['metaTag']);
     if (is_array($metaTagsToFetch)) {
         $metaTags->andWhere([MetaTag::tableName() . '.name' => $metaTagsToFetch]);
     } else {
         $metaTags->andWhere([MetaTag::tableName() . '.is_active' => 1]);
     }
     if (!is_string($language)) {
         $language = Yii::$app->language;
     }
     $metaTags->andWhere([MetaTagContent::tableName() . '.language' => $language]);
     /** @var MetaTagContent[] $metaTags */
     $metaTags = $metaTags->all();
     foreach ($metaTags as $metaTag) {
         $content = $metaTag->getMetaTagContent();
         foreach ($model->attributes as $name => $value) {
             $content = str_replace("[{$name}]", $value, $content);
         }
         if (!empty($content)) {
             if (strtolower($metaTag->metaTag->name) === MetaTag::META_TITLE_NAME) {
                 Yii::$app->getView()->title = $content;
             } elseif (strtolower($metaTag->metaTag->name) === MetaTag::META_SEO_TEXT) {
                 self::$seoText .= $content;
             } elseif (strtolower($metaTag->metaTag->name) === MetaTag::META_ROBOTS) {
                 if ($content) {
                     Yii::$app->view->registerMetaTag(['name' => 'robots', 'content' => 'noindex, FOLLOW'], 'robots');
                 }
             } else {
                 if ($metaTag->metaTag->name) {
                     Yii::$app->view->registerMetaTag([$metaTag->metaTag->is_http_equiv ? 'http-equiv' : 'name' => $metaTag->metaTag->name, 'content' => $content], $metaTag->metaTag->name);
                 }
             }
         }
     }
 }
 /**
  * @return MetaTagContent[]
  */
 protected function getExistingMetaTags()
 {
     /** @var ActiveRecord $model */
     $model = $this->owner;
     $metaTagList = $this->getTagList();
     $id = $model->id;
     $modelName = $model->formName();
     $metaTags = [];
     if (!$model->isNewRecord) {
         $metaTags = MetaTagContent::find()->where([MetaTagContent::tableName() . '.model_name' => $modelName])->andWhere([MetaTagContent::tableName() . '.model_id' => $id])->joinWith(['metaTag'])->orderBy([MetaTag::tableName() . '.position' => SORT_DESC])->indexBy(function ($row) {
             return $row->meta_tag_id . $row->language;
         })->all();
     }
     foreach ($metaTagList as $tag) {
         foreach ($this->languages as $language) {
             //Record is not yet in DB
             if (!isset($metaTags[$tag->id . $language])) {
                 $data = new MetaTagContent();
                 $data->model_id = $id;
                 $data->model_name = $modelName;
                 $data->meta_tag_id = $tag->id;
                 $data->content = isset($this->metaTags[$tag->id . $language]['content']) ? $this->metaTags[$tag->id . $language]['content'] : $tag->default_value;
                 $data->language = $language;
                 $data->populateRelation('metaTag', $tag);
                 $metaTags[$tag->id . $language] = $data;
             } else {
                 //Update exist data
                 if (isset($this->metaTags[$tag->id . $language]['content'])) {
                     $metaTags[$tag->id . $language]['content'] = $this->metaTags[$tag->id . $language]['content'];
                 }
             }
         }
     }
     return $metaTags;
 }
Exemplo n.º 4
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getMetaTagContents()
 {
     return $this->hasMany(MetaTagContent::className(), ['meta_tag_id' => 'id']);
 }