| 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 |
/** |
| 11 |
* Request/response helpers for IvyForms MCP field reorder operations. |
| 12 |
*/ |
| 13 |
class McpFieldReorderSupportHelper |
| 14 |
{ |
| 15 |
/** |
| 16 |
* Returns reorder context or an error response (same shape as {@see McpFieldReorderHelper::reorderError}). |
| 17 |
* |
| 18 |
* @param array<string, mixed> $input |
| 19 |
* @return array<string, mixed> |
| 20 |
*/ |
| 21 |
public static function contextOrError(array $input): array |
| 22 |
{ |
| 23 |
$formId = (int) ($input['formId'] ?? 0); |
| 24 |
$fieldUpdates = $input['fields'] ?? []; |
| 25 |
|
| 26 |
if ($formId <= 0) { |
| 27 |
return McpFieldReorderHelper::reorderError( |
| 28 |
$formId, |
| 29 |
__('A valid form ID is required.', 'ivyforms') |
| 30 |
); |
| 31 |
} |
| 32 |
|
| 33 |
if (!is_array($fieldUpdates) || $fieldUpdates === []) { |
| 34 |
return McpFieldReorderHelper::reorderError( |
| 35 |
$formId, |
| 36 |
__('At least one field update is required.', 'ivyforms') |
| 37 |
); |
| 38 |
} |
| 39 |
|
| 40 |
$formData = McpAbilitiesHelper::restRequest('GET', '/form/' . $formId); |
| 41 |
$form = McpAbilitiesHelper::extractIvyFormsRestPayload($formData); |
| 42 |
|
| 43 |
if (!is_array($form) || $form === []) { |
| 44 |
return McpFieldReorderHelper::reorderError($formId, "Form {$formId} not found."); |
| 45 |
} |
| 46 |
|
| 47 |
$existingFields = $form['fields'] ?? []; |
| 48 |
$updatesById = McpFieldReorderHelper::reorderNormalizeUpdates($existingFields, $fieldUpdates); |
| 49 |
|
| 50 |
if ($updatesById === []) { |
| 51 |
return McpFieldReorderHelper::reorderError( |
| 52 |
$formId, |
| 53 |
__('No valid field updates were provided.', 'ivyforms') |
| 54 |
); |
| 55 |
} |
| 56 |
|
| 57 |
return [ |
| 58 |
'success' => true, |
| 59 |
'formId' => $formId, |
| 60 |
'form' => $form, |
| 61 |
'existingFields' => $existingFields, |
| 62 |
'updatesById' => $updatesById, |
| 63 |
]; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Builds final reorder response from REST update result. |
| 68 |
* |
| 69 |
* @param mixed $result REST response |
| 70 |
* @return array<string, mixed> |
| 71 |
*/ |
| 72 |
public static function resultFromRest(int $formId, $result, int $updatedFieldsCount): array |
| 73 |
{ |
| 74 |
$success = McpAbilitiesHelper::isRestMutationSuccessful($result); |
| 75 |
|
| 76 |
$message = __('Field order updated successfully.', 'ivyforms'); |
| 77 |
if (!$success) { |
| 78 |
$message = McpAbilitiesHelper::restMutationErrorMessage( |
| 79 |
$result, |
| 80 |
__('Failed to update field order.', 'ivyforms') |
| 81 |
); |
| 82 |
} |
| 83 |
|
| 84 |
return [ |
| 85 |
'formId' => $formId, |
| 86 |
'success' => $success, |
| 87 |
'message' => $message, |
| 88 |
'updatedFieldsCount' => $updatedFieldsCount, |
| 89 |
'adminLink' => McpAbilitiesHelper::adminFormUrl($formId), |
| 90 |
]; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* @param array<int, array<string, int|string|null>> $updatesById |
| 95 |
* @param array<string, mixed> $form |
| 96 |
* @return array<int, array<string, int|string|null>>|array<string, mixed> |
| 97 |
*/ |
| 98 |
public static function validatePageIds(array $updatesById, array $form, int $formId): array |
| 99 |
{ |
| 100 |
$validated = []; |
| 101 |
|
| 102 |
foreach ($updatesById as $fieldId => $update) { |
| 103 |
if (!is_array($update)) { |
| 104 |
continue; |
| 105 |
} |
| 106 |
|
| 107 |
$pageId = $update['pageId'] ?? null; |
| 108 |
if ($pageId !== null && $pageId !== '') { |
| 109 |
$normalizedPageId = McpMultiPageHelper::normalizePageIdForForm((string) $pageId, $form); |
| 110 |
if ($normalizedPageId === null) { |
| 111 |
return McpFieldReorderHelper::reorderError( |
| 112 |
$formId, |
| 113 |
sprintf( |
| 114 |
__('Invalid pageId "%s" for this form.', 'ivyforms'), |
| 115 |
sanitize_text_field((string) $pageId) |
| 116 |
) |
| 117 |
); |
| 118 |
} |
| 119 |
$update['pageId'] = $normalizedPageId; |
| 120 |
} |
| 121 |
|
| 122 |
$validated[$fieldId] = $update; |
| 123 |
} |
| 124 |
|
| 125 |
return $validated; |
| 126 |
} |
| 127 |
} |
| 128 |
|