| 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 |
* Create-form logic for IvyForms MCP abilities. |
| 14 |
*/ |
| 15 |
class McpFormCreateHelper |
| 16 |
{ |
| 17 |
/** |
| 18 |
* @param array<string, mixed> $input |
| 19 |
* @return array<string, mixed> |
| 20 |
*/ |
| 21 |
public static function createForm(array $input): array |
| 22 |
{ |
| 23 |
$data = McpAbilitiesHelper::restRequest( |
| 24 |
'POST', |
| 25 |
'/form/add', |
| 26 |
['template_id' => self::resolveTemplateId($input)] |
| 27 |
); |
| 28 |
$strings = BackendStrings::getAllFormsStrings(); |
| 29 |
$failedMessage = $strings['failed_to_create_form']; |
| 30 |
|
| 31 |
if (!McpAbilitiesHelper::isRestMutationSuccessful($data)) { |
| 32 |
$message = McpAbilitiesHelper::restMutationErrorMessage($data, $failedMessage); |
| 33 |
|
| 34 |
return self::createFormFailure($message, $data); |
| 35 |
} |
| 36 |
|
| 37 |
return self::buildCreatedFormResult($data, $strings['form_created'], $failedMessage); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Map common natural-language / alias template ids to canonical IvyForms ids. |
| 42 |
*/ |
| 43 |
public static function normalizeTemplateId(string $rawTemplateId): string |
| 44 |
{ |
| 45 |
$normalized = strtolower(trim($rawTemplateId)); |
| 46 |
$normalized = (string) preg_replace('/[\s\-]+/', '_', $normalized); |
| 47 |
$normalized = (string) preg_replace('/_+/', '_', $normalized); |
| 48 |
$normalized = trim($normalized, '_'); |
| 49 |
|
| 50 |
$aliases = [ |
| 51 |
'blank' => 'blank_form', |
| 52 |
'blank_form' => 'blank_form', |
| 53 |
'empty' => 'blank_form', |
| 54 |
'empty_form' => 'blank_form', |
| 55 |
'scratch' => 'blank_form', |
| 56 |
'start_from_scratch' => 'blank_form', |
| 57 |
'new' => 'blank_form', |
| 58 |
'new_form' => 'blank_form', |
| 59 |
'contact' => 'contact_form', |
| 60 |
'contact_form' => 'contact_form', |
| 61 |
'contact_us' => 'contact_form', |
| 62 |
'contactus' => 'contact_form', |
| 63 |
]; |
| 64 |
|
| 65 |
return $aliases[$normalized] ?? $normalized; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* @param array<string, mixed> $input |
| 70 |
*/ |
| 71 |
private static function resolveTemplateId(array $input): string |
| 72 |
{ |
| 73 |
$candidate = $input['templateId'] ?? $input['template_id'] ?? 'blank_form'; |
| 74 |
if (!is_scalar($candidate)) { |
| 75 |
$candidate = 'blank_form'; |
| 76 |
} |
| 77 |
|
| 78 |
return self::normalizeTemplateId(sanitize_text_field((string) $candidate)); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* @param mixed $raw |
| 83 |
* @return array<string, mixed> |
| 84 |
*/ |
| 85 |
private static function createFormFailure(string $message, $raw): array |
| 86 |
{ |
| 87 |
return [ |
| 88 |
'error' => $message, |
| 89 |
'raw' => $raw, |
| 90 |
'formId' => null, |
| 91 |
'message' => $message, |
| 92 |
]; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* @param mixed $data |
| 97 |
* @return array<string, mixed> |
| 98 |
*/ |
| 99 |
private static function buildCreatedFormResult($data, string $successMessage, string $failedMessage): array |
| 100 |
{ |
| 101 |
$created = McpAbilitiesHelper::extractIvyFormsRestPayload($data); |
| 102 |
if (!is_array($created)) { |
| 103 |
return self::createFormFailure($failedMessage, $data); |
| 104 |
} |
| 105 |
|
| 106 |
$payloadError = self::createdPayloadErrorMessage($created, $failedMessage); |
| 107 |
if ($payloadError !== null) { |
| 108 |
return self::createFormFailure($payloadError, $created); |
| 109 |
} |
| 110 |
|
| 111 |
$formId = isset($created['id']) ? (int) $created['id'] : 0; |
| 112 |
if ($formId <= 0) { |
| 113 |
return self::createFormFailure($failedMessage, $created); |
| 114 |
} |
| 115 |
|
| 116 |
return [ |
| 117 |
'formId' => $formId, |
| 118 |
'adminLink' => McpAbilitiesHelper::adminFormUrl($formId), |
| 119 |
'message' => $successMessage, |
| 120 |
'raw' => $created, |
| 121 |
]; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* @param array<string, mixed> $created |
| 126 |
*/ |
| 127 |
private static function createdPayloadErrorMessage(array $created, string $failedMessage): ?string |
| 128 |
{ |
| 129 |
if (!isset($created['error'])) { |
| 130 |
return null; |
| 131 |
} |
| 132 |
|
| 133 |
if (is_string($created['error']) && $created['error'] !== '') { |
| 134 |
return $created['error']; |
| 135 |
} |
| 136 |
|
| 137 |
return $failedMessage; |
| 138 |
} |
| 139 |
} |
| 140 |
|