| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @copyright © Melograno Venture Studio. All rights reserved. |
| 5 |
* @licence See LICENCE.md for license details. |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace IvyForms\Common\Helpers; |
| 9 |
|
| 10 |
use IvyForms\Common\Sanitizer\Sanitizer; |
| 11 |
use IvyForms\Services\Translations\BackendStrings; |
| 12 |
|
| 13 |
/** |
| 14 |
* Maps form entity arrays to database row shapes for persistence. |
| 15 |
*/ |
| 16 |
class FormPersistenceHelper |
| 17 |
{ |
| 18 |
/** |
| 19 |
* Build the JSON settings blob stored in the forms.settings column. |
| 20 |
* |
| 21 |
* @param array<string, mixed> $data |
| 22 |
* @return array<string, mixed> |
| 23 |
*/ |
| 24 |
public static function buildSettingsBlob(array $data): array |
| 25 |
{ |
| 26 |
$settingsBlob = [ |
| 27 |
'showTitle' => (int) $data['showTitle'], |
| 28 |
'showDescription' => (int) $data['showDescription'], |
| 29 |
'storeEntries' => (int) $data['storeEntries'], |
| 30 |
'pages' => $data['pages'] ?? [], |
| 31 |
'progressIndicator' => $data['progressIndicator'] ?? [], |
| 32 |
'formActionButtons' => $data['formActionButtons'] ?? self::defaultFormActionButtons(), |
| 33 |
'paymentSettings' => Sanitizer::sanitizePaymentSettings($data['paymentSettings'] ?? null), |
| 34 |
]; |
| 35 |
|
| 36 |
/** |
| 37 |
* Merge form-level extension data into the settings JSON column. |
| 38 |
* |
| 39 |
* @param array<string, mixed> $settingsBlob Core keys written by Lite. |
| 40 |
* @param array<string, mixed> $data Full form array from entity->toArray(). |
| 41 |
*/ |
| 42 |
return apply_filters('ivyforms/form/persistence/settings_blob', $settingsBlob, $data); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Map form entity data to a database insert row. |
| 47 |
* |
| 48 |
* @param array<string, mixed> $data |
| 49 |
* @return array<string, mixed> |
| 50 |
*/ |
| 51 |
public static function buildInsertRow(array $data): array |
| 52 |
{ |
| 53 |
return [ |
| 54 |
'name' => $data['name'], |
| 55 |
'author' => wp_get_current_user()->display_name, |
| 56 |
'starred' => (int) $data['starred'], |
| 57 |
'published' => (int) $data['published'], |
| 58 |
'dateCreated' => current_time('mysql'), |
| 59 |
'dateEdited' => current_time('mysql'), |
| 60 |
'description' => $data['description'], |
| 61 |
'formType' => $data['formType'] ?? 'classic', |
| 62 |
'settings' => json_encode(self::buildSettingsBlob($data)), |
| 63 |
'integrationSettings' => isset($data['integrationSettings']) |
| 64 |
? json_encode($data['integrationSettings']) |
| 65 |
: json_encode([]), |
| 66 |
'styleSettings' => isset($data['styleSettings']) ? json_encode($data['styleSettings']) : null, |
| 67 |
]; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Map form entity data to a database update row. |
| 72 |
* |
| 73 |
* @param array<string, mixed> $data |
| 74 |
* @return array<string, mixed> |
| 75 |
*/ |
| 76 |
public static function buildUpdateRow(array $data): array |
| 77 |
{ |
| 78 |
return [ |
| 79 |
'name' => $data['name'], |
| 80 |
'starred' => (int) $data['starred'], |
| 81 |
'published' => (int) $data['published'], |
| 82 |
'description' => $data['description'], |
| 83 |
'formType' => $data['formType'] ?? 'classic', |
| 84 |
'dateEdited' => current_time('mysql'), |
| 85 |
'settings' => json_encode(self::buildSettingsBlob($data)), |
| 86 |
'integrationSettings' => isset($data['integrationSettings']) |
| 87 |
? json_encode($data['integrationSettings']) |
| 88 |
: json_encode([]), |
| 89 |
'styleSettings' => isset($data['styleSettings']) ? json_encode($data['styleSettings']) : null, |
| 90 |
]; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* @return array<string, array<string, string>> |
| 95 |
*/ |
| 96 |
private static function defaultFormActionButtons(): array |
| 97 |
{ |
| 98 |
return [ |
| 99 |
'submitButtonSettings' => [ |
| 100 |
'label' => BackendStrings::getCommonStrings()['submit'], |
| 101 |
'position' => 'default', |
| 102 |
], |
| 103 |
]; |
| 104 |
} |
| 105 |
} |
| 106 |
|