PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 3.6.65
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v3.6.65
6.2.14 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 All 196 releases
fluentform / app / Services / Parser / Form.php

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

381 lines 9.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\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 = [
276 'custom_payment_component',
277 'multi_payment_component',
278 'payment_method',
279 'item_quantity_component',
280 'payment_coupon'
281 ];
282
283 return array_filter($fields, function ($field) use ($paymentElements) {
284 return in_array($field['element'], $paymentElements);
285 });
286 }
287
288 /**
289 * Get Payment Input Fields
290 *
291 * @return array
292 */
293 public function getPaymentInputFields($with = ['element'])
294 {
295 $fields = $this->getInputs($with);
296 $paymentElements = [
297 'custom_payment_component',
298 'multi_payment_component'
299 ];
300
301 return array_filter($fields, function ($field) use ($paymentElements) {
302 return in_array($field['element'], $paymentElements);
303 });
304 }
305
306 /**
307 * Determine whether the form has payment elements
308 *
309 * @return bool
310 */
311 public function hasPaymentFields()
312 {
313 $fields = $this->getInputs(['element']);
314
315 $paymentElements = [
316 'custom_payment_component',
317 'multi_payment_component',
318 'payment_method'
319 ];
320 foreach ($fields as $field) {
321 if(in_array($field['element'], $paymentElements)) {
322 return true;
323 }
324 }
325
326 return false;
327 }
328
329
330 /**
331 * Get an specific field for an element type.
332 *
333 * @param $element
334 * @param $attribute
335 * @param array $with
336 * @return array|null
337 */
338 public function getField($element, $attribute, $with = [])
339 {
340 $element = $this->getElement($element, $with);
341
342 return array_intersect_key($element, array_flip((array) $attribute));
343 }
344
345
346 /**
347 * Get Payment Input Fields
348 *
349 * @return array
350 */
351 public function getAttachmentInputFields($with = ['element'])
352 {
353 $fields = $this->getInputs($with);
354 $paymentElements = [
355 'input_file',
356 'input_image',
357 'featured_image',
358 'signature'
359 ];
360
361 return array_filter($fields, function ($field) use ($paymentElements) {
362 return in_array($field['element'], $paymentElements);
363 });
364 }
365
366 /**
367 * Get Any Field Type
368 * @return array
369 */
370
371 public function getInputsByElementTypes($types, $with = ['element'])
372 {
373 $fields = $this->getInputs($with);
374
375 return array_filter($fields, function ($field) use ($types) {
376 return in_array($field['element'], $types);
377 });
378 }
379
380 }
381