Commit 6b9d55c28b0f0b36c3a431a4a0978add999c06a8

Authored by Alexey Boroda
1 parent 3cf50e4c

-Languages ready for test

frontend/components/UrlManager.php
@@ -48,7 +48,7 @@ @@ -48,7 +48,7 @@
48 48
49 parent::__construct($config); 49 parent::__construct($config);
50 } 50 }
51 - 51 +
52 /** 52 /**
53 * @param \yii\web\Request $request 53 * @param \yii\web\Request $request
54 * 54 *
@@ -58,7 +58,7 @@ @@ -58,7 +58,7 @@
58 */ 58 */
59 public function parseRequest($request) 59 public function parseRequest($request)
60 { 60 {
61 - $this->checkRedirect($request->url); 61 + // $this->checkRedirect($request->url);
62 62
63 $request = $this->parseLanguage($request); 63 $request = $this->parseLanguage($request);
64 /** 64 /**
@@ -77,7 +77,7 @@ @@ -77,7 +77,7 @@
77 ) 77 )
78 ->one(); 78 ->one();
79 79
80 - if ($alias !== NULL) { 80 + if ($alias !== null) {
81 $params = Json::decode($alias->route); 81 $params = Json::decode($alias->route);
82 82
83 $route = array_shift($params); 83 $route = array_shift($params);
@@ -105,27 +105,29 @@ @@ -105,27 +105,29 @@
105 */ 105 */
106 public function createUrl($params) 106 public function createUrl($params)
107 { 107 {
  108 + if ($this->hideDefaultLanguagePrefix && ( $this->languages->getCurrent(
  109 + )->url == $this->languages->getDefault()->url )) {
  110 + $prefix = '';
  111 + } else {
  112 + $prefix = '/' . $this->languages->getCurrent()->url;
  113 + }
  114 +
108 if (isset($params[ 'alias' ])) { 115 if (isset($params[ 'alias' ])) {
109 if ($params[ 'alias' ] instanceof Alias) { 116 if ($params[ 'alias' ] instanceof Alias) {
110 - return '/' . $this->languages->getCurrent()->url . '/' . $params[ 'alias' ]->value; 117 + return $prefix . '/' . $params[ 'alias' ]->value;
111 } elseif (is_array($params[ 'alias' ])) { 118 } elseif (is_array($params[ 'alias' ])) {
112 - return '/' . $this->languages->getCurrent()->url . '/' . $params[ 'alias' ][ 'value' ]; 119 + return $prefix . '/' . $params[ 'alias' ][ 'value' ];
113 } 120 }
114 } 121 }
115 122
116 - if ($this->hideDefaultLanguagePrefix && ( $this->languages->getCurrent(  
117 - )->url == $this->languages->getDefault()->url )) {  
118 - return parent::createUrl($params);  
119 - } else {  
120 - return '/' . $this->languages->getCurrent()->url . parent::createUrl($params);  
121 -  
122 - } 123 + return $prefix . parent::createUrl($params);
123 } 124 }
124 125
125 /** 126 /**
126 - * @param Request $request 127 + * @param $request
127 * 128 *
128 - * @return Request 129 + * @return mixed
  130 + * @throws \yii\base\ExitException
129 * @throws \yii\base\InvalidConfigException 131 * @throws \yii\base\InvalidConfigException
130 */ 132 */
131 protected function parseLanguage($request) 133 protected function parseLanguage($request)
@@ -137,6 +139,7 @@ @@ -137,6 +139,7 @@
137 139
138 \Yii::$app->response->redirect('/' . implode('/', $split), 301) 140 \Yii::$app->response->redirect('/' . implode('/', $split), 301)
139 ->send(); 141 ->send();
  142 + \Yii::$app->end();
140 } else { 143 } else {
141 $this->languages->setCurrent($split[ 0 ]); 144 $this->languages->setCurrent($split[ 0 ]);
142 145
@@ -153,6 +156,7 @@ @@ -153,6 +156,7 @@
153 301 156 301
154 ) 157 )
155 ->send(); 158 ->send();
  159 + \Yii::$app->end();
156 } 160 }
157 } 161 }
158 162
frontend/views/layouts/main.php
1 <?php 1 <?php
2 2
3 /** 3 /**
4 - * @var View $this  
5 - * @var string $content  
6 - * @var SeoComponent $seo  
7 - * @var User $user  
8 - * @var PageCategory[] $pageCategories  
9 - * @var \artbox\core\forms\Module $module; 4 + * @var View $this
  5 + * @var string $content
  6 + * @var SeoComponent $seo
  7 + * @var User $user
  8 + * @var PageCategory[] $pageCategories
  9 + * @var \artbox\core\forms\Module $module ;
10 */ 10 */
11 11
12 use artbox\core\components\SeoComponent; 12 use artbox\core\components\SeoComponent;
@@ -19,6 +19,7 @@ @@ -19,6 +19,7 @@
19 use frontend\assets\AppAsset; 19 use frontend\assets\AppAsset;
20 use artbox\core\components\imagemanager\models\ImageManager; 20 use artbox\core\components\imagemanager\models\ImageManager;
21 use frontend\widgets\ArtboxModalWidget; 21 use frontend\widgets\ArtboxModalWidget;
  22 + use frontend\widgets\LanguageWidget;
22 use yii\bootstrap\Nav; 23 use yii\bootstrap\Nav;
23 use yii\bootstrap\Html; 24 use yii\bootstrap\Html;
24 use yii\db\ActiveQuery; 25 use yii\db\ActiveQuery;
@@ -26,7 +27,6 @@ @@ -26,7 +27,6 @@
26 use yii\web\View; 27 use yii\web\View;
27 use yii\widgets\Breadcrumbs; 28 use yii\widgets\Breadcrumbs;
28 29
29 -  
30 AppAsset::register($this); 30 AppAsset::register($this);
31 $user = \Yii::$app->user->identity; 31 $user = \Yii::$app->user->identity;
32 $seo = Yii::$app->get('seo'); 32 $seo = Yii::$app->get('seo');
@@ -36,19 +36,19 @@ @@ -36,19 +36,19 @@
36 $default_controller = Yii::$app->defaultRoute; 36 $default_controller = Yii::$app->defaultRoute;
37 $isHome = ( ( $controller->id === $default_controller ) && ( $controller->action->id === $controller->defaultAction ) ) ? true : false; 37 $isHome = ( ( $controller->id === $default_controller ) && ( $controller->action->id === $controller->defaultAction ) ) ? true : false;
38 $pageCategories = Category::find() 38 $pageCategories = Category::find()
39 - ->with(  
40 - [  
41 - 'language',  
42 - 'pages' => function (ActiveQuery $query) {  
43 - $query->with('language.alias')  
44 - ->where(['in_menu' => true])  
45 - ->orderBy([ 'sort' => SORT_ASC ]);  
46 - },  
47 - ]  
48 - )  
49 - ->where([ 'status' => true ])  
50 - ->orderBy([ 'sort' => SORT_ASC ])  
51 - ->all(); 39 + ->with(
  40 + [
  41 + 'language',
  42 + 'pages' => function (ActiveQuery $query) {
  43 + $query->with('language.alias')
  44 + ->where([ 'in_menu' => true ])
  45 + ->orderBy([ 'sort' => SORT_ASC ]);
  46 + },
  47 + ]
  48 + )
  49 + ->where([ 'status' => true ])
  50 + ->orderBy([ 'sort' => SORT_ASC ])
  51 + ->all();
52 $logo = null; 52 $logo = null;
53 if ($settings->logo) { 53 if ($settings->logo) {
54 $logo_img = ImageManager::findOne($settings->logo); 54 $logo_img = ImageManager::findOne($settings->logo);
@@ -79,24 +79,24 @@ @@ -79,24 +79,24 @@
79 <body> 79 <body>
80 <?php $this->beginBody() ?> 80 <?php $this->beginBody() ?>
81 <!-- Google Analytics --> 81 <!-- Google Analytics -->
82 - <?php if (!empty($settings->ga_code)) { ?>  
83 - <script>  
84 - (function(i, s, o, g, r, a, m) {  
85 - i[ 'GoogleAnalyticsObject' ] = r;  
86 - i[ r ] = i[ r ] || function() { 82 + <?php if (!empty($settings->ga_code)) { ?>
  83 + <script>
  84 + (function(i, s, o, g, r, a, m) {
  85 + i[ 'GoogleAnalyticsObject' ] = r;
  86 + i[ r ] = i[ r ] || function() {
87 (i[ r ].q = i[ r ].q || []).push(arguments) 87 (i[ r ].q = i[ r ].q || []).push(arguments)
88 }, i[ r ].l = 1 * new Date(); 88 }, i[ r ].l = 1 * new Date();
89 - a = s.createElement(o), m = s.getElementsByTagName(o)[ 0 ];  
90 - a.async = 1;  
91 - a.src = g;  
92 - m.parentNode.insertBefore(a, m)  
93 - })(window, document, 'script', 'https://www.google-analytics.com/analytics.js', 'ga'); 89 + a = s.createElement(o), m = s.getElementsByTagName(o)[ 0 ];
  90 + a.async = 1;
  91 + a.src = g;
  92 + m.parentNode.insertBefore(a, m)
  93 + })(window, document, 'script', 'https://www.google-analytics.com/analytics.js', 'ga');
94 94
95 - ga('create', '<?=$settings->ga_code?>', 'auto');  
96 - ga('send', 'pageview');  
97 -  
98 - </script>  
99 - <?php } ?> 95 + ga('create', '<?=$settings->ga_code?>', 'auto');
  96 + ga('send', 'pageview');
  97 +
  98 + </script>
  99 + <?php } ?>
100 <div id="all"> 100 <div id="all">
101 <header> 101 <header>
102 <!-- *** TOP *** 102 <!-- *** TOP ***
@@ -238,25 +238,9 @@ _________________________________________________________ --&gt; @@ -238,25 +238,9 @@ _________________________________________________________ --&gt;
238 ); 238 );
239 } 239 }
240 ?> 240 ?>
241 - <?php $lang = explode("-", \Yii::$app->language);  
242 - ?>  
243 - <div class="langs">  
244 - <a<?php if ($lang[ '0' ] == 'ru') {  
245 - echo ' class="active"';  
246 - } else {  
247 - echo ' href="/ru"';  
248 - } ?>>ru</a>  
249 - <a<?php if ($lang[ '0' ] == 'en') {  
250 - echo ' class="active"';  
251 - } else {  
252 - echo ' href="/en"';  
253 - } ?>>en</a>  
254 - <a<?php if ($lang[ '0' ] == 'ua') {  
255 - echo ' class="active"';  
256 - } else {  
257 - echo ' href="/ua"';  
258 - } ?>>ua</a>  
259 - </div> 241 +
  242 + <?= LanguageWidget::widget() ?>
  243 +
260 </div> 244 </div>
261 </div> 245 </div>
262 </div> 246 </div>
@@ -300,35 +284,35 @@ _________________________________________________________ --&gt; @@ -300,35 +284,35 @@ _________________________________________________________ --&gt;
300 'url' => [ 'site/index' ], 284 'url' => [ 'site/index' ],
301 ]; 285 ];
302 foreach ($pageCategories as $category) { 286 foreach ($pageCategories as $category) {
303 - 287 +
304 if (empty($category->pages)) { 288 if (empty($category->pages)) {
305 - continue; 289 + continue;
306 } 290 }
307 $pages = []; 291 $pages = [];
308 foreach ($category->pages as $page) { 292 foreach ($category->pages as $page) {
309 - $pages[] = [  
310 - 'label' => $page->title,  
311 - 'url' => Url::to(['alias' => $page->alias]),  
312 - ]; 293 + $pages[] = [
  294 + 'label' => $page->title,
  295 + 'url' => Url::to([ 'alias' => $page->alias ]),
  296 + ];
313 } 297 }
314 $items[] = [ 298 $items[] = [
315 - 'label' => $category->title,  
316 - 'items' => $pages, 299 + 'label' => $category->title,
  300 + 'items' => $pages,
317 ]; 301 ];
318 } 302 }
319 $items[] = [ 303 $items[] = [
320 'label' => \Yii::t('app', 'Contacts'), 304 'label' => \Yii::t('app', 'Contacts'),
321 'url' => [ 'site/contact' ], 305 'url' => [ 'site/contact' ],
322 ]; 306 ];
323 - $items[] = [ 307 + $items[] = [
324 'label' => \Yii::t('app', 'About'), 308 'label' => \Yii::t('app', 'About'),
325 'url' => [ 'site/about' ], 309 'url' => [ 'site/about' ],
326 ]; 310 ];
327 - $items[] = [ 311 + $items[] = [
328 'label' => \Yii::t('app', 'Blog'), 312 'label' => \Yii::t('app', 'Blog'),
329 'url' => [ 'blog/index' ], 313 'url' => [ 'blog/index' ],
330 ]; 314 ];
331 - $items[] = [ 315 + $items[] = [
332 'label' => \Yii::t('app', 'Events'), 316 'label' => \Yii::t('app', 'Events'),
333 'url' => [ 'event/index' ], 317 'url' => [ 'event/index' ],
334 ]; 318 ];
@@ -374,36 +358,38 @@ _________________________________________________________ --&gt; @@ -374,36 +358,38 @@ _________________________________________________________ --&gt;
374 358
375 <!-- *** FeedBack MODAL *** 359 <!-- *** FeedBack MODAL ***
376 _________________________________________________________ --> 360 _________________________________________________________ -->
377 - <?php  
378 - ArtboxModalWidget::begin([  
379 - 'modalTagOptions' => [  
380 - 'id' => 'feedback-modal'  
381 - ]  
382 - ]);  
383 -  
384 - $module->renderForm($this);  
385 -  
386 - ArtboxModalWidget::end();  
387 - ?> 361 + <?php
  362 + ArtboxModalWidget::begin(
  363 + [
  364 + 'modalTagOptions' => [
  365 + 'id' => 'feedback-modal',
  366 + ],
  367 + ]
  368 + );
  369 +
  370 + $module->renderForm($this);
  371 +
  372 + ArtboxModalWidget::end();
  373 + ?>
388 374
389 <!-- *** FeedBack MODAL END *** --> 375 <!-- *** FeedBack MODAL END *** -->
390 376
391 <!-- *** FeedBack MODAL *** 377 <!-- *** FeedBack MODAL ***
392 _________________________________________________________ --> 378 _________________________________________________________ -->
393 <div class="success_" id="success-modal" aria-labelledby="Success" aria-hidden="true"> 379 <div class="success_" id="success-modal" aria-labelledby="Success" aria-hidden="true">
394 - <div class="forms_wr_">  
395 - <div class="modal-header">  
396 - <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times</button>  
397 - <h3 class="modal-title" id="Success">Success</h3>  
398 - </div>  
399 - <div class="modal-body">  
400 -  
401 - <p>Thank for your reply, we will call you, maybe.</p>  
402 - <p class="text-center">  
403 - <button type="button" class="btn btn-template-primary" data-dismiss="modal">Close</button>  
404 - </p>  
405 - </div> 380 + <div class="forms_wr_">
  381 + <div class="modal-header">
  382 + <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times</button>
  383 + <h3 class="modal-title" id="Success">Success</h3>
  384 + </div>
  385 + <div class="modal-body">
  386 +
  387 + <p>Thank for your reply, we will call you, maybe.</p>
  388 + <p class="text-center">
  389 + <button type="button" class="btn btn-template-primary" data-dismiss="modal">Close</button>
  390 + </p>
406 </div> 391 </div>
  392 + </div>
407 </div> 393 </div>
408 394
409 <!-- *** FeedBack MODAL END *** --> 395 <!-- *** FeedBack MODAL END *** -->
@@ -450,7 +436,10 @@ _________________________________________________________ --&gt; @@ -450,7 +436,10 @@ _________________________________________________________ --&gt;
450 <?php 436 <?php
451 } 437 }
452 ?> 438 ?>
453 - <a href="#" class="btn btn-template-transparent-primary modal-link" data-form="feedback-modal" data-toggle="modal"><?=\Yii::t('app', 'Contact us!')?></a> 439 + <a href="#" class="btn btn-template-transparent-primary modal-link" data-form="feedback-modal" data-toggle="modal"><?= \Yii::t(
  440 + 'app',
  441 + 'Contact us!'
  442 + ) ?></a>
454 </div> 443 </div>
455 444
456 <div class="col-md-4 col-sm-12 col-md-offset-2"> 445 <div class="col-md-4 col-sm-12 col-md-offset-2">
@@ -486,7 +475,7 @@ _________________________________________________________ --&gt; @@ -486,7 +475,7 @@ _________________________________________________________ --&gt;
486 <!-- /#footer --> 475 <!-- /#footer -->
487 476
488 <!-- *** FOOTER END *** --> 477 <!-- *** FOOTER END *** -->
489 - 478 +
490 <span id="back-to-top" title="Back to top"><i class="fa fa-arrow-up"></i></span> 479 <span id="back-to-top" title="Back to top"><i class="fa fa-arrow-up"></i></span>
491 480
492 </div> 481 </div>
frontend/widgets/LanguageWidget.php
@@ -2,29 +2,124 @@ @@ -2,29 +2,124 @@
2 2
3 namespace frontend\widgets; 3 namespace frontend\widgets;
4 4
  5 + use artbox\core\models\Alias;
5 use artbox\core\services\Languages; 6 use artbox\core\services\Languages;
6 use yii\base\Widget; 7 use yii\base\Widget;
7 - use yii\web\Request;  
8 - 8 +
  9 + /**
  10 + * Class LanguageWidget
  11 + *
  12 + * @package frontend\widgets
  13 + */
9 class LanguageWidget extends Widget 14 class LanguageWidget extends Widget
10 { 15 {
11 - protected $request; 16 + /**
  17 + * @var \artbox\core\components\SeoComponent
  18 + */
  19 + protected $seo;
12 20
  21 + /**
  22 + * @var \artbox\core\services\Languages
  23 + */
13 protected $languages; 24 protected $languages;
14 25
15 - public function __construct(Languages $languages ,Request $request ,array $config = []) 26 + /**
  27 + * @var array
  28 + */
  29 + protected $links = [];
  30 +
  31 + /**
  32 + * LanguageWidget constructor.
  33 + *
  34 + * @param \artbox\core\services\Languages $languages
  35 + * @param array $config
  36 + *
  37 + * @throws \yii\base\InvalidConfigException
  38 + * @throws \yii\di\NotInstantiableException
  39 + */
  40 + public function __construct(Languages $languages, array $config = [])
16 { 41 {
17 - parent::__construct($config);  
18 -  
19 - $this->request = $request;  
20 -  
21 $this->languages = $languages; 42 $this->languages = $languages;
  43 +
  44 + $this->seo = \Yii::$container->get('artbox\core\components\SeoComponent');
  45 +
  46 + parent::__construct($config);
22 } 47 }
23 48
24 public function init() 49 public function init()
25 { 50 {
26 parent::init(); 51 parent::init();
27 52
28 -// $route = $this->request->get 53 + if ($this->seo->loaded) {
  54 + $currentAlias = Alias::findOne($this->seo->getAliasId());
  55 +
  56 + $aliases = Alias::find()
  57 + ->where(
  58 + [
  59 + 'route' => $currentAlias->route,
  60 + ]
  61 + )
  62 + ->indexBy('language_id')
  63 + ->all();
  64 +
  65 + /**
  66 + * @var Alias[] $aliases
  67 + */
  68 + foreach ($aliases as $alias) {
  69 + if ($alias->id === $this->seo->getAliasId()) {
  70 + $active = true;
  71 + } else {
  72 + $active = false;
  73 + }
  74 +
  75 + if (( $alias->language_id === $this->languages->getDefault(
  76 + )->id ) && \Yii::$app->urlManager->hideDefaultLanguagePrefix) {
  77 + $url = '/' . $alias->value;
  78 + } else {
  79 + $url = '/' . $this->languages->getUrlById($alias->language_id) . '/' . $alias->value;
  80 + }
  81 +
  82 + $this->links[] = [
  83 + 'active' => $active,
  84 + 'url' => $url,
  85 + 'title' => $this->languages->getUrlById($alias->id),
  86 + ];
  87 + }
  88 + } else {
  89 + $languages = $this->languages->getActive();
  90 +
  91 + foreach ($languages as $language) {
  92 + if ($language->id === $this->languages->getCurrent()->id) {
  93 + $active = true;
  94 + } else {
  95 + $active = false;
  96 + }
  97 +
  98 + if (( $language->id === $this->languages->getDefault(
  99 + )->id ) && \Yii::$app->urlManager->hideDefaultLanguagePrefix) {
  100 + $prefix = '';
  101 + } else {
  102 + $prefix = '/' . $language->url;
  103 + }
  104 +
  105 + $url = $prefix . '/' . \Yii::$app->request->pathInfo;
  106 +
  107 + $this->links[] = [
  108 + 'active' => $active,
  109 + 'url' => $url,
  110 + 'title' => $language->url,
  111 + ];
  112 + }
  113 + }
  114 + }
  115 +
  116 + public function run()
  117 + {
  118 + return $this->render(
  119 + '_languages',
  120 + [
  121 + 'links' => $this->links,
  122 + ]
  123 + );
29 } 124 }
30 } 125 }
31 \ No newline at end of file 126 \ No newline at end of file
frontend/widgets/views/_languages.php 0 → 100644
  1 +<?php
  2 +
  3 + /**
  4 + * @var View $this
  5 + * @var array $links
  6 + */
  7 +
  8 + use yii\web\View;
  9 +
  10 +?>
  11 +
  12 +<div class="langs">
  13 +
  14 + <?php
  15 + foreach ($links as $link) {
  16 + ?>
  17 +
  18 + <a class="<?= $link[ 'active' ] ? 'active' : '' ?>" href="<?= $link[ 'url' ] ?>"><?= $link[ 'title' ] ?></a>
  19 +
  20 + <?php
  21 + }
  22 + ?>
  23 +
  24 +</div>