PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 6.2.8
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v6.2.8
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 / Migrator / Classes / CalderaMigrator.php

CalderaMigrator.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 6.2.8, at app/Services/Migrator/Classes/CalderaMigrator.php

617 lines 20.9 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\Migrator\Classes;
4
5
6 use FluentForm\App\Modules\Form\Form;
7 use FluentForm\Framework\Helpers\ArrayHelper;
8
9 class CalderaMigrator extends BaseMigrator
10 {
11
12 /**
13 * @var bool
14 */
15 protected $hasStep = false;
16
17 public function __construct()
18 {
19 $this->key = 'caldera';
20 $this->title = 'Caldera Forms';
21 $this->shortcode = 'caldera_form';
22 $this->hasStep = false;
23 }
24
25 /**
26 * @return bool
27 */
28 public function exist()
29 {
30 return defined('CFCORE_VER');
31 }
32
33 /**
34 * @return array
35 */
36 public function getForms()
37 {
38 $forms = [];
39 $items = \Caldera_Forms_Forms::get_forms();
40 foreach ($items as $item) {
41 $forms[] = \Caldera_Forms_Forms::get_form($item);
42 }
43 return $forms;
44 }
45
46 public function getForm($id)
47 {
48 return \Caldera_Forms_Forms::get_form($id);
49 }
50
51 public function getFormsFormatted()
52 {
53 $forms = [];
54 $items = \Caldera_Forms_Forms::get_forms();
55 foreach ($items as $item) {
56 $item = \Caldera_Forms_Forms::get_form($item);
57 $forms[] = [
58 'name' => $this->getFormName($item),
59 'id' => $this->getFormId($item),
60 'imported_ff_id' => $this->isAlreadyImported($item),
61 'entryImportSupported' => true
62 ];
63 }
64 return $forms;
65 }
66
67 /**
68 * @param $form
69 * @return array
70 */
71 public function getFields($form)
72 {
73 $fluentFields = [];
74 $fields = \Caldera_Forms_Forms::get_fields($form);
75 foreach ($fields as $name => $field) {
76 $field = (array)$field;
77 list($type, $args) = $this->formatFieldData($field, $form);
78 if ($value = $this->getFluentClassicField($type, $args)) {
79 $fluentFields[$field['ID']] = $value;
80 } else {
81 //submit button is imported separately
82 if (ArrayHelper::get($field, 'type') != 'button') {
83 $this->unSupportFields[] = ArrayHelper::get($field, 'label');
84 }
85 }
86 }
87
88 $returnData = [
89 'fields' => $this->getContainer($form, $fluentFields),
90 'submitButton' => $this->submitBtn
91 ];
92
93 if ($this->hasStep && defined('FLUENTFORMPRO')) {
94 $returnData['stepsWrapper'] = $this->getStepWrapper();
95 }
96
97 return $returnData;
98 }
99
100 private function formatFieldData($field, $form)
101 {
102 if (ArrayHelper::get($field, 'config.type_override')) {
103 $field['type'] = $field['config']['type_override'];
104 }
105
106 $args = [
107 'uniqElKey' => $field['ID'],
108 'index' => $field['ID'], // get the order id from order array
109 'required' => isset($field['required']),
110 'label' => $field['label'],
111 'label_placement' => $this->getLabelPlacement($field),
112 'name' => $field['slug'],
113 'placeholder' => ArrayHelper::get($field, 'config.placeholder'),
114 'class' => $field['config']['custom_class'],
115 'value' => ArrayHelper::get($field, 'config.default'),
116 'help_message' => ArrayHelper::get($field, 'caption'),
117 ];
118
119 $type = ArrayHelper::get($this->fieldTypes(), $field['type'], '');
120
121 switch ($type) {
122 case 'phone':
123 case 'input_text':
124 if (ArrayHelper::isTrue($field, 'config.masked')) {
125 $type = 'input_mask';
126 $args['temp_mask'] = 'custom';
127 $args['mask'] = str_replace('9', '0', $field['config']['mask']);//replace mask 9 with 0 for numbers
128 }
129 break;
130 case 'email':
131 case 'input_textarea':
132 $args['rows'] = ArrayHelper::get($field, 'config.rows');
133 break;
134 case 'input_url':
135 case 'color_picker':
136 case 'section_break':
137 case 'select':
138 case 'input_radio':
139 case 'input_checkbox':
140 case 'dropdown':
141 if ($args['placeholder'] == '') {
142 $args['placeholder'] = __('-Select-', 'fluentform');
143 }
144 $args['options'] = $this->getOptions(ArrayHelper::get($field, 'config.option', []));
145 $args['calc_value_status'] = (bool)ArrayHelper::get($field, 'config.show_values');
146
147 // Toggle switch field in Caldera
148 $isBttnType = ArrayHelper::get($field, 'type') == 'toggle_switch';
149 if ($isBttnType) {
150 $args['layout_class'] = 'ff_list_buttons'; //for btn type radio
151 }
152 if ($type == 'section_break') {
153 $args['label'] = '';
154 }
155 break;
156 case 'multi_select':
157 $args['options'] = $this->getOptions(ArrayHelper::get($field, 'config.option', []));
158 $args['calc_value_status'] = ArrayHelper::get($field, 'config.show_values') ? true : false;
159 break;
160 case 'input_date':
161 $args['format'] = Arrayhelper::get($field, 'config.format');
162 break;
163 case 'input_number':
164 $args['step'] = ArrayHelper::get($field, 'config.step');
165 $args['min'] = ArrayHelper::get($field, 'config.min');
166 $args['max'] = ArrayHelper::get($field, 'config.max');
167
168 // Caldera Calculation field
169 if (ArrayHelper::get($field, 'type') == 'calculation') {
170 $args['prefix'] = $field['config']['before'];
171 $args['suffix'] = $field['config']['after'];
172
173 if (ArrayHelper::isTrue($field, 'config.manual') || !empty(Arrayhelper::get($field,
174 'config.formular'))) {
175 $args['enable_calculation'] = true;
176 $args['calculation_formula'] = $this->convertFormulas($field, $form);
177 }
178 }
179
180 break;
181 case 'rangeslider':
182 $args['step'] = $field['config']['step'];
183 $args['min'] = $field['config']['min'];
184 $args['max'] = $field['config']['max'];
185 break;
186 case 'ratings':
187 $number = ArrayHelper::get($field, 'config.number', 5);
188 $args['options'] = array_combine(range(1, $number), range(1, $number));
189 break;
190 case 'input_file':
191 $args['help_message'] = $field['caption'];
192 $args['allowed_file_types'] = $this->getFileTypes($field, 'config.allowed');
193 $args['max_size_unit'] = 'KB';
194 $args['max_file_size'] = $this->getFileSize($field);
195 $args['max_file_count'] = ArrayHelper::isTrue($field,
196 'config.multi_upload') ? 5 : 1; //limit 5 for unlimited files
197 $args['upload_btn_text'] = ArrayHelper::get($field, 'config.multi_upload_text') ?: 'File Upload';
198 break;
199 case 'custom_html':
200 $args['html_codes'] = $field['config']['default'];
201 $args['container_class'] = $field['config']['custom_class'];
202 break;
203
204 case 'gdpr_agreement':
205 $args['tnc_html'] = $field['config']['agreement'];
206 break;
207 case 'button':
208 $pageLength = count(ArrayHelper::get($form, 'page_names'));
209 if ($field['config']['type'] == 'next' && $pageLength > 1) {
210 $this->hasStep = true;
211 $type = 'form_step';
212 break; //skipped prev button ,only one is required
213 } elseif ($field['config']['type'] != 'submit') {
214 break;
215 }
216 $this->submitBtn = $this->getSubmitBttn([
217 'uniqElKey' => $field['ID'],
218 'label' => $field['label'],
219 'class' => $field['config']['custom_class'],
220 ]);
221 break;
222 }
223
224 return array($type, $args);
225
226 }
227
228 private function getLabelPlacement($field)
229 {
230 if (ArrayHelper::get($field, 'hide_label') == 1) {
231 return 'hide_label';
232 }
233 return '';
234 }
235
236 // Function to convert shortcodes in numeric field calculations (todo)
237 private function convertFormulas($calculationField, $form)
238 {
239
240 $calderaFormula = '';
241 $fieldSlug = [];
242 $fieldID = [];
243
244 foreach ($form['fields'] as $field) {
245 $prefixTypes = ArrayHelper::get($this->fieldPrefix(), $field['type'], '');
246
247 // FieldSlug for Manual Formula
248 $fieldSlug[$field['slug']] = '{' . $prefixTypes . '.' . $field['slug'] . '}';
249
250 // FieldID for Direct Formula
251 $fieldID[$field['ID']] = '{' . $prefixTypes . '.' . $field['slug'] . '}';
252 }
253
254 // Check if Manual Formula Enabled in Caldera Otherwise get Direct Formula
255 if (!empty($calculationField['config']['manual'])) {
256
257 $calderaFormula = $calculationField['config']['manual_formula'];
258
259 $refactorShortcode = str_replace("%", "", $calderaFormula);
260
261 $refactoredFormula = str_replace(array_keys($fieldSlug), array_values($fieldSlug), $refactorShortcode);
262
263 } else {
264 if (!empty($calculationField['config']['formular'])) {
265
266 $calderaFormula = $calculationField['config']['formular'];
267
268 $refactoredFormula = str_replace(array_keys($fieldID), array_values($fieldID), $calderaFormula);
269
270 }
271 }
272
273 return $refactoredFormula;
274 }
275
276 /**
277 * @param $field
278 * @return int
279 */
280 private function getFileSize($field)
281 {
282 $fileSizeByte = ArrayHelper::get($field, 'config.max_upload', 6000);
283 $fileSizeKilobyte = ceil(($fileSizeByte * 1024) / 1000);
284
285 return $fileSizeKilobyte;
286 }
287
288 /**
289 * @return array
290 */
291 public function fieldPrefix()
292 {
293 $fieldPrefix = [
294 'number' => 'input',
295 'hidden' => 'input',
296 'range_slider' => 'input',
297 'calculation' => 'input',
298 'checkbox' => 'checkbox',
299 'radio' => 'radio',
300 'toggle_switch' => 'radio',
301 'dropdown' => 'select',
302 'filtered_select2' => 'select'
303 ];
304
305 return $fieldPrefix;
306 }
307
308 /**
309 * @return array
310 */
311 public function fieldTypes()
312 {
313 $fieldTypes = [
314 'email' => 'email',
315 'text' => 'input_text',
316 'hidden' => 'input_hidden',
317 'textarea' => 'input_textarea',
318 'paragraph' => 'input_textarea',
319 'wysiwyg' => 'input_textarea',
320 'url' => 'input_url',
321 'color_picker' => 'color_picker',
322 'phone_better' => 'phone',
323 'phone' => 'phone',
324 'select' => 'select',
325 'dropdown' => 'select',
326 'filtered_select2' => 'multi_select',
327 'radio' => 'input_radio',
328 'checkbox' => 'input_checkbox',
329 'toggle_switch' => 'input_radio',
330 'date_picker' => 'input_date',
331 'date' => 'input_date',
332 'range' => 'input_number',
333 'number' => 'input_number',
334 'calculation' => 'input_number',
335 'range_slider' => 'rangeslider',
336 'star_rating' => 'ratings',
337 'file' => 'input_file',
338 'cf2_file' => 'input_file',
339 'advanced_file' => 'input_file',
340 'html' => 'custom_html',
341 'section_break' => 'section_break',
342 'gdpr' => 'gdpr_agreement',
343 'button' => 'button',
344 ];
345 return $fieldTypes;
346 }
347
348 /**
349 * @param $options
350 * @return array
351 */
352 public function getOptions($options)
353 {
354 $formattedOptions = [];
355 foreach ($options as $key => $option) {
356 $formattedOptions[] = [
357 'label' => ArrayHelper::get($option, 'label', 'Item -' . $key),
358 'value' => $key,
359 'calc_value' => ArrayHelper::get($option, 'calc_value'),
360 'id' => $key
361 ];
362 }
363 return $formattedOptions;
364 }
365
366 /**
367 * @param $form
368 * @param $fluentFields
369 * @return array
370 */
371 private function getContainer($form, $fluentFields)
372 {
373 $containers = [];
374 if (empty($form['layout_grid']['fields'])) {
375 return $fluentFields;
376 }
377 //set fields array map for inserting into containers
378 foreach ($form['layout_grid']['fields'] as $field_id => $location) {
379 if (isset($fluentFields[$field_id])) {
380 $location = explode(':', $location);
381 $containers[$location[0]][$location[1]]['fields'][] = $fluentFields[$field_id];
382 }
383 }
384 $withContainer = [];
385 foreach ($containers as $row => $columns) {
386
387 $colsCount = count($columns);
388 $containerConfig = [];
389 if ($colsCount != 1) {
390 //with container
391 $containerConfig[] = [
392 'index' => $row,
393 'element' => 'container',
394 'attributes' => [],
395 'settings' => [
396 'container_class',
397 'conditional_logics'
398 ],
399 'editor_options' => [
400 'title' => $colsCount . ' Column Container',
401 'icon_class' => $colsCount . 'dashicons dashicons-align-center'
402 ],
403 'columns' => $columns,
404 'uniqElKey' => 'col' . '_' . md5(uniqid(wp_rand(), true))
405 ];
406 } else {
407 //without container
408 $containerConfig = $columns[1]['fields'];
409 }
410 $withContainer[] = $containerConfig;
411 }
412 array_filter($withContainer);
413 return (self::arrayFlat($withContainer));
414 }
415
416 /**
417 * @param null $array
418 * @param int $depth
419 * @return array
420 */
421 public static function arrayFlat($array = null, $depth = 1)
422 {
423 $result = [];
424 if (!is_array($array)) {
425 $array = func_get_args();
426 }
427 foreach ($array as $key => $value) {
428 if (is_array($value) && $depth) {
429 $result = array_merge($result, self::arrayFlat($value, $depth - 1));
430 } else {
431 $result = array_merge($result, [$key => $value]);
432 }
433 }
434 return $result;
435 }
436
437 /**
438 * @return array
439 */
440 private function getStepWrapper()
441 {
442 return [
443 'stepStart' => [
444 'element' => 'step_start',
445 'attributes' => [
446 'id' => '',
447 'class' => '',
448 ],
449 'settings' => [
450 'progress_indicator' => 'progress-bar',
451 'step_titles' => [],
452 'disable_auto_focus' => 'no',
453 'enable_auto_slider' => 'no',
454 'enable_step_data_persistency' => 'no',
455 'enable_step_page_resume' => 'no',
456 ],
457 'editor_options' => [
458 'title' => 'Start Paging'
459 ],
460 ],
461 'stepEnd' => [
462 'element' => 'step_end',
463 'attributes' => [
464 'id' => '',
465 'class' => '',
466 ],
467 'settings' => [
468 'prev_btn' => [
469 'type' => 'default',
470 'text' => 'Previous',
471 'img_url' => ''
472 ]
473 ],
474 'editor_options' => [
475 'title' => 'End Paging'
476 ],
477 ]
478
479 ];
480 }
481
482 /**
483 * @param $form
484 * @return array default parsed form metas
485 * @throws \Exception
486 */
487 public function getFormMetas($form)
488 {
489 $formObject = new Form(wpFluentForm());
490 $defaults = $formObject->getFormsDefaultSettings();
491 $confirmation = wp_parse_args(
492 [
493 'messageToShow' => $form['success'],
494 'samePageFormBehavior' => isset($form['hide_form']) ? 'hide_form' : 'reset_form',
495 ], $defaults['confirmation']
496 );
497 $advancedValidation = [
498 'status' => false,
499 'type' => 'all',
500 'conditions' => [
501 [
502 'field' => '',
503 'operator' => '=',
504 'value' => ''
505 ]
506 ],
507 'error_message' => '',
508 'validation_type' => 'fail_on_condition_met'
509 ];
510 $notifications =
511 [
512 'sendTo' => [
513 'type' => 'email',
514 'email' => ArrayHelper::get($form, 'mailer.recipients'),
515 'field' => '',
516 'routing' => [],
517 ],
518 'enabled' => $form['mailer']['on_insert'] ? true : false,
519 'name' => 'Admin Notification',
520 'subject' => ArrayHelper::get($form, 'mailer.email_subject', 'Admin Notification'),
521 'to' => ArrayHelper::get($form, 'mailer.recipients', '{wp.admin_email}'),
522 'replyTo' => ArrayHelper::get($form, 'mailer.reply_to', '{wp.admin_email}'),
523 'message' => str_replace('{summary}', '{all_data}', ArrayHelper::get($form, 'mailer.email_message')),
524 'fromName' => ArrayHelper::get($form, 'mailer.sender_name'),
525 'fromEmail' => ArrayHelper::get($form, 'mailer.sender_email'),
526 'bcc' => ArrayHelper::get($form, 'mailer.bcc_to'),
527 ];
528 return [
529 'formSettings' => [
530 'confirmation' => $confirmation,
531 'restrictions' => $defaults['restrictions'],
532 'layout' => $defaults['layout'],
533 ],
534 'advancedValidationSettings' => $advancedValidation,
535 'delete_entry_on_submission' => 'no',
536 'notifications' => [$notifications]
537 ];
538 }
539
540 /**
541 * @param $form
542 * @return mixed
543 */
544 protected function getFormId($form)
545 {
546 return $form['ID'];
547 }
548
549 /**
550 * @param $form
551 * @return mixed
552 */
553 protected function getFormName($form)
554 {
555 return $form['name'];
556 }
557
558 public function getEntries($formId)
559 {
560
561 $form = \Caldera_Forms::get_form($formId);
562 $totalEntries = (new \Caldera_Forms_Entry_Entries($form, 9999))->get_total('active');
563 $max_limit = apply_filters('fluentform/entry_migration_max_limit', static::DEFAULT_ENTRY_MIGRATION_MAX_LIMIT, $this->key, $totalEntries, $formId);
564 $data = \Caldera_Forms_Admin::get_entries($form, 1, $max_limit);
565 $nameKeyMap = $this->getFieldsNameMap($form);
566 $entries = [];
567 if (!is_array(ArrayHelper::get($data, 'entries'))) {
568 return $entries;
569 }
570 foreach ($data['entries'] as $entry) {
571
572 $entryId = ArrayHelper::get($entry, '_entry_id');
573 $entryFields = ArrayHelper::get(\Caldera_Forms::get_entry($entryId, $form), 'data');
574 $formattedEntry = [];
575
576
577 foreach ($entryFields as $key => $field) {
578 $value = $field['value'];
579 if (is_array($value)) {
580 $selectedOption = array_pop($value);
581 $value = \json_decode($selectedOption, true);
582 $value = array_keys($value);
583 }
584 $inputName = $nameKeyMap[$key];
585 $formattedEntry[$inputName] = $value;
586 }
587
588 $entries[] = $formattedEntry;
589
590 }
591
592 return array_reverse($entries);
593 }
594
595 /**
596 * Map Field key with its name to insert entry with input name
597 *
598 * @param array|null $form
599 * @return array|mixed
600 */
601 public function getFieldsNameMap($form)
602 {
603 $fields = \Caldera_Forms_Forms::get_fields($form);
604 $map = [];
605
606 if (is_array($fields) && !empty($fields)) {
607
608 foreach ($fields as $key => $field) {
609 $map[$key] = $field['slug'];
610 }
611 }
612 return $map;
613
614 }
615
616 }
617