Blame view

frontend/widgets/LanguageWidget.php 3.96 KB
24a63083   Anastasia   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
124
125
  <?php
      
      namespace frontend\widgets;
      
      use artbox\core\models\Alias;
      use artbox\core\services\Languages;
      use yii\base\Widget;
      
      /**
       * Class LanguageWidget
       *
       * @package frontend\widgets
       */
      class LanguageWidget extends Widget
      {
          /**
           * @var \artbox\core\components\SeoComponent
           */
          protected $seo;
          
          /**
           * @var \artbox\core\services\Languages
           */
          protected $languages;
          
          /**
           * @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 = [])
          {
              $this->languages = $languages;
      
              $this->seo = \Yii::$container->get('artbox\core\components\SeoComponent');
              
              parent::__construct($config);
          }
          
          public function init()
          {
              parent::init();
              
              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,
                  ]
              );
          }
      }