Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
vkontakteformbuilder.php
1<?php
2
4
8
9Loc::loadMessages(__FILE__);
10
12{
13 protected const PAGE_TYPE_QUESTION = 'question';
15 private $mapper;
16
17 private const EMPTY_FIELD_NAME = null;
18
22 public function __construct(LeadAds\Mapper $mapper)
23 {
24 $this->mapper = $mapper;
25 }
26
33 public function buildForm(array $form): LeadAdsForm
34 {
35 $fields = $this->buildFields($form['CONTACT_FIELDS'] ?? []);
36 $questions = $this->buildQuestions($form['PAGES'] ?? []);
37 return new LeadAdsForm(
38 [
39 "id" => $form['ID'],
40 "name" => $form['NAME'],
41 // "description" => $form['DESCRIPTION'],
42 "title" => $form['NAME'],
43 "fields" => array_merge($fields, $questions)
44 // "message" => $form['CONFIRMATION'],
45 // "link" => $form['URL'],
46 ]
47 );
48 }
49
55 private function buildFields(array $questions) : array
56 {
57 foreach ($questions as $key => $externalField)
58 {
59 if ($fieldName = $this->mapper->getCrmName($externalField))
60 {
61 $questions[$key] = new LeadAds\Field(
62 $fieldName,
63 self::EMPTY_FIELD_NAME,
64 $this->getFormLabel($fieldName),
65 $externalField
66 );
67 }
68 else
69 {
70 unset($questions[$key]);
71 }
72 }
73
74 return $questions;
75 }
76
81 private function getFormLabel(string $type) : string
82 {
83 if (!$phrase = Loc::getMessage("SEO_VKONTAKTE_FORM_BUILDER_TYPE_{$type}"))
84 {
85 $phrase = Loc::getMessage("SEO_VKONTAKTE_FORM_BUILDER_UNKNOWN_TYPE");
86 }
87
88 return $phrase;
89 }
90
91 private function buildQuestions(array $pages)
92 {
93 $result = [];
94 foreach ($pages as $page)
95 {
96 if ( ! (isset($page['blocks']) && is_array($page['blocks'])))
97 {
98 continue;
99 }
100
101 foreach ($page['blocks'] as $block)
102 {
103 if (
104 ! (isset($block['block_data']) && is_array($block['block_data']))
105 && $block['type'] !== self::PAGE_TYPE_QUESTION
106 && ! (isset($block['block_data']['data']) && is_array($block['block_data']['data']))
107 )
108 {
109 continue;
110 }
111
112
113 if ($fieldName = $this->mapper->getCrmName(self::PAGE_TYPE_QUESTION))
114 {
115 $result[] = new LeadAds\Field(
116 $fieldName,
117 self::EMPTY_FIELD_NAME,
118 $block['block_data']['data']['text'],
119 $block['id']
120 );
121 }
122 }
123 }
124
125 return $result;
126 }
127}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29