Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
map.php
1<?php
2
4
10
11Loc::loadMessages(__FILE__);
12Loc::loadMessages(Manager::getDocRoot() . '/bitrix/modules/landing/lib/subtype/map_ru.php');
13
17class Map
18{
22 protected const PROVIDER_GOOGLE = 'google';
23 protected const PROVIDER_YANDEX = 'yandex';
25
29 protected const PROVIDER_REGEXP = '/data-map-provider=[\'"]([\w-]+)[\'"]/i';
30
35 protected static $settings = [];
36
41 protected static $provider = Map::PROVIDER_DEFAULT;
42
46 protected const MAP_SELECTOR = '.landing-block-node-map';
47
51 protected const ASSET_NAME = 'map_init';
52
53 protected static $manifestStore = [];
54
62 public static function prepareManifest(array $manifest, Block $block = null, array $params = []): array
63 {
64 if (
65 $block === null
66 || !self::isMapBlock($block)
67 )
68 {
69 return $manifest;
70 }
71
72 if (
73 isset(self::$manifestStore[$block->getId()])
74 && !empty(self::$manifestStore[$block->getId()])
75 )
76 {
77 return self::$manifestStore[$block->getId()];
78 }
79
80 self::readSettings(Hook::getForSite($block->getSiteId()));
81 self::readProviderFromBlock($block);
82
83 // save provider in content
84 $manifest['callbacks'] = [
85 'afterAdd' => function (Block &$block)
86 {
87 $dom = $block->getDom();
88 $node = $dom->querySelector(self::MAP_SELECTOR);
89 if (!$node)
90 {
91 return;
92 }
93
94 $attrsExists = $node->getAttributes();
95 if (isset($attrsExists['data-map']) && $attrsExists['data-map'] !== '')
96 {
97 return;
98 }
99
100 $isUseYandex = self::$settings['YMAP']['USE'] && !empty(self::$settings['YMAP']['CODE']);
101 $isNotSettings =
102 (!self::$settings['YMAP']['USE'] || empty(self::$settings['YMAP']['CODE']))
103 && (!self::$settings['GMAP']['USE'] || empty(self::$settings['GMAP']['CODE']));
104
105 $providerForNewBlock = self::PROVIDER_DEFAULT;
106 if (
107 ($isUseYandex || $isNotSettings)
108 && self::canUseYandex()
109 )
110 {
111 $providerForNewBlock = self::PROVIDER_YANDEX;
112 }
113
114 $block->setAttributes([
115 self::MAP_SELECTOR => [
116 'data-map-provider' => $providerForNewBlock,
117 ],
118 ]);
119
120 $defaultOptions = [
121 'center' => self::getDefaultMapCenter(),
122 'zoom' => 17,
123 'markers' => [
124 [
125 'latLng' => self::getDefaultMapCenter(),
126 'title' => 'Bitrix24',
127 'description' => 'Bitrix24 - Your company. United.',
128 ],
129 ],
130 ];
131 $block->setAttributes([
132 self::MAP_SELECTOR => [
133 'data-map' => $defaultOptions,
134 ],
135 ]);
136
137 $block->save();
138
139 unset(self::$manifestStore[$block->getId()]);
140 },
141 ];
142
143 $manifest = self::addRequiredUserAction($manifest);
144 $manifest = self::addNodes($manifest);
145 $manifest = self::addSettings($manifest);
146 $manifest = self::addVisualSettings($manifest);
147 $manifest = self::addAssets($manifest);
148
149 // save local, but disable common cache
150 $manifest['disableCache'] = true;
151 self::$manifestStore[$block->getId()] = $manifest;
152
153 return $manifest;
154 }
155
161 protected static function isMapBlock(Block $block): bool
162 {
163 return (bool)$block->getDom()->querySelector(self::MAP_SELECTOR);
164 }
165
170 protected static function readSettings(array $hooks): void
171 {
172 $readHook = static function (string $hook) use ($hooks)
173 {
174 $fields = $hooks[$hook]->getFields();
175 if ($fields)
176 {
177 self::$settings[$hook] = [
178 'USE' => isset($fields['USE']) && $fields['USE']->getValue() === 'Y',
179 'CODE' => isset($fields['CODE']) ? $fields['CODE']->getValue() : '',
180 ];
181 }
182 };
183
184 $readHook('GMAP');
185 if (self::canUseYandex())
186 {
187 $readHook('YMAP');
188 }
189 }
190
196 protected static function readProviderFromBlock(Block $block): void
197 {
198 self::$provider = self::PROVIDER_DEFAULT;
199 $content = $block->getContent();
200
201 if (preg_match(
202 self::PROVIDER_REGEXP,
203 $content,
204 $matches
205 ))
206 {
207 $provider = $matches[1];
208 if ($provider === self::PROVIDER_GOOGLE || $provider === self::PROVIDER_YANDEX)
209 {
210 self::$provider = $provider;
211 }
212 }
213 }
214
215 protected static function canUseYandex(): bool
216 {
218 }
219
220 protected static function getDefaultMapCenter(): array
221 {
222 switch (Manager::getZone())
223 {
224 case 'ru':
225 return [
226 'lat' => 54.71916849999999,
227 'lng' => 20.48854240000003,
228 ];
229
230 case 'ua':
231 return [
232 'lat' => 50.440333,
233 'lng' => 30.526835,
234 ];
235
236 default:
237 return [
238 'lat' => 38.814089,
239 'lng' => -77.042356,
240 ];
241 }
242 }
243
249 protected static function addRequiredUserAction(array $manifest): array
250 {
251 $isGoogleFail =
252 self::$provider === self::PROVIDER_GOOGLE
253 && (!self::$settings['GMAP']['USE'] || empty(self::$settings['GMAP']['CODE']));
254 $isYandexFail =
255 self::$provider === self::PROVIDER_YANDEX
256 && (!self::$settings['YMAP']['USE'] || empty(self::$settings['YMAP']['CODE']));
257
258 $error = '';
259 $description = '';
260
261 if ($isYandexFail && self::canUseYandex())
262 {
263 $error = Loc::getMessage('LANDING_BLOCK_EMPTY_YMAP_TITLE');
264 $description = Loc::getMessage('LANDING_BLOCK_EMPTY_YMAP_DESC');
265 }
266 elseif ($isGoogleFail)
267 {
268 $error = Loc::getMessage('LANDING_BLOCK_EMPTY_GMAP_TITLE');
269 $description = Loc::getMessage('LANDING_BLOCK_EMPTY_GMAP_DESC');
270 }
271
272 $isEmptyRequiredUserAction =
273 !isset($manifest['requiredUserAction'])
274 || !is_array($manifest['requiredUserAction'])
275 ;
276 if ($error && $isEmptyRequiredUserAction)
277 {
278 $manifest['requiredUserAction'] = [
279 'header' => $error,
280 'description' => $description,
281 'text' => Loc::getMessage('LANDING_BLOCK_EMPTY_GMAP_SETTINGS'),
282 'href' => '#page_url_site_edit@map_required_key',
283 'className' => 'landing-required-link',
284 'targetNodeSelector' => self::MAP_SELECTOR,
285 ];
286 }
287
288 return $manifest;
289 }
290
291 protected static function addNodes(array $manifest): array
292 {
293 if (!isset($manifest['nodes']) || !is_array($manifest['nodes']))
294 {
295 $manifest['nodes'] = [];
296 }
297 $manifest['nodes'][self::MAP_SELECTOR] = [
298 'name' => Loc::getMessage('LANDING_GOOGLE_MAP--STYLE_TITLE'),
299 'type' => 'map',
300 ];
301
302 return $manifest;
303 }
304
310 protected static function addSettings(array $manifest): array
311 {
312 $attrs = [
313 [
314 'name' => 'Map',
315 'attribute' => 'data-map',
316 'type' => 'string',
317 'hidden' => true,
318 ],
319 ];
320
321 if (self::canUseYandex())
322 {
323 $attrs[] = [
324 'name' => Loc::getMessage('LANDING_GOOGLE_MAP-PROVIDER'),
325 'attribute' => 'data-map-provider',
326 'type' => 'list',
327 'items' => [
328 ['name' => Loc::getMessage('LANDING_GOOGLE_MAP-PROVIDER-G'), 'value' => self::PROVIDER_GOOGLE],
329 ['name' => Loc::getMessage('LANDING_GOOGLE_MAP-PROVIDER-Y'), 'value' => self::PROVIDER_YANDEX],
330 ],
331 'requireReload' => true,
332 ];
333 }
334
335 if (
336 !isset($manifest['attrs'][self::MAP_SELECTOR])
337 || !is_array($manifest['attrs'][self::MAP_SELECTOR])
338 )
339 {
340 $manifest['attrs'][self::MAP_SELECTOR] = [];
341 }
342 $manifest['attrs'][self::MAP_SELECTOR] = array_merge($manifest['attrs'][self::MAP_SELECTOR], $attrs);
343
344 return $manifest;
345 }
346
352 protected static function addVisualSettings(array $manifest): array
353 {
354 $additional = [];
355 if (self::$provider === self::PROVIDER_GOOGLE)
356 {
357 $additional = [
358 'name' => Loc::getMessage('LANDING_GOOGLE_MAP--STYLE_TITLE'),
359 'attrs' => [
360 [
361 'name' => Loc::getMessage('LANDING_GOOGLE_MAP--STYLE_THEME_TITLE'),
362 'type' => 'dropdown',
363 'attribute' => 'data-map-theme',
364 'items' => [
365 [
366 'name' => Loc::getMessage('LANDING_GOOGLE_MAP--STYLE_THEME_DEFAULT'),
367 'value' => '',
368 ],
369 [
370 'name' => Loc::getMessage('LANDING_GOOGLE_MAP--STYLE_THEME_SILVER'),
371 'value' => 'SILVER',
372 ],
373 [
374 'name' => Loc::getMessage('LANDING_GOOGLE_MAP--STYLE_THEME_RETRO'),
375 'value' => 'RETRO',
376 ],
377 [
378 'name' => Loc::getMessage('LANDING_GOOGLE_MAP--STYLE_THEME_DARK'),
379 'value' => 'DARK',
380 ],
381 [
382 'name' => Loc::getMessage('LANDING_GOOGLE_MAP--STYLE_THEME_NIGHT'),
383 'value' => 'NIGHT',
384 ],
385 [
386 'name' => Loc::getMessage('LANDING_GOOGLE_MAP--STYLE_THEME_AUBERGINE'),
387 'value' => 'AUBERGINE',
388 ],
389 ],
390 ],
391
392 [
393 'name' => Loc::getMessage('LANDING_GOOGLE_MAP--STYLE_ROADS_TITLE'),
394 'type' => 'dropdown',
395 'attribute' => 'data-map-roads',
396 'items' => [
397 [
398 'name' => Loc::getMessage('LANDING_GOOGLE_MAP--STYLE_ON'),
399 'value' => '',
400 ],
401 [
402 'name' => Loc::getMessage('LANDING_GOOGLE_MAP--STYLE_OFF'),
403 'value' => 'off',
404 ],
405 ],
406 ],
407
408 [
409 'name' => Loc::getMessage('LANDING_GOOGLE_MAP--STYLE_LANDMARKS_TITLE'),
410 'type' => 'dropdown',
411 'attribute' => 'data-map-landmarks',
412 'items' => [
413 [
414 'name' => Loc::getMessage('LANDING_GOOGLE_MAP--STYLE_ON'),
415 'value' => '',
416 ],
417 [
418 'name' => Loc::getMessage('LANDING_GOOGLE_MAP--STYLE_OFF'),
419 'value' => 'off',
420 ],
421 ],
422 ],
423 ],
424 ];
425 }
426
427 // check block/nodes style notation
428 if (!isset($manifest['style']['block']) && !isset($manifest['style']['nodes']))
429 {
430 $manifest['style'] = [
431 'block' => ['type' => Block::DEFAULT_WRAPPER_STYLE],
432 'nodes' => $manifest['style'],
433 ];
434 }
435 if (!empty($additional))
436 {
437 if (!isset($manifest['style']['nodes'][self::MAP_SELECTOR]['additional']))
438 {
439 $manifest['style']['nodes'][self::MAP_SELECTOR]['additional'] = [];
440 }
441 $manifest['style']['nodes'][self::MAP_SELECTOR]['additional'][] = $additional;
442 }
443 else
444 {
445 unset($manifest['style']['nodes'][self::MAP_SELECTOR]['additional']);
446 }
447
448 return $manifest;
449 }
450
456 protected static function addAssets(array $manifest): array
457 {
458 if (
459 !is_array($manifest['assets']['ext'])
460 || !in_array(self::ASSET_NAME, $manifest['assets']['ext'], true)
461 )
462 {
463 $manifest['assets']['ext'][] = self::ASSET_NAME;
464 }
465
466 return $manifest;
467 }
468}
getDom($clear=false)
Definition block.php:3962
static getForSite($id)
Definition hook.php:251
static availableOnlyForZone(string $zone)
Definition manager.php:975
static addVisualSettings(array $manifest)
Definition map.php:352
static addRequiredUserAction(array $manifest)
Definition map.php:249
static addSettings(array $manifest)
Definition map.php:310
static addNodes(array $manifest)
Definition map.php:291
static prepareManifest(array $manifest, Block $block=null, array $params=[])
Definition map.php:62
static addAssets(array $manifest)
Definition map.php:456
static isMapBlock(Block $block)
Definition map.php:161
static readSettings(array $hooks)
Definition map.php:170
static getDefaultMapCenter()
Definition map.php:220
static readProviderFromBlock(Block $block)
Definition map.php:196
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29