Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
facebookformbuilder.php
1<?php
2
4
7
9{
10 private const EMPTY_FIELD_NAME = null;
11
13 private $mapper;
14
18 public function __construct(LeadAds\Mapper $mapper)
19 {
20 $this->mapper = $mapper;
21 }
22
28 public function buildForm(array $form): LeadAdsForm
29 {
30 return new LeadAdsForm(
31 [
32 "id" => $form["ID"],
33 "name" => $form["NAME"],
34 "description" => !$form['CONTEXT_CARD']['content']
35 ? null
36 : implode(PHP_EOL, $form['CONTEXT_CARD']['content']),
37 "title" => $form['CONTEXT_CARD']['title'],
38 "fields" => $this->buildQuestions($form['QUESTIONS'] ?? []),
39 "message" => $form['THANK_YOU_PAGE']['title'],
40 "link" => $form['FOLLOW_UP_ACTION_URL'],
41 "active" => $form['STATUS'] === 'ACTIVE'
42 ]
43 );
44 }
45
51 private function buildQuestions(array $questions): array
52 {
53 $questionsResult = [];
54 foreach ($questions as $field)
55 {
56 if ($this->mapper->getCrmName($field['type']))
57 {
58 $questionsResult[] = new LeadAds\Field(
59 $field['type'],
60 self::EMPTY_FIELD_NAME,
61 $field['label'],
62 $field['key']
63 );
64 }
65 elseif ($field['type'] === "CUSTOM")
66 {
67 //if question is custom condition
68 if (
69 is_array($field['dependent_conditional_questions'])
70 && is_array($field['conditional_questions_choices'])
71 )
72 {
73 $questionsResult[] = new LeadAds\Field(
74 LeadAds\Field::TYPE_RADIO,
75 self::EMPTY_FIELD_NAME,
76 $field['label'],
77 $field['key'],
78 $this->getOptions($field['conditional_questions_choices'], 0)
79 );
80 foreach ($field['dependent_conditional_questions'] as $key => $question)
81 {
82 $questionsResult[] = new LeadAds\Field(
83 LeadAds\Field::TYPE_RADIO,
84 self::EMPTY_FIELD_NAME,
85 $question['name'],
86 $question['field_key'],
87 $this->getOptions($field['conditional_questions_choices'], $key + 1)
88 );
89 }
90 }
91 elseif (is_array($field['options']))
92 {
93 $questionsResult[] = new LeadAds\Field(
94 LeadAds\Field::TYPE_RADIO,
95 self::EMPTY_FIELD_NAME,
96 $field['label'],
97 $field['key'],
98 array_map(
99 static function ($option): array {
100 return [
101 "key" => $option["key"],
102 "label" => $option['value'],
103 ];
104 },
105 $field['options']
106 )
107 );
108 }
109 else
110 {
111 $questionsResult[] = new LeadAds\Field(
112 LeadAds\Field::TYPE_INPUT,
113 self::EMPTY_FIELD_NAME,
114 $field['label'],
115 $field['key']
116 );
117 }
118 }
119 }
120
121 return $questionsResult;
122 }
123
130 private function getOptions(array $options, int $depth): array
131 {
132 $result = [];
133 foreach ($options as $value)
134 {
135 if ($depth === 0)
136 {
137 $result[$value['customized_token']] = ['key' => $value['customized_token'], 'label' => $value['value']];
138 }
139 elseif ($depth > 0 && $value['next_question_choices'])
140 {
141 foreach ($this->getOptions($value['next_question_choices'], $depth - 1) as $option)
142 {
143 if (array_key_exists($option["key"], $result))
144 {
145 continue;
146 }
147
148 $result[$option["key"]] = $option;
149 }
150 }
151 }
152
153 return $result;
154 }
155}