Blame view

frontend/widgets/LanguageWidget.php 3.96 KB
ff81af45   Alexey Boroda   -Languages in pro...
1
2
3
4
  <?php
      
      namespace frontend\widgets;
      
6b9d55c2   Alexey Boroda   -Languages ready ...
5
      use artbox\core\models\Alias;
ff81af45   Alexey Boroda   -Languages in pro...
6
7
      use artbox\core\services\Languages;
      use yii\base\Widget;
6b9d55c2   Alexey Boroda   -Languages ready ...
8
9
10
11
12
13
      
      /**
       * Class LanguageWidget
       *
       * @package frontend\widgets
       */
ff81af45   Alexey Boroda   -Languages in pro...
14
15
      class LanguageWidget extends Widget
      {
6b9d55c2   Alexey Boroda   -Languages ready ...
16
17
18
19
          /**
           * @var \artbox\core\components\SeoComponent
           */
          protected $seo;
ff81af45   Alexey Boroda   -Languages in pro...
20
          
6b9d55c2   Alexey Boroda   -Languages ready ...
21
22
23
          /**
           * @var \artbox\core\services\Languages
           */
ff81af45   Alexey Boroda   -Languages in pro...
24
25
          protected $languages;
          
6b9d55c2   Alexey Boroda   -Languages ready ...
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
          /**
           * @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 = [])
ff81af45   Alexey Boroda   -Languages in pro...
41
          {
ff81af45   Alexey Boroda   -Languages in pro...
42
              $this->languages = $languages;
6b9d55c2   Alexey Boroda   -Languages ready ...
43
44
45
46
      
              $this->seo = \Yii::$container->get('artbox\core\components\SeoComponent');
              
              parent::__construct($config);
ff81af45   Alexey Boroda   -Languages in pro...
47
48
49
50
51
52
          }
          
          public function init()
          {
              parent::init();
              
6b9d55c2   Alexey Boroda   -Languages ready ...
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
              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,
                  ]
              );
ff81af45   Alexey Boroda   -Languages in pro...
124
125
          }
      }