PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 4.3.6
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v4.3.6
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 / Form.php

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

405 lines 10.0 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\App\Helpers\Str;
6 use FluentForm\Framework\Helpers\ArrayHelper as Arr;
7
8 class Form
9 {
10 /**
11 * @var \stdClass $form
12 */
13 protected $form;
14
15 /**
16 * @var array $inputTypes
17 */
18 protected $inputTypes;
19
20 /**
21 * The parsed form fields.
22 *
23 * @var array
24 */
25 protected $parsed;
26
27 /**
28 * The parsed validations
29 *
30 * @var array
31 */
32 protected $validations;
33
34 /**
35 * Form Parser constructor.
36 *
37 * @param \stdClass $form
38 */
39 public function __construct($form)
40 {
41 $this->form = $form;
42
43 $this->setInputTypes();
44 }
45
46 /**
47 * Set input types of the form.
48 *
49 * @param array $types
50 * @return \FluentForm\App\Services\Parser\Form $this
51 */
52 public function setInputTypes($types = [])
53 {
54 // If the $types is empty we'll use the default input types.
55 $types = $types ?: [
56 'input_text',
57 'input_name',
58 'textarea',
59 'select',
60 'input_radio',
61 'input_checkbox',
62 'input_email',
63 'input_url',
64 'input_password',
65 'input_file',
66 'input_image',
67 'input_date',
68 'select_country',
69 'input_number',
70 'input_repeat',
71 'address',
72 'terms_and_condition',
73 'input_hidden',
74 'ratings',
75 'net_promoter',
76 'tabular_grid',
77 'gdpr_agreement',
78 'taxonomy'
79 ];
80
81 // Firing an event so that others can hook into it and add other input types.
82 $this->inputTypes = apply_filters('fluentform_form_input_types', $types);
83
84 return $this;
85 }
86
87 /**
88 * Get form fields.
89 *
90 * @param boolean $asArray
91 * @return array
92 */
93 public function getFields($asArray = false)
94 {
95 $fields = json_decode($this->form->form_fields, $asArray);
96
97 $default = $asArray ? [] : null;
98
99 return Arr::get((array)$fields, 'fields', $default);
100 }
101
102 /**
103 * Get flatten form inputs. Flatten implies that all
104 * of the form fields will be in a simple array.
105 *
106 * @param array $with
107 * @return array
108 */
109 public function getInputs($with = [])
110 {
111 // If the form is already parsed we'll return it. Otherwise,
112 // we'll parse the form and return the data after saving it.
113 if (!$this->parsed) {
114 $fields = $this->getFields(true);
115
116 $with = $with ?: ['admin_label', 'element', 'options', 'attributes', 'raw'];
117 $this->parsed = (new Extractor($fields, $with, $this->inputTypes))->extract();
118 }
119
120 return $this->parsed;
121 }
122
123 /**
124 * Get the inputs just as they setup in the form editor.
125 * e.g. `names` as `names` not with the child fields.
126 *
127 * @param array $with
128 * @return array
129 */
130 public function getEntryInputs($with = ['admin_label'])
131 {
132 $inputs = $this->getInputs($with);
133
134 // The inputs that has `[]` in their keys are custom fields
135 // & for the purpose of this scenario we'll remove those.
136 foreach ($inputs as $key => $value) {
137 if (Str::contains($key, '[')) {
138 unset($inputs[$key]);
139 }
140 }
141
142 return $inputs;
143 }
144
145 /**
146 * Get the flatten inputs as the result of the `getInputs`
147 * method but replace the keys those have `[]` with `.`
148 * And also remove the repeat fields' child fields.
149 *
150 * @param array $with
151 * @param array
152 */
153 public function getShortCodeInputs($with = ['admin_label'])
154 {
155 $inputs = $this->getInputs($with);
156
157 $result = [];
158
159 // For the purpose of this scenario we'll rename
160 // the keys that have `[]` in 'em to `.` and
161 // remove the keys that have `*` in 'em.
162 foreach ($inputs as $key => $value) {
163 if (Str::contains($key, '*')) {
164 unset($inputs[$key]);
165 } else {
166 $key = str_replace(['[', ']'], ['.'], $key);
167
168 $result[$key] = $value;
169 }
170 }
171
172 return $result;
173 }
174
175 /**
176 * Get admin labels of the form fields.
177 *
178 * @param array $fields
179 * @return array
180 */
181 public function getAdminLabels($fields = [])
182 {
183 $fields = $fields ?: $this->getInputs(['admin_label']);
184
185 $labels = [];
186
187 foreach ($fields as $key => $field) {
188 $labels[$key] = Arr::get($field, 'admin_label');
189 }
190
191 return $labels;
192 }
193
194 /**
195 * Get admin labels of the form fields.
196 *
197 * @param array $inputs
198 * @param array $fields
199 * @return array
200 */
201 public function getValidations($inputs, $fields = [])
202 {
203 // If the form validations are already parsed we'll return it.
204 // Otherwise, we'll parse the form validation and return
205 // the data after saving it to the validations array.
206 if (!$this->validations) {
207 $fields = $fields ?: $this->getInputs(['rules']);
208 $this->validations = (new Validations($fields, $inputs))->get();
209 }
210
211 return $this->validations;
212 }
213
214 /**
215 * Get an element by it's name.
216 *
217 * @param string|array $name
218 * @param array $with
219 * @return array
220 */
221 public function getElement($name, $with = [])
222 {
223 $this->inputTypes = (array) $name;
224
225 return $this->getInputs($with);
226 }
227
228 /**
229 * Determine whether the form has an element.
230 *
231 * @param string $name
232 * @return bool
233 */
234 public function hasElement($name)
235 {
236 return array_key_exists($name, $this->getElement($name, ['element']));
237 }
238
239 /**
240 * Determine whether the form has any required fields.
241 *
242 * @param array $fields
243 * @return bool
244 */
245 public function hasRequiredFields($fields = [])
246 {
247 // $fields can be user provided when called this method or,
248 // the current object could have already parsed fields or,
249 // we should parse the form and use the processed result.
250 $fields = $fields ?: $this->parsed ?: $this->getInputs(['rules']);
251
252 $exist = false;
253
254 foreach ($fields as $field) {
255 $exist = Arr::get($field, 'rules.required.value');
256
257 if ($exist) {
258 break;
259 }
260 }
261
262 return (boolean)$exist;
263 }
264
265
266 /**
267 * Get Payment Related Fields
268 *
269 * @param array $with array
270 * @return array
271 */
272 public function getPaymentFields($with = ['element'])
273 {
274 $fields = $this->getInputs($with);
275 $paymentElements = apply_filters('fluentform_form_payment_fields',
276 [
277 'custom_payment_component',
278 'multi_payment_component',
279 'payment_method',
280 'item_quantity_component',
281 'payment_coupon',
282 'subscription_payment_component'
283 ]);
284
285 return array_filter($fields, function ($field) use ($paymentElements) {
286 return in_array($field['element'], $paymentElements);
287 });
288 }
289
290 /**
291 * Get Payment Input Fields
292 *
293 * @return array
294 */
295 public function getPaymentInputFields($with = ['element'])
296 {
297 $fields = $this->getInputs($with);
298
299 $paymentElements = apply_filters('fluentform_form_payment_inputs', [
300 'custom_payment_component',
301 'multi_payment_component'
302 ]);
303
304 return array_filter($fields, function ($field) use ($paymentElements) {
305 return in_array($field['element'], $paymentElements);
306 });
307 }
308
309 /**
310 * Determine whether the form has payment elements
311 *
312 * @return bool
313 */
314 public function hasPaymentFields()
315 {
316 $fields = $this->getInputs(['element']);
317
318 $paymentElements = apply_filters('fluentform_form_payment_fields',
319 [
320 'custom_payment_component',
321 'multi_payment_component',
322 'payment_method',
323 'item_quantity_component',
324 'payment_coupon',
325 'subscription_payment_component'
326 ]);
327
328 foreach ($fields as $field) {
329 if (in_array($field['element'], $paymentElements)) {
330 return true;
331 }
332 }
333
334 return false;
335 }
336
337
338 /**
339 * Get an specific field for an element type.
340 *
341 * @param $element
342 * @param $attribute
343 * @param array $with
344 * @return array|null
345 */
346 public function getField($element, $attribute, $with = [])
347 {
348 $element = $this->getElement($element, $with);
349
350 return array_intersect_key($element, array_flip((array)$attribute));
351 }
352
353
354 /**
355 * Get Payment Input Fields
356 *
357 * @return array
358 */
359 public function getAttachmentInputFields($with = ['element'])
360 {
361 $fields = $this->getInputs($with);
362 $paymentElements = [
363 'input_file',
364 'input_image',
365 'featured_image',
366 'signature'
367 ];
368
369 return array_filter($fields, function ($field) use ($paymentElements) {
370 return in_array($field['element'], $paymentElements);
371 });
372 }
373
374 /**
375 * Get Any Field Type
376 * @return array
377 */
378
379 public function getInputsByElementTypes($types, $with = ['element'])
380 {
381 $fields = $this->getInputs($with);
382
383 return array_filter($fields, function ($field) use ($types) {
384 return in_array($field['element'], $types);
385 });
386 }
387
388 /**
389 * Get Address Fields
390 *
391 * @return array
392 */
393 public function getAddressFields($with = ['admin_label', 'attributes'])
394 {
395 $fields = $this->getInputs($with);
396 $addressElements = [
397 'address'
398 ];
399
400 return array_filter($fields, function ($field) use ($addressElements) {
401 return in_array($field['element'], $addressElements);
402 });
403 }
404 }
405