| 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\Factory\Form; |
| 9 |
|
| 10 |
use IvyForms\Common\Exceptions\ValidationException; |
| 11 |
use IvyForms\Common\Helpers\Helper; |
| 12 |
use IvyForms\Entity\Form\Form as FormEntity; |
| 13 |
use IvyForms\ValueObjects\Form\Form; |
| 14 |
use IvyForms\ValueObjects\Form\IntegrationSettings; |
| 15 |
use IvyForms\ValueObjects\Form\FormMetadata; |
| 16 |
use IvyForms\ValueObjects\Form\FormSettings; |
| 17 |
use IvyForms\ValueObjects\Form\StyleSettings; |
| 18 |
use IvyForms\Services\Translations\BackendStrings; |
| 19 |
|
| 20 |
/** |
| 21 |
* Class FormFactory |
| 22 |
* |
| 23 |
* @package IvyForms\Factory\Form |
| 24 |
*/ |
| 25 |
class FormFactory |
| 26 |
{ |
| 27 |
/** |
| 28 |
* @param array<string, mixed> $data |
| 29 |
* |
| 30 |
* @return FormEntity |
| 31 |
* |
| 32 |
* @throws ValidationException |
| 33 |
*/ |
| 34 |
public static function create(array $data): FormEntity |
| 35 |
{ |
| 36 |
$settings = isset($data['settings']) ? json_decode($data['settings'], true) : []; |
| 37 |
$integrationSettings = Helper::parseToArray($data['integrationSettings']); |
| 38 |
$pages = $data['pages'] ?? null; |
| 39 |
$progressIndicator = $data['progressIndicator'] ?? null; |
| 40 |
$rawStyleSettings = isset($data['styleSettings']) ? Helper::parseToArray($data['styleSettings']) : null; |
| 41 |
$styleSettings = new StyleSettings(is_array($rawStyleSettings) ? $rawStyleSettings : null); |
| 42 |
|
| 43 |
if (!empty($settings)) { |
| 44 |
$data['showTitle'] = $settings['showTitle'] ?? true; |
| 45 |
$data['showDescription'] = $settings['showDescription'] ?? false; |
| 46 |
$data['storeEntries'] = !isset($settings['storeEntries']) || (bool)$settings['storeEntries']; |
| 47 |
$data['formActionButtons'] = $settings['formActionButtons'] ?? [ |
| 48 |
'submitButtonSettings' => [ |
| 49 |
'label' => BackendStrings::getCommonStrings()['submit'], |
| 50 |
'position' => 'default' |
| 51 |
] |
| 52 |
]; |
| 53 |
|
| 54 |
// Prefer values from decoded settings, fallback to top-level API payload. |
| 55 |
$pages = array_key_exists('pages', $settings) |
| 56 |
? $settings['pages'] |
| 57 |
: ($data['pages'] ?? null); |
| 58 |
$progressIndicator = array_key_exists('progressIndicator', $settings) |
| 59 |
? $settings['progressIndicator'] |
| 60 |
: ($data['progressIndicator'] ?? null); |
| 61 |
} |
| 62 |
|
| 63 |
// Process extensible form options via one universal Pro hook. |
| 64 |
$formOptions = [ |
| 65 |
'pages' => $pages, |
| 66 |
'progressIndicator' => $progressIndicator, |
| 67 |
]; |
| 68 |
|
| 69 |
$formOptions = apply_filters('ivyforms/process/form_options', $formOptions, $data['id'] ?? 0); |
| 70 |
|
| 71 |
$data['pages'] = $formOptions['pages'] ?? null; |
| 72 |
$data['progressIndicator'] = $formOptions['progressIndicator'] ?? null; |
| 73 |
|
| 74 |
// Process form action buttons via Pro hook if available |
| 75 |
// This runs for both database-loaded and API-submitted formActionButtons |
| 76 |
if (isset($data['formActionButtons'])) { |
| 77 |
$data['formActionButtons'] = apply_filters( |
| 78 |
'ivyforms/process/form_action_buttons', |
| 79 |
$data['formActionButtons'], |
| 80 |
$data['id'] ?? 0 |
| 81 |
); |
| 82 |
} else { |
| 83 |
// Set default if not provided |
| 84 |
$data['formActionButtons'] = [ |
| 85 |
'submitButtonSettings' => [ |
| 86 |
'label' => BackendStrings::getCommonStrings()['submit'], |
| 87 |
'position' => 'default' |
| 88 |
] |
| 89 |
]; |
| 90 |
} |
| 91 |
|
| 92 |
if (isset($data['pages']) && is_array($data['pages'])) { |
| 93 |
$allFields = []; |
| 94 |
foreach ($data['pages'] as $page) { |
| 95 |
if (isset($page['fields']) && is_array($page['fields'])) { |
| 96 |
foreach ($page['fields'] as $field) { |
| 97 |
$allFields[] = $field; |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
if (!empty($allFields)) { |
| 102 |
$data['fields'] = $allFields; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
$formObject = new Form( |
| 107 |
$data['id'] ?? 0, |
| 108 |
$data['name'] ?? '', |
| 109 |
$data['description'] ?? '', |
| 110 |
new FormMetadata( |
| 111 |
$data['author'] ?? '', |
| 112 |
$data['starred'] ?? false, |
| 113 |
$data['published'] ?? true, |
| 114 |
isset($data['dateCreated']) ? (string)$data['dateCreated'] : null, |
| 115 |
isset($data['dateEdited']) ? (string)$data['dateEdited'] : null |
| 116 |
), |
| 117 |
new FormSettings( |
| 118 |
$data['showTitle'] ?? true, |
| 119 |
$data['showDescription'] ?? false, |
| 120 |
isset($data['storeEntries']) ? (bool)$data['storeEntries'] : true |
| 121 |
), |
| 122 |
$data['fields'] ?? [], |
| 123 |
new IntegrationSettings($integrationSettings), |
| 124 |
$data['pages'] ?? null, |
| 125 |
$data['progressIndicator'] ?? null, |
| 126 |
$styleSettings, |
| 127 |
$data['formActionButtons'] ?? null |
| 128 |
); |
| 129 |
|
| 130 |
$form = new FormEntity($formObject); |
| 131 |
|
| 132 |
if (isset($data['id'])) { |
| 133 |
$form->setId($data['id']); |
| 134 |
} |
| 135 |
|
| 136 |
if (isset($data['name'])) { |
| 137 |
$form->setName($data['name']); |
| 138 |
} |
| 139 |
|
| 140 |
if (isset($data['author'])) { |
| 141 |
$form->setAuthor($data['author']); |
| 142 |
} |
| 143 |
|
| 144 |
if (isset($data['starred'])) { |
| 145 |
$form->setStarred($data['starred']); |
| 146 |
} |
| 147 |
|
| 148 |
if (isset($data['published'])) { |
| 149 |
$form->setPublished($data['published']); |
| 150 |
} |
| 151 |
|
| 152 |
if (isset($data['description'])) { |
| 153 |
$form->setDescription($data['description']); |
| 154 |
} |
| 155 |
|
| 156 |
if (isset($data['fields'])) { |
| 157 |
$form->setFields($data['fields']); |
| 158 |
} |
| 159 |
|
| 160 |
if (isset($data['showTitle'])) { |
| 161 |
$form->setTitleVisible($data['showTitle']); |
| 162 |
} |
| 163 |
|
| 164 |
if (isset($data['showDescription'])) { |
| 165 |
$form->setDescriptionVisible($data['showDescription']); |
| 166 |
} |
| 167 |
|
| 168 |
if (isset($data['storeEntries'])) { |
| 169 |
$form->setStoreEntries($data['storeEntries']); |
| 170 |
} |
| 171 |
|
| 172 |
if (isset($data['pages'])) { |
| 173 |
$form->setPages($data['pages']); |
| 174 |
} |
| 175 |
|
| 176 |
if (isset($data['progressIndicator'])) { |
| 177 |
$form->setProgressIndicator($data['progressIndicator']); |
| 178 |
} |
| 179 |
|
| 180 |
if (isset($data['formActionButtons'])) { |
| 181 |
$form->setFormActionButtons($data['formActionButtons']); |
| 182 |
} |
| 183 |
|
| 184 |
return $form; |
| 185 |
} |
| 186 |
} |
| 187 |
|