PluginProbe
The Innovative Form Builder – IvyForms / 1.4
The Innovative Form Builder – IvyForms v1.4
1.5.1 1.5 1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 All 27 releases
ivyforms / backend / src / Common / Helpers / McpHelpers / McpFormCreateHelper.php

McpFormCreateHelper.php in The Innovative Form Builder – IvyForms 1.4, at backend/src/Common/Helpers/McpHelpers/McpFormCreateHelper.php

140 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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