1: <?php
2:
3: 4: 5:
6: class Quform_Migrator
7: {
8: 9: 10:
11: protected $repository;
12:
13: 14: 15:
16: protected $builder;
17:
18: 19: 20:
21: protected $scriptLoader;
22:
23: 24: 25:
26: protected $options;
27:
28: 29: 30:
31: protected $formFactory;
32:
33: 34: 35: 36: 37: 38: 39:
40: public function __construct(
41: Quform_Repository $repository,
42: Quform_Builder $builder,
43: Quform_ScriptLoader $scriptLoader,
44: Quform_Options $options,
45: Quform_Form_Factory $formFactory
46: ) {
47: $this->repository = $repository;
48: $this->builder = $builder;
49: $this->scriptLoader = $scriptLoader;
50: $this->options = $options;
51: $this->formFactory = $formFactory;
52: }
53:
54: 55: 56:
57: public function getAllFormIds()
58: {
59: if ( ! Quform::isPostRequest()) {
60: wp_send_json(array(
61: 'type' => 'error',
62: 'message' => __('Bad request', 'quform')
63: ));
64: }
65:
66: if ( ! current_user_can('quform_full_access')) {
67: wp_send_json(array(
68: 'type' => 'error',
69: 'message' => __('Insufficient permissions', 'quform')
70: ));
71: }
72:
73: $oldForms = $this->get1xForms();
74: $formIds = array();
75:
76: foreach ($oldForms as $oldForm) {
77: $oldFormId = (int) $oldForm['id'];
78:
79: if ($oldFormId > 0) {
80: $formIds[] = $oldFormId;
81: }
82: }
83:
84: wp_send_json(array(
85: 'type' => 'success',
86: 'formIds' => $formIds
87: ));
88: }
89:
90: 91: 92:
93: public function migrateForm()
94: {
95: $this->validateMigrateFormRequest();
96:
97: @set_time_limit(3600);
98:
99: $formId = (int) $_POST['form_id'];
100:
101: $form = $this->get1xForm($formId);
102:
103: if (is_array($form)) {
104: $result = $this->convertForm($form);
105:
106: if ($result['type'] == 'success') {
107: $config = $this->builder->sanitizeForm($result['config']);
108:
109: $config = $this->repository->add($config);
110:
111: if ( ! is_array($config)) {
112: wp_send_json(array(
113: 'type' => 'error',
114: 'message' => wp_kses(sprintf(
115:
116: __('Failed to insert into database, check the %1$serror log%2$s for more information', 'quform'),
117: '<a href="https://support.themecatcher.net/quform-wordpress-v2/guides/advanced/enabling-debug-logging">',
118: '</a>'
119: ), array('a' => array('href' => array())))
120: ));
121: }
122:
123: $this->scriptLoader->handleSaveForm($config);
124:
125: if ($_POST['migrate_entries'] === '1') {
126: $this->migrateEntries($form, $config);
127: }
128:
129: wp_send_json(array(
130: 'type' => 'success'
131: ));
132: } else {
133: wp_send_json(array(
134: 'type' => 'error',
135: 'message' => $result['message']
136: ));
137: }
138: } else {
139: wp_send_json(array(
140: 'type' => 'error',
141: 'message' => __('Form not found', 'quform')
142: ));
143: }
144: }
145:
146: 147: 148: 149: 150: 151:
152: protected function convertForm(array $form)
153: {
154: $config = Quform_Form::getDefaultConfig();
155:
156: $config['name'] = $form['name'];
157: $config['title'] = $form['title'];
158: $config['active'] = $form['active'];
159: $config['description'] = $form['description'];
160:
161: $confirmation = Quform_Confirmation::getDefaultConfig();
162:
163: $confirmation['id'] = 1;
164: $confirmation['name'] = __('Default confirmation', 'quform');
165:
166: if ($form['success_type'] == 'message') {
167: $confirmation['type'] = 'message';
168: $confirmation['message'] = $this->convertPlaceholders($form['success_message']);
169: $confirmation['messageAutoFormat'] = false;
170: $confirmation['messagePosition'] = $form['success_message_position'];
171: $confirmation['messageTimeout'] = $form['success_message_timeout'];
172: } else if ($form['success_type'] == 'redirect') {
173: if (in_array($form['success_redirect_type'], array('post', 'page'))) {
174: $confirmation['type'] = 'redirect-page';
175: $confirmation['redirectPage'] = $form['success_redirect_value'];
176: } else if ($form['success_redirect_type'] == 'url') {
177: $confirmation['type'] = 'redirect-url';
178: $confirmation['redirectUrl'] = $form['success_redirect_value'];
179: }
180: }
181:
182: $confirmation['resetForm'] = Quform::get($form, 'reset_form_values', '');
183:
184: $config['confirmations'] = array($confirmation);
185: $config['nextConfirmationId'] = 2;
186:
187: $config['submitText'] = $form['submit_button_text'];
188: $config['ajax'] = $form['use_ajax'];
189: $config['honeypot'] = $form['use_honeypot'];
190: $config['logicAnimation'] = $form['conditional_logic_animation'];
191:
192: if (Quform::isNonEmptyString($form['theme'])) {
193: $theme = explode('|', $form['theme']);
194: $config['theme'] = $theme[0];
195: }
196:
197: $config['responsiveElements'] = $form['responsive'] ? 'phone-landscape' : '';
198: $config['responsiveColumns'] = $form['responsive'] ? 'phone-landscape' : '';
199: $config['labelPosition'] = str_replace('above', '', $form['label_placement']);
200: $config['labelWidth'] = $form['label_width'];
201: $config['requiredText'] = $form['required_text'];
202: $config['tooltipsEnabled'] = $form['use_tooltips'];
203: $config['tooltipType'] = $form['tooltip_type'];
204: $config['tooltipEvent'] = $form['tooltip_event'];
205: $config['tooltipCustom'] = $form['tooltip_custom'];
206: $config['tooltipStyle'] = $form['tooltip_style'];
207: $config['tooltipMy'] = $form['tooltip_my'];
208: $config['tooltipAt'] = $form['tooltip_at'];
209: $config['tooltipShadow'] = $form['tooltip_shadow'];
210: $config['tooltipRounded'] = $form['tooltip_rounded'];
211: $config['tooltipClasses'] = $this->getTooltipClasses($form);
212: $config['fieldBackgroundColor'] = $form['element_background_colour'];
213: $config['fieldBackgroundColorHover'] = $form['element_background_colour'];
214: $config['fieldBackgroundColorFocus'] = $form['element_background_colour'];
215: $config['fieldBorderColor'] = $form['element_border_colour'];
216: $config['fieldBorderColorHover'] = $form['element_border_colour'];
217: $config['fieldBorderColorFocus'] = $form['element_border_colour'];
218: $config['fieldTextColor'] = $form['element_text_colour'];
219: $config['fieldTextColorHover'] = $form['element_text_colour'];
220: $config['fieldTextColorFocus'] = $form['element_text_colour'];
221: $config['labelTextColor'] = $form['label_text_colour'];
222:
223: $config['styles'] = $this->convertStyles($form['styles'], false);
224:
225: if (isset($form['entries_table_layout']['active']) && is_array($form['entries_table_layout']['active'])) {
226: foreach ($form['entries_table_layout']['active'] as $column) {
227: if ($column['type'] == 'element') {
228: $config['entriesTableColumns'][] = sprintf('element_%d', $column['id']);
229: } else if ($column['type'] == 'column') {
230: if ($column['id'] == 'date_added') {
231: $config['entriesTableColumns'][] = 'created_at';
232: } else if (in_array($column['id'], array('form_url', 'referring_url', 'post_id', 'created_by', 'updated_at', 'ip', 'id'))) {
233: $config['entriesTableColumns'][] = $column['id'];
234: }
235: }
236: }
237: }
238:
239: $config['saveEntry'] = $form['save_to_database'];
240: $config['databaseEnabled'] = is_array($form['db_fields']) && count($form['db_fields']);
241: $config['databaseWordpress'] = $form['use_wp_db'];
242: $config['databaseHost'] = $form['db_host'];
243: $config['databaseUsername'] = $form['db_username'];
244: $config['databasePassword'] = $form['db_password'];
245: $config['databaseDatabase'] = $form['db_name'];
246: $config['databaseTable'] = $form['db_table'];
247: $config['databaseColumns'] = array();
248:
249: if (is_array($form['db_fields'])) {
250: foreach ($form['db_fields'] as $dbFieldName => $dbFieldValue) {
251: $config['databaseColumns'][] = array(
252: 'name' => $dbFieldName,
253: 'value' => $this->convertPlaceholders($dbFieldValue)
254: );
255: }
256: }
257:
258: $config['elements'] = array();
259:
260:
261: $config['nextElementId'] = 1;
262: foreach ($form['elements'] as $element) {
263: $config['nextElementId'] = max($config['nextElementId'], $element['id']);
264: }
265: $config['nextElementId']++;
266:
267: $page1 = Quform_Element_Page::getDefaultConfig();
268: $page1['id'] = $config['nextElementId']++;
269: $page1['parentId'] = 0;
270: $page1['position'] = 0;
271: $page1['elements'] = array();
272:
273: $stack = array();
274: $notificationAttachments = array();
275: $allOptions = array();
276:
277: foreach ($form['elements'] as $element) {
278: if ( ! isset($element['type']) || ! Quform::isNonEmptyString($element['type'])) {
279: continue;
280: }
281:
282: if ($element['type'] == 'groupend') {
283: if ( ! count($stack)) {
284: return array(
285: 'type' => 'error',
286: 'message' => __('Group end element found before group start element', 'quform')
287: );
288: }
289:
290: $group = array_pop($stack);
291:
292: if (count($stack)) {
293: $columnIndex = $stack[count($stack) - 1][2] % $stack[count($stack) - 1][1];
294: $stack[count($stack) - 1][0]['elements'][0]['elements'][$columnIndex]['elements'][] = $group[0];
295: $stack[count($stack) - 1][2]++;
296: } else {
297: $page1['elements'][] = $group[0];
298: }
299:
300: continue;
301: } else if ($element['type'] == 'groupstart') {
302: $element['type'] = 'group';
303: }
304:
305: $class = 'Quform_Element_' . ucfirst($element['type']);
306:
307: if ( ! class_exists($class)) {
308: continue;
309: }
310:
311: $eConfig = call_user_func(array($class, 'getDefaultConfig'));
312: $eConfig['id'] = $element['id'];
313:
314: switch ($element['type']) {
315: case 'text':
316: case 'textarea':
317: case 'email':
318: $eConfig['label'] = $element['label'];
319: $eConfig['placeholder'] = Quform::get($element, 'placeholder', '');
320: $eConfig['description'] = $element['description'];
321: $eConfig['required'] = $element['required'];
322: $eConfig['defaultValue'] = $this->convertPlaceholders($element['default_value']);
323: $eConfig['dynamicDefaultValue'] = $element['dynamic_default_value'];
324: $eConfig['dynamicKey'] = $element['dynamic_key'];
325: $eConfig['tooltip'] = $element['tooltip'];
326: $eConfig['adminLabel'] = $element['admin_label'];
327: $eConfig['messageRequired'] = $element['required_message'];
328: $eConfig['showInEmail'] = ! $element['is_hidden'];
329: $eConfig['saveToDatabase'] = $element['save_to_database'];
330: $eConfig['labelPosition'] = str_replace('above', '', $element['label_placement']);
331: $eConfig['labelWidth'] = $element['label_width'];
332: $eConfig['tooltipType'] = $element['tooltip_type'];
333: $eConfig['tooltipEvent'] = $element['tooltip_event'];
334: $eConfig['logicEnabled'] = $element['logic'];
335: $eConfig['logicAction'] = Quform::get($element, 'logic_action') != 'hide';
336: $eConfig['logicMatch'] = $element['logic_match'];
337: $eConfig['logicRules'] = $this->convertLogicRules($element['logic_rules']);
338: $eConfig['filters'] = $this->convertFilters($element['filters']);
339:
340: if ($element['type'] == 'email') {
341: foreach ($element['validators'] as $key => $validator) {
342: if ($validator['type'] == 'email') {
343: array_splice($element['validators'], $key, 1);
344: }
345: }
346: }
347:
348: $eConfig['validators'] = $this->convertValidators($element['validators']);
349: $eConfig['styles'] = $this->convertStyles($element['styles']);
350:
351: if ($element['prevent_duplicates']) {
352: $eConfig['validators'][] = $this->createPreventDuplicatesValidator($element['duplicate_found_message']);
353: }
354: break;
355: case 'select':
356: case 'checkbox':
357: case 'radio':
358: $eConfig['label'] = $element['label'];
359: $eConfig['description'] = $element['description'];
360: $eConfig['required'] = $element['required'];
361: $eConfig['options'] = array();
362: $eConfig['nextOptionId'] = 1;
363:
364: foreach ($element['options'] as $option) {
365: $newOption = array(
366: 'id' => $eConfig['nextOptionId']++,
367: 'label' => $option['label'],
368: 'value' => $option['value']
369: );
370:
371: $eConfig['options'][] = $newOption;
372: $allOptions[$element['id']][] = $newOption;
373: }
374:
375: if ($element['type'] == 'checkbox' || $element['type'] == 'radio') {
376: $eConfig['optionsLayout'] = $element['options_layout'];
377: }
378:
379: $eConfig['customiseValues'] = $element['customise_values'];
380: $eConfig['defaultValue'] = $element['default_value'];
381:
382:
383: if ($element['type'] == 'select' || $element['type'] == 'radio') {
384: if (is_array($eConfig['defaultValue']) && count($eConfig['defaultValue']) && is_string($eConfig['defaultValue'][0])) {
385: $eConfig['defaultValue'] = $eConfig['defaultValue'][0];
386: } else if ( ! is_string($eConfig['defaultValue'])) {
387: $eConfig['defaultValue'] = '';
388: }
389: }
390:
391: if ($element['type'] == 'select') {
392: $eConfig['noneOption'] = false;
393: }
394:
395: $eConfig['dynamicDefaultValue'] = $element['dynamic_default_value'];
396: $eConfig['dynamicKey'] = $element['dynamic_key'];
397: $eConfig['tooltip'] = $element['tooltip'];
398: $eConfig['adminLabel'] = $element['admin_label'];
399: $eConfig['messageRequired'] = $element['required_message'];
400: $eConfig['showInEmail'] = ! $element['is_hidden'];
401: $eConfig['saveToDatabase'] = $element['save_to_database'];
402: $eConfig['labelPosition'] = str_replace('above', '', $element['label_placement']);
403: $eConfig['labelWidth'] = $element['label_width'];
404: $eConfig['tooltipType'] = $element['tooltip_type'];
405: $eConfig['tooltipEvent'] = $element['tooltip_event'];
406: $eConfig['logicEnabled'] = $element['logic'];
407: $eConfig['logicAction'] = Quform::get($element, 'logic_action') != 'hide';
408: $eConfig['logicMatch'] = $element['logic_match'];
409: $eConfig['logicRules'] = $this->convertLogicRules($element['logic_rules']);
410: $eConfig['styles'] = $this->convertStyles($element['styles']);
411:
412: if ($element['prevent_duplicates']) {
413: $eConfig['validators'][] = $this->createPreventDuplicatesValidator($element['duplicate_found_message']);
414: }
415: break;
416: case 'file':
417: $eConfig['label'] = $element['label'];
418: $eConfig['description'] = $element['description'];
419: $eConfig['required'] = $element['required'];
420: $eConfig['enhancedUploadEnabled'] = $element['enable_swf_upload'];
421: $eConfig['maximumNumberOfFiles'] = $element['allow_multiple_uploads'] ? '0' : '1';
422: $eConfig['allowedExtensions'] = $element['upload_allowed_extensions'];
423: $eConfig['maximumFileSize'] = $element['upload_maximum_size'];
424: $eConfig['tooltip'] = $element['tooltip'];
425: $eConfig['adminLabel'] = $element['admin_label'];
426: $eConfig['showInEmail'] = ! $element['is_hidden'];
427: $eConfig['saveToDatabase'] = $element['save_to_database'];
428: $eConfig['labelPosition'] = str_replace('above', '', $element['label_placement']);
429: $eConfig['labelWidth'] = $element['label_width'];
430:
431: if ($element['add_as_attachment']) {
432: $notificationAttachments[] = $element['id'];
433: }
434:
435: $eConfig['saveToServer'] = $element['save_to_server'];
436: $eConfig['savePath'] = $element['save_path'];
437: $eConfig['browseText'] = $element['browse_text'];
438: $eConfig['tooltipType'] = $element['tooltip_type'];
439: $eConfig['tooltipEvent'] = $element['tooltip_event'];
440: $eConfig['logicEnabled'] = $element['logic'];
441: $eConfig['logicAction'] = Quform::get($element, 'logic_action') != 'hide';
442: $eConfig['logicMatch'] = $element['logic_match'];
443: $eConfig['logicRules'] = $this->convertLogicRules($element['logic_rules']);
444: $eConfig['styles'] = $this->convertStyles($element['styles']);
445: $eConfig['messageFileUploadRequired'] = $element['messages']['field_required'];
446: $eConfig['messageFileTooBigFilename'] = $element['messages']['too_big_with_filename'];
447: $eConfig['messageFileTooBig'] = $element['messages']['too_big'];
448: $eConfig['messageNotAllowedTypeFilename'] = $element['messages']['not_allowed_type_with_filename'];
449: $eConfig['messageNotAllowedType'] = $element['messages']['not_allowed_type'];
450:
451: if ($eConfig['enhancedUploadEnabled']) {
452: $config['hasEnhancedUploader'] = true;
453: }
454: break;
455: case 'captcha':
456: $eConfig['label'] = $element['label'];
457: $eConfig['placeholder'] = Quform::get($element, 'placeholder', '');
458: $eConfig['description'] = $element['description'];
459: $eConfig['captchaLength'] = $element['options']['length'];
460: $eConfig['captchaWidth'] = $element['options']['width'];
461: $eConfig['captchaHeight'] = $element['options']['height'];
462: $eConfig['captchaBgColor'] = $element['options']['bgColour'];
463: $eConfig['captchaBgColorRgba'] = $this->hexToRgb($element['options']['bgColour']);
464: $eConfig['captchaTextColor'] = $element['options']['textColour'];
465: $eConfig['captchaTextColorRgba'] = $this->hexToRgb($element['options']['textColour']);
466: $eConfig['captchaFont'] = $element['options']['font'];
467: $eConfig['captchaMinFontSize'] = $element['options']['minFontSize'];
468: $eConfig['captchaMaxFontSize'] = $element['options']['maxFontSize'];
469: $eConfig['captchaMinAngle'] = $element['options']['minAngle'];
470: $eConfig['captchaMaxAngle'] = $element['options']['maxAngle'];
471: $eConfig['tooltip'] = $element['tooltip'];
472: $eConfig['messageRequired'] = $element['required_message'];
473: $eConfig['messageCaptchaNotMatch'] = $element['invalid_message'];
474: $eConfig['labelPosition'] = str_replace('above', '', $element['label_placement']);
475: $eConfig['labelWidth'] = $element['label_width'];
476: $eConfig['tooltipType'] = $element['tooltip_type'];
477: $eConfig['tooltipEvent'] = $element['tooltip_event'];
478: $eConfig['logicEnabled'] = $element['logic'];
479: $eConfig['logicAction'] = Quform::get($element, 'logic_action') != 'hide';
480: $eConfig['logicMatch'] = $element['logic_match'];
481: $eConfig['logicRules'] = $this->convertLogicRules($element['logic_rules']);
482: $eConfig['styles'] = $this->convertStyles($element['styles']);
483: break;
484: case 'recaptcha':
485: $eConfig['label'] = $element['label'];
486: $eConfig['description'] = $element['description'];
487: $eConfig['recaptchaTheme'] = $element['recaptcha_theme'];
488: $eConfig['recaptchaType'] = $element['recaptcha_type'];
489: $eConfig['recaptchaSize'] = $element['recaptcha_size'];
490: $eConfig['recaptchaBadge'] = $element['recaptcha_badge_position'];
491: $eConfig['recaptchaLang'] = $element['recaptcha_lang'];
492: $eConfig['tooltip'] = $element['tooltip'];
493: $eConfig['messageRequired'] = $element['required_message'];
494: $eConfig['labelPosition'] = str_replace('above', '', $element['label_placement']);
495: $eConfig['labelWidth'] = $element['label_width'];
496: $eConfig['tooltipType'] = $element['tooltip_type'];
497: $eConfig['tooltipEvent'] = $element['tooltip_event'];
498: $eConfig['logicEnabled'] = $element['logic'];
499: $eConfig['logicAction'] = Quform::get($element, 'logic_action') != 'hide';
500: $eConfig['logicMatch'] = $element['logic_match'];
501: $eConfig['logicRules'] = $this->convertLogicRules($element['logic_rules']);
502: $eConfig['messageRecaptchaMissingInputSecret'] = $element['messages']['missing-input-secret'];
503: $eConfig['messageRecaptchaInvalidInputSecret'] = $element['messages']['invalid-input-secret'];
504: $eConfig['messageRecaptchaMissingInputResponse'] = $element['messages']['missing-input-response'];
505: $eConfig['messageRecaptchaInvalidInputResponse'] = $element['messages']['invalid-input-response'];
506: $eConfig['messageRecaptchaError'] = $element['messages']['error'];
507: $eConfig['styles'] = $this->convertStyles($element['styles']);
508: break;
509: case 'html':
510: $eConfig['content'] = $element['content'];
511: $eConfig['showInEntry'] = $element['show_in_entry'];
512: $eConfig['logicEnabled'] = $element['logic'];
513: $eConfig['logicAction'] = Quform::get($element, 'logic_action') != 'hide';
514: $eConfig['logicMatch'] = $element['logic_match'];
515: $eConfig['logicRules'] = $this->convertLogicRules($element['logic_rules']);
516: break;
517: case 'date':
518: $eConfig['label'] = $element['label'];
519: $eConfig['description'] = $element['description'];
520: $eConfig['required'] = $element['required'];
521: $eConfig['tooltip'] = $element['tooltip'];
522:
523: $day = $element['default_value']['day'];
524: $month = $element['default_value']['month'];
525: $year = $element['default_value']['year'];
526:
527: if (is_numeric($day) && is_numeric($month) && is_numeric($year)) {
528: $day = str_pad($day, 2, '0', STR_PAD_LEFT);
529: $month = str_pad($month, 2, '0', STR_PAD_LEFT);
530:
531: if (checkdate($month, $day, $year)) {
532: $eConfig['defaultValue'] = $year . '-' . $month . '-' . $day;
533: }
534: }
535:
536: $eConfig['dynamicDefaultValue'] = $element['dynamic_default_value'];
537: $eConfig['dynamicKey'] = $element['dynamic_key'];
538: $eConfig['adminLabel'] = $element['admin_label'];
539: $eConfig['messageRequired'] = $element['required_message'];
540: $eConfig['messageDateInvalidDate'] = $element['date_validator_message_invalid'];
541: $eConfig['showInEmail'] = ! $element['is_hidden'];
542: $eConfig['saveToDatabase'] = $element['save_to_database'];
543: $eConfig['labelPosition'] = str_replace('above', '', $element['label_placement']);
544: $eConfig['labelWidth'] = $element['label_width'];
545: $eConfig['tooltipType'] = $element['tooltip_type'];
546: $eConfig['tooltipEvent'] = $element['tooltip_event'];
547: $eConfig['logicEnabled'] = $element['logic'];
548: $eConfig['logicAction'] = Quform::get($element, 'logic_action') != 'hide';
549: $eConfig['logicMatch'] = $element['logic_match'];
550: $eConfig['logicRules'] = $this->convertLogicRules($element['logic_rules']);
551: $eConfig['styles'] = $this->convertStyles($element['styles']);
552:
553: if ($element['prevent_duplicates']) {
554: $eConfig['validators'][] = $this->createPreventDuplicatesValidator($element['duplicate_found_message']);
555: }
556:
557: $config['hasDatepicker'] = true;
558: $config['locales'][] = '';
559: break;
560: case 'time':
561: $eConfig['label'] = $element['label'];
562: $eConfig['description'] = $element['description'];
563: $eConfig['required'] = $element['required'];
564: $eConfig['tooltip'] = $element['tooltip'];
565:
566: $hour = $element['default_value']['hour'];
567: $minute = $element['default_value']['minute'];
568:
569: if ($element['time_12_24'] == '12') {
570: $ampm = $element['default_value']['ampm'];
571:
572: if (is_numeric($hour) && is_numeric($minute) && in_array($ampm, array('am', 'pm'))) {
573: try {
574: $eConfig['defaultValue'] = Quform::date(
575: 'H:i',
576: new DateTime("$hour:$minute$ampm", new DateTimeZone('UTC')),
577: new DateTimeZone('UTC')
578: );
579: } catch (Exception $e) {
580:
581: }
582: }
583: } else {
584: if (is_numeric($hour) && is_numeric($minute)) {
585: $eConfig['defaultValue'] = "$hour:$minute";
586: }
587: }
588:
589: if (is_numeric($element['start_hour'])) {
590: $eConfig['timeMin'] = $element['start_hour'] . ':00';
591: }
592:
593: if (is_numeric($element['end_hour'])) {
594: $eConfig['timeMax'] = $element['end_hour'] . ':00';
595: }
596:
597: $eConfig['timeInterval'] = $element['minute_granularity'];
598: $eConfig['messageTimeInvalidTime'] = $element['time_validator_message_invalid'];
599: $eConfig['adminLabel'] = $element['admin_label'];
600: $eConfig['messageRequired'] = $element['required_message'];
601: $eConfig['showInEmail'] = ! $element['is_hidden'];
602: $eConfig['saveToDatabase'] = $element['save_to_database'];
603: $eConfig['labelPosition'] = str_replace('above', '', $element['label_placement']);
604: $eConfig['labelWidth'] = $element['label_width'];
605: $eConfig['tooltipType'] = $element['tooltip_type'];
606: $eConfig['tooltipEvent'] = $element['tooltip_event'];
607: $eConfig['dynamicDefaultValue'] = $element['dynamic_default_value'];
608: $eConfig['dynamicKey'] = $element['dynamic_key'];
609: $eConfig['logicEnabled'] = $element['logic'];
610: $eConfig['logicAction'] = Quform::get($element, 'logic_action') != 'hide';
611: $eConfig['logicMatch'] = $element['logic_match'];
612: $eConfig['logicRules'] = $this->convertLogicRules($element['logic_rules']);
613: $eConfig['styles'] = $this->convertStyles($element['styles']);
614:
615: if ($element['prevent_duplicates']) {
616: $eConfig['validators'][] = $this->createPreventDuplicatesValidator($element['duplicate_found_message']);
617: }
618:
619: $config['hasTimepicker'] = true;
620: $config['locales'][] = '';
621: break;
622: case 'password':
623: $eConfig['label'] = $element['label'];
624: $eConfig['placeholder'] = Quform::get($element, 'placeholder', '');
625: $eConfig['description'] = $element['description'];
626: $eConfig['required'] = $element['required'];
627: $eConfig['tooltip'] = $element['tooltip'];
628: $eConfig['adminLabel'] = $element['admin_label'];
629: $eConfig['messageRequired'] = $element['required_message'];
630: $eConfig['showInEmail'] = ! $element['is_hidden'];
631: $eConfig['saveToDatabase'] = $element['save_to_database'];
632: $eConfig['labelPosition'] = str_replace('above', '', $element['label_placement']);
633: $eConfig['labelWidth'] = $element['label_width'];
634: $eConfig['tooltipType'] = $element['tooltip_type'];
635: $eConfig['tooltipEvent'] = $element['tooltip_event'];
636: $eConfig['logicEnabled'] = $element['logic'];
637: $eConfig['logicAction'] = Quform::get($element, 'logic_action') != 'hide';
638: $eConfig['logicMatch'] = $element['logic_match'];
639: $eConfig['logicRules'] = $this->convertLogicRules($element['logic_rules']);
640: $eConfig['validators'] = $this->convertValidators($element['validators']);
641: $eConfig['styles'] = $this->convertStyles($element['styles']);
642: break;
643: case 'hidden':
644: $eConfig['label'] = $element['label'];
645: $eConfig['defaultValue'] = $this->convertPlaceholders($element['default_value']);
646: $eConfig['dynamicDefaultValue'] = $element['dynamic_default_value'];
647: $eConfig['dynamicKey'] = $element['dynamic_key'];
648: $eConfig['showInEmail'] = ! $element['is_hidden'];
649: $eConfig['saveToDatabase'] = $element['save_to_database'];
650: break;
651: case 'group':
652: $eConfig['label'] = $element['admin_title'];
653: $eConfig['title'] = $element['title'];
654: $eConfig['showLabelInEmail'] = $element['show_name_in_email'];
655: $eConfig['description'] = $element['description'];
656: $eConfig['labelPosition'] = str_replace('above', '', $element['label_placement']);
657: $eConfig['labelWidth'] = $element['label_width'];
658: $eConfig['tooltipType'] = $element['tooltip_type'];
659: $eConfig['tooltipEvent'] = $element['tooltip_event'];
660: $eConfig['groupStyle'] = $element['group_style'];
661: $eConfig['borderColor'] = $element['border_colour'];
662: $eConfig['backgroundColor'] = $element['background_colour'];
663: $eConfig['logicEnabled'] = $element['logic'];
664: $eConfig['logicAction'] = Quform::get($element, 'logic_action') != 'hide';
665: $eConfig['logicMatch'] = $element['logic_match'];
666: $eConfig['logicRules'] = $this->convertLogicRules($element['logic_rules']);
667: $eConfig['styles'] = $this->convertStyles($element['styles']);
668: $eConfig['elements'] = array();
669:
670: $row = Quform_Element_Row::getDefaultConfig();
671: $row['id'] = $config['nextElementId']++;
672: $row['columnSize'] = $element['column_alignment'] == 'proportional' ? 'fixed' : 'float';
673: $row['elements'] = array();
674:
675: $columnCount = (int) $element['number_of_columns'];
676: $columnCount = $columnCount > 0 ? $columnCount : 1;
677:
678: for ($i = 0; $i < $columnCount; $i++) {
679: $column = Quform_Element_Column::getDefaultConfig();
680: $column['id'] = $config['nextElementId']++;
681: $row['elements'][] = $column;
682: }
683:
684: $eConfig['elements'][] = $row;
685: $stack[] = array($eConfig, $columnCount, 0);
686: break;
687: }
688:
689: if ($element['type'] != 'group') {
690: if (count($stack)) {
691: $columnIndex = $stack[count($stack) - 1][2] % $stack[count($stack) - 1][1];
692: $stack[count($stack) - 1][0]['elements'][0]['elements'][$columnIndex]['elements'][] = $eConfig;
693: $stack[count($stack) - 1][2]++;
694: } else {
695: $page1['elements'][] = $eConfig;
696: }
697: }
698: }
699:
700: $submit = Quform_Element_Submit::getDefaultConfig();
701: $submit['id'] = $config['nextElementId']++;
702: $submit['parentId'] = $page1['id'];
703: $submit['position'] = count($page1['elements']);
704:
705: $page1['elements'][] = $submit;
706:
707: $config['elements'][] = $page1;
708:
709: $config['notifications'] = array();
710:
711: $notification1 = Quform_Notification::getDefaultConfig();
712:
713: $notification1['id'] = 1;
714: $notification1['name'] = __('Admin notification', 'quform');
715: $notification1['enabled'] = $form['send_notification'];
716: $notification1['subject'] = $this->convertPlaceholders($form['subject']);
717: $notification1['format'] = $form['customise_email_content'] && $form['notification_format'] == 'plain' ? 'text' : 'html';
718:
719: if ($form['customise_email_content']) {
720: $notification1['html'] = $form['notification_format'] == 'html' ? $this->convertPlaceholders($form['notification_email_content']) : '';
721: } else {
722: $notification1['html'] = ! empty($form['notification_show_empty_fields']) ? '{all_form_data|showEmptyFields:true}' : '{all_form_data}';
723: }
724:
725: $notification1['autoFormat'] = false;
726:
727: if ($form['customise_email_content']) {
728: $notification1['text'] = $form['notification_format'] == 'plain' ? $this->convertPlaceholders($form['notification_email_content']) : '';
729: } else {
730: $notification1['text'] = ! empty($form['notification_show_empty_fields']) ? '{all_form_data|showEmptyFields:true}' : '{all_form_data}';
731: }
732:
733: $notification1['recipients'] = array();
734:
735: if (is_array($form['recipients'])) {
736: foreach ($form['recipients'] as $recipient) {
737: $notification1['recipients'][] = array('type' => 'to', 'address' => $recipient, 'name' => '');
738: }
739: }
740:
741: if ( ! empty($form['bcc']) && is_array($form['bcc'])) {
742: foreach ($form['bcc'] as $recipient) {
743: $notification1['recipients'][] = array('type' => 'bcc', 'address' => $recipient, 'name' => '');
744: }
745: }
746:
747: if (Quform::isNonEmptyString($form['notification_reply_to_element'])) {
748: $notification1['recipients'][] = array('type' => 'reply', 'address' => '{element|id:' . $form['notification_reply_to_element'] . '}', 'name' => '');
749: }
750:
751: if (is_array($form['conditional_recipients'])) {
752: if (count($form['conditional_recipients'])) {
753: $notification1['conditional'] = true;
754:
755: foreach ($form['conditional_recipients'] as $conditionalRecipient) {
756: $conditional = array(
757: 'recipients' => array(array('type' => 'to', 'address' => $conditionalRecipient['recipient'], 'name' => '')),
758: 'logicAction' => true,
759: 'logicMatch' => 'all',
760: 'logicRules' => array(array(
761: 'elementId' => $conditionalRecipient['element'],
762: 'operator' => $conditionalRecipient['operator'],
763: 'value' => $conditionalRecipient['value'],
764: 'optionId' => null
765: ))
766: );
767:
768: $notification1['conditionals'][] = $conditional;
769: }
770: }
771: }
772:
773: if ($form['notification_from_type'] == 'element') {
774: $notification1['from'] = array('address' => '{element|id:' . $form['notification_from_element'] . '}', 'name' => '');
775: } else {
776: $notification1['from'] = array('address' => str_replace('{admin_email}', '{default_from_email_address}', $form['from_email']), 'name' => $form['from_name']);
777: }
778:
779: if (count($notificationAttachments)) {
780: $notification1['attachments'] = array();
781:
782: foreach ($notificationAttachments as $notificationAttachment) {
783: $notification1['attachments'][] = array(
784: 'source' => 'element',
785: 'element' => (string) $notificationAttachment,
786: 'media' => array()
787: );
788: }
789: }
790:
791: $config['notifications'][] = $notification1;
792: $config['nextNotificationId'] = 2;
793:
794: if ($form['send_autoreply'] &&
795: Quform::isNonEmptyString($form['autoreply_recipient_element']) &&
796: is_numeric($form['autoreply_recipient_element']) &&
797: Quform::isNonEmptyString($form['autoreply_email_content'])
798: ) {
799: $notification2 = Quform_Notification::getDefaultConfig();
800: $notification2['id'] = 2;
801: $notification2['name'] = 'Autoreply email';
802: $notification2['enabled'] = true;
803: $notification2['subject'] = $this->convertPlaceholders($form['autoreply_subject']);
804: $notification2['format'] = $form['autoreply_format'] == 'plain' ? 'text' : 'html';
805: $notification2['html'] = $form['autoreply_format'] == 'html' ? $this->convertPlaceholders($form['autoreply_email_content']) : '';
806: $notification2['autoFormat'] = false;
807: $notification2['text'] = $form['autoreply_format'] == 'plain' ? $this->convertPlaceholders($form['autoreply_email_content']) : '';
808:
809: $notification2['recipients'] = array(
810: array('type' => 'to', 'address' => '{element|id:' . $form['autoreply_recipient_element'] . '}', 'name' => '')
811: );
812:
813: if ($form['autoreply_from_type'] == 'element') {
814: $notification2['from'] = array('address' => '{element|id:' . $form['autoreply_from_element'] . '}', 'name' => '');
815: } else {
816: $notification2['from'] = array('address' => str_replace('{admin_email}', '{default_from_email_address}', $form['autoreply_from_email']), 'name' => $form['autoreply_from_name']);
817: }
818:
819: $config['notifications'][] = $notification2;
820: $config['nextNotificationId'] = 3;
821: }
822:
823: $config = $this->setPositionData($config);
824: $config = $this->convertLogic($config, $allOptions);
825:
826: return array(
827: 'type' => 'success',
828: 'config' => $config
829: );
830: }
831:
832: 833: 834: 835: 836: 837: 838: 839:
840: protected function getTooltipClasses(array $form)
841: {
842: $classes = array();
843:
844: if ($form['tooltip_style'] == 'custom') {
845: if (isset($form['tooltip_custom']) && Quform::isNonEmptyString($form['tooltip_custom'])) {
846: $classes[] = $form['tooltip_custom'];
847: }
848: } else {
849: $classes[] = $form['tooltip_style'];
850: }
851:
852: if (isset($form['tooltip_shadow']) && is_bool($form['tooltip_shadow']) && $form['tooltip_shadow']) {
853: $classes[] = 'qtip-shadow';
854: }
855:
856: if (isset($form['tooltip_rounded']) && is_bool($form['tooltip_rounded']) && $form['tooltip_rounded']) {
857: $classes[] = 'qtip-rounded';
858: }
859:
860: return join(' ', $classes);
861: }
862:
863: 864: 865: 866: 867: 868:
869: protected function convertPlaceholders($value)
870: {
871: $value = str_replace(
872: array(
873: '{post_id}',
874: '{post_title}',
875: '{user_display_name}',
876: '{user_email}',
877: '{user_login}',
878: '{current_date}',
879: '{submit_date}',
880: '{current_time}',
881: '{submit_time}'
882: ),
883: array(
884: '{post|ID}',
885: '{post|post_title}',
886: '{user|display_name}',
887: '{user|user_email}',
888: '{user|user_login}',
889: '{date}',
890: '{date}',
891: '{time}',
892: '{time}'
893: ),
894: $value
895: );
896:
897: $value = preg_replace(
898: array(
899: '/\{current_date\|(.+?)\}/',
900: '/\{submit_date\|(.+?)\}/',
901: '/\{current_time\|(.+?)\}/',
902: '/\{submit_time\|(.+?)\}/',
903: '/{([^}\r\n]*?)\|(\d+)}/'
904: ),
905: array(
906: '{date|format:$1}',
907: '{date|format:$1}',
908: '{time|format:$1}',
909: '{time|format:$1}',
910: '{element|id:$2|$1}'
911: ),
912: $value
913: );
914:
915: return $value;
916: }
917:
918: 919: 920: 921: 922: 923:
924: protected function setPositionData($config)
925: {
926: for ($i = 0; $i < count($config['elements']); $i++) {
927: $config['elements'][$i]['parentId'] = 0;
928: $config['elements'][$i]['position'] = $i;
929:
930: if (in_array($config['elements'][$i]['type'], array('page', 'group', 'row', 'column'))) {
931: $config['elements'][$i]['elements'] = $this->setContainerPositionData($config['elements'][$i]);
932: }
933: }
934:
935: return $config;
936: }
937:
938: 939: 940: 941: 942: 943:
944: protected function setContainerPositionData(array $container)
945: {
946: for ($i = 0; $i < count($container['elements']); $i++) {
947: $container['elements'][$i]['parentId'] = $container['id'];
948: $container['elements'][$i]['position'] = $i;
949:
950: if (in_array($container['elements'][$i]['type'], array('page', 'group', 'row', 'column'))) {
951: $container['elements'][$i]['elements'] = $this->setContainerPositionData($container['elements'][$i]);
952: }
953: }
954:
955: return $container['elements'];
956: }
957:
958: 959: 960: 961: 962: 963: 964: 965: 966:
967: protected function convertLogic(array $config, array $allOptions)
968: {
969:
970: foreach ($config['notifications'][0]['conditionals'] as $key => $conditional) {
971: foreach ($config['notifications'][0]['conditionals'][$key]['logicRules'] as $conditionalLogicRuleKey => $conditionalLogicRule) {
972: $config['notifications'][0]['conditionals'][$key]['logicRules'][$conditionalLogicRuleKey] = $this->setLogicRuleOptionId($conditionalLogicRule, $allOptions);
973: }
974: }
975:
976: $config = $this->convertContainerLogic($config, $allOptions);
977:
978: return $config;
979: }
980:
981: 982: 983: 984: 985: 986: 987:
988: protected function convertContainerLogic(array $container, array $allOptions)
989: {
990: foreach ($container['elements'] as $key => $element) {
991: if (isset($element['logicRules']) && is_array($element['logicRules'])) {
992: foreach ($element['logicRules'] as $logicRuleKey => $logicRule) {
993: $container['elements'][$key]['logicRules'][$logicRuleKey] = $this->setLogicRuleOptionId($logicRule, $allOptions);
994: }
995: }
996:
997: if (in_array($container['elements'][$key]['type'], array('page', 'group', 'row', 'column'))) {
998: $container['elements'][$key] = $this->convertContainerLogic($container['elements'][$key], $allOptions);
999: }
1000: }
1001:
1002: return $container;
1003: }
1004:
1005: 1006: 1007: 1008: 1009: 1010: 1011:
1012: protected function setLogicRuleOptionId(array $rule, array $allOptions)
1013: {
1014: if (isset($allOptions[$rule['elementId']])) {
1015: foreach ($allOptions[$rule['elementId']] as $option) {
1016: if ($option['value'] == $rule['value']) {
1017: $rule['optionId'] = $option['id'];
1018: break;
1019: }
1020: }
1021: }
1022:
1023: return $rule;
1024: }
1025:
1026: 1027: 1028: 1029: 1030: 1031: 1032:
1033: protected function convertStyles($styles, $isElement = true)
1034: {
1035: $converted = array();
1036:
1037: if (is_array($styles)) {
1038: foreach ($styles as $style) {
1039: if (in_array($style['type'], array('submitOuter', 'submit', 'submitButton', 'submitSpan', 'submitEm'))) {
1040: continue;
1041: }
1042:
1043: $converted[] = array(
1044: 'type' => $this->convertStyleType($style['type'], $isElement),
1045: 'css' => $style['css']
1046: );
1047: }
1048: }
1049:
1050: return $converted;
1051: }
1052:
1053: 1054: 1055: 1056: 1057: 1058: 1059:
1060: protected function convertStyleType($type, $isElement)
1061: {
1062: switch($type) {
1063: case 'success';
1064: $type = 'formSuccess';
1065: break;
1066: case 'title';
1067: $type = 'formTitle';
1068: break;
1069: case 'description';
1070: $type = $isElement ? 'groupDescription' : 'formDescription';
1071: break;
1072: case 'elements';
1073: $type = 'formElements';
1074: break;
1075: case 'outer';
1076: $type = 'element';
1077: break;
1078: case 'label';
1079: $type = 'elementLabel';
1080: break;
1081: case 'inner';
1082: $type = 'elementInner';
1083: break;
1084: case 'input';
1085: $type = 'elementText';
1086: break;
1087: case 'textarea';
1088: $type = 'elementTextarea';
1089: break;
1090: case 'select';
1091: $type = 'elementSelect';
1092: break;
1093: case 'optionUl';
1094: $type = 'options';
1095: break;
1096: case 'optionLi';
1097: $type = 'option';
1098: break;
1099: }
1100:
1101: return $type;
1102: }
1103:
1104: 1105: 1106: 1107: 1108: 1109:
1110: protected function convertLogicRules($logicRules)
1111: {
1112: $converted = array();
1113:
1114: if (is_array($logicRules)) {
1115: foreach ($logicRules as $logicRule) {
1116: $converted[] = array(
1117: 'elementId' => $logicRule['element_id'],
1118: 'operator' => $logicRule['operator'],
1119: 'value' => $logicRule['value'],
1120: 'optionId' => null
1121: );
1122: }
1123: }
1124:
1125: return $converted;
1126: }
1127:
1128: 1129: 1130: 1131: 1132: 1133:
1134: protected function convertFilters($filters)
1135: {
1136: $converted = array();
1137:
1138: if (is_array($filters)) {
1139: foreach ($filters as $filter) {
1140: switch ($filter['type']) {
1141: case 'alpha':
1142: case 'alphaNumeric':
1143: case 'digits':
1144: $converted[] = array(
1145: 'type' => $filter['type'],
1146: 'allowWhiteSpace' => $filter['allow_white_space']
1147: );
1148: break;
1149: case 'regex':
1150: $converted[] = array(
1151: 'type' => 'regex',
1152: 'pattern' => $filter['pattern']
1153: );
1154: break;
1155: case 'stripTags';
1156: $converted[] = array(
1157: 'type' => 'stripTags',
1158: 'allowableTags' => $filter['allowable_tags']
1159: );
1160: break;
1161: case 'trim':
1162: $converted[] = array(
1163: 'type' => 'trim'
1164: );
1165: break;
1166: }
1167: }
1168: }
1169:
1170: return $converted;
1171: }
1172:
1173: 1174: 1175: 1176: 1177: 1178:
1179: protected function convertValidators($validators)
1180: {
1181: $converted = array();
1182:
1183: if (is_array($validators)) {
1184: foreach ($validators as $validator) {
1185: switch ($validator['type']) {
1186: case 'alpha':
1187: $converted[] = array(
1188: 'type' => 'alpha',
1189: 'allowWhiteSpace' => $validator['allow_white_space'],
1190: 'messages' => array(
1191: 'notAlpha' => $validator['messages']['invalid']
1192: )
1193: );
1194: break;
1195: case 'alphaNumeric':
1196: $converted[] = array(
1197: 'type' => 'alphaNumeric',
1198: 'allowWhiteSpace' => $validator['allow_white_space'],
1199: 'messages' => array(
1200: 'notAlphaNumeric' => $validator['messages']['invalid']
1201: )
1202: );
1203: break;
1204: case 'digits':
1205: $converted[] = array(
1206: 'type' => 'digits',
1207: 'allowWhiteSpace' => $validator['allow_white_space'],
1208: 'messages' => array(
1209: 'notDigits' => $validator['messages']['invalid']
1210: )
1211: );
1212: break;
1213: case 'email':
1214: $converted[] = array(
1215: 'type' => 'email',
1216: 'messages' => array(
1217: 'emailAddressInvalidFormat' => $validator['messages']['invalid']
1218: )
1219: );
1220: break;
1221: case 'greaterThan':
1222: $converted[] = array(
1223: 'type' => 'greaterThan',
1224: 'min' => $validator['min'],
1225: 'messages' => array(
1226: 'notGreaterThan' => $validator['messages']['not_greater_than']
1227: )
1228: );
1229: break;
1230: case 'identical':
1231: $converted[] = array(
1232: 'type' => 'identical',
1233: 'token' => $validator['token'],
1234: 'messages' => array(
1235: 'notSame' => $validator['messages']['not_match']
1236: )
1237: );
1238: break;
1239: case 'lessThan':
1240: $converted[] = array(
1241: 'type' => 'lessThan',
1242: 'max' => $validator['max'],
1243: 'messages' => array(
1244: 'notLessThan' => $validator['messages']['not_less_than']
1245: )
1246: );
1247: break;
1248: case 'length':
1249: $converted[] = array(
1250: 'type' => 'length',
1251: 'min' => $validator['min'],
1252: 'max' => $validator['max'],
1253: 'messages' => array(
1254: 'lengthTooShort' => $validator['messages']['too_short'],
1255: 'lengthTooLong' => $validator['messages']['too_long']
1256: )
1257: );
1258: break;
1259: case 'regex':
1260: $converted[] = array(
1261: 'type' => 'regex',
1262: 'pattern' => $validator['pattern'],
1263: 'messages' => array(
1264: 'regexNotMatch' => $validator['messages']['invalid']
1265: )
1266: );
1267: break;
1268: }
1269: }
1270: }
1271:
1272: return $converted;
1273: }
1274:
1275: 1276: 1277: 1278: 1279: 1280:
1281: protected function createPreventDuplicatesValidator($message)
1282: {
1283: $validator = Quform_Validator_Duplicate::getDefaultConfig();
1284:
1285: if (Quform::isNonEmptyString($message)) {
1286: $validator['messages']['isDuplicate'] = $message;
1287: }
1288:
1289: return $validator;
1290: }
1291:
1292: 1293: 1294: 1295: 1296: 1297:
1298: protected function hexToRgb($hex) {
1299: $hex = str_replace('#', '', $hex);
1300: $length = strlen($hex);
1301:
1302: $rgb['r'] = hexdec($length == 6 ? substr($hex, 0, 2) : ($length == 3 ? str_repeat(substr($hex, 0, 1), 2) : 0));
1303: $rgb['g'] = hexdec($length == 6 ? substr($hex, 2, 2) : ($length == 3 ? str_repeat(substr($hex, 1, 1), 2) : 0));
1304: $rgb['b'] = hexdec($length == 6 ? substr($hex, 4, 2) : ($length == 3 ? str_repeat(substr($hex, 2, 1), 2) : 0));
1305:
1306: return $rgb;
1307: }
1308:
1309: 1310: 1311: 1312: 1313: 1314:
1315: protected function migrateEntries(array $oldConfig, array $newConfig)
1316: {
1317: global $wpdb;
1318: $oldEntriesTableName = $wpdb->prefix . 'iphorm_form_entries';
1319: $oldEntryDataTableName = $wpdb->prefix . 'iphorm_form_entry_data';
1320:
1321: $newForm = $this->formFactory->create($newConfig);
1322:
1323: $oldEntries = $wpdb->get_results($wpdb->prepare("SELECT * FROM " . $oldEntriesTableName . " WHERE form_id = %d", $oldConfig['id']), ARRAY_A);
1324:
1325: if (is_array($oldEntries) && count($oldEntries)) {
1326: foreach ($oldEntries as $oldEntry) {
1327: $currentTime = Quform::date('Y-m-d H:i:s', null, new DateTimeZone('UTC'));
1328:
1329: $createdBy = null;
1330: if (Quform::isNonEmptyString(Quform::get($oldEntry, 'user_login'))) {
1331: $createdBy = get_user_by('login', $oldEntry['user_login']);
1332:
1333: if ($createdBy instanceof WP_User) {
1334: $createdBy = $createdBy->ID;
1335: }
1336: }
1337:
1338: $newEntry = array(
1339: 'form_id' => $newForm->getId(),
1340: 'unread' => Quform::get($oldEntry, 'unread') == 1 ? 1 : 0,
1341: 'ip' => Quform::substr(Quform::get($oldEntry, 'ip'), 0, 45),
1342: 'form_url' => Quform::substr(Quform::get($oldEntry, 'form_url'), 0, 512),
1343: 'referring_url' => Quform::substr(Quform::get($oldEntry, 'referring_url'), 0, 512),
1344: 'post_id' => is_numeric($postId = Quform::get($oldEntry, 'post_id')) && $postId > 0 ? (int) $postId : null,
1345: 'created_by' => $createdBy,
1346: 'created_at' => Quform::get($oldEntry, 'date_added'),
1347: 'updated_at' => $currentTime
1348: );
1349:
1350: $newEntry['data'] = array();
1351:
1352: $oldEntryDataRows = $wpdb->get_results($wpdb->prepare("SELECT * FROM " . $oldEntryDataTableName . " WHERE entry_id = %d", $oldEntry['id']), ARRAY_A);
1353:
1354: foreach ($oldEntryDataRows as $oldEntryDataRow) {
1355: $element = $newForm->getElementById($oldEntryDataRow['element_id']);
1356:
1357: if ($element instanceof Quform_Element_Editable && $element->config('saveToDatabase')) {
1358: $element->setValue($this->convertEntryValue($element, $oldEntryDataRow['value']));
1359:
1360: if ( ! $element->isEmpty()) {
1361: $newEntry['data'][$element->getId()] = $element->getValueForStorage();
1362: }
1363: }
1364: }
1365:
1366: $this->repository->saveEntry($newEntry);
1367: }
1368: }
1369: }
1370:
1371: 1372: 1373: 1374: 1375: 1376: 1377:
1378: protected function convertEntryValue(Quform_Element $element, $value)
1379: {
1380: $originalValue = $value;
1381:
1382: switch ($element->config('type')) {
1383: case 'checkbox':
1384: $values = preg_split('#(<br>|<br />)#', $value);
1385: $newValue = array();
1386:
1387: foreach ($values as $key => $value) {
1388: if (is_string($value)) {
1389: $newValue[$key] = $this->undoEscHtml($value);
1390: }
1391: }
1392:
1393: $value = $newValue;
1394: break;
1395: case 'date':
1396: if (Quform::isNonEmptyString($value)) {
1397: try {
1398: $value = Quform::date(
1399: 'Y-m-d',
1400: new DateTime($value, new DateTimeZone('UTC')),
1401: new DateTimeZone('UTC')
1402: );
1403: } catch (Exception $e) {
1404:
1405: }
1406: }
1407: break;
1408: case 'email':
1409: $value = $this->undoEscHtml(wp_strip_all_tags($value, true));
1410: break;
1411: case 'file':
1412: $files = preg_split('#(<br>|<br />)#', $value);
1413: $newValue = array();
1414:
1415: foreach ($files as $file) {
1416: if (Quform::isNonEmptyString($file)) {
1417: if (preg_match('#<a href="(.+)">(.+)</a>#', $file, $matches)) {
1418: $newValue[] = array(
1419: 'name' => $matches[2],
1420: 'url' => $matches[1],
1421: 'quform_upload_uid' => Quform::randomString(40)
1422: );
1423: } else {
1424: $newValue[] = array(
1425: 'name' => $file,
1426: 'quform_upload_uid' => Quform::randomString(40)
1427: );
1428: }
1429: }
1430: }
1431:
1432: $value = $newValue;
1433: break;
1434: case 'text':
1435: case 'hidden':
1436: case 'password':
1437: case 'radio':
1438: case 'select':
1439: $value = $this->undoEscHtml($value);
1440: break;
1441: case 'textarea':
1442: $value = $this->undoEscHtml(str_replace(array('<br>', '<br />'), '', $value));
1443: break;
1444: case 'time':
1445: if (Quform::isNonEmptyString($value)) {
1446: try {
1447: $value = Quform::date(
1448: 'H:i',
1449: new DateTime($value, new DateTimeZone('UTC')),
1450: new DateTimeZone('UTC')
1451: );
1452: } catch (Exception $e) {
1453:
1454: }
1455: }
1456: break;
1457: }
1458:
1459: $value = apply_filters('quform_migrator_convert_entry_value', $value, $element, $originalValue, $this);
1460:
1461: return $value;
1462: }
1463:
1464: 1465: 1466: 1467: 1468: 1469:
1470: protected function undoEscHtml($value)
1471: {
1472: return htmlspecialchars_decode($value, ENT_QUOTES);
1473: }
1474:
1475: 1476: 1477:
1478: protected function validateMigrateFormRequest()
1479: {
1480: if ( ! Quform::isPostRequest() || ! isset($_POST['form_id'], $_POST['migrate_entries'])) {
1481: wp_send_json(array(
1482: 'type' => 'error',
1483: 'message' => __('Bad request', 'quform')
1484: ));
1485: }
1486:
1487: if ( ! current_user_can('quform_full_access')) {
1488: wp_send_json(array(
1489: 'type' => 'error',
1490: 'message' => __('Insufficient permissions', 'quform')
1491: ));
1492: }
1493:
1494: if ( ! check_ajax_referer('quform_migrate_form', false, false)) {
1495: wp_send_json(array(
1496: 'type' => 'error',
1497: 'message' => __('Nonce check failed', 'quform')
1498: ));
1499: }
1500: }
1501:
1502: 1503: 1504:
1505: public function migrateImportForm()
1506: {
1507: $this->validateMigrateImportFormRequest();
1508:
1509: $form = maybe_unserialize(base64_decode(wp_unslash($_POST['config'])));
1510:
1511: if ( ! is_array($form)) {
1512: wp_send_json(array(
1513: 'type' => 'error',
1514: 'errors' => array(
1515: 'qfb-import-form-data' => __('The import data is invalid', 'quform')
1516: )
1517: ));
1518: }
1519:
1520: $result = $this->convertForm($form);
1521:
1522: if ($result['type'] == 'success') {
1523: $config = $this->builder->sanitizeForm($result['config']);
1524:
1525: $config = $this->repository->add($config);
1526:
1527: if ( ! is_array($config)) {
1528: wp_send_json(array(
1529: 'type' => 'error',
1530: 'message' => wp_kses(sprintf(
1531:
1532: __('Failed to insert into database, check the %1$serror log%2$s for more information', 'quform'),
1533: '<a href="https://support.themecatcher.net/quform-wordpress-v2/guides/advanced/enabling-debug-logging">',
1534: '</a>'
1535: ), array('a' => array('href' => array())))
1536: ));
1537: }
1538:
1539: $this->scriptLoader->handleSaveForm($config);
1540:
1541: wp_send_json(array(
1542: 'type' => 'success',
1543: 'message' => wp_kses(sprintf(
1544:
1545: __('Form imported successfully, %1$sedit the form%2$s', 'quform'),
1546: '<a href="' . esc_url(admin_url('admin.php?page=quform.forms&sp=edit&id=' . $config['id'])) . '">',
1547: '</a>'
1548: ), array('a' => array('href' => array())))
1549: ));
1550: } else {
1551: wp_send_json(array(
1552: 'type' => 'error',
1553: 'message' => $result['message']
1554: ));
1555: }
1556: }
1557:
1558: 1559: 1560:
1561: protected function validateMigrateImportFormRequest()
1562: {
1563: if ( ! Quform::isPostRequest() || ! isset($_POST['config']) || ! is_string($_POST['config'])) {
1564: wp_send_json(array(
1565: 'type' => 'error',
1566: 'message' => __('Bad request', 'quform')
1567: ));
1568: }
1569:
1570: if ( ! current_user_can('quform_import_forms')) {
1571: wp_send_json(array(
1572: 'type' => 'error',
1573: 'message' => __('Insufficient permissions', 'quform')
1574: ));
1575: }
1576:
1577: if ( ! check_ajax_referer('quform_migrate_import_form', false, false)) {
1578: wp_send_json(array(
1579: 'type' => 'error',
1580: 'message' => __('Nonce check failed', 'quform')
1581: ));
1582: }
1583: }
1584:
1585: 1586: 1587:
1588: public function migrateSettings()
1589: {
1590: $this->validateMigrateSettingsRequest();
1591:
1592: $options = array();
1593:
1594: if ($_POST['migrate_license_key'] === '1') {
1595: $licenseKey = get_option('iphorm_licence_key');
1596:
1597: if (Quform::isNonEmptyString($licenseKey)) {
1598: $options['licenseKey'] = $licenseKey;
1599: }
1600: }
1601:
1602: if ($_POST['migrate_recaptcha_keys'] === '1') {
1603: $siteKey = get_option('iphorm_recaptcha_site_key');
1604: $secretKey = get_option('iphorm_recaptcha_secret_key');
1605:
1606: if ( ! Quform::isNonEmptyString($siteKey) && ! Quform::isNonEmptyString($secretKey)) {
1607:
1608: $siteKey = get_option('iphorm_recaptcha_public_key');
1609: $secretKey = get_option('iphorm_recaptcha_private_key');
1610: }
1611:
1612: if (Quform::isNonEmptyString($siteKey)) {
1613: $options['recaptchaSiteKey'] = $siteKey;
1614: }
1615:
1616: if (Quform::isNonEmptyString($secretKey)) {
1617: $options['recaptchaSecretKey'] = $secretKey;
1618: }
1619: }
1620:
1621: if (count($options)) {
1622: $this->options->set($options);
1623: }
1624:
1625: wp_send_json(array(
1626: 'type' => 'success'
1627: ));
1628: }
1629:
1630: 1631: 1632:
1633: protected function validateMigrateSettingsRequest()
1634: {
1635: if (
1636: ! Quform::isPostRequest() ||
1637: ! isset($_POST['migrate_license_key'], $_POST['migrate_recaptcha_keys']) ||
1638: ! is_string($_POST['migrate_license_key']) ||
1639: ! is_string($_POST['migrate_recaptcha_keys'])
1640: ) {
1641: wp_send_json(array(
1642: 'type' => 'error',
1643: 'message' => __('Bad request', 'quform')
1644: ));
1645: }
1646:
1647: if ( ! current_user_can('quform_full_access')) {
1648: wp_send_json(array(
1649: 'type' => 'error',
1650: 'message' => __('Insufficient permissions', 'quform')
1651: ));
1652: }
1653:
1654: if ( ! check_ajax_referer('quform_migrate_settings', false, false)) {
1655: wp_send_json(array(
1656: 'type' => 'error',
1657: 'message' => __('Nonce check failed', 'quform')
1658: ));
1659: }
1660: }
1661:
1662: 1663: 1664: 1665: 1666:
1667: public function get1xForms()
1668: {
1669: global $wpdb;
1670:
1671: $table = $wpdb->prefix . 'iphorm_forms';
1672: $forms = array();
1673:
1674: if ($wpdb->get_var($wpdb->prepare("SHOW TABLES LIKE '%s'", $wpdb->esc_like($table))) == $table) {
1675: $results = $wpdb->get_results("SELECT * FROM " . $table . " ORDER BY id ASC", ARRAY_A);
1676:
1677: foreach ($results as $result) {
1678: $form = maybe_unserialize($result['config']);
1679:
1680: if (is_array($form)) {
1681: $forms[] = $form;
1682: }
1683: }
1684: }
1685:
1686: return $forms;
1687: }
1688:
1689: 1690: 1691: 1692: 1693: 1694:
1695: public function get1xForm($formId)
1696: {
1697: global $wpdb;
1698:
1699: $table = $wpdb->prefix . 'iphorm_forms';
1700: $form = null;
1701:
1702: if ($wpdb->get_var($wpdb->prepare("SHOW TABLES LIKE '%s'", $wpdb->esc_like($table))) == $table) {
1703: $form = $wpdb->get_var($wpdb->prepare("SELECT config FROM " . $table . " WHERE id = %d", (int) $formId));
1704: $form = maybe_unserialize($form);
1705: }
1706:
1707: return $form;
1708: }
1709: }
1710: