Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
googlesource.php
1<?php
2
4
14use Bitrix\Fileman;
15
21class GoogleSource extends Source
22{
26 public function makeRepository(): IRepository
27 {
28 static $result = null;
29
30 if (!is_null($result))
31 {
32 return $result;
33 }
34
35 $httpClient = new HttpClient(
36 [
37 'version' => '1.1',
38 'socketTimeout' => 30,
39 'streamTimeout' => 30,
40 'redirect' => true,
41 'redirectMax' => 5,
42 ]
43 );
44
45 $cacheTTL = 2592000; //month
46 $poolSize = 100;
47 $pool = new Pool($poolSize);
48
49 $cachePool = new CachedPool(
50 $pool,
51 $cacheTTL,
52 'locationSourceGoogleRequester',
53 Cache::createInstance(),
54 EventManager::getInstance()
55 );
56
57 $result = new Repository(
58 $this->getBackendKey(),
59 $httpClient,
60 $this,
61 $cachePool
62 );
63
64 return $result;
65 }
66
70 public function getJSParams(): array
71 {
72 return [
73 'apiKey' => $this->getFrontendKey(),
74 'showPhotos' => $this->config->getValue('SHOW_PHOTOS_ON_MAP'),
75 'useGeocodingService' => $this->config->getValue('USE_GEOCODING_SERVICE'),
76 ];
77 }
78
82 private function getBackendKey(): string
83 {
84 $configKey = $this->config->getValue('API_KEY_BACKEND');
85 if ($configKey)
86 {
87 return (string)$configKey;
88 }
89
90 return (string)Option::get('location', 'google_map_api_key_backend', '');
91 }
92
96 private function getFrontendKey(): string
97 {
98 $key = $this->config->getValue('API_KEY_FRONTEND');
99 if ($key)
100 {
101 return (string)$key;
102 }
103
104 $key = Option::get('location', 'google_map_api_key', '');
105 if ($key !== '')
106 {
107 return (string)$key;
108 }
109
110 if (Loader::includeModule('fileman'))
111 {
112 $key = Fileman\UserField\Types\AddressType::getApiKey();
113 if ($key !== '' && !is_null($key))
114 {
115 return $key;
116 }
117 }
118
119 return '';
120 }
121
127 public function convertLang(string $bitrixLang): string
128 {
129 $langMap = [
130 'br' => 'pt-BR', // Portuguese (Brazil)
131 'la' => 'es', // Spanish
132 'sc' => 'zh-CN', // Chinese (Simplified)
133 'tc' => 'zh-TW', // Chinese (Traditional)
134 'vn' => 'vi', // Vietnamese
135 'ua' => 'uk', // Ukrainian
136 ];
137
138 return $langMap[$bitrixLang] ?? $bitrixLang;
139 }
140
141 public function isAvailable(): bool
142 {
143 return $this->getBackendKey() && $this->getFrontendKey();
144 }
145}