LanguageWidget.php 4.29 KB
<?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 = [];
        
        public $mobile = false;
    
        /**
         * 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()
        {
            if (!$this->mobile){
                return $this->render(
                    '_languages',
                    [
                        'links' => $this->links,
                    ]
                );
            }else{
                return $this->render(
                    '_languages_mobile',
                    [
                        'links' => $this->links,
                    ]
                );
            }
            
        }
    }