| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @copyright © Melograno Venture Studio. All rights reserved. |
| 5 |
* @licence See COPYING.md for license details. |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace IvyForms\Common\Helpers\McpHelpers; |
| 9 |
|
| 10 |
use IvyForms\Services\Translations\BackendStrings; |
| 11 |
|
| 12 |
/** |
| 13 |
* REST persistence helpers for IvyForms MCP multipage operations. |
| 14 |
*/ |
| 15 |
class McpMultiPagePersistenceHelper |
| 16 |
{ |
| 17 |
/** |
| 18 |
* @return array<string, mixed>|null Error response when formId or Pro check fails. |
| 19 |
*/ |
| 20 |
public static function validateFormIdAndPro(int $formId): ?array |
| 21 |
{ |
| 22 |
if ($formId <= 0) { |
| 23 |
return self::errorResponse(0, self::multipageStrings()['valid_form_id_required']); |
| 24 |
} |
| 25 |
|
| 26 |
return McpMultiPageHelper::proRequiredError($formId); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* @return array<string, mixed>|null |
| 31 |
*/ |
| 32 |
public static function loadFormOrNull(int $formId): ?array |
| 33 |
{ |
| 34 |
$formData = McpAbilitiesHelper::restRequest('GET', '/form/' . $formId); |
| 35 |
$form = McpAbilitiesHelper::extractIvyFormsRestPayload($formData); |
| 36 |
|
| 37 |
return is_array($form) && $form !== [] ? $form : null; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* @param array<string, mixed> $form |
| 42 |
* @return array<string, mixed> |
| 43 |
*/ |
| 44 |
public static function persistForm(int $formId, array $form, string $successMessage): array |
| 45 |
{ |
| 46 |
$result = McpAbilitiesHelper::restRequest('POST', '/form/update/' . $formId, $form); |
| 47 |
$success = McpAbilitiesHelper::isRestMutationSuccessful($result); |
| 48 |
|
| 49 |
$updatedForm = self::loadFormOrNull($formId); |
| 50 |
|
| 51 |
$pages = is_array($updatedForm) && is_array($updatedForm['pages'] ?? null) |
| 52 |
? $updatedForm['pages'] |
| 53 |
: []; |
| 54 |
|
| 55 |
$message = $success |
| 56 |
? $successMessage |
| 57 |
: McpAbilitiesHelper::restMutationErrorMessage( |
| 58 |
$result, |
| 59 |
self::multipageStrings()['failed_update_multipage_form'] |
| 60 |
); |
| 61 |
|
| 62 |
return [ |
| 63 |
'formId' => $formId, |
| 64 |
'success' => $success, |
| 65 |
'message' => $message, |
| 66 |
'pages' => McpMultiPagePagesHelper::stripFieldsFromPages($pages), |
| 67 |
'adminLink' => McpAbilitiesHelper::adminFormUrl($formId), |
| 68 |
]; |
| 69 |
} |
| 70 |
|
| 71 |
public static function formNotFoundMessage(int $formId): string |
| 72 |
{ |
| 73 |
return sprintf(self::multipageStrings()['form_not_found_with_id'], $formId); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* @return array<string, mixed> |
| 78 |
*/ |
| 79 |
public static function errorResponse(int $formId, string $message): array |
| 80 |
{ |
| 81 |
return [ |
| 82 |
'formId' => $formId, |
| 83 |
'success' => false, |
| 84 |
'message' => $message, |
| 85 |
'pages' => [], |
| 86 |
'adminLink' => $formId > 0 ? McpAbilitiesHelper::adminFormUrl($formId) : '', |
| 87 |
]; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* @return array<string, string> |
| 92 |
*/ |
| 93 |
private static function multipageStrings(): array |
| 94 |
{ |
| 95 |
/** @var array<string, string> */ |
| 96 |
return BackendStrings::getMcpMultipageStrings(); |
| 97 |
} |
| 98 |
} |
| 99 |
|