/** * 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; } } }
/** * 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; }
/** * @return \yii\db\ActiveQuery */ public function getSeo() { return $this->hasMany(SeoModel::className(), ['categoryId' => 'id']); }
/** * Find by urlRule * @author Kravchuk Dmitry * * @param string $urlRule * * @return $this */ public function byUrlRule($urlRule) { $tableColumn = SeoModel::tableName() . '.urlRule'; $aliasColumn = ':' . SeoModel::tableName() . 'UrlRule'; $this->andWhere($tableColumn . ' = ' . $aliasColumn, [$aliasColumn => $urlRule]); return $this; }
/** * Finds the SeoModel model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return SeoModel the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = SeoModel::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }