| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Services\Migrator\Classes; |
| 4 |
|
| 5 |
|
| 6 |
use FluentForm\App\Helpers\Helper; |
| 7 |
use FluentForm\App\Modules\Form\Form; |
| 8 |
use FluentForm\Framework\Helpers\ArrayHelper; |
| 9 |
|
| 10 |
class GravityFormsMigrator extends BaseMigrator |
| 11 |
{ |
| 12 |
/** |
| 13 |
* @var bool |
| 14 |
*/ |
| 15 |
protected $hasStep = false; |
| 16 |
|
| 17 |
public function __construct() |
| 18 |
{ |
| 19 |
$this->key = 'gravityform'; |
| 20 |
$this->title = 'Gravity Forms'; |
| 21 |
$this->shortcode = 'gravity_form'; |
| 22 |
$this->hasStep = false; |
| 23 |
} |
| 24 |
|
| 25 |
public function exist() |
| 26 |
{ |
| 27 |
return class_exists('GFForms'); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* @param $form |
| 32 |
* @return array |
| 33 |
*/ |
| 34 |
public function getFields($form) |
| 35 |
{ |
| 36 |
$fluentFields = []; |
| 37 |
$fields = $form['fields']; |
| 38 |
|
| 39 |
foreach ($fields as $name => $field) { |
| 40 |
$field = (array)$field; |
| 41 |
list($type, $args) = $this->formatFieldData($field); |
| 42 |
if ($value = $this->getFluentClassicField($type, $args)) { |
| 43 |
$fluentFields[$field['id']] = $value; |
| 44 |
} else { |
| 45 |
$this->unSupportFields[] = ArrayHelper::get($field, 'label'); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
$submitBtn = $this->getSubmitBttn([ |
| 50 |
'uniqElKey' => time(), |
| 51 |
'class' => '', |
| 52 |
'label' => ArrayHelper::get($form, 'button.text', 'Submit'), |
| 53 |
'type' => ArrayHelper::get($form, 'button.type') == 'text' ? 'default' : 'image', |
| 54 |
'img_url' => ArrayHelper::get($form, 'button.imageUrl'), |
| 55 |
]); |
| 56 |
|
| 57 |
$returnData = [ |
| 58 |
'fields' => $this->getContainer($fields, $fluentFields), |
| 59 |
'submitButton' => $submitBtn |
| 60 |
]; |
| 61 |
|
| 62 |
if ($this->hasStep && defined('FLUENTFORMPRO')) { |
| 63 |
$returnData['stepsWrapper'] = $this->getStepWrapper(); |
| 64 |
} |
| 65 |
|
| 66 |
return $returnData; |
| 67 |
} |
| 68 |
|
| 69 |
private function formatFieldData(array $field) |
| 70 |
{ |
| 71 |
$args = [ |
| 72 |
'uniqElKey' => $field['id'], |
| 73 |
'index' => $field['id'], |
| 74 |
'required' => $field['isRequired'], |
| 75 |
'label' => $field['label'], |
| 76 |
'label_placement' => $this->getLabelPlacement($field), |
| 77 |
'admin_field_label' => ArrayHelper::get($field, 'adminLabel'), |
| 78 |
'name' => $this->getInputName($field), |
| 79 |
'placeholder' => ArrayHelper::get($field, 'placeholder'), |
| 80 |
'class' => $field['cssClass'], |
| 81 |
'value' => ArrayHelper::get($field, 'defaultValue'), |
| 82 |
'help_message' => ArrayHelper::get($field, 'description'), |
| 83 |
]; |
| 84 |
|
| 85 |
$type = ArrayHelper::get($this->fieldTypes(), $field['type'], ''); |
| 86 |
|
| 87 |
switch ($type) { |
| 88 |
|
| 89 |
case 'input_name': |
| 90 |
$args['input_name_args'] = $field['inputs']; |
| 91 |
$args['input_name_args']['first_name']['name'] = $this->getInputName($field['inputs'][1]); |
| 92 |
$args['input_name_args']['middle_name']['name'] = $this->getInputName($field['inputs'][2]); |
| 93 |
$args['input_name_args']['last_name']['name'] = $this->getInputName($field['inputs'][3]); |
| 94 |
$args['input_name_args']['first_name']['label'] = ArrayHelper::get($field['inputs'][1], 'label'); |
| 95 |
$args['input_name_args']['middle_name']['label'] = ArrayHelper::get($field['inputs'][2], 'label'); |
| 96 |
$args['input_name_args']['last_name']['label'] = ArrayHelper::get($field['inputs'][3], 'label'); |
| 97 |
$args['input_name_args']['first_name']['visible'] = ArrayHelper::get($field, 'inputs.1.isHidden', true); |
| 98 |
$args['input_name_args']['middle_name']['visible'] = ArrayHelper::get($field, 'inputs.2.isHidden', |
| 99 |
true); |
| 100 |
$args['input_name_args']['last_name']['visible'] = ArrayHelper::get($field, 'inputs.3.isHidden', true); |
| 101 |
break; |
| 102 |
case 'input_textarea': |
| 103 |
$args['maxlength'] = $field['maxLength']; |
| 104 |
break; |
| 105 |
case 'input_text': |
| 106 |
$args['maxlength'] = $field['maxLength']; |
| 107 |
$args['is_unique'] = ArrayHelper::isTrue($field, 'noDuplicates'); |
| 108 |
if (ArrayHelper::isTrue($field, 'inputMask')) { |
| 109 |
$type = 'input_mask'; |
| 110 |
$args['temp_mask'] = 'custom'; |
| 111 |
$args['mask'] = $field['inputMaskValue']; |
| 112 |
} |
| 113 |
if (ArrayHelper::isTrue($field, 'enablePasswordInput')) { |
| 114 |
$type = 'input_password'; |
| 115 |
} |
| 116 |
break; |
| 117 |
case 'address': |
| 118 |
$args['address_args'] = $this->getAddressArgs($field); |
| 119 |
break; |
| 120 |
case 'select': |
| 121 |
case 'input_radio': |
| 122 |
$optionData = $this->getOptions(ArrayHelper::get($field, 'choices')); |
| 123 |
$args['options'] = ArrayHelper::get($optionData, 'options'); |
| 124 |
$args['value'] = ArrayHelper::get($optionData, 'selectedOption.0'); |
| 125 |
case 'multi_select': |
| 126 |
case 'input_checkbox': |
| 127 |
$optionData = $this->getOptions(ArrayHelper::get($field, 'choices')); |
| 128 |
$args['options'] = ArrayHelper::get($optionData, 'options'); |
| 129 |
$args['value'] = ArrayHelper::get($optionData, 'selectedOption'); |
| 130 |
|
| 131 |
break; |
| 132 |
case 'input_date': |
| 133 |
if ($field['type'] == 'time') { |
| 134 |
$args['format'] = 'H:i'; |
| 135 |
$args['is_time_enabled'] = true; |
| 136 |
} |
| 137 |
break; |
| 138 |
case 'input_number': |
| 139 |
$args['min'] = $field['rangeMin']; |
| 140 |
$args['max'] = $field['rangeMax']; |
| 141 |
break; |
| 142 |
case 'repeater_field': |
| 143 |
$repeaterFields = ArrayHelper::get($field, 'choices', []); |
| 144 |
$args['fields'] = $this->getRepeaterFields($repeaterFields, $field['label']);; |
| 145 |
case 'input_file': |
| 146 |
$args['allowed_file_types'] = $this->getFileTypes($field, 'allowedExtensions'); |
| 147 |
$args['max_size_unit'] = 'MB'; |
| 148 |
$args['max_file_size'] = $this->getFileSize($field);; |
| 149 |
$args['max_file_count'] = ArrayHelper::isTrue($field, |
| 150 |
'multipleFiles') ? $field['maxFiles'] : 1; |
| 151 |
$args['upload_btn_text'] = 'File Upload'; |
| 152 |
break; |
| 153 |
case 'custom_html': |
| 154 |
$args['html_codes'] = $field['content']; |
| 155 |
break; |
| 156 |
case 'section_break': |
| 157 |
$args['section_break_desc'] = $field['description']; |
| 158 |
break; |
| 159 |
case 'terms_and_condition': |
| 160 |
$args['tnc_html'] = $field['description']; |
| 161 |
break; |
| 162 |
case 'form_step': |
| 163 |
$this->hasStep = true; |
| 164 |
$args['next_btn'] = $field['nextButton']; |
| 165 |
$args['next_btn']['type'] = $field['nextButton']['type'] == 'text' ? 'default' : 'img'; |
| 166 |
$args['next_btn']['img_url'] = $field['nextButton']['imageUrl']; |
| 167 |
$args['prev_btn'] = $field['previousButton']; |
| 168 |
$args['prev_btn']['type'] = $field['previousButton']['type'] == 'text' ? 'default' : 'img'; |
| 169 |
$args['prev_btn']['img_url'] = $field['previousButton']['imageUrl']; |
| 170 |
|
| 171 |
break; |
| 172 |
} |
| 173 |
return array($type, $args); |
| 174 |
|
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
private function getInputName($field) |
| 179 |
{ |
| 180 |
return str_replace('-', '_', sanitize_title($field['label'] . '-' . $field['id'])); |
| 181 |
} |
| 182 |
|
| 183 |
private function getLabelPlacement($field) |
| 184 |
{ |
| 185 |
if ($field['labelPlacement'] == 'hidden_label') { |
| 186 |
return 'hide_label'; |
| 187 |
} |
| 188 |
return 'top'; |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* @param $field |
| 193 |
* @return filesize in MB |
| 194 |
*/ |
| 195 |
private function getFileSize($field) |
| 196 |
{ |
| 197 |
$fileSizeByte = ArrayHelper::get($field, 'maxFileSize', 10); |
| 198 |
|
| 199 |
if (empty($fileSizeByte)) { |
| 200 |
$fileSizeByte = 1; |
| 201 |
} |
| 202 |
|
| 203 |
$fileSizeMB = ceil($fileSizeByte * 1048576); // 1MB = 1048576 Bytes |
| 204 |
|
| 205 |
return $fileSizeMB; |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* @return array |
| 210 |
*/ |
| 211 |
public function fieldTypes() |
| 212 |
{ |
| 213 |
$fieldTypes = [ |
| 214 |
'email' => 'email', |
| 215 |
'text' => 'input_text', |
| 216 |
'name' => 'input_name', |
| 217 |
'hidden' => 'input_hidden', |
| 218 |
'textarea' => 'input_textarea', |
| 219 |
'website' => 'input_url', |
| 220 |
'phone' => 'phone', |
| 221 |
'select' => 'select', |
| 222 |
'list' => 'repeater_field', |
| 223 |
'multiselect' => 'multi_select', |
| 224 |
'checkbox' => 'input_checkbox', |
| 225 |
'radio' => 'input_radio', |
| 226 |
'date' => 'input_date', |
| 227 |
'time' => 'input_date', |
| 228 |
'number' => 'input_number', |
| 229 |
'fileupload' => 'input_file', |
| 230 |
'consent' => 'terms_and_condition', |
| 231 |
'captcha' => 'reCaptcha', |
| 232 |
'html' => 'custom_html', |
| 233 |
'section' => 'section_break', |
| 234 |
'page' => 'form_step', |
| 235 |
'address' => 'address', |
| 236 |
]; |
| 237 |
//todo pro fields remove |
| 238 |
return $fieldTypes; |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* @param array $field |
| 243 |
* @return array[] |
| 244 |
*/ |
| 245 |
private function getAddressArgs(array $field) |
| 246 |
{ |
| 247 |
$required = ArrayHelper::isTrue($field, 'isRequired'); |
| 248 |
return [ |
| 249 |
'address_line_1' => [ |
| 250 |
'name' => $this->getInputName($field['inputs'][0]), |
| 251 |
'label' => $field['inputs'][0]['label'], |
| 252 |
'visible' => ArrayHelper::get($field, 'inputs.0.isHidden', true), |
| 253 |
'required' => $required, |
| 254 |
], |
| 255 |
'address_line_2' => [ |
| 256 |
'name' => $this->getInputName($field['inputs'][1]), |
| 257 |
'label' => $field['inputs'][1]['label'], |
| 258 |
'visible' => ArrayHelper::get($field, 'inputs.1.isHidden', true), |
| 259 |
'required' => $required, |
| 260 |
], |
| 261 |
'city' => [ |
| 262 |
'name' => $this->getInputName($field['inputs'][2]), |
| 263 |
'label' => $field['inputs'][2]['label'], |
| 264 |
'visible' => ArrayHelper::get($field, 'inputs.2.isHidden', true), |
| 265 |
'required' => $required, |
| 266 |
], |
| 267 |
'state' => [ |
| 268 |
'name' => $this->getInputName($field['inputs'][3]), |
| 269 |
'label' => $field['inputs'][3]['label'], |
| 270 |
'visible' => ArrayHelper::get($field, 'inputs.3.isHidden', true), |
| 271 |
'required' => $required, |
| 272 |
], |
| 273 |
'zip' => [ |
| 274 |
'name' => $this->getInputName($field['inputs'][4]), |
| 275 |
'label' => $field['inputs'][4]['label'], |
| 276 |
'visible' => ArrayHelper::get($field, 'inputs.4.isHidden', true), |
| 277 |
'required' => $required, |
| 278 |
], |
| 279 |
'country' => [ |
| 280 |
'name' => $this->getInputName($field['inputs'][5]), |
| 281 |
'label' => $field['inputs'][5]['label'], |
| 282 |
'visible' => ArrayHelper::get($field, 'inputs.5.isHidden', true), |
| 283 |
'required' => $required, |
| 284 |
], |
| 285 |
]; |
| 286 |
} |
| 287 |
|
| 288 |
/** |
| 289 |
* @param $options |
| 290 |
* @return array |
| 291 |
*/ |
| 292 |
public function getOptions($options = []) |
| 293 |
{ |
| 294 |
$formattedOptions = []; |
| 295 |
$selectedOption = []; |
| 296 |
foreach ($options as $key => $option) { |
| 297 |
$arr = [ |
| 298 |
'label' => ArrayHelper::get($option, 'text', 'Item -' . $key), |
| 299 |
'value' => ArrayHelper::get($option, 'value'), |
| 300 |
'id' => ArrayHelper::get($option, $key) |
| 301 |
]; |
| 302 |
if (ArrayHelper::isTrue($option, 'isSelected')) { |
| 303 |
$selectedOption[] = ArrayHelper::get($option, 'value', ''); |
| 304 |
} |
| 305 |
$formattedOptions[] = $arr; |
| 306 |
} |
| 307 |
|
| 308 |
return ['options' => $formattedOptions, 'selectedOption' => $selectedOption]; |
| 309 |
} |
| 310 |
|
| 311 |
/** |
| 312 |
* @param $repeaterFields |
| 313 |
* @param $label |
| 314 |
* @return array |
| 315 |
*/ |
| 316 |
protected function getRepeaterFields($repeaterFields, $label) |
| 317 |
{ |
| 318 |
$arr = []; |
| 319 |
if (empty($repeaterFields)) { |
| 320 |
$arr[] = [ |
| 321 |
'element' => 'input_text', |
| 322 |
'attributes' => array( |
| 323 |
'type' => 'text', |
| 324 |
'value' => '', |
| 325 |
'placeholder' => '', |
| 326 |
), |
| 327 |
'settings' => array( |
| 328 |
'label' => $label, |
| 329 |
'help_message' => '', |
| 330 |
'validation_rules' => array( |
| 331 |
'required' => array( |
| 332 |
'value' => false, |
| 333 |
'message' => __('This field is required', 'fluentform'), |
| 334 |
), |
| 335 |
) |
| 336 |
) |
| 337 |
]; |
| 338 |
} else { |
| 339 |
foreach ($repeaterFields as $serial => $repeaterField) { |
| 340 |
$arr[] = [ |
| 341 |
'element' => 'input_text', |
| 342 |
'attributes' => array( |
| 343 |
'type' => 'text', |
| 344 |
'value' => '', |
| 345 |
'placeholder' => '', |
| 346 |
), |
| 347 |
'settings' => array( |
| 348 |
'label' => ArrayHelper::get($repeaterField, 'label', ''), |
| 349 |
'help_message' => '', |
| 350 |
'validation_rules' => array( |
| 351 |
'required' => array( |
| 352 |
'value' => false, |
| 353 |
'message' => __('This field is required', 'fluentform'), |
| 354 |
), |
| 355 |
) |
| 356 |
) |
| 357 |
]; |
| 358 |
|
| 359 |
} |
| 360 |
} |
| 361 |
return $arr; |
| 362 |
} |
| 363 |
|
| 364 |
private function getContainer($fields, $fluentFields) |
| 365 |
{ |
| 366 |
|
| 367 |
$layoutGroupIds = array_column($fields, 'layoutGroupId'); |
| 368 |
$cols = array_count_values($layoutGroupIds); // if inputs has more then one duplicate layoutGroupIds then it has container |
| 369 |
if (intval($cols) < 2) { |
| 370 |
return $fluentFields; |
| 371 |
} |
| 372 |
|
| 373 |
$final = []; |
| 374 |
//get fields array for inserting into containers |
| 375 |
$containers = self::getLayout($fields); |
| 376 |
|
| 377 |
//set fields array map for inserting into containers |
| 378 |
foreach ($containers as $index => $fields) { |
| 379 |
$final[$index][] = array_map(function ($id) use ($fluentFields) { |
| 380 |
if (isset($fluentFields[$id])) { |
| 381 |
return $fluentFields[$id]; |
| 382 |
} |
| 383 |
}, $fields); |
| 384 |
} |
| 385 |
$final = self::arrayFlat($final); |
| 386 |
$withContainer = []; |
| 387 |
foreach ($final as $row => $columns) { |
| 388 |
$colsCount = count($columns); |
| 389 |
$containerConfig = []; |
| 390 |
//with container |
| 391 |
if ($colsCount != 1) { |
| 392 |
|
| 393 |
$fields = []; |
| 394 |
foreach ($columns as $col) { |
| 395 |
$fields[]['fields'] = [$col]; |
| 396 |
} |
| 397 |
|
| 398 |
$containerConfig[] = [ |
| 399 |
'index' => $row, |
| 400 |
'element' => 'container', |
| 401 |
"attributes" => [], |
| 402 |
'settings' => [ |
| 403 |
'container_class', |
| 404 |
'conditional_logics' |
| 405 |
], |
| 406 |
'editor_options' => [ |
| 407 |
'title' => $colsCount . ' Column Container', |
| 408 |
'icon_class' => 'ff-edit-column-' . $colsCount |
| 409 |
], |
| 410 |
'columns' => $fields, |
| 411 |
'uniqElKey' => 'col' . '_' . md5(uniqid(wp_rand(), true)) |
| 412 |
]; |
| 413 |
} else { |
| 414 |
//without container |
| 415 |
$containerConfig = $columns; |
| 416 |
|
| 417 |
} |
| 418 |
$withContainer[] = $containerConfig; |
| 419 |
} |
| 420 |
return (array_filter(self::arrayFlat($withContainer))); |
| 421 |
} |
| 422 |
|
| 423 |
protected static function getLayout($fields, $id = '') |
| 424 |
{ |
| 425 |
$layoutGroupIds = array_column($fields, 'layoutGroupId'); |
| 426 |
$rows = array_count_values($layoutGroupIds); |
| 427 |
$layout = []; |
| 428 |
foreach ($rows as $key => $value) { |
| 429 |
$layout[] = self::getInputIdsFromLayoutGrp($key, $fields); |
| 430 |
} |
| 431 |
return $layout; |
| 432 |
} |
| 433 |
|
| 434 |
public static function getInputIdsFromLayoutGrp($id, $array) |
| 435 |
{ |
| 436 |
$keys = []; |
| 437 |
foreach ($array as $key => $val) { |
| 438 |
if ($val['layoutGroupId'] === $id) { |
| 439 |
$keys[] = $val['id']; |
| 440 |
} |
| 441 |
} |
| 442 |
return $keys; |
| 443 |
} |
| 444 |
|
| 445 |
/** |
| 446 |
* @param null $array |
| 447 |
* @param int $depth |
| 448 |
* @return array |
| 449 |
*/ |
| 450 |
public static function arrayFlat($array = null, $depth = 1) |
| 451 |
{ |
| 452 |
$result = []; |
| 453 |
if (!is_array($array)) { |
| 454 |
$array = func_get_args(); |
| 455 |
} |
| 456 |
foreach ($array as $key => $value) { |
| 457 |
if (is_array($value) && $depth) { |
| 458 |
$result = array_merge($result, self::arrayFlat($value, $depth - 1)); |
| 459 |
} else { |
| 460 |
$result = array_merge($result, [$key => $value]); |
| 461 |
} |
| 462 |
} |
| 463 |
return $result; |
| 464 |
} |
| 465 |
|
| 466 |
/** |
| 467 |
* @return array |
| 468 |
*/ |
| 469 |
private function getStepWrapper() |
| 470 |
{ |
| 471 |
return [ |
| 472 |
'stepStart' => [ |
| 473 |
'element' => 'step_start', |
| 474 |
'attributes' => [ |
| 475 |
'id' => '', |
| 476 |
'class' => '', |
| 477 |
], |
| 478 |
'settings' => [ |
| 479 |
'progress_indicator' => 'progress-bar', |
| 480 |
'step_titles' => [], |
| 481 |
'disable_auto_focus' => 'no', |
| 482 |
'enable_auto_slider' => 'no', |
| 483 |
'enable_step_data_persistency' => 'no', |
| 484 |
'enable_step_page_resume' => 'no', |
| 485 |
], |
| 486 |
'editor_options' => [ |
| 487 |
'title' => 'Start Paging' |
| 488 |
], |
| 489 |
], |
| 490 |
'stepEnd' => [ |
| 491 |
'element' => 'step_end', |
| 492 |
'attributes' => [ |
| 493 |
'id' => '', |
| 494 |
'class' => '', |
| 495 |
], |
| 496 |
'settings' => [ |
| 497 |
'prev_btn' => [ |
| 498 |
'type' => 'default', |
| 499 |
'text' => 'Previous', |
| 500 |
'img_url' => '' |
| 501 |
] |
| 502 |
], |
| 503 |
'editor_options' => [ |
| 504 |
'title' => 'End Paging' |
| 505 |
], |
| 506 |
] |
| 507 |
|
| 508 |
]; |
| 509 |
} |
| 510 |
|
| 511 |
/** |
| 512 |
* @param $form |
| 513 |
* @return array default parsed form metas |
| 514 |
* @throws \Exception |
| 515 |
*/ |
| 516 |
public function getFormMetas($form) |
| 517 |
{ |
| 518 |
$formObject = new Form(wpFluentForm()); |
| 519 |
$defaults = $formObject->getFormsDefaultSettings(); |
| 520 |
$confirmationsFormatted = $this->getConfirmations($form, $defaults['confirmation']); |
| 521 |
$defaultConfirmation = array_shift($confirmationsFormatted); |
| 522 |
$notifications = $this->getNotifications($form); |
| 523 |
$defaults['restrictions']['requireLogin']['enabled'] = ArrayHelper::isTrue($form, 'requireLogin'); |
| 524 |
$defaults['restrictions']['requireLogin']['requireLoginMsg'] = ArrayHelper::isTrue($form, |
| 525 |
'requireLoginMessage'); |
| 526 |
$defaults['restrictions']['limitNumberOfEntries']['enabled'] = ArrayHelper::isTrue($form, 'limitEntries'); |
| 527 |
$defaults['restrictions']['limitNumberOfEntries']['numberOfEntries'] = ArrayHelper::isTrue($form, |
| 528 |
'limitEntriesCount'); |
| 529 |
$defaults['restrictions']['limitNumberOfEntries']['period'] = ArrayHelper::isTrue($form, 'limitEntriesPeriod'); |
| 530 |
$defaults['restrictions']['limitNumberOfEntries']['limitReachedMsg'] = ArrayHelper::isTrue($form, |
| 531 |
'limitEntriesMessage'); |
| 532 |
$defaults['restrictions']['scheduleForm']['enabled'] = ArrayHelper::isTrue($form, 'scheduleForm'); |
| 533 |
$defaults['restrictions']['scheduleForm']['start'] = ArrayHelper::isTrue($form, 'scheduleStart'); |
| 534 |
$defaults['restrictions']['scheduleForm']['end'] = ArrayHelper::isTrue($form, 'scheduleEnd'); |
| 535 |
$defaults['restrictions']['scheduleForm']['pendingMsg'] = ArrayHelper::isTrue($form, 'schedulePendingMessage'); |
| 536 |
$defaults['restrictions']['scheduleForm']['expiredMsg'] = ArrayHelper::isTrue($form, 'scheduleMessage'); |
| 537 |
$advancedValidation = [ |
| 538 |
'status' => false, |
| 539 |
'type' => 'all', |
| 540 |
'conditions' => [ |
| 541 |
[ |
| 542 |
'field' => '', |
| 543 |
'operator' => '=', |
| 544 |
'value' => '' |
| 545 |
] |
| 546 |
], |
| 547 |
'error_message' => '', |
| 548 |
'validation_type' => 'fail_on_condition_met' |
| 549 |
]; |
| 550 |
return [ |
| 551 |
'formSettings' => [ |
| 552 |
'confirmation' => $defaultConfirmation, |
| 553 |
'restrictions' => $defaults['restrictions'], |
| 554 |
'layout' => $defaults['layout'], |
| 555 |
], |
| 556 |
'advancedValidationSettings' => $advancedValidation, |
| 557 |
'delete_entry_on_submission' => 'no', |
| 558 |
'confirmations' => $confirmationsFormatted, |
| 559 |
'notifications' => $notifications |
| 560 |
]; |
| 561 |
} |
| 562 |
private function getNotifications($form) |
| 563 |
{ |
| 564 |
$notificationsFormatted = []; |
| 565 |
foreach (ArrayHelper::get($form, 'notifications', []) as $notification) { |
| 566 |
$fieldType = ArrayHelper::get($notification, 'toType', 'email'); |
| 567 |
$sendTo = [ |
| 568 |
'type' => $fieldType, |
| 569 |
'email' => '', |
| 570 |
'field' => '', |
| 571 |
'routing' => [], |
| 572 |
]; |
| 573 |
if ('field' == $fieldType) { |
| 574 |
$sendTo['field'] = $this->getFormFieldName(ArrayHelper::get($notification, 'to', ''), $form); |
| 575 |
} elseif('routing' == $fieldType) { |
| 576 |
foreach (ArrayHelper::get($notification, 'routing', []) as $route) { |
| 577 |
$fieldName = $this->getFormFieldName(ArrayHelper::get($route, 'fieldId'), $form); |
| 578 |
$routeEmail = ArrayHelper::get($route, 'email'); |
| 579 |
if (!$fieldName || !$routeEmail) { |
| 580 |
continue; |
| 581 |
} |
| 582 |
if ($operator = $this->getResolveOperator(ArrayHelper::get($route, 'operator', ''))) { |
| 583 |
$sendTo['routing'][] = [ |
| 584 |
'field' => $fieldName, |
| 585 |
'operator' => $operator, |
| 586 |
'input_value' => $routeEmail, |
| 587 |
'value' => ArrayHelper::get($route, 'value', '') |
| 588 |
]; |
| 589 |
} |
| 590 |
} |
| 591 |
} else { |
| 592 |
$sendTo['email'] = $this->getResolveShortcodes(ArrayHelper::get($notification, 'to', ''), $form); |
| 593 |
} |
| 594 |
$message = $this->getResolveShortcodes(ArrayHelper::get($notification, 'message', ''), $form); |
| 595 |
$replyTo = $this->getResolveShortcodes(ArrayHelper::get($notification, 'replyTo', ''), $form); |
| 596 |
$notificationsFormatted[] = [ |
| 597 |
'sendTo' => $sendTo, |
| 598 |
'enabled' => ArrayHelper::get($notification, 'isActive', true), |
| 599 |
'name' => ArrayHelper::get($notification, 'name', 'Admin Notification'), |
| 600 |
'subject' => $this->getResolveShortcodes(ArrayHelper::get($notification, 'subject', 'Notification'), $form), |
| 601 |
'to' => $sendTo['email'], |
| 602 |
'replyTo' => $replyTo ?: '{wp.admin_email}', |
| 603 |
'message' => str_replace("\n", "<br />", $message), |
| 604 |
'fromName' => $this->getResolveShortcodes(ArrayHelper::get($notification, 'fromName', ''), $form), |
| 605 |
'fromEmail' => $this->getResolveShortcodes(ArrayHelper::get($notification, 'from', ''), $form), |
| 606 |
'bcc' => $this->getResolveShortcodes(ArrayHelper::get($notification, 'bcc', ''), $form), |
| 607 |
'conditionals' => $this->getConditionals($notification,'notification', $form) |
| 608 |
]; |
| 609 |
} |
| 610 |
return $notificationsFormatted; |
| 611 |
} |
| 612 |
|
| 613 |
private function getConditionals($notification, $key, $form) |
| 614 |
{ |
| 615 |
$conditionals = ArrayHelper::get($notification, 'conditionalLogic', []); |
| 616 |
$conditions = []; |
| 617 |
if (!$conditionals) { |
| 618 |
$conditionals = ArrayHelper::get($notification, $key.'_conditional_logic_object', []); |
| 619 |
} |
| 620 |
$type = 'any'; |
| 621 |
if ($conditionals) { |
| 622 |
$type = ArrayHelper::get($conditionals, 'logicType', 'any'); |
| 623 |
foreach (ArrayHelper::get($conditionals, 'rules', []) as $rule) { |
| 624 |
$fieldName = $this->getFormFieldName(ArrayHelper::get($rule, 'fieldId'), $form); |
| 625 |
if (!$fieldName) { |
| 626 |
continue; |
| 627 |
} |
| 628 |
if ($operator = $this->getResolveOperator(ArrayHelper::get($rule, 'operator', ''))) { |
| 629 |
$conditions[] = [ |
| 630 |
'field' => $fieldName, |
| 631 |
'operator' => $operator, |
| 632 |
'value' => ArrayHelper::get($rule, 'value', '') |
| 633 |
]; |
| 634 |
} |
| 635 |
} |
| 636 |
} |
| 637 |
return [ |
| 638 |
"status" => ArrayHelper::isTrue($notification, $key . '_conditional_logic'), |
| 639 |
"type" => $type, |
| 640 |
'conditions' => $conditions |
| 641 |
]; |
| 642 |
} |
| 643 |
|
| 644 |
private function getConfirmations($form, $defaultValues) |
| 645 |
{ |
| 646 |
$confirmationsFormatted = []; |
| 647 |
foreach (ArrayHelper::get($form, 'confirmations', []) as $confirmation) { |
| 648 |
$type = ArrayHelper::get($confirmation, 'type'); |
| 649 |
$queryString = ""; |
| 650 |
if ($type == 'redirect') { |
| 651 |
$redirectTo = 'customUrl'; |
| 652 |
$queryString = $this->getResolveShortcodes(ArrayHelper::get($confirmation, 'queryString', ''), $form); |
| 653 |
} elseif ($type == 'page') { |
| 654 |
$queryString = $this->getResolveShortcodes(ArrayHelper::get($confirmation, 'queryString', ''), $form); |
| 655 |
$redirectTo = 'customPage'; |
| 656 |
} else { |
| 657 |
$redirectTo = 'samePage'; |
| 658 |
} |
| 659 |
|
| 660 |
$format = [ |
| 661 |
'name' => ArrayHelper::get($confirmation, 'name', 'Confirmation'), |
| 662 |
'messageToShow' => str_replace("\n", "<br />", $this->getResolveShortcodes(ArrayHelper::get($confirmation, 'message', ''), $form)), |
| 663 |
'samePageFormBehavior' => 'hide_form', |
| 664 |
'redirectTo' => $redirectTo, |
| 665 |
'customPage' => intval(ArrayHelper::get($confirmation, 'page')), |
| 666 |
'customUrl' => ArrayHelper::get($confirmation, 'url'), |
| 667 |
'active' => ArrayHelper::get($confirmation, 'isActive', true), |
| 668 |
'enable_query_string' => $queryString ? 'yes' : 'no', |
| 669 |
'query_strings' => $queryString, |
| 670 |
]; |
| 671 |
$isDefault = ArrayHelper::isTrue($confirmation, 'isDefault'); |
| 672 |
if (!$isDefault) { |
| 673 |
$format['conditionals'] = $this->getConditionals($confirmation,'confirmation', $form); |
| 674 |
} |
| 675 |
$confirmationsFormatted[] = wp_parse_args($format, $defaultValues); |
| 676 |
} |
| 677 |
return $confirmationsFormatted; |
| 678 |
} |
| 679 |
|
| 680 |
/** |
| 681 |
* Get form field name in fluentforms format |
| 682 |
* |
| 683 |
* @param string $str |
| 684 |
* @param array $form |
| 685 |
* @return string |
| 686 |
*/ |
| 687 |
private function getFormFieldName($str, $form) |
| 688 |
{ |
| 689 |
preg_match('/[0-9]+[.]?[0-9]*/', $str, $fieldId); |
| 690 |
$fieldId = ArrayHelper::get($fieldId, 0, '0'); |
| 691 |
if (!$fieldId) { |
| 692 |
return ''; |
| 693 |
} |
| 694 |
$fieldIds = []; |
| 695 |
if (strpos($fieldId, '.') !== false) { |
| 696 |
$fieldIds = explode('.', $fieldId); |
| 697 |
$fieldId = $fieldId[0]; |
| 698 |
} |
| 699 |
$field = []; |
| 700 |
foreach (ArrayHelper::get($form, 'fields', []) as $formField) { |
| 701 |
if (isset($formField->id) && $formField->id == $fieldId) { |
| 702 |
$field = (array)$formField; |
| 703 |
break; |
| 704 |
} |
| 705 |
} |
| 706 |
list($type, $args) = $this->formatFieldData($field); |
| 707 |
$name = ArrayHelper::get($args, 'name', ''); |
| 708 |
if ($fieldIds && ArrayHelper::get($fieldIds, 1)) { |
| 709 |
foreach (ArrayHelper::get($field, "inputs", []) as $input) { |
| 710 |
if (ArrayHelper::get($input, 'id') != join('.', $fieldIds)) { |
| 711 |
continue; |
| 712 |
} |
| 713 |
if ($subName = $this->getInputName($input)) { |
| 714 |
$name .= ".$subName"; |
| 715 |
} |
| 716 |
break; |
| 717 |
} |
| 718 |
} |
| 719 |
return $name; |
| 720 |
} |
| 721 |
|
| 722 |
/** |
| 723 |
* Resolve shortcode in fluentforms format |
| 724 |
* |
| 725 |
* @param string $message |
| 726 |
* @param array $form |
| 727 |
* @return string |
| 728 |
*/ |
| 729 |
private function getResolveShortcodes($message, $form) |
| 730 |
{ |
| 731 |
if (!$message) { |
| 732 |
return $message; |
| 733 |
} |
| 734 |
preg_match_all('/{(.*?)}/', $message, $matches); |
| 735 |
if (!$matches[0]) { |
| 736 |
return $message; |
| 737 |
} |
| 738 |
$shortcodes = $this->dynamicShortcodes(); |
| 739 |
foreach ($matches[0] as $match) { |
| 740 |
$replace = ''; |
| 741 |
if (isset($shortcodes[$match])) { |
| 742 |
$replace = $shortcodes[$match]; |
| 743 |
} elseif ($this->isFormField($match) && $name = $this->getFormFieldName($match, $form)) { |
| 744 |
$replace = "{inputs.$name}"; |
| 745 |
} |
| 746 |
$message = str_replace($match, $replace, $message); |
| 747 |
} |
| 748 |
return $message; |
| 749 |
} |
| 750 |
|
| 751 |
/** |
| 752 |
* Get bool value depend on shortcode is form inputs or not |
| 753 |
* |
| 754 |
* @param string $shortcode |
| 755 |
* @return boolean |
| 756 |
*/ |
| 757 |
private function isFormField($shortcode) |
| 758 |
{ |
| 759 |
preg_match('/:[0-9]+[.]?[0-9]*/', $shortcode, $fieldId); |
| 760 |
return ArrayHelper::isTrue($fieldId, '0'); |
| 761 |
} |
| 762 |
|
| 763 |
/** |
| 764 |
* Get shortcode in fluentforms format |
| 765 |
* @return array |
| 766 |
*/ |
| 767 |
private function dynamicShortcodes() |
| 768 |
{ |
| 769 |
return [ |
| 770 |
'{all_fields}' => '{all_data}', |
| 771 |
'{admin_email}' => '{wp.admin_email}', |
| 772 |
'{ip}' => '{ip}', |
| 773 |
'{date_mdy}' => '{date.m/d/Y}', |
| 774 |
'{date_dmy}' => '{date.d/m/Y}', |
| 775 |
'{embed_post:ID}' => '{embed_post.ID}', |
| 776 |
'{embed_post:post_title}' => '{embed_post.post_title}', |
| 777 |
'{embed_url}' => '{embed_post.permalink}', |
| 778 |
'{user:id}' => '{user.ID}', |
| 779 |
'{user:first_name}' => '{user.first_name}', |
| 780 |
'{user:last_name}' => '{user.last_name}', |
| 781 |
'{user:user_login}' => '{user.user_login}', |
| 782 |
'{user:display_name}' => '{user.display_name}', |
| 783 |
'{user_full_name}' => '{user.first_name} {user.last_name}', |
| 784 |
'{user:user_email}' => '{user.user_email}', |
| 785 |
'{entry_id}' => '{submission.id}', |
| 786 |
'{entry_url}' => '{submission.admin_view_url}', |
| 787 |
'{referer}' => '{http_referer}', |
| 788 |
'{user_agent}' => '{browser.name}' |
| 789 |
]; |
| 790 |
} |
| 791 |
|
| 792 |
public function getFormsFormatted() |
| 793 |
{ |
| 794 |
$forms = []; |
| 795 |
$items = $this->getForms(); |
| 796 |
foreach ($items as $item) { |
| 797 |
$forms[] = [ |
| 798 |
'name' => $this->getFormName($item), |
| 799 |
'id' => $this->getFormId($item), |
| 800 |
'imported_ff_id' => $this->isAlreadyImported($item), |
| 801 |
]; |
| 802 |
} |
| 803 |
return $forms; |
| 804 |
} |
| 805 |
|
| 806 |
protected function getForms() |
| 807 |
{ |
| 808 |
$forms = \GFAPI::get_forms(); |
| 809 |
return $forms; |
| 810 |
} |
| 811 |
|
| 812 |
protected function getForm($id) |
| 813 |
{ |
| 814 |
if ($form = \GFAPI::get_form($id)) { |
| 815 |
return $form; |
| 816 |
} |
| 817 |
return false; |
| 818 |
} |
| 819 |
|
| 820 |
protected function getFormName($form) |
| 821 |
{ |
| 822 |
return $form['title']; |
| 823 |
} |
| 824 |
|
| 825 |
public function getEntries($formId) |
| 826 |
{ |
| 827 |
$form = $this->getForm($formId); |
| 828 |
if (empty($form)) { |
| 829 |
return false; |
| 830 |
} |
| 831 |
|
| 832 |
/** |
| 833 |
* Note - more-then 5000/6000 (based on sever) entries process make timout response / set default limit 1000 |
| 834 |
* @todo need silently async processing for support all entries migrate at a time, and improve frontend entry-migrate with more settings options |
| 835 |
*/ |
| 836 |
$totalEntries = \GFAPI::count_entries($formId); |
| 837 |
$perPage = apply_filters('fluentform/entry_migration_max_limit', static::DEFAULT_ENTRY_MIGRATION_MAX_LIMIT, $this->key , $totalEntries, $formId); |
| 838 |
$offset = 0; |
| 839 |
$paging = [ |
| 840 |
'offset' => $offset, |
| 841 |
'page_size' => $perPage |
| 842 |
]; |
| 843 |
$shorting = [ |
| 844 |
'key' => 'id', |
| 845 |
'direction' => 'ASC', |
| 846 |
]; |
| 847 |
$submissions = \GFAPI::get_entries($formId, [], $shorting, $paging); |
| 848 |
$entries = []; |
| 849 |
if (!is_array($submissions)) { |
| 850 |
return $entries; |
| 851 |
} |
| 852 |
|
| 853 |
$fieldsMap = $this->getFields($form); |
| 854 |
foreach ($submissions as $submission) { |
| 855 |
$entry = []; |
| 856 |
foreach ($fieldsMap['fields'] as $id => $field) { |
| 857 |
$name = ArrayHelper::get($field, 'attributes.name'); |
| 858 |
if (!$name) { |
| 859 |
continue; |
| 860 |
} |
| 861 |
|
| 862 |
$type = ArrayHelper::get($field, 'element'); |
| 863 |
$fieldModel = \GFFormsModel::get_field($form, $id); |
| 864 |
|
| 865 |
// format entry value by field name |
| 866 |
$finalValue = null; |
| 867 |
if ("input_file" == $type && $value = $this->getSubmissionValue($id, $submission)) { |
| 868 |
$finalValue = $this->migrateFilesAndGetUrls($value); |
| 869 |
} elseif ("repeater_field" == $type && $value = $this->getSubmissionValue($id, $submission)) { |
| 870 |
if ($repeatData = (array)Helper::safeUnserialize($value)) { |
| 871 |
$finalValue = []; |
| 872 |
foreach ($repeatData as $data) { |
| 873 |
$finalValue[] = array_values($data); |
| 874 |
} |
| 875 |
} |
| 876 |
} elseif ( |
| 877 |
"select" == $type && |
| 878 |
ArrayHelper::isTrue($field, 'attributes.multiple') && |
| 879 |
$value = $this->getSubmissionValue($id, $submission) |
| 880 |
) { |
| 881 |
$finalValue = \json_decode($value); |
| 882 |
} elseif ( |
| 883 |
in_array($type, ["input_checkbox", "address", "input_name", "terms_and_condition"]) && |
| 884 |
isset($fieldModel['inputs']) |
| 885 |
) { |
| 886 |
$finalValue = $this->getSubmissionArrayValue($type, $field, $fieldModel['inputs'], $submission); |
| 887 |
if ("input_checkbox" == $type) { |
| 888 |
$finalValue = array_values($finalValue); |
| 889 |
} elseif ("terms_and_condition" == $type) { |
| 890 |
$finalValue = $finalValue ? 'on' : 'off'; |
| 891 |
} |
| 892 |
} |
| 893 |
|
| 894 |
if (!$finalValue) { |
| 895 |
$finalValue = is_object($fieldModel) ? $fieldModel->get_value_export($submission, $id) : ''; |
| 896 |
} |
| 897 |
$entry[$name] = $finalValue; |
| 898 |
} |
| 899 |
if ($created_at = ArrayHelper::get($submission, 'date_created')) { |
| 900 |
$entry['created_at'] = $created_at; |
| 901 |
} |
| 902 |
if ($updated_at = ArrayHelper::get($submission, 'date_updated')) { |
| 903 |
$entry['updated_at'] = $updated_at; |
| 904 |
} |
| 905 |
if ($is_favourite = ArrayHelper::get($submission, 'is_starred')) { |
| 906 |
$entry['is_favourite'] = $is_favourite; |
| 907 |
} |
| 908 |
if ($status = ArrayHelper::get($submission, 'status')) { |
| 909 |
if ('trash' == $status || 'spam' == $status) { |
| 910 |
$entry['status'] = 'trashed'; |
| 911 |
} elseif ('active' == $status && ArrayHelper::isTrue($submission, 'is_read')) { |
| 912 |
$entry['status'] = 'read'; |
| 913 |
} |
| 914 |
} |
| 915 |
$entries[] = $entry; |
| 916 |
} |
| 917 |
return $entries; |
| 918 |
} |
| 919 |
|
| 920 |
/** |
| 921 |
* @param $form |
| 922 |
* @return mixed |
| 923 |
*/ |
| 924 |
protected function getFormId($form) |
| 925 |
{ |
| 926 |
if (isset($form['id'])) { |
| 927 |
return $form['id']; |
| 928 |
} |
| 929 |
return false; |
| 930 |
} |
| 931 |
|
| 932 |
protected function getSubmissionArrayValue($type, $field, $inputs, $submission) |
| 933 |
{ |
| 934 |
$arrayValue = []; |
| 935 |
foreach ($inputs as $input) { |
| 936 |
if (!isset($submission[$input['id']])) { |
| 937 |
continue; |
| 938 |
} |
| 939 |
if ("input_name" == $type && $subFields = ArrayHelper::get($field, 'fields')) { |
| 940 |
foreach ($subFields as $subField) { |
| 941 |
if ( |
| 942 |
$input['label'] == ArrayHelper::get($subField, 'settings.label') && |
| 943 |
$subName = ArrayHelper::get($subField, 'attributes.name', '') |
| 944 |
) { |
| 945 |
$arrayValue[$subName] = $submission[$input['id']]; |
| 946 |
} |
| 947 |
} |
| 948 |
} else { |
| 949 |
$arrayValue[] = $submission[$input['id']]; |
| 950 |
} |
| 951 |
} |
| 952 |
if ('address' == $type) { |
| 953 |
$arrayValue = array_combine([ |
| 954 |
"address_line_1", |
| 955 |
"address_line_2", |
| 956 |
"city", |
| 957 |
"state", |
| 958 |
"zip", |
| 959 |
"country" |
| 960 |
], $arrayValue); |
| 961 |
} |
| 962 |
return array_filter($arrayValue); |
| 963 |
} |
| 964 |
|
| 965 |
protected function getSubmissionValue($id, $submission) |
| 966 |
{ |
| 967 |
return isset($submission[$id]) ? $submission[$id] : ""; |
| 968 |
} |
| 969 |
} |
| 970 |
|