Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
gmap.php
1<?php
3
10use Bitrix\Main\Page\Asset;
12
13Loc::loadMessages(__FILE__);
14
15class GMap extends Page
16{
17 private $lang;
18 private $siteId;
19
24 protected function getMap()
25 {
26 $helpUrl = Help::getHelpUrl('GMAP_EDIT');
27 return [
28 'USE' => new Field\Checkbox('USE', [
29 'title' => Loc::getMessage('LANDING_HOOK_MAPCODE_USE'),
30 ]),
31 'CODE' => new Field\Text('CODE', [
32 'title' => Loc::getMessage('LANDING_HOOK_MAPCODE'),
33 'placeholder' => Loc::getMessage('LANDING_HOOK_MAPCODE_PLACEHOLDER'),
34 'help' => $helpUrl
35 ? '<a href="' . $helpUrl . '" target="_blank">'
36 . Loc::getMessage('LANDING_HOOK_MAPCODE_HELP')
37 . '</a>'
38 : '',
39 ]),
40 ];
41 }
42
47 public function enabled()
48 {
49 if ($this->issetCustomExec())
50 {
51 return true;
52 }
53
54 if ($this->isPage())
55 {
56 return false;
57 }
58
59 return
60 $this->fields['USE']->getValue() === 'Y'
61 && !empty($this->fields['CODE']->getValue())
62 ;
63 }
64
69 public function exec()
70 {
71 if ($this->execCustom())
72 {
73 return;
74 }
75
76 $code = HtmlFilter::encode(trim($this->fields['CODE']));
77 $assets = Asset::getInstance();
78 $assets->addString(
79 "<script defer>
80 (function(){
81 'use strict';
82 //fake function, if API will loaded fasten than blocks
83 window.onGoogleMapApiLoaded = function(){}
84 })();
85 </script>"
86 );
87 $lang = $this->getLang();
88 $assets->addString(
89 '<script defer src="https://maps.googleapis.com/maps/api/js?key='
90 . $code
91 . '&region='
92 . $lang
93 . '&language='
94 . $lang
95 . '&callback=onGoogleMapApiLoaded"></script>'
96 );
97 }
98
104 public function setSiteId(int $siteId): void
105 {
106 $this->siteId = $siteId;
107 }
108
113 protected function getLang(): string
114 {
115 $lang = null;
116 if ($this->siteId > 0)
117 {
118 $res = SiteTable::getList([
119 'select' => [
120 'LANG'
121 ],
122 'filter' => [
123 '=ID' => $this->siteId
124 ]
125 ])->fetch();
126 if ($res['LANG'])
127 {
128 $lang = $res['LANG'];
129 if ($lang === 'tc')
130 {
131 $lang = 'zh';
132 }
133 if ($lang === 'vn')
134 {
135 $lang = 'vi';
136 }
137 }
138 }
139 return $lang ?: Manager::getZone();
140 }
141}
static getHelpUrl(string $code)
Definition help.php:268
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29