diff --git a/frontend/components/UrlManager.php b/frontend/components/UrlManager.php index 6c70334..3c5da8a 100644 --- a/frontend/components/UrlManager.php +++ b/frontend/components/UrlManager.php @@ -48,7 +48,7 @@ parent::__construct($config); } - + /** * @param \yii\web\Request $request * @@ -58,7 +58,7 @@ */ public function parseRequest($request) { - $this->checkRedirect($request->url); + // $this->checkRedirect($request->url); $request = $this->parseLanguage($request); /** @@ -77,7 +77,7 @@ ) ->one(); - if ($alias !== NULL) { + if ($alias !== null) { $params = Json::decode($alias->route); $route = array_shift($params); @@ -105,27 +105,29 @@ */ public function createUrl($params) { + if ($this->hideDefaultLanguagePrefix && ( $this->languages->getCurrent( + )->url == $this->languages->getDefault()->url )) { + $prefix = ''; + } else { + $prefix = '/' . $this->languages->getCurrent()->url; + } + if (isset($params[ 'alias' ])) { if ($params[ 'alias' ] instanceof Alias) { - return '/' . $this->languages->getCurrent()->url . '/' . $params[ 'alias' ]->value; + return $prefix . '/' . $params[ 'alias' ]->value; } elseif (is_array($params[ 'alias' ])) { - return '/' . $this->languages->getCurrent()->url . '/' . $params[ 'alias' ][ 'value' ]; + return $prefix . '/' . $params[ 'alias' ][ 'value' ]; } } - if ($this->hideDefaultLanguagePrefix && ( $this->languages->getCurrent( - )->url == $this->languages->getDefault()->url )) { - return parent::createUrl($params); - } else { - return '/' . $this->languages->getCurrent()->url . parent::createUrl($params); - - } + return $prefix . parent::createUrl($params); } /** - * @param Request $request + * @param $request * - * @return Request + * @return mixed + * @throws \yii\base\ExitException * @throws \yii\base\InvalidConfigException */ protected function parseLanguage($request) @@ -137,6 +139,7 @@ \Yii::$app->response->redirect('/' . implode('/', $split), 301) ->send(); + \Yii::$app->end(); } else { $this->languages->setCurrent($split[ 0 ]); @@ -153,6 +156,7 @@ 301 ) ->send(); + \Yii::$app->end(); } } diff --git a/frontend/views/layouts/main.php b/frontend/views/layouts/main.php index 797f37d..f91e998 100755 --- a/frontend/views/layouts/main.php +++ b/frontend/views/layouts/main.php @@ -1,12 +1,12 @@ user->identity; $seo = Yii::$app->get('seo'); @@ -36,19 +36,19 @@ $default_controller = Yii::$app->defaultRoute; $isHome = ( ( $controller->id === $default_controller ) && ( $controller->action->id === $controller->defaultAction ) ) ? true : false; $pageCategories = Category::find() - ->with( - [ - 'language', - 'pages' => function (ActiveQuery $query) { - $query->with('language.alias') - ->where(['in_menu' => true]) - ->orderBy([ 'sort' => SORT_ASC ]); - }, - ] - ) - ->where([ 'status' => true ]) - ->orderBy([ 'sort' => SORT_ASC ]) - ->all(); + ->with( + [ + 'language', + 'pages' => function (ActiveQuery $query) { + $query->with('language.alias') + ->where([ 'in_menu' => true ]) + ->orderBy([ 'sort' => SORT_ASC ]); + }, + ] + ) + ->where([ 'status' => true ]) + ->orderBy([ 'sort' => SORT_ASC ]) + ->all(); $logo = null; if ($settings->logo) { $logo_img = ImageManager::findOne($settings->logo); @@ -79,24 +79,24 @@ beginBody() ?> - ga_code)) { ?> - - + ga('create', 'ga_code?>', 'auto'); + ga('send', 'pageview'); + + +
); } ?> - language); - ?> -
- >ru - >en - >ua -
+ + +
@@ -300,35 +284,35 @@ _________________________________________________________ --> 'url' => [ 'site/index' ], ]; foreach ($pageCategories as $category) { - + if (empty($category->pages)) { - continue; + continue; } $pages = []; foreach ($category->pages as $page) { - $pages[] = [ - 'label' => $page->title, - 'url' => Url::to(['alias' => $page->alias]), - ]; + $pages[] = [ + 'label' => $page->title, + 'url' => Url::to([ 'alias' => $page->alias ]), + ]; } $items[] = [ - 'label' => $category->title, - 'items' => $pages, + 'label' => $category->title, + 'items' => $pages, ]; } $items[] = [ 'label' => \Yii::t('app', 'Contacts'), 'url' => [ 'site/contact' ], ]; - $items[] = [ + $items[] = [ 'label' => \Yii::t('app', 'About'), 'url' => [ 'site/about' ], ]; - $items[] = [ + $items[] = [ 'label' => \Yii::t('app', 'Blog'), 'url' => [ 'blog/index' ], ]; - $items[] = [ + $items[] = [ 'label' => \Yii::t('app', 'Events'), 'url' => [ 'event/index' ], ]; @@ -374,36 +358,38 @@ _________________________________________________________ --> - [ - 'id' => 'feedback-modal' - ] - ]); - - $module->renderForm($this); - - ArtboxModalWidget::end(); - ?> + [ + 'id' => 'feedback-modal', + ], + ] + ); + + $module->renderForm($this); + + ArtboxModalWidget::end(); + ?>
@@ -486,7 +475,7 @@ _________________________________________________________ --> - +
diff --git a/frontend/widgets/LanguageWidget.php b/frontend/widgets/LanguageWidget.php index b3a2acc..8b4aaed 100644 --- a/frontend/widgets/LanguageWidget.php +++ b/frontend/widgets/LanguageWidget.php @@ -2,29 +2,124 @@ namespace frontend\widgets; + use artbox\core\models\Alias; use artbox\core\services\Languages; use yii\base\Widget; - use yii\web\Request; - + + /** + * Class LanguageWidget + * + * @package frontend\widgets + */ class LanguageWidget extends Widget { - protected $request; + /** + * @var \artbox\core\components\SeoComponent + */ + protected $seo; + /** + * @var \artbox\core\services\Languages + */ protected $languages; - public function __construct(Languages $languages ,Request $request ,array $config = []) + /** + * @var array + */ + protected $links = []; + + /** + * LanguageWidget constructor. + * + * @param \artbox\core\services\Languages $languages + * @param array $config + * + * @throws \yii\base\InvalidConfigException + * @throws \yii\di\NotInstantiableException + */ + public function __construct(Languages $languages, array $config = []) { - parent::__construct($config); - - $this->request = $request; - $this->languages = $languages; + + $this->seo = \Yii::$container->get('artbox\core\components\SeoComponent'); + + parent::__construct($config); } public function init() { parent::init(); -// $route = $this->request->get + if ($this->seo->loaded) { + $currentAlias = Alias::findOne($this->seo->getAliasId()); + + $aliases = Alias::find() + ->where( + [ + 'route' => $currentAlias->route, + ] + ) + ->indexBy('language_id') + ->all(); + + /** + * @var Alias[] $aliases + */ + foreach ($aliases as $alias) { + if ($alias->id === $this->seo->getAliasId()) { + $active = true; + } else { + $active = false; + } + + if (( $alias->language_id === $this->languages->getDefault( + )->id ) && \Yii::$app->urlManager->hideDefaultLanguagePrefix) { + $url = '/' . $alias->value; + } else { + $url = '/' . $this->languages->getUrlById($alias->language_id) . '/' . $alias->value; + } + + $this->links[] = [ + 'active' => $active, + 'url' => $url, + 'title' => $this->languages->getUrlById($alias->id), + ]; + } + } else { + $languages = $this->languages->getActive(); + + foreach ($languages as $language) { + if ($language->id === $this->languages->getCurrent()->id) { + $active = true; + } else { + $active = false; + } + + if (( $language->id === $this->languages->getDefault( + )->id ) && \Yii::$app->urlManager->hideDefaultLanguagePrefix) { + $prefix = ''; + } else { + $prefix = '/' . $language->url; + } + + $url = $prefix . '/' . \Yii::$app->request->pathInfo; + + $this->links[] = [ + 'active' => $active, + 'url' => $url, + 'title' => $language->url, + ]; + } + } + } + + public function run() + { + return $this->render( + '_languages', + [ + 'links' => $this->links, + ] + ); } } \ No newline at end of file diff --git a/frontend/widgets/views/_languages.php b/frontend/widgets/views/_languages.php new file mode 100644 index 0000000..88f1b70 --- /dev/null +++ b/frontend/widgets/views/_languages.php @@ -0,0 +1,24 @@ + + +
+ + + + + + + +
-- libgit2 0.21.4