Last active
September 22, 2020 06:44
-
-
Save davletyarov/5ca60bfe67f4c9e728f59e6882a23599 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Company\Components; | |
defined('B_PROLOG_INCLUDED') || die(); | |
class SiteSelector extends \CBitrixComponent | |
{ | |
/** | |
* @var array | |
*/ | |
private $siteList = []; | |
/** | |
* @var string | |
*/ | |
private $defaultLang = 'en'; | |
/** | |
* @return mixed|void|null | |
*/ | |
public function executeComponent() | |
{ | |
if ($this->startResultCache()) { | |
$this->arResult['LANGUAGES'] = $this->getLanguages(); | |
$this->arResult['SITES'] = $this->getSites(); | |
$currentSite = $this->getCurrentSite(); | |
if ($currentSite) { | |
$this->arResult['CURRENT_LANG'] = $currentSite; | |
} | |
$this->includeComponentTemplate(); | |
$this->setResultCacheKeys(['SITES']); | |
$this->endResultCache(); | |
} | |
if ($this->request->getQuery('location') == true) { | |
$lang = htmlspecialchars($this->request->getQuery('lang')); | |
foreach ($this->arResult['SITES'] as $site) { | |
if ($site['LANGUAGE_ID'] == $lang) { | |
$this->setSelectedCountry($lang); | |
} | |
} | |
} | |
$this->redirectUserToSite(); | |
} | |
/** | |
* @param $lang | |
*/ | |
private function setSelectedCountry($lang) | |
{ | |
$_SESSION['USER_LANG'] = $lang; | |
} | |
/** | |
* @return mixed | |
*/ | |
private function getSelectedCountry() | |
{ | |
return $_SESSION['USER_LANG']; | |
} | |
/** | |
* Ридирект на текущий раздел | |
*/ | |
private function redirectUserToSite() | |
{ | |
global $APPLICATION; | |
if (!$this->getSelectedCountry()) { | |
$current = $this->getCurrentSite(); | |
$site = $this->getPreferredSite(); | |
if ($site['LANGUAGE_ID'] != $current['LANGUAGE_ID']) { | |
LocalRedirect($site['DIR'] . str_replace($current['DIR'], '', $APPLICATION->GetCurDir())); | |
} | |
} | |
} | |
/** | |
* Получить первый предпочитаемый язык для сайта | |
* | |
* @return array | |
*/ | |
private function getPreferredSite(): array | |
{ | |
$userLanguages = $this->request->getAcceptedLanguages(); | |
$sites = $this->getSites(); | |
$arLangResult = []; | |
foreach ($sites as $site) { | |
foreach ($userLanguages as $sort => $language) { | |
if (substr($language, 0, 2) == $site['LANGUAGE_ID']) { | |
$arLangResult[$sort] = $site; | |
} | |
} | |
} | |
if (empty($arLangResult)) { | |
foreach ($sites as $site) { | |
if ($this->defaultLang == $site['LANGUAGE_ID']) { | |
$arLangResult = [$site]; | |
} | |
} | |
} | |
ksort($arLangResult); | |
return array_shift($arLangResult); | |
} | |
/** | |
* Возвращает список сайтов | |
* @return array | |
*/ | |
private function getSites(): array | |
{ | |
if (empty($this->siteList)) { | |
$this->siteList = $this->loadSites(); | |
} | |
return $this->siteList; | |
} | |
/** | |
* Вовращает массив из всех языков для сайтов | |
* | |
* @return array | |
*/ | |
private function getLanguages(): array | |
{ | |
$langs = []; | |
$by = 'LANGUAGE_ID'; | |
$order = 'ASC'; | |
$rsLangs = \CLanguage::GetList($by, $order); | |
while ($arLang = $rsLangs->Fetch()) { | |
$langs[$arLang['ID']] = $arLang; | |
} | |
return $langs; | |
} | |
/** | |
* Взять текущий сайт | |
* | |
* @return false|mixed | |
*/ | |
private function getCurrentSite() | |
{ | |
$sites = $this->getSites(); | |
foreach ($sites as $site) { | |
if ($site['LANGUAGE_ID'] == LANGUAGE_ID) { | |
return $site; | |
} | |
} | |
return false; | |
} | |
/** | |
* Вовращает все активные сайты | |
* | |
* @return array | |
*/ | |
private function loadSites(): array | |
{ | |
$sites = []; | |
$by = 'LANGUAGE_ID'; | |
$order = 'ASC'; | |
$rsSites = \CSite::GetList($by, $order, ['ACTIVE' => 'Y']); | |
while ($arSite = $rsSites->Fetch()) { | |
$sites[] = $arSite; | |
} | |
return $sites; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment