PluginProbe
The Innovative Form Builder – IvyForms / 1.5
The Innovative Form Builder – IvyForms v1.5
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 / McpFieldAddHelper.php

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

165 lines 5.5 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 * Add-field logic for IvyForms MCP field abilities.
14 */
15 class McpFieldAddHelper
16 {
17 /**
18 * @param array<string, mixed> $input
19 * @return array<string, mixed>
20 */
21 public static function addFormField(array $input): array
22 {
23 $strings = BackendStrings::getMcpFieldStrings();
24 $formId = (int) ($input['formId'] ?? 0);
25 if ($formId <= 0) {
26 return self::singleFieldFailure(0, BackendStrings::getExceptionStrings()['invalid_form_id']);
27 }
28
29 $loaded = self::loadFormForMutation($formId);
30 if (isset($loaded['error'])) {
31 return self::singleFieldFailure($formId, $loaded['error']);
32 }
33
34 /** @var array<string, mixed> $form */
35 $form = $loaded['form'];
36 $appendResult = McpFieldAbilitiesHelper::appendFieldsToForm([$input], $formId, $form);
37 if (!$appendResult['success']) {
38 return self::singleFieldFailure($formId, $appendResult['message']);
39 }
40
41 $form['fields'] = $appendResult['fields'];
42 $result = McpAbilitiesHelper::restRequest('POST', '/form/update/' . $formId, $form);
43 $success = McpAbilitiesHelper::isRestMutationSuccessful($result);
44 $label = $appendResult['labels'][0] ?? $strings['field'];
45
46 return [
47 'formId' => $formId,
48 'success' => $success,
49 'message' => $success
50 ? sprintf($strings['field_added_successfully_to_form'], $label, $formId)
51 : McpAbilitiesHelper::restMutationErrorMessage($result, $strings['failed_to_add_field']),
52 'adminLink' => McpAbilitiesHelper::adminFormUrl($formId),
53 ];
54 }
55
56 /**
57 * @param array<string, mixed> $input
58 * @return array<string, mixed>
59 */
60 public static function addFormFields(array $input): array
61 {
62 $strings = BackendStrings::getMcpFieldStrings();
63 $formId = (int) ($input['formId'] ?? 0);
64 $fieldInputs = $input['fields'] ?? [];
65
66 if ($formId <= 0) {
67 return self::multiFieldFailure(0, BackendStrings::getExceptionStrings()['invalid_form_id']);
68 }
69
70 if (!is_array($fieldInputs) || $fieldInputs === []) {
71 return self::multiFieldFailure(
72 $formId,
73 $strings['at_least_one_field_definition_required']
74 );
75 }
76
77 $loaded = self::loadFormForMutation($formId);
78 if (isset($loaded['error'])) {
79 return self::multiFieldFailure($formId, $loaded['error']);
80 }
81
82 /** @var array<string, mixed> $form */
83 $form = $loaded['form'];
84 $appendResult = McpFieldAbilitiesHelper::appendFieldsToForm($fieldInputs, $formId, $form);
85 if (!$appendResult['success']) {
86 return self::multiFieldFailure($formId, $appendResult['message']);
87 }
88
89 $form['fields'] = $appendResult['fields'];
90 $result = McpAbilitiesHelper::restRequest('POST', '/form/update/' . $formId, $form);
91 $success = McpAbilitiesHelper::isRestMutationSuccessful($result);
92
93 return [
94 'formId' => $formId,
95 'success' => $success,
96 'message' => $success
97 ? sprintf($strings['added_fields_successfully'], count($appendResult['labels']))
98 : McpAbilitiesHelper::restMutationErrorMessage(
99 $result,
100 $strings['failed_to_add_fields']
101 ),
102 'addedFields' => $appendResult['labels'],
103 'adminLink' => McpAbilitiesHelper::adminFormUrl($formId),
104 ];
105 }
106
107 /**
108 * @return array{form?: array<string, mixed>, error?: string}
109 */
110 private static function loadFormForMutation(int $formId): array
111 {
112 $exceptionStrings = BackendStrings::getExceptionStrings();
113 $formData = McpAbilitiesHelper::restRequest('GET', '/form/' . $formId);
114
115 // Root REST error envelopes (code/status) must not be treated as form payloads.
116 if (!McpAbilitiesHelper::isRestMutationSuccessful($formData)) {
117 return [
118 'error' => McpAbilitiesHelper::restMutationErrorMessage(
119 $formData,
120 $exceptionStrings['form_not_found']
121 ),
122 ];
123 }
124
125 $form = McpAbilitiesHelper::extractIvyFormsRestPayload($formData);
126 if (!is_array($form) || $form === [] || !isset($form['id'])) {
127 return [
128 'error' => sprintf(
129 BackendStrings::getMcpMultipageStrings()['form_not_found_with_id'],
130 $formId
131 ),
132 ];
133 }
134
135 return ['form' => $form];
136 }
137
138 /**
139 * @return array<string, mixed>
140 */
141 private static function singleFieldFailure(int $formId, string $message): array
142 {
143 return [
144 'formId' => $formId,
145 'success' => false,
146 'message' => $message,
147 'adminLink' => '',
148 ];
149 }
150
151 /**
152 * @return array<string, mixed>
153 */
154 private static function multiFieldFailure(int $formId, string $message): array
155 {
156 return [
157 'formId' => $formId,
158 'success' => false,
159 'message' => $message,
160 'addedFields' => [],
161 'adminLink' => '',
162 ];
163 }
164 }
165