| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Services\Form; |
| 4 |
|
| 5 |
use FluentForm\App\Models\Form; |
| 6 |
use FluentForm\Framework\Helpers\ArrayHelper as Arr; |
| 7 |
|
| 8 |
class Duplicator |
| 9 |
{ |
| 10 |
public function duplicateFormMeta(Form $form, Form $existingForm) |
| 11 |
{ |
| 12 |
$extras = []; |
| 13 |
|
| 14 |
foreach ($existingForm->formMeta as $meta) { |
| 15 |
if ('notifications' == $meta->meta_key || '_pdf_feeds' == $meta->meta_key) { |
| 16 |
$extras[$meta->meta_key][] = $meta; |
| 17 |
|
| 18 |
continue; |
| 19 |
} |
| 20 |
if ("ffc_form_settings_generated_css" == $meta->meta_key || "ffc_form_settings_meta" == $meta->meta_key) { |
| 21 |
$meta->value = str_replace('ff_conv_app_' . $existingForm->id, 'ff_conv_app_' . $form->id, $meta->value); |
| 22 |
} |
| 23 |
|
| 24 |
$form->formMeta()->create([ |
| 25 |
'meta_key' => $meta->meta_key, |
| 26 |
'value' => $meta->value, |
| 27 |
]); |
| 28 |
} |
| 29 |
|
| 30 |
$pdfFeedMap = $this->getPdfFeedMap($form, $extras); |
| 31 |
|
| 32 |
if (array_key_exists('notifications', $extras)) { |
| 33 |
$extras = $this->notificationWithPdfMap($extras, $pdfFeedMap); |
| 34 |
|
| 35 |
foreach ($extras['notifications'] as $notify) { |
| 36 |
$notifyData = [ |
| 37 |
'meta_key' => $notify->meta_key, |
| 38 |
'value' => $notify->value, |
| 39 |
]; |
| 40 |
|
| 41 |
$form->formMeta()->create($notifyData); |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
public function maybeDuplicateFiles($form, $existingForm, $data) |
| 48 |
{ |
| 49 |
if ( |
| 50 |
isset($data['form_fields']) && |
| 51 |
$formFields = \json_decode($data['form_fields'], true) |
| 52 |
) { |
| 53 |
$fields = Arr::get($formFields, 'fields', []); |
| 54 |
foreach ($fields as $field) { |
| 55 |
if ( |
| 56 |
"chained_select" === $field['element'] && |
| 57 |
"file" === Arr::get($field, 'settings.data_source.type', '') && |
| 58 |
$metaKey = Arr::get($field, 'settings.data_source.meta_key', '') |
| 59 |
) { |
| 60 |
// duplicate csv file for chained select field, if uploaded. |
| 61 |
$path = wp_upload_dir()['basedir'] . FLUENTFORM_UPLOAD_DIR; |
| 62 |
$target = $path . '/' . $metaKey . '_' . $existingForm->id . '.csv'; |
| 63 |
if (file_exists($target)) { |
| 64 |
copy($target, $path . '/' . $metaKey . '_' . $form->id . '.csv'); |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Map pdf feed ID to replace with duplicated PDF feed ID when duplicating form |
| 73 |
* |
| 74 |
* @param \FluentForm\App\Models\Form $form |
| 75 |
* @param array $formMeta |
| 76 |
* @return array $pdfFeedMap |
| 77 |
*/ |
| 78 |
private function getPdfFeedMap(Form $form, $formMeta) |
| 79 |
{ |
| 80 |
$pdfFeedMap = []; |
| 81 |
|
| 82 |
if (array_key_exists('_pdf_feeds', $formMeta)) { |
| 83 |
foreach ($formMeta['_pdf_feeds'] as $pdf_feed) { |
| 84 |
$pdfData = [ |
| 85 |
'meta_key' => $pdf_feed->meta_key, |
| 86 |
'value' => $pdf_feed->value, |
| 87 |
'form_id' => $form->id, |
| 88 |
]; |
| 89 |
$pdfFeedMap[$pdf_feed->id] = $form->formMeta()->insertGetId($pdfData); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
return $pdfFeedMap; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Map notification data with PDF feed map |
| 98 |
* |
| 99 |
* @param array $formMeta |
| 100 |
* @param array $pdfFeedMap |
| 101 |
* @return array $formMeta |
| 102 |
*/ |
| 103 |
private function notificationWithPdfMap($formMeta, $pdfFeedMap) |
| 104 |
{ |
| 105 |
foreach ($formMeta['notifications'] as $key => $notification) { |
| 106 |
$notificationValue = json_decode($notification->value); |
| 107 |
$pdf_attachments = []; |
| 108 |
$hasPdfAttachments = isset($notificationValue->pdf_attachments) && count($notificationValue->pdf_attachments); |
| 109 |
|
| 110 |
if ($hasPdfAttachments) { |
| 111 |
foreach ($notificationValue->pdf_attachments as $attachment) { |
| 112 |
$pdf_attachments[] = json_encode($pdfFeedMap[$attachment]); |
| 113 |
} |
| 114 |
} |
| 115 |
$notificationValue->pdf_attachments = $pdf_attachments; |
| 116 |
$notification->value = json_encode($notificationValue); |
| 117 |
|
| 118 |
$formMeta['notifications'][$key] = $notification; |
| 119 |
} |
| 120 |
|
| 121 |
return $formMeta; |
| 122 |
} |
| 123 |
} |
| 124 |
|