Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
remotedictionary.php
1<?php
10
20
22{
23 const ID = 'generic';
24
25 const CACHE_TTL = 86400;
26 const CACHE_PREFIX = 'rest_dictionary';
27
28 protected $baseUrl = array(
29 'ru' => 'https://www.bitrix24.ru/util/',
30 'ua' => 'https://www.bitrix24.eu/util/',
31 'by' => 'https://www.bitrix24.by/util/',
32 'in' => 'https://www.bitrix24.in/util/',
33 'en' => 'https://www.bitrix24.com/util/',
34 'de' => 'https://www.bitrix24.de/util/',
35 'kz' => 'https://www.bitrix24.kz/util/',
36 'br' => 'https://www.bitrix24.com.br/util/',
37 'pl' => 'https://www.bitrix24.pl/util/',
38 'fr' => 'https://www.bitrix24.fr/util/',
39 'la' => 'https://www.bitrix24.es/util/',
40 'eu' => 'https://www.bitrix24.eu/util/',
41 'cn' => 'https://www.bitrix24.cn/util/',
42 'tc' => 'https://www.bitrix24.cn/util/',
43 'sc' => 'https://www.bitrix24.cn/util/',
44 'tr' => 'https://www.bitrix24.com.tr/util/',
45 );
46 protected $language = null;
47
48 public function __construct()
49 {
50 $this->language = LANGUAGE_ID;
51
52 $values = $this->init();
53
54 parent::__construct($values);
55 }
56
57 public function setLanguage($language)
58 {
59 if($language !== $this->language)
60 {
61 $this->language = $language;
62 $this->set($this->init());
63 }
64 }
65
66 protected function init()
67 {
68 $managedCache = Application::getInstance()->getManagedCache();
69 if($managedCache->read(static::CACHE_TTL, $this->getCacheId()))
70 {
71 $dictionary = $managedCache->get($this->getCacheId());
72 }
73 else
74 {
75 $dictionary = $this->load();
76
77 $managedCache->set($this->getCacheId(), $dictionary);
78 }
79
80 $event = new Event('rest', 'onRemoteDictionaryLoad', array(
81 'ID' => static::ID,
82 'DICTIONARY' => &$dictionary
83 ));
84 $event->send();
85
86 return $dictionary;
87 }
88
89 protected function load()
90 {
91 $httpClient = new HttpClient();
92
93 $uri = $this->getDictionaryUri();
94
95 $httpResult = $httpClient->get($uri->getLocator());
96
97 try
98 {
99 $result = Json::decode($httpResult);
100 }
101 catch(ArgumentException $e)
102 {
103 $result = null;
104 }
105
106 return $result;
107 }
108
109 protected function getCacheId()
110 {
111 return static::CACHE_PREFIX.'/'.static::ID.'/'.$this->language;
112 }
113
118 protected function getDictionaryUri()
119 {
120 if(Loader::includeModule('bitrix24'))
121 {
122 $lang = \CBitrix24::getLicensePrefix();
123 }
124 else
125 {
126 $lang = $this->language;
127 }
128
129 $baseUrl = array_key_exists($lang, $this->baseUrl)
130 ? $this->baseUrl[$lang]
131 : $this->baseUrl[Loc::getDefaultLang($lang)];
132
133 $uri = new Uri($baseUrl);
134 $uri->addParams(array(
135 'type' => static::ID,
136 'lng' => $this->language,
137 ));
138
139 return $uri;
140 }
141}
static getDefaultLang($lang)
Definition loc.php:460