| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Modules\Form; |
| 4 |
|
| 5 |
use FluentForm\App\Helpers\Helper; |
| 6 |
use FluentForm\Framework\Request\File; |
| 7 |
use FluentForm\Framework\Helpers\ArrayHelper; |
| 8 |
use FluentForm\Framework\Foundation\Application; |
| 9 |
|
| 10 |
class Transfer |
| 11 |
{ |
| 12 |
/** |
| 13 |
* @var \FluentForm\Framework\Request\Request $request |
| 14 |
*/ |
| 15 |
protected $request; |
| 16 |
|
| 17 |
/** |
| 18 |
* Transfer constructor. |
| 19 |
* |
| 20 |
* @param \FluentForm\Framework\Foundation\Application $application |
| 21 |
*/ |
| 22 |
public function __construct(Application $application) |
| 23 |
{ |
| 24 |
$this->request = $application->make('request'); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Export forms as JSON. |
| 29 |
*/ |
| 30 |
public function export() |
| 31 |
{ |
| 32 |
// Get the IDs of the forms the user wants to be exported. |
| 33 |
$formIds = $this->request->get('forms'); |
| 34 |
|
| 35 |
// The associated form settings the user wants to be exported as well. |
| 36 |
// Firing an event so that others can hook into it and add their |
| 37 |
// choice of the settings to be exported with the form. |
| 38 |
$metaKeys = apply_filters('fluentform_export_settings', [ |
| 39 |
'formSettings', |
| 40 |
'notifications', |
| 41 |
'mailchimp_feeds', |
| 42 |
'slack' |
| 43 |
]); |
| 44 |
|
| 45 |
// Load the forms for a given form IDs and the settings from the DB. |
| 46 |
$result = wpFluent()->table('fluentform_forms') |
| 47 |
->select(['fluentform_forms.*']) |
| 48 |
->whereIn('fluentform_forms.id', $formIds) |
| 49 |
->when($metaKeys, function ($query) use ($metaKeys) { |
| 50 |
$query->select([ |
| 51 |
'fluentform_form_meta.meta_key', |
| 52 |
'fluentform_form_meta.value' |
| 53 |
])->join( |
| 54 |
'fluentform_form_meta', |
| 55 |
'fluentform_form_meta.form_id', |
| 56 |
'=', |
| 57 |
'fluentform_forms.id' |
| 58 |
)->whereIn('fluentform_form_meta.meta_key', $metaKeys); |
| 59 |
}) |
| 60 |
->get(); |
| 61 |
|
| 62 |
// Prepare the loaded query results to form and it's settings objects. |
| 63 |
$forms = []; |
| 64 |
foreach ($result as $item) { |
| 65 |
$form = isset($forms[$item->id]) ? $forms[$item->id] : [ |
| 66 |
'id' => $item->id, |
| 67 |
'title' => $item->title, |
| 68 |
'form' => json_decode($item->form_fields) |
| 69 |
]; |
| 70 |
|
| 71 |
$settings = [ |
| 72 |
$item->meta_key => Helper::isJson($item->value) ? json_decode($item->value) : $item->value |
| 73 |
]; |
| 74 |
|
| 75 |
$forms[$item->id] = array_merge($form, $settings); |
| 76 |
} |
| 77 |
|
| 78 |
$fileName = 'fluentform-export-forms-'.date('d-m-Y').'.json'; |
| 79 |
|
| 80 |
header('Content-disposition: attachment; filename='.$fileName); |
| 81 |
|
| 82 |
header('Content-type: application/json'); |
| 83 |
|
| 84 |
echo json_encode(array_values($forms)); |
| 85 |
|
| 86 |
die(); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Import forms from a previously exported JSON file. |
| 91 |
*/ |
| 92 |
public function import() |
| 93 |
{ |
| 94 |
$file = $this->request->get('file'); |
| 95 |
|
| 96 |
if ($file instanceof File) { |
| 97 |
$forms = json_decode($file->getContents(), true); |
| 98 |
|
| 99 |
if ($forms && is_array($forms)) { |
| 100 |
foreach ($forms as $formItem) { |
| 101 |
// First of all make the form object. |
| 102 |
$form = [ |
| 103 |
'title' => ArrayHelper::get($formItem, 'title'), |
| 104 |
'form_fields' => json_encode(ArrayHelper::get($formItem, 'form', '')), |
| 105 |
'created_by' => get_current_user_id() |
| 106 |
]; |
| 107 |
|
| 108 |
// Insert the form to the DB. |
| 109 |
$formId = wpFluent()->table('fluentform_forms')->insert($form); |
| 110 |
|
| 111 |
// Remove the form object specific keys. |
| 112 |
unset($formItem['title']); |
| 113 |
unset($formItem['id']); |
| 114 |
unset($formItem['form']); |
| 115 |
|
| 116 |
// At this point all we have are the |
| 117 |
// settings associated with the form. |
| 118 |
$settings = []; |
| 119 |
foreach ($formItem as $settingsName => $settingsValue) { |
| 120 |
$settings[] = [ |
| 121 |
'form_id' => $formId, |
| 122 |
'meta_key' => $settingsName, |
| 123 |
'value' => json_encode($settingsValue) |
| 124 |
]; |
| 125 |
} |
| 126 |
|
| 127 |
// Fire an event letting others know which settings we are importing for this form. |
| 128 |
$settings = apply_filters('fluentform_import_form_settings', $settings, $formId); |
| 129 |
|
| 130 |
if (count($settings)) { |
| 131 |
wpFluent()->table('fluentform_form_meta')->insert($settings); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
wp_send_json([ |
| 136 |
'message' => __('You form has been successfully imported.', 'fluentform') |
| 137 |
], 200); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
wp_send_json([ |
| 142 |
'message' => __('You have a faulty JSON file, please export the Fluent Forms again.', 'fluentform') |
| 143 |
], 423); |
| 144 |
} |
| 145 |
} |