public function getSeoData() { if ($this->owner != null) { $this->seoData = SeoData::findOne(['entity_id' => $this->owner->getPrimaryKey(), 'entity_name' => $this->owner->className()]); } if ($this->seoData == null) { $this->seoData = new SeoData(); } }
/** * Parses the given request and returns the corresponding route and parameters. * @param UrlManager $manager the URL manager * @param Request $request the request component * @return array|bool the parsing result. The route and the parameters are returned as an array. * If false, it means this rule cannot be used to parse this path info. * @throws NotFoundHttpException */ public function parseRequest($manager, $request) { $this->currentLanguage = Language::getCurrent(); $this->pathInfo = $request->getPathInfo(); if ($this->pathInfo == $this->albumsRoute) { if ($this->disableDefault) { throw new NotFoundHttpException(); } if (!empty($request->getQueryParams()['id'])) { $id = $request->getQueryParams()['id']; $album = GalleryAlbum::findOne($id); if (!$album) { return false; } $translation = $album->getTranslation($this->currentLanguage->id); if ($translation) { if (!empty($translation->seoUrl)) { throw new NotFoundHttpException(); } } } } if (!empty($this->prefix)) { if (strpos($this->pathInfo, $this->prefix) === 0) { $this->pathInfo = substr($this->pathInfo, strlen($this->prefix)); } else { return false; } } $this->initRoutes($this->pathInfo); if (!empty($this->prefix) && $this->routesCount == 1) { return [$this->albumsRoute, []]; } else { if ($this->routesCount == 2) { $seoUrl = $this->routes[1]; /* @var SeoData $seoData */ $seoData = SeoData::find()->where(['entity_name' => GalleryAlbumTranslation::className(), 'seo_url' => $seoUrl])->one(); if ($seoData) { /* @var GalleryAlbum $album */ $album = GalleryAlbum::find()->joinWith('translations translation')->where(['translation.id' => $seoData->entity_id, 'translation.language_id' => $this->currentLanguage->id])->one(); if ($album) { return [$this->albumsRoute, ['id' => $album->id]]; } } } } return false; }
/** * Creates a URL according to the given route and parameters. * @param UrlManager $manager the URL manager * @param string $route the route. It should not have slashes at the beginning or the end. * @param array $params the parameters * @return string|boolean the created URL, or false if this rule cannot be used for creating this URL. */ public function createUrl($manager, $route, $params) { /* @var $modelClass ActiveRecordInterface */ if (empty($this->route)) { return false; } if (empty($this->modelClass) || !class_exists($this->modelClass)) { return false; } else { $modelClass = $this->modelClass; } if ($route == $this->route) { $condition = []; if (!empty($this->params)) { foreach ($this->params as $paramName => $param) { if (isset($params[$paramName])) { $condition[$param] = $params[$paramName]; } } } if (!empty($this->condition)) { foreach ($this->condition as $key => $value) { if (is_callable($value)) { $value = $value(); } $condition[$key] = $value; } } $model = $modelClass::find()->where($condition)->one(); if ($model) { $seoData = SeoData::find()->where(['entity_name' => $this->modelClass, 'entity_id' => $model->getPrimaryKey()])->one(); if ($seoData) { return $this->prefix . $seoData->seo_url; } } } return false; }
private function findCategoryBySeoUrl($seoUrl, $parentId, $options = []) { $categoriesSeoData = SeoData::find()->where(['entity_name' => CategoryTranslation::className(), 'seo_url' => $seoUrl])->all(); if ($categoriesSeoData) { foreach ($categoriesSeoData as $categorySeoData) { if ($category = Category::find()->joinWith('translations translation')->where(array_merge(['translation.id' => $categorySeoData->entity_id, 'parent_id' => $parentId, 'translation.language_id' => $this->currentLanguage->id], $options))->one()) { return $category; } } } return null; }