PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 4.3.17
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v4.3.17
6.2.13 6.2.12 6.2.10 6.2.11 6.2.9 6.2.8 6.2.7 6.2.6 6.2.5 6.2.4 6.2.3 6.2.2 3.6.22 3.6.31 3.6.40 3.6.41 3.6.42 3.6.50 3.6.51 3.6.60 3.6.61 3.6.62 3.6.64 3.6.65 3.6.66 All 195 releases
fluentform / app / Services / Parser / Extractor.php

Extractor.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 4.3.17, at app/Services/Parser/Extractor.php

369 lines 10.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentForm\App\Services\Parser;
4
5 use FluentForm\Framework\Helpers\ArrayHelper as Arr;
6
7 class Extractor
8 {
9 /**
10 * The form field settings.
11 *
12 * @var array
13 */
14 protected $fields;
15
16 /**
17 * The properties that need to be extracted.
18 *
19 * @var array
20 */
21 protected $with;
22
23 /**
24 * The supported form input types defined for Fluent Forms.
25 *
26 * @var array
27 */
28 protected $inputTypes;
29
30 /**
31 * The extracted result.
32 *
33 * @var array
34 */
35 protected $result = [];
36
37 /**
38 * The current form field that's being set when we loop
39 * through all the form fields in the looper method.
40 *
41 * @var array
42 */
43 protected $field = [];
44
45 /**
46 * The current field attribute that's being set when we loop
47 * through all the form fields in the looper method.
48 *
49 * @var string
50 */
51 protected $attribute;
52
53 /**
54 * Extractor constructor.
55 *
56 * @param array $fields
57 * @param array $with
58 * @param array $inputTypes
59 */
60 public function __construct($fields, $with, $inputTypes)
61 {
62 $this->fields = $fields;
63 $this->with = $with;
64 $this->inputTypes = $inputTypes;
65 }
66
67 /**
68 * The extractor initializer for getting the extracted data.
69 *
70 * @return array
71 */
72 public function extract()
73 {
74 $this->looper($this->fields);
75
76 return $this->result;
77 }
78
79 /**
80 * The recursive looper method to loop each
81 * of the fields and extract it's data.
82 *
83 * @param array $fields
84 */
85 protected function looper($fields = [])
86 {
87 foreach ($fields as $field) {
88 // If the field is a Container (collection of other fields)
89 // then we will recursively call this function to resolve.
90 if ($field['element'] === 'container') {
91 foreach ($field['columns'] as $item) {
92 $this->looper($item['fields']);
93 }
94 }
95
96 // Now the field is supposed to be a flat field.
97 // We can extract the desired keys as we want.
98 else {
99 if (in_array($field['element'], $this->inputTypes)) {
100 $this->extractField($field);
101 }
102 }
103 }
104 }
105
106 /**
107 * Extract the form field.
108 *
109 * @param array $field
110 * @return $this
111 */
112 protected function extractField($field)
113 {
114 // Before starting the extraction we'll set the current
115 // field and it's attribute name at first. And then we
116 // will proceed to extract the field settings that
117 // the developer demanded using $with initially.
118 $this->prepareIteration($field, Arr::get($field, 'attributes.name'))
119 ->setElement()
120 ->setAdminLabel()
121 ->setLabel()
122 ->setOptions()
123 ->setAdvancedOptions()
124 ->setSettings()
125 ->setRaw()
126 ->setAttributes()
127 ->setValidations()
128 ->handleCustomField();
129
130 return $this;
131 }
132
133 /**
134 * Set the field and attribute of the current iteration when
135 * we loop through the form fields using the looper method.
136 *
137 * @param array $field
138 * @param string $attribute
139 * @return $this
140 */
141 protected function prepareIteration($field, $attribute)
142 {
143 $this->field = $field;
144
145 $this->attribute = $attribute;
146
147 return $this;
148 }
149
150 /**
151 * Set the element of the form field.
152 *
153 * @param array $field
154 * @param string $attributeName
155 * @return $this
156 */
157 protected function setElement()
158 {
159 $this->result[$this->attribute]['element'] = $this->field['element'];
160 return $this;
161 }
162
163
164 /**
165 * Set the label of the form field.
166 *
167 * @return $this
168 */
169 protected function setLabel()
170 {
171 if (in_array('label', $this->with)) {
172 $this->result[$this->attribute]['label'] = Arr::get($this->field, 'settings.label', '');
173 }
174
175 return $this;
176 }
177
178 /**
179 * Set the admin label of the form field.
180 *
181 * @return $this
182 */
183 protected function setAdminLabel()
184 {
185 if (in_array('admin_label', $this->with)) {
186 $adminLabel = Arr::get($this->field, 'settings.admin_field_label') ?:
187 Arr::get($this->field, 'settings.label') ?:
188 Arr::get($this->field, 'element');
189
190 $this->result[$this->attribute]['admin_label'] = $adminLabel;
191 }
192
193
194 return $this;
195 }
196
197 /**
198 * Set the options of the form field.
199 *
200 * @return $this
201 */
202 protected function setOptions()
203 {
204 if (in_array('options', $this->with)) {
205 $options = Arr::get($this->field, 'options', []);
206
207 if(!$options) {
208 $newOptions = Arr::get($this->field, 'settings.advanced_options', []);
209
210 if(
211 !$newOptions
212 && Arr::get($this->field,'element') == 'multi_payment_component'
213 && Arr::get($this->field,'attributes.type') != 'single'
214 ) {
215 $pricingOptions = Arr::get($this->field, 'settings.pricing_options', []);
216 foreach ($pricingOptions as $pricingOption) {
217 $newOptions[] = [
218 'value' => $pricingOption['label'],
219 'label' => $pricingOption['label']
220 ];
221 }
222 }
223
224 $options = [];
225 if($newOptions) {
226 foreach ($newOptions as $option) {
227 $value = sanitize_text_field($option['value']);
228 $options[$value] = sanitize_text_field($option['label']);
229 }
230 }
231 }
232 $this->result[$this->attribute]['options'] = $options;
233 }
234 return $this;
235 }
236
237 /**
238 * Set the advanced options of the form field.
239 *
240 * @return $this
241 */
242 protected function setAdvancedOptions()
243 {
244 if (in_array('advanced_options', $this->with)) {
245 $this->result[$this->attribute]['advanced_options'] = Arr::get($this->field, 'settings.advanced_options', []);
246 }
247 return $this;
248 }
249
250 protected function setSettings()
251 {
252 if (in_array('settings', $this->with)) {
253 $this->result[$this->attribute]['settings'] = Arr::get($this->field, 'settings', []);
254 }
255 return $this;
256 }
257
258 /**
259 * Set the attributes of the form field.
260 *
261 * @return $this
262 */
263 protected function setAttributes()
264 {
265 if (in_array('attributes', $this->with)) {
266 $this->result[$this->attribute]['attributes'] = Arr::get($this->field, 'attributes');
267 }
268
269
270 return $this;
271 }
272
273 /**
274 * Set the validation rules and conditions of the form field.
275 *
276 * @return $this
277 */
278 protected function setValidations()
279 {
280 if (in_array('rules', $this->with)) {
281 $this->result[$this->attribute]['rules'] = Arr::get(
282 $this->field,
283 'settings.validation_rules'
284 );
285
286 $this->result[$this->attribute]['conditionals'] = Arr::get(
287 $this->field,
288 'settings.conditional_logics'
289 );
290 }
291
292 return $this;
293 }
294
295 /**
296 * Handle the child fields of the custom field.
297 *
298 * @return $this
299 */
300 protected function handleCustomField()
301 {
302 // If this field is a custom field we'll assume it has it's child fields
303 // under the `fields` key. Then we are gonna modify those child fields'
304 // attribute `name`, `label` & `conditional_logics` properties using
305 // the parent field. The current implementation will modify those
306 // properties in a way so that we can use dot notation to access.
307 $customFields = Arr::get($this->field, 'fields');
308
309 if ($customFields) {
310 $parentAttribute = Arr::get($this->field, 'attributes.name');
311
312 $parentConditionalLogics = Arr::get($this->field, 'settings.conditional_logics', []);
313
314 $isAddressOrNameField = in_array(Arr::get($this->field, 'element'), ['address', 'input_name']);
315
316 $isRepeatField = Arr::get($this->field, 'element') === 'input_repeat' || Arr::get($this->field, 'element') == 'repeater_field';
317
318 foreach ($customFields as $index => $customField) {
319 // If the current field is in fact `address` || `name` field
320 // then we have to only keep the enabled child fields
321 // by the user from the form editor settings.
322 if ($isAddressOrNameField) {
323 if (!Arr::get($customField, 'settings.visible', false)) {
324 unset($customFields[$index]);
325 continue;
326 }
327 }
328
329 // Depending on whether the parent field is a repeat field or not
330 // the modified attribute name of the child field will vary.
331 if ($isRepeatField) {
332 $modifiedAttribute = $parentAttribute.'['.$index.'].*';
333 } else {
334 $modifiedAttribute = $parentAttribute.'['.Arr::get($customField, 'attributes.name').']';
335 }
336
337 $modifiedLabel = $parentAttribute.'['.Arr::get($customField, 'settings.label').']';
338
339 $customField['attributes']['name'] = $modifiedAttribute;
340
341 $customField['settings']['label'] = $modifiedLabel;
342
343 // Now, we'll replace the `conditional_logics` property
344 $customField['settings']['conditional_logics'] = $parentConditionalLogics;
345
346 // Now that this field's properties are handled we can pass
347 // it to the extract field method to extract it's data.
348 $this->extractField($customField);
349 }
350 }
351
352 return $this;
353 }
354
355 /**
356 * Set the raw field of the form field.
357 *
358 * @return $this
359 */
360 protected function setRaw()
361 {
362 if (in_array('raw', $this->with)) {
363 $this->result[$this->attribute]['raw'] = $this->field;
364 }
365
366 return $this;
367 }
368 }
369