예제 #1
0
파일: Seo.php 프로젝트: kravchukdim/yii2seo
 /**
  * Create $this->seoPage by current url
  * @author Kravchuk Dmitry
  * @throws Exception
  */
 private function __construct()
 {
     // find seo model by url
     $url = Url::current();
     $actionId = Yii::$app->controller->action->uniqueId;
     $i = 1;
     $seoModelQuery = SeoModel::find()->select('*')->byUrl($url)->active();
     $seoModelQuery->union(SeoModel::find()->byUrlRuleWithTableAlias('/' . trim($actionId, '/'), 'rule' . $i), true);
     $actionIdArray = explode('/', $actionId);
     if (is_array($actionIdArray)) {
         do {
             ++$i;
             array_pop($actionIdArray);
             $urlRule = empty($actionIdArray) ? '/*' : '/' . implode('/', $actionIdArray) . '/*';
             $unionQuery = SeoModel::find()->byUrlRuleWithTableAlias($urlRule, 'rule' . $i);
             $seoModelQuery->union($unionQuery, true);
         } while (!empty($actionIdArray));
     }
     $seoModel = $seoModelQuery->one();
     if (!empty($seoModel)) {
         $category = $seoModel->category;
         $config = ['class' => $seoModel->seoPageClass, 'metaTitle' => !empty($category) && empty($seoModel->metaTitle) ? $category->metaTitle : $seoModel->metaTitle, 'metaKeyWords' => !empty($category) && empty($seoModel->metaKeywords) ? $category->metaKeywords : $seoModel->metaKeywords, 'metaDescription' => !empty($category) && empty($seoModel->metaDescription) ? $category->metaDescription : $seoModel->metaDescription, 'pageContent' => !empty($category) && empty($seoModel->pageContent) ? $category->pageContent : $seoModel->pageContent];
         $seoPage = Yii::createObject($config);
         if (!empty($seoPage)) {
             if (!is_subclass_of($seoPage, 'kravchukdim\\yii2seo\\components\\SeoPageInterface')) {
                 throw new Exception('Error, invalid seo page class');
             }
             $this->seoPage = $seoPage;
         }
     }
 }
예제 #2
0
 /**
  * Creates data provider instance with search query applied
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = SeoModel::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     if (!($this->load($params) && $this->validate())) {
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'status' => $this->status, 'categoryId' => $this->categoryId, 'createdAt' => $this->createdAt, 'updatedAt' => $this->updatedAt]);
     $query->andFilterWhere(['like', 'url', $this->url])->andFilterWhere(['like', 'urlRule', $this->urlRule])->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'comment', $this->comment])->andFilterWhere(['like', 'pageContent', $this->pageContent])->andFilterWhere(['like', 'metaTitle', $this->metaTitle])->andFilterWhere(['like', 'metaDescription', $this->metaDescription])->andFilterWhere(['like', 'metaKeywords', $this->metaKeywords])->andFilterWhere(['like', 'seoPageClass', $this->seoPageClass]);
     return $dataProvider;
 }