Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
form.php
1<?php
2
4
5use Bitrix\Crm\Integration\UserConsent;
6use Bitrix\Crm\Settings\LeadSettings;
7use Bitrix\Crm\UI\Webpack;
8use Bitrix\Crm\WebForm;
18
19Loc::loadMessages(__FILE__);
20
25class Form
26{
27 protected const ATTR_FORM_PARAMS = 'data-b24form';
28 protected const ATTR_FORM_EMBED = 'data-b24form-embed';
29 protected const ATTR_FORM_STYLE = 'data-b24form-design';
30 protected const ATTR_FORM_USE_STYLE = 'data-b24form-use-style';
31 protected const ATTR_FORM_FROM_CONNECTOR = 'data-b24form-connector';
32 protected const ATTR_FORM_OLD_DOMAIN = 'data-b24form-original-domain';
33 protected const ATTR_FORM_OLD_HEADER = 'data-b24form-show-header';
34 protected const SELECTOR_FORM_NODE = '.bitrix24forms';
35 protected const SELECTOR_OLD_STYLE_NODE = '.landing-block-form-styles';
36 protected const STYLE_SETTING = 'crm-form';
37 protected const REGEXP_FORM_STYLE = '/data-b24form-design *= *[\'"](\{.+\})[\'"]/i';
38 protected const REGEXP_FORM_ID_INLINE = '/data-b24form=["\']#crmFormInline(?<id>[\d]+)["\']/i';
39
40 public const INLINE_MARKER_PREFIX = '#crmFormInline';
41 public const POPUP_MARKER_PREFIX = '#crmFormPopup';
42
43 protected const AVAILABLE_FORM_FIELDS = [
44 'ID',
45 'NAME',
46 'SECURITY_CODE',
47 'IS_CALLBACK_FORM',
48 'ACTIVE',
49 'XML_ID',
50 ];
51
52 private static array $errors = [];
53
54 // region replaces for view and public
55
61 public static function prepareFormsToPublication(string $content): string
62 {
63 // change - replace markers always, not only if connector
64 return self::replaceFormMarkers($content);
65 }
66
72 public static function prepareFormsToView(string $content): string
73 {
74 if (self::isCrm())
75 {
76 $content = self::replaceFormMarkers($content);
77 }
78 return $content;
79 }
80
87 protected static function replaceFormMarkers(string $content): string
88 {
89 $replace = preg_replace_callback(
90 '/(?<pre><a[^>]+href=|data-b24form=)["\'](form:)?#crmForm(?<type>Inline|Popup)(?<id>[\d]+)["\']/i',
91 static function ($matches)
92 {
93 $id = (int)$matches['id'];
94 if (!$id)
95 {
96 return $matches[0];
97 }
98
99 $form = self::getFormById($id);
100 if (!$form || !$form['URL'])
101 {
102 return $matches[0];
103 }
104
105 if (strtolower($matches['type']) === 'inline')
106 {
107 $param = "{$form['ID']}|{$form['SECURITY_CODE']}|{$form['URL']}";
108
109 return $matches['pre'] . "\"{$param}\"";
110 }
111
112 if (strtolower($matches['type']) === 'popup')
113 {
114 $script = "<script data-b24-form=\"click/{$id}/{$form['SECURITY_CODE']}\" data-skip-moving=\"true\">
115 (function(w,d,u){
116 var s=d.createElement('script');s.async=true;s.src=u+'?'+(Date.now()/180000|0);
117 var h=d.getElementsByTagName('script')[0];h.parentNode.insertBefore(s,h);
118 })(window,document,'{$form['URL']}');
119 </script>";
120
121 return $script . $matches['pre'] . "\"#\" onclick=\"BX.PreventDefault();\"";
122 }
123
124 return $matches[0];
125 },
126 $content
127 );
128
129 $replace = $replace ?? $content;
130
131 //replace link to form in data-pseudo-url
132 $replace = preg_replace_callback(
133 '/(?<pre><img|<i.*)data-pseudo-url="{.*(form:)?#crmForm(?<type>Inline|Popup)(?<id>[\d]+).*}"(?<pre2>.*>)/i',
134 static function ($matches)
135 {
136 if (
137 !(int)$matches['id']
138 || !($form = self::getFormById((int)$matches['id']))
139 )
140 {
141 return $matches[0];
142 }
143
144 if (strtolower($matches['type']) === 'popup')
145 {
146 $script = "<script data-b24-form=\"click/{$matches['id']}/{$form['SECURITY_CODE']}\" data-skip-moving=\"true\">
147 (function(w,d,u){
148 var s=d.createElement('script');s.async=true;s.src=u+'?'+(Date.now()/180000|0);
149 var h=d.getElementsByTagName('script')[0];h.parentNode.insertBefore(s,h);
150 })(window,document,'{$form['URL']}');
151 </script>";
152
153 //add class g-cursor-pointer
154 preg_match_all('/(class="[^"]*)/i', $matches['pre'], $matchesPre);
155 $matches['pre'] = str_replace($matchesPre[1][0], $matchesPre[1][0]. ' g-cursor-pointer', $matches['pre']);
156
157 return $script . $matches['pre'] . ' '. $matches['pre2'];
158 }
159
160 return $matches[0];
161 },
162 $replace
163 );
164
165 return $replace ?? $content;
166 }
167
172 public static function clearCache(): void
173 {
174 $sites = [];
175 $res = BlockTable::getList(
176 [
177 'select' => [
178 'SITE_ID' => 'LANDING.SITE_ID',
179 ],
180 'filter' => [
181 '=LANDING.ACTIVE' => 'Y',
182 '=LANDING.SITE.ACTIVE' => 'Y',
183 '=PUBLIC' => 'Y',
184 '=DELETED' => 'N',
185 'CONTENT' => '%bitrix24forms%',
186 ],
187 'group' => [
188 'LANDING.SITE_ID',
189 ],
190 ]
191 );
192 while ($row = $res->fetch())
193 {
194 if (!in_array($row['SITE_ID'], $sites))
195 {
196 $sites[] = $row['SITE_ID'];
197 }
198 }
199
200 foreach ($sites as $site)
201 {
202 Site::update($site, [
203 'DATE_MODIFY' => false
204 ]);
205 }
206 }
207 // endregion
208
209 // region get forms
215 public static function getForms(bool $force = false): array
216 {
217 static $forms = [];
218 if ($forms && !$force)
219 {
220 return $forms;
221 }
222
223 if (self::isCrm())
224 {
225 $forms = self::getFormsForPortal();
226 }
227 elseif (Manager::isB24Connector())
228 {
229 $forms = self::getFormsViaConnector();
230 }
231
232 return $forms;
233 }
234
239 protected static function isCrm(): bool
240 {
241 return Loader::includeModule('crm');
242 }
243
244 protected static function getFormsForPortal(array $filter = []): array
245 {
246 $res = Webform\Internals\FormTable::getDefaultTypeList(
247 [
248 'select' => self::AVAILABLE_FORM_FIELDS,
249 'filter' => $filter,
250 'order' => [
251 'ID' => 'ASC',
252 ],
253 ]
254 );
255
256 $forms = [];
257 while ($form = $res->fetch())
258 {
259 $form['ID'] = (int)$form['ID'];
260 $webpack = Webpack\Form::instance($form['ID']);
261 if (!$webpack->isBuilt())
262 {
263 $webpack->build();
264 $webpack = Webpack\Form::instance($form['ID']);
265 }
266 $form['URL'] = $webpack->getEmbeddedFileUrl();
267 $forms[$form['ID']] = $form;
268 }
269
270 return $forms;
271 }
272
273 protected static function getFormsViaConnector(): array
274 {
275 $forms = [];
276 $client = ApClient::init();
277 if ($client)
278 {
279 $res = $client->call('crm.webform.list', ['GET_INACTIVE' => 'Y']);
280 if (isset($res['result']) && is_array($res['result']))
281 {
282 foreach ($res['result'] as $form)
283 {
284 $form['ID'] = (int)$form['ID'];
285 $forms[$form['ID']] = $form;
286 }
287 }
288 else if (isset($res['error']))
289 {
290 self::$errors[] = [
291 'code' => $res['error'],
292 'message' => $res['error_description'] ?? $res['error'],
293 ];
294 }
295 }
296
297 return $forms;
298 }
299
304 public static function getFormById(int $id): array
305 {
306 $forms = self::getFormsByFilter(['ID' => $id]);
307
308 return !empty($forms) ? array_shift($forms) : [];
309 }
310
315 public static function getCallbackForms(): array
316 {
317 return self::getFormsByFilter(['IS_CALLBACK_FORM' => 'Y', 'ACTIVE' => 'Y']);
318 }
319
320 protected static function getFormsByFilter(array $filter): array
321 {
322 $filter = array_filter(
323 $filter,
324 static function ($key)
325 {
326 return in_array($key, self::AVAILABLE_FORM_FIELDS, true);
327 },
328 ARRAY_FILTER_USE_KEY
329 );
330 $forms = [];
331
332 if (self::isCrm())
333 {
334 $forms = self::getFormsForPortal($filter);
335 }
336 elseif (Manager::isB24Connector())
337 {
338 foreach (self::getFormsViaConnector() as $form)
339 {
340 $filtred = true;
341 foreach ($filter as $key => $value)
342 {
343 if (!$form[$key] || $form[$key] !== $value)
344 {
345 $filtred = false;
346 break;
347 }
348 }
349 if ($filtred)
350 {
351 $forms[$form['ID']] = $form;
352 }
353 }
354 }
355
356 return $forms;
357 }
358
359 // endregion
360
361 // region prepare manifest
369 public static function prepareManifest(array $manifest, Block $block = null, array $params = []): array
370 {
371 // add extension
372 if (!isset($manifest['assets']) || !is_array($manifest['assets']))
373 {
374 $manifest['assets'] = [];
375 }
376 if (!isset($manifest['assets']['ext']))
377 {
378 $manifest['assets']['ext'] = [];
379 }
380 if (!is_array($manifest['assets']['ext']))
381 {
382 $manifest['assets']['ext'] = [$manifest['assets']['ext']];
383 }
384 if (!in_array('landing_form', $manifest['assets']['ext'], true))
385 {
386 $manifest['assets']['ext'][] = 'landing_form';
387 }
388
389 // style setting
390 if (
391 !isset($manifest['style']['block']) && !isset($manifest['style']['nodes'])
392 )
393 {
394 $manifest['style'] = [
395 'block' => ['type' => Block::DEFAULT_WRAPPER_STYLE],
396 'nodes' => $manifest['style'] ?? [],
397 ];
398 }
399 $manifest['style']['nodes'][self::SELECTOR_FORM_NODE] = [
400 'type' => self::STYLE_SETTING,
401 ];
402
403 if (Manager::isB24())
404 {
405 $link = '/crm/webform/';
406 }
407 else if (Manager::isB24Connector())
408 {
409 $link = '/bitrix/admin/b24connector_crm_forms.php?lang=' . LANGUAGE_ID;
410 }
411 if (isset($link))
412 {
413 $manifest['block']['attrsFormDescription'] = '<a href="' . $link . '" target="_blank">' .
414 Loc::getMessage('LANDING_BLOCK_FORM_CONFIG') .
415 '</a>';
416 }
417
418 // add callbacks
419 $manifest['callbacks'] = [
420 'afterAdd' => function (Block &$block)
421 {
422 $historyActivity = History::isActive();
424
425 $dom = $block->getDom();
426 if (!($node = $dom->querySelector(self::SELECTOR_FORM_NODE)))
427 {
428 return;
429 }
430
431 $attrsToSet = [self::ATTR_FORM_EMBED => ''];
432 if (!self::isCrm())
433 {
434 $attrsToSet[self::ATTR_FORM_FROM_CONNECTOR] = 'Y';
435 }
436
437 // if block copy - not update params
438 if (
439 ($attrsExists = $node->getAttributes())
440 && isset($attrsExists[self::ATTR_FORM_PARAMS])
441 && $attrsExists[self::ATTR_FORM_PARAMS]
442 && $attrsExists[self::ATTR_FORM_PARAMS]->getValue()
443 )
444 {
445 $attrsToSet[self::ATTR_FORM_PARAMS] = $attrsExists[self::ATTR_FORM_PARAMS]->getValue();
446 }
447 else
448 {
449 // try to get 1) default callback form 2) last added form 3) create new form
450 $forms = self::getFormsByFilter([
451 'XML_ID' => 'crm_preset_fb'
452 ]);
453 $forms = self::prepareFormsToAttrs($forms);
454 if (empty($forms))
455 {
456 $forms = self::getForms(true); // force to preserve cycle when create form landing block
457 $forms = self::prepareFormsToAttrs($forms);
458 if (empty($forms))
459 {
460 $forms = self::createDefaultForm();
461 $forms = self::prepareFormsToAttrs($forms);
462 }
463 }
464
465 if (!empty($forms))
466 {
467 self::setFormIdParam(
468 $block,
469 str_replace(self::INLINE_MARKER_PREFIX, '', $forms[0]['value'])
470 );
471 }
472 }
473
474 // preload alert
475 $node->setInnerHTML(
476 '<div class="g-landing-alert">'
477 . Loc::getMessage('LANDING_BLOCK_WEBFORM_PRELOADER')
478 . '</div>'
479 );
480 $block->saveContent($dom->saveHTML());
481
482 // save
483 $block->setAttributes([self::SELECTOR_FORM_NODE => $attrsToSet]);
484 $block->save();
485
486 $historyActivity ? History::activate() : History::deactivate();
487 },
488 ];
489
490 // add attrs
491 if (
492 !array_key_exists('attrs', $manifest)
493 || !is_array($manifest['attrs'])
494 )
495 {
496 $manifest['attrs'] = [];
497 }
498
499 // hard operation getAttrs is only FOR EDITOR, in public set fake array for saveAttributes later
500 $manifest['attrs'][self::SELECTOR_FORM_NODE] =
501 Landing::getEditMode()
502 ? self::getAttrs()
503 : [['attribute' => self::ATTR_FORM_PARAMS]];
504
505 return $manifest;
506 }
507
512 protected static function getAttrs(): array
513 {
514 static $attrs = [];
515 if ($attrs)
516 {
517 return $attrs;
518 }
519
520 // get from CRM or via connector
521 $forms = self::getForms();
522 $forms = self::prepareFormsToAttrs($forms);
523
524 $attrs = [
525 $attrs[] = [
526 'name' => 'Embed form flag',
527 'attribute' => self::ATTR_FORM_EMBED,
528 'type' => 'string',
529 'hidden' => true,
530 ],
531 [
532 'name' => 'Form design',
533 'attribute' => self::ATTR_FORM_STYLE,
534 'type' => 'string',
535 'hidden' => true,
536 ],
537 [
538 'name' => 'Form from connector flag',
539 'attribute' => self::ATTR_FORM_FROM_CONNECTOR,
540 'type' => 'string',
541 'hidden' => true,
542 ],
543 ];
544
545 if (!empty($forms))
546 {
547 // get forms list
548 $attrs[] = [
549 'name' => Loc::getMessage('LANDING_BLOCK_WEBFORM'),
550 'attribute' => self::ATTR_FORM_PARAMS,
551 'items' => $forms,
552 'type' => 'list',
553 ];
554 // show header
555 // use custom design
556 $attrs[] = [
557 'name' => Loc::getMessage('LANDING_BLOCK_WEBFORM_USE_STYLE'),
558 'attribute' => self::ATTR_FORM_USE_STYLE,
559 'type' => 'list',
560 'items' => [
561 [
562 'name' => Loc::getMessage('LANDING_BLOCK_WEBFORM_USE_STYLE_Y'),
563 'value' => 'Y',
564 ],
565 [
566 'name' => Loc::getMessage('LANDING_BLOCK_WEBFORM_USE_STYLE_N'),
567 'value' => 'N',
568 ],
569 ],
570 ];
571 }
572 // no form - no settings, just message for user
573 else
574 {
575 $attrs[] = [
576 'name' => Loc::getMessage('LANDING_BLOCK_WEBFORM'),
577 'attribute' => self::ATTR_FORM_PARAMS,
578 'type' => 'list',
579 'items' => !empty(self::$errors)
580 ? array_map(fn ($item) => ['name' => $item['message'], 'value' => false], self::$errors)
581 : [[
582 'name' => Loc::getMessage('LANDING_BLOCK_WEBFORM_NO_FORM'),
583 'value' => false,
584 ]],
585 ];
586 }
587
588 return $attrs;
589 }
590
596 protected static function prepareFormsToAttrs(array $forms): array
597 {
598 $sorted = [];
599 foreach ($forms as $form)
600 {
601 if (array_key_exists('ACTIVE', $form) && $form['ACTIVE'] !== 'Y')
602 {
603 continue;
604 }
605
606 $item = [
607 'name' => $form['NAME'],
608 'value' => self::INLINE_MARKER_PREFIX . $form['ID'],
609 ];
610
611 if ($form['IS_CALLBACK_FORM'] === 'Y')
612 {
613 $sorted[] = $item;
614 }
615 else
616 {
617 array_unshift($sorted, $item);
618 }
619 }
620
621 return $sorted;
622 }
623 // endregion
624
625 // region actions with blocks and forms
633 public static function getLandingFormBlocks($landingIds): array
634 {
635 if (empty($landingIds))
636 {
637 return [];
638 }
639
640 if (!is_array($landingIds))
641 {
642 $landingIds = [$landingIds];
643 }
644
645 return BlockTable::getList(
646 [
647 'select' => ['ID', 'LID'],
648 'filter' => [
649 '=LID' => $landingIds,
650 '=DELETED' => 'N',
651 'CONTENT' => '%data-b24form=%',
652 ],
653 ]
654 )->fetchAll()
655 ;
656 }
657
663 public static function getFormByBlock(int $blockId): ?int
664 {
665 $block = new Block($blockId);
666 if (preg_match(self::REGEXP_FORM_ID_INLINE, $block->getContent(), $matches))
667 {
668 return (int)$matches[1];
669 }
670 return null;
671 }
672
679 public static function setFormIdToBlock(int $blockId, int $formId): bool
680 {
681 $block = new Block($blockId);
682 self::setFormIdParam($block, $formId);
683 $block->save();
684
685 return $block->getError()->isEmpty();
686 }
687
693 protected static function setFormIdParam(Block $block, int $formId): void
694 {
695 if (($form = self::getFormById($formId)))
696 {
697 // todo: can add force public flag for replaces, when we know exactly that block is public
698 $newParam = self::INLINE_MARKER_PREFIX . $form['ID'];
699
700 $block->setAttributes([
701 self::SELECTOR_FORM_NODE => [self::ATTR_FORM_PARAMS => $newParam],
702 ]);
703 }
704 }
705
710 protected static function createDefaultForm(): array
711 {
712 if ($formId = self::createForm([]))
713 {
714 return self::getFormsByFilter(['ID' => $formId]);
715 }
716
717 return [];
718 }
719
724 protected static function createForm(array $formData): ?int
725 {
726 if (self::isCrm())
727 {
728 $form = new WebForm\Form;
729
730 $defaultData = WebForm\Preset::getById('crm_preset_cd');
731
732 $defaultData['XML_ID'] = '';
733 $defaultData['ACTIVE'] = 'Y';
734 $defaultData['IS_SYSTEM'] = 'N';
735 $defaultData['IS_CALLBACK_FORM'] = 'N';
736 $defaultData['BUTTON_CAPTION'] = $form->getButtonCaption();
737
738 $agreementId = UserConsent::getDefaultAgreementId();
739 $defaultData['USE_LICENCE'] = $agreementId ? 'Y' : 'N';
740 if ($agreementId)
741 {
742 $defaultData['LICENCE_BUTTON_IS_CHECKED'] = 'Y';
743 $defaultData['AGREEMENT_ID'] = $agreementId;
744 }
745
746 $isLeadEnabled = LeadSettings::getCurrent()->isEnabled();
747 $defaultData['ENTITY_SCHEME'] = (string)(
748 $isLeadEnabled
749 ? WebForm\Entity::ENUM_ENTITY_SCHEME_LEAD
750 : WebForm\Entity::ENUM_ENTITY_SCHEME_DEAL
751 );
752
753 $currentUserId = is_object($GLOBALS['USER']) ? $GLOBALS['USER']->getId() : null;
754 $defaultData['ACTIVE_CHANGE_BY'] = $currentUserId;
755 $defaultData['ASSIGNED_BY_ID'] = $currentUserId;
756
757 $formData = array_merge($defaultData, $formData);
758 $form->merge($formData);
759 $form->save();
760
761 return !$form->hasErrors() ? $form->getId() : null;
762 }
763
764 return null;
765 }
766
771 public static function setSpecialFormToBlock(Block $block, string $xmlId): void
772 {
773 if (($formData = self::getSpecialFormsData()[$xmlId]))
774 {
775 $formId = null;
776 foreach (self::getForms() as $form)
777 {
778 if (
779 array_key_exists('XML_ID', $form)
780 && $form['XML_ID'] === $xmlId
781 )
782 {
783 $formId = $form['ID'];
784 break;
785 }
786 }
787
788 if (!$formId)
789 {
790 $formId = self::createForm($formData);
791 }
792
793 if ($formId)
794 {
795 self::setFormIdParam($block, $formId);
796 $block->save();
797 }
798 }
799 }
800
801 protected static function getSpecialFormsData(): ?array
802 {
803 if (self::isCrm())
804 {
805 $data = [
806 'crm_preset_store_v3' => [
807 'XML_ID' => 'crm_preset_store_v3',
808 'NAME' => Loc::getMessage('LANDING_FORM_SPECIAL_STOREV3_NAME'),
809 'IS_SYSTEM' => 'N',
810 'ACTIVE' => 'Y',
811 'RESULT_SUCCESS_TEXT' => Loc::getMessage('LANDING_FORM_SPECIAL_STOREV3_RESULT_SUCCESS'),
812 'RESULT_FAILURE_TEXT' => Loc::getMessage('LANDING_FORM_SPECIAL_STOREV3_RESULT_FAILURE'),
813 'COPYRIGHT_REMOVED' => 'N',
814 'IS_PAY' => 'N',
815 'FORM_SETTINGS' => [
816 'DEAL_DC_ENABLED' => 'Y',
817 ],
818 'BUTTON_CAPTION' => '',
819 'FIELDS' => [
820 [
821 'TYPE' => 'string',
822 'CODE' => 'CONTACT_NAME',
823 'CAPTION' => Loc::getMessage('LANDING_FORM_SPECIAL_STOREV3_FIELD_NAME'),
824 'SORT' => 100,
825 'REQUIRED' => 'N',
826 'MULTIPLE' => 'N',
827 'PLACEHOLDER' => '',
828 ],
829 [
830 'TYPE' => 'phone',
831 'CODE' => 'CONTACT_PHONE',
832 'CAPTION' => Loc::getMessage('LANDING_FORM_SPECIAL_STOREV3_FIELD_PHONE'),
833 'SORT' => 200,
834 'REQUIRED' => 'N',
835 'MULTIPLE' => 'N',
836 'PLACEHOLDER' => '',
837 ],
838 [
839 'TYPE' => 'text',
840 'CODE' => 'DEAL_COMMENTS',
841 'CAPTION' => Loc::getMessage('LANDING_FORM_SPECIAL_STOREV3_FIELD_COMMENT'),
842 'SORT' => 300,
843 'REQUIRED' => 'N',
844 'MULTIPLE' => 'N',
845 'PLACEHOLDER' => '',
846 ],
847 ],
848 ],
849 ];
850
851 $isLeadEnabled = LeadSettings::getCurrent()->isEnabled();
852
853 foreach ($data as $id => $form)
854 {
855 if ($isLeadEnabled)
856 {
857 foreach ($data[$id]['FIELDS'] as $key => $field)
858 {
859 $field['CODE'] = str_replace(['CONTACT', 'DEAL'], 'LEAD', $field['CODE']);
860 $data[$id]['FIELDS'][$key] = $field;
861 }
862 }
863 }
864
865 return $data;
866 }
867
868 return null;
869 }
870
871 // endregion
872
873 // region update
878 public static function updateLandingToEmbedForms(int $landingId): void
879 {
880 $res = BlockTable::getList(
881 [
882 'select' => [
883 'ID',
884 ],
885 'filter' => [
886 'LID' => $landingId,
887 '=DELETED' => 'N',
888 ],
889 ]
890 );
891 while ($row = $res->fetch())
892 {
893 $block = new Block($row['ID']);
894 self::updateBlockToEmbed($block);
895 }
896 }
897
902 protected static function updateBlockToEmbed(Block $block): void
903 {
904 // check if update needed
905 $manifest = $block->getManifest();
906 if (
907 !$manifest['block']['subtype']
908 || (!is_array($manifest['block']['subtype']) && $manifest['block']['subtype'] !== 'form')
909 || (is_array($manifest['block']['subtype']) && !in_array('form', $manifest['block']['subtype'], true))
910 )
911 {
912 return;
913 }
914 $dom = $block->getDom();
915 if (
916 !($resultNode = $dom->querySelector(self::SELECTOR_FORM_NODE))
917 || !($attrs = $resultNode->getAttributes())
918 || !array_key_exists(self::ATTR_FORM_PARAMS, $attrs))
919 {
920 return;
921 }
922 $formParams = explode('|', $attrs[self::ATTR_FORM_PARAMS]->getValue());
923 if (count($formParams) !== 2 || !(int)$formParams[0])
924 {
925 return;
926 }
927
928 // update
929 $forms = self::getForms();
930 if (array_key_exists($formParams[0], $forms))
931 {
932 $form = $forms[$formParams[0]];
933 self::setFormIdParam($block, $form['ID']);
934 $resultNode->setAttribute(self::ATTR_FORM_EMBED, '');
935 $resultNode->removeAttribute(self::ATTR_FORM_OLD_DOMAIN);
936 $resultNode->removeAttribute(self::ATTR_FORM_OLD_HEADER);
937
938 if (
939 !array_key_exists(self::ATTR_FORM_STYLE, $attrs)
940 || !$attrs[self::ATTR_FORM_STYLE]->getValue()
941 )
942 {
943 // find new styles
944 $contentFromRepo = Block::getContentFromRepository($block->getCode());
945 if (
946 $contentFromRepo
947 && preg_match(self::REGEXP_FORM_STYLE, $contentFromRepo, $style)
948 )
949 {
950 $resultNode->setAttribute(self::ATTR_FORM_STYLE, $style[1]);
951 }
952 }
953 }
954
955 if (($oldStyleNode = $dom->querySelector(self::SELECTOR_OLD_STYLE_NODE)))
956 {
957 $oldStyleNode->getParentNode()->removeChild($oldStyleNode);
958 }
959
960 $block->saveContent($dom->saveHTML());
961 $block->save();
962 }
963
969 public static function getOriginalFormDomain(): string
970 {
971 trigger_error(
972 "Now using embedded forms, no need domain",
973 E_USER_WARNING
974 );
975
976 return '';
977 }
978 // endregion
979}
save(array $additionalFields=[])
Definition block.php:3136
getManifest($extended=false, $missCache=false, array $params=array())
Definition block.php:2117
saveContent(string $content, $designed=false)
Definition block.php:3108
getDom($clear=false)
Definition block.php:3962
static setFormIdToBlock(int $blockId, int $formId)
Definition form.php:679
static prepareFormsToAttrs(array $forms)
Definition form.php:596
static prepareFormsToPublication(string $content)
Definition form.php:61
static setSpecialFormToBlock(Block $block, string $xmlId)
Definition form.php:771
static getOriginalFormDomain()
Definition form.php:969
static getFormsByFilter(array $filter)
Definition form.php:320
static createForm(array $formData)
Definition form.php:724
static setFormIdParam(Block $block, int $formId)
Definition form.php:693
static prepareManifest(array $manifest, Block $block=null, array $params=[])
Definition form.php:369
static getFormsViaConnector()
Definition form.php:273
static prepareFormsToView(string $content)
Definition form.php:72
static updateBlockToEmbed(Block $block)
Definition form.php:902
static replaceFormMarkers(string $content)
Definition form.php:87
static updateLandingToEmbedForms(int $landingId)
Definition form.php:878
static getFormByBlock(int $blockId)
Definition form.php:663
static getFormsForPortal(array $filter=[])
Definition form.php:244
static getFormById(int $id)
Definition form.php:304
static getLandingFormBlocks($landingIds)
Definition form.php:633
static getForms(bool $force=false)
Definition form.php:215
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
$GLOBALS['____1979065141']
Definition mutator.php:1