Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
osmsource.php
1<?php
2
4
10
16final class OsmSource extends Source
17{
18 public const API_PATH = '/api';
19
21 private $tokenRequester;
22
26 public function __construct()
27 {
28 $this->tokenRequester = (new TokenRequester())->setSource($this);
29 }
30
34 public function makeRepository(): IRepository
35 {
36 static $result = null;
37
38 if ($result !== null)
39 {
40 return $result;
41 }
42
43 $result = new Repository(
44 new Api($this),
45 $this
46 );
47
48 return $result;
49 }
50
54 public function getJSParams(): array
55 {
56 $token = $this->getOsmToken();
57
58 return [
59 'serviceUrl' => $this->getOsmApiUrl(),
60 'mapServiceUrl' => $this->getOsmMapServiceUrl(),
61 'token' => $token ? $token->getToken() : null,
62 'useGeocodingService' => true,
63 'hostName' => $this->getOsmHostName()
64 ];
65 }
66
70 public function convertLang(string $bitrixLang): string
71 {
72 $langMap = [
73 'br' => 'pt', // Portuguese (Brazil)
74 'la' => 'es', // Spanish
75 'sc' => 'zh', // Chinese (Simplified)
76 'tc' => 'zh', // Chinese (Traditional)
77 'vn' => 'vi', // Vietnamese
78 'ua' => 'uk', // Ukrainian
79 ];
80
81 return $langMap[$bitrixLang] ?? $bitrixLang;
82 }
83
87 public function getOsmHostName(): string
88 {
89 if(defined('BX24_HOST_NAME') && ModuleManager::isModuleInstalled('bitrix24'))
90 {
91 $result = BX24_HOST_NAME;
92 }
93 else
94 {
95 $result = Context::getCurrent()->getServer()->get('HTTP_HOST');
96
97 if(strpos($result, ':') !== false)
98 {
99 $result = explode(':', $result)[0];
100 }
101 }
102
103 return $result;
104 }
105
109 public function getOsmApiUrl(): ?string
110 {
111 $serviceUrl = $this->getOsmServiceUrl();
112 if (!$serviceUrl)
113 {
114 return null;
115 }
116
117 return $serviceUrl . static::API_PATH;
118 }
119
123 public function getOsmServiceUrl(): ?string
124 {
125 if (defined('LOCATION_OSM_SERVICE_URL') && LOCATION_OSM_SERVICE_URL)
126 {
127 return (string)LOCATION_OSM_SERVICE_URL;
128 }
129
130 return $this->getConfig()
131 ? $this->getConfig()->getValue('SERVICE_URL')
132 : null;
133 }
134
138 public function getOsmMapServiceUrl(): ?string
139 {
140 if (defined('LOCATION_OSM_MAP_SERVICE_URL') && LOCATION_OSM_MAP_SERVICE_URL)
141 {
142 return (string)LOCATION_OSM_MAP_SERVICE_URL;
143 }
144
145 return $this->getConfig()
146 ? $this->getConfig()->getValue('MAP_SERVICE_URL')
147 : null;
148 }
149
153 public function getOsmToken(): ?Token
154 {
155 return $this->tokenRequester->getToken();
156 }
157
158 public function isAvailable(): bool
159 {
160 $token = $this->tokenRequester->getToken();
161 if ($token)
162 {
163 return true;
164 }
165
166 return !$this->tokenRequester->hasLicenseIssues();
167 }
168}
static getCurrent()
Definition context.php:241