Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
b24button.php
1<?php
3
5use Bitrix\Crm\SiteButton;
11
12Loc::loadMessages(__FILE__);
13
15{
16 protected const COLOR_TYPE_BUTTON = 'button';
17 protected const COLOR_TYPE_SITE = 'site';
18 protected const COLOR_TYPE_CUSTOM = 'custom';
19 protected const COLOR_DEFAULT = '#03c1fe';
20
26 protected static function getScriptUrl($script)
27 {
28 if (preg_match('/\}\‍)\‍(window,document,\'([^\']+)\'\‍);/is', $script, $matches))
29 {
30 return $matches[1];
31 }
32
33 return '';
34 }
35
40 public static function getButtons(): array
41 {
42 static $buttons = null;
43 if ($buttons !== null)
44 {
45 return $buttons;
46 }
47
48 $buttons = [];
49 foreach (self::getButtonsData() as $button)
50 {
51 $key = self::getScriptUrl($button['SCRIPT']);
52 if ($key)
53 {
54 $buttons[$key] = \htmlspecialcharsbx($button['NAME']);
55 }
56 }
57
58 return $buttons;
59 }
60
70 public static function getButtonsData(): ?array
71 {
72 static $buttonsData = null;
73
74 if ($buttonsData !== null)
75 {
76 return $buttonsData;
77 }
78
79 $buttonsData = [];
80
81 // b24 crm
82 if (Loader::includeModule('crm'))
83 {
84 // if buttons not exist (new portal) - create before
85 if (SiteButton\Preset::checkVersion())
86 {
87 $preset = new SiteButton\Preset();
88 $preset->install();
89 }
90
91 $buttonsData = SiteButton\Manager::getList([
92 'filter' => ['=ACTIVE' => 'Y'],
93 'select' => [
94 'ID', 'SECURITY_CODE', 'NAME'
95 ],
96 'order' => [
97 'ID' => 'DESC'
98 ]
99 ]);
100 }
101 // site manager
103 {
104 $client = ApClient::init();
105 if ($client)
106 {
107 $res = $client->call('crm.button.list');
108 if (isset($res['result']) && is_array($res['result']))
109 {
110 $buttonsData = $res['result'];
111 }
112 }
113 }
114
115 return $buttonsData;
116 }
117
127 public static function getButtonIdByCode($code)
128 {
129 foreach (self::getButtonsData() as $button)
130 {
131 if ($code === self::getScriptUrl($button['SCRIPT']))
132 {
133 return $button['ID'];
134 }
135 }
136
137 return false;
138 }
139
144 protected function getMap()
145 {
146 $items = array(
147 'N' => Loc::getMessage('LANDING_HOOK_B24BUTTONCODE_NO')
148 );
149
150 // show connectors only for edit
151 if ($this->isEditMode())
152 {
153 $context = Application::getInstance()->getContext();
154 $server = $context->getServer();
155 $items += $this->getButtons();
156 }
157
158
159 $helpUrl = \Bitrix\Landing\Help::getHelpUrl('B24BUTTON');
160 return [
161 'USE' => new Landing\Field\Checkbox('USE', [
162 'title' => Loc::getMessage('LANDING_HOOK_B24BUTTONCODE_USE'),
163 ]),
164 'CODE' => new Landing\Field\Select('CODE', [
165 'title' => Loc::getMessage('LANDING_HOOK_B24BUTTONCODE'),
166 'options' => $items
167 ]),
168 'COLOR' => new Landing\Field\Select('COLOR', [
169 'title' => Loc::getMessage('LANDING_HOOK_B24BUTTONCOLOR'),
170 'options' => [
171 self::COLOR_TYPE_SITE => Loc::getMessage('LANDING_HOOK_B24BUTTONCOLOR_SITE'),
172 self::COLOR_TYPE_BUTTON => Loc::getMessage('LANDING_HOOK_B24BUTTONCOLOR_BUTTON'),
173 self::COLOR_TYPE_CUSTOM => Loc::getMessage('LANDING_HOOK_B24BUTTONCOLOR_CUSTOM')
174 ]
175 ]),
176 'COLOR_VALUE' => new Landing\Field\Text('COLOR_VALUE', [
177 'title' => Loc::getMessage('LANDING_HOOK_B24BUTTONCOLOR_VALUE'),
178 'default' => self::COLOR_DEFAULT,
179 ]),
180 'HELP' => new Landing\Field\Text('HELP', [
181 'title' => '',
182 'help' => $helpUrl
183 ? '<a href="' . $helpUrl . '" target="_blank">' .
184 Loc::getMessage('LANDING_HOOK_DETAIL_HELP') .
185 '</a>'
186 : ''
187 ]),
188 ];
189 }
190
195 public function enabled()
196 {
197 if ($this->issetCustomExec())
198 {
199 return true;
200 }
201
202 $isTelegramWebView = self::isTelegramWebView();
203
204 if ($this->fields['USE']->getValue() === null)
205 {
206 return
207 trim($this->fields['CODE']) !== ''
208 && !$isTelegramWebView;
209 }
210
211 return
212 $this->fields['USE']->getValue() === 'Y'
213 && !$isTelegramWebView;
214 }
215
220 protected static function isTelegramWebView(): bool
221 {
222 if (!$application = Application::getInstance())
223 {
224 return false;
225 }
226 $session = $application->getSession();
227 $request = $application->getContext()->getRequest();
228 if ($request->get('tgWebApp') !== null)
229 {
230 $session->set('tgWebApp', 'Y');
231
232 return true;
233 }
234
235 return $session->has('tgWebApp') && $session->get('tgWebApp') === 'Y';
236 }
237
242 public function enabledInEditMode()
243 {
244 return false;
245 }
246
251 public function exec()
252 {
253 if ($this->execCustom())
254 {
255 return;
256 }
257
258 $code = \htmlspecialcharsbx(trim($this->fields['CODE']));
259 if ($code != 'N')
260 {
261 Landing\Manager::setPageView(
262 'BeforeBodyClose',
263 '<script data-skip-moving="true">
264 (function(w,d,u,b){ \'use strict\';
265 var s=d.createElement(\'script\');var r=(Date.now()/1000|0);s.async=1;s.src=u+\'?\'+r;
266 var h=d.getElementsByTagName(\'script\')[0];h.parentNode.insertBefore(s,h);
267 })(window,document,\'' . $code . '\');
268 </script>'
269 );
270
271 // set COLOR
272 if ($this->fields['COLOR']->getValue() !== self::COLOR_TYPE_BUTTON)
273 {
274 Landing\Manager::setPageView(
275 'BodyClass',
276 'landing-b24button-use-style'
277 );
278
279 $color =
280 (
281 $this->fields['COLOR']->getValue() === self::COLOR_TYPE_CUSTOM
282 && !empty($this->fields['COLOR_VALUE']->getValue())
283 )
284 ? Theme::prepareColor($this->fields['COLOR_VALUE']->getValue())
285 : 'var(--primary)';
286
287 Page\Asset::getInstance()->addString(
288 "<style type=\"text/css\">
289 :root {
290 --theme-color-b24button: {$color};
291 }
292 </style>",
293 false,
294 Page\AssetLocation::BEFORE_CSS
295 );
296 }
297 }
298 }
299
304 public function getPageTitle()
305 {
306 return Loc::getMessage('LANDING_HOOK_B24BUTTONCODE_PAGE_TITLE');
307 }
308
309}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29