| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Modules\Form; |
| 4 |
|
| 5 |
use FluentForm\App\Helpers\Helper; |
| 6 |
use FluentForm\Framework\Foundation\Application; |
| 7 |
use FluentForm\Framework\Helpers\ArrayHelper; |
| 8 |
use FluentForm\Framework\Request\File; |
| 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 |
// Load the forms for a given form IDs and the settings from the DB. |
| 36 |
$result = wpFluent() |
| 37 |
->table('fluentform_forms') |
| 38 |
->whereIn('id', $formIds) |
| 39 |
->get(); |
| 40 |
|
| 41 |
// Prepare the loaded query results to form and it's settings objects. |
| 42 |
$forms = []; |
| 43 |
foreach ($result as $item) { |
| 44 |
$form = $item; |
| 45 |
$form->form_fields = json_decode($form->form_fields); |
| 46 |
$form->metas = $this->getFormMetas($item->id); |
| 47 |
$forms[] = $form; |
| 48 |
} |
| 49 |
|
| 50 |
$fileName = 'fluentform-export-forms-'. count($forms) .'-'. date('d-m-Y') . '.json'; |
| 51 |
|
| 52 |
header('Content-disposition: attachment; filename=' . $fileName); |
| 53 |
|
| 54 |
header('Content-type: application/json'); |
| 55 |
|
| 56 |
echo json_encode(array_values($forms)); |
| 57 |
|
| 58 |
die(); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Import forms from a previously exported JSON file. |
| 63 |
*/ |
| 64 |
public function import() |
| 65 |
{ |
| 66 |
$file = $this->request->get('file'); |
| 67 |
|
| 68 |
if ($file instanceof File) { |
| 69 |
$forms = \json_decode($file->getContents(), true); |
| 70 |
$insertedForms = []; |
| 71 |
if ($forms && is_array($forms)) { |
| 72 |
foreach ($forms as $formItem) { |
| 73 |
// First of all make the form object. |
| 74 |
$formFields = json_encode([]); |
| 75 |
if($fields = ArrayHelper::get($formItem, 'form', '')) { |
| 76 |
$formFields = json_encode($fields); |
| 77 |
} else if($fields = ArrayHelper::get($formItem, 'form_fields', '')) { |
| 78 |
$formFields = json_encode($fields); |
| 79 |
} else { |
| 80 |
wp_send_json([ |
| 81 |
'message' => __('You have a faulty JSON file, please export the Fluent Forms again.', 'fluentform') |
| 82 |
], 422); |
| 83 |
} |
| 84 |
|
| 85 |
$form = [ |
| 86 |
'title' => ArrayHelper::get($formItem, 'title'), |
| 87 |
'form_fields' => $formFields, |
| 88 |
'status' => ArrayHelper::get($formItem, 'status', 'published'), |
| 89 |
'has_payment' => ArrayHelper::get($formItem, 'has_payment', 0), |
| 90 |
'type' => ArrayHelper::get($formItem, 'type', 'form'), |
| 91 |
'created_by' => get_current_user_id() |
| 92 |
]; |
| 93 |
|
| 94 |
if( ArrayHelper::get($formItem, 'conditions')) { |
| 95 |
$form['conditions'] = ArrayHelper::get($formItem, 'conditions'); |
| 96 |
} |
| 97 |
|
| 98 |
if(isset($formItem['appearance_settings'])) { |
| 99 |
$form['appearance_settings'] = $formItem['appearance_settings']; |
| 100 |
} |
| 101 |
|
| 102 |
// Insert the form to the DB. |
| 103 |
$formId = wpFluent()->table('fluentform_forms')->insert($form); |
| 104 |
|
| 105 |
$insertedForms[$formId] = [ |
| 106 |
'title' => $form['title'], |
| 107 |
'edit_url' => admin_url('admin.php?page=fluent_forms&route=editor&form_id='.$formId) |
| 108 |
]; |
| 109 |
|
| 110 |
if(isset($formItem['metas'])) { |
| 111 |
|
| 112 |
foreach ($formItem['metas'] as $metaData) { |
| 113 |
$settings = [ |
| 114 |
'form_id' => $formId, |
| 115 |
'meta_key' => $metaData['meta_key'], |
| 116 |
'value' => $metaData['value'] |
| 117 |
]; |
| 118 |
wpFluent()->table('fluentform_form_meta')->insert($settings); |
| 119 |
} |
| 120 |
|
| 121 |
} else { |
| 122 |
$oldKeys = [ |
| 123 |
'formSettings', |
| 124 |
'notifications', |
| 125 |
'mailchimp_feeds', |
| 126 |
'slack' |
| 127 |
]; |
| 128 |
foreach ($oldKeys as $key) { |
| 129 |
if(isset($formItem[$key])) { |
| 130 |
$settings = [ |
| 131 |
'form_id' => $formId, |
| 132 |
'meta_key' => $key, |
| 133 |
'value' => json_encode($formItem[$key]) |
| 134 |
]; |
| 135 |
wpFluent()->table('fluentform_form_meta')->insert($settings); |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
do_action('fluentform_form_imported', $formId); |
| 141 |
|
| 142 |
} |
| 143 |
|
| 144 |
wp_send_json([ |
| 145 |
'message' => __('You form has been successfully imported.', 'fluentform'), |
| 146 |
'inserted_forms' => $insertedForms |
| 147 |
], 200); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
wp_send_json([ |
| 152 |
'message' => __('You have a faulty JSON file, please export the Fluent Forms again.', 'fluentform') |
| 153 |
], 422); |
| 154 |
} |
| 155 |
|
| 156 |
public function getFormMetas($formId) |
| 157 |
{ |
| 158 |
return wpFluent() |
| 159 |
->table('fluentform_form_meta') |
| 160 |
->select(['meta_key', 'value']) |
| 161 |
->where('form_id', $formId) |
| 162 |
->whereNotIn('meta_key', ['_total_views', '_ff_form_styler_css']) |
| 163 |
->get(); |
| 164 |
|
| 165 |
} |
| 166 |
} |