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 / McpFormSettingsHelper.php

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

200 lines 6.0 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 * Update-form-settings logic for IvyForms MCP abilities.
14 */
15 class McpFormSettingsHelper
16 {
17 /**
18 * @param array<string, mixed> $input
19 * @return array<string, mixed>
20 */
21 public static function updateFormSettings(array $input): array
22 {
23 $formId = (int) ($input['formId'] ?? 0);
24 if ($formId <= 0) {
25 return ['error' => BackendStrings::getExceptionStrings()['invalid_form_id']];
26 }
27
28 $loaded = self::loadFormForSettingsUpdate($formId);
29 if (isset($loaded['error'])) {
30 return ['error' => $loaded['error']];
31 }
32
33 $merged = self::mergeValidatedSettings($loaded['form'], self::aliasTitleToName($input));
34 if ($merged['error'] !== null) {
35 return ['error' => $merged['error']];
36 }
37
38 $form = $merged['form'];
39 [$success, $result] = self::persistFormSettingsUpdate($formId, $form);
40
41 return [
42 'formId' => $formId,
43 'updated' => $success,
44 'name' => self::formName($form),
45 'adminLink' => McpAbilitiesHelper::adminFormUrl($formId),
46 'raw' => $result,
47 ];
48 }
49
50 /**
51 * @param array<string, mixed> $input
52 * @return array<string, mixed>
53 */
54 private static function aliasTitleToName(array $input): array
55 {
56 if (!array_key_exists('name', $input) && array_key_exists('title', $input)) {
57 $input['name'] = $input['title'];
58 }
59
60 return $input;
61 }
62
63 /**
64 * @return array{form?: array<string, mixed>, error?: string}
65 */
66 private static function loadFormForSettingsUpdate(int $formId): array
67 {
68 $exceptionStrings = BackendStrings::getExceptionStrings();
69 $formData = McpAbilitiesHelper::restRequest('GET', '/form/' . $formId);
70
71 // Root REST error envelopes (code/status) must not be treated as form payloads.
72 if (!McpAbilitiesHelper::isRestMutationSuccessful($formData)) {
73 return [
74 'error' => McpAbilitiesHelper::restMutationErrorMessage(
75 $formData,
76 $exceptionStrings['form_not_found']
77 ),
78 ];
79 }
80
81 $form = McpAbilitiesHelper::extractIvyFormsRestPayload($formData);
82 if (!is_array($form) || $form === [] || !isset($form['id'])) {
83 return [
84 'error' => sprintf(
85 BackendStrings::getMcpMultipageStrings()['form_not_found_with_id'],
86 $formId
87 ),
88 ];
89 }
90
91 return ['form' => $form];
92 }
93
94 /**
95 * @param array<string, mixed> $form
96 * @param array<string, mixed> $input
97 * @return array{form: array<string, mixed>, error: string|null}
98 */
99 private static function mergeValidatedSettings(array $form, array $input): array
100 {
101 foreach (['name' => 255, 'description' => 1000] as $key => $maxLength) {
102 $merge = self::mergeValidatedTextSetting($form, $input, $key, $maxLength);
103 if ($merge['error'] !== null) {
104 return $merge;
105 }
106 $form = $merge['form'];
107 }
108
109 return [
110 'form' => self::mergeValidatedBooleanSettings($form, $input),
111 'error' => null,
112 ];
113 }
114
115 /**
116 * @param array<string, mixed> $form
117 * @param array<string, mixed> $input
118 * @return array{form: array<string, mixed>, error: string|null}
119 */
120 private static function mergeValidatedTextSetting(
121 array $form,
122 array $input,
123 string $key,
124 int $maxLength
125 ): array {
126 if (!array_key_exists($key, $input)) {
127 return ['form' => $form, 'error' => null];
128 }
129
130 if (!is_string($input[$key])) {
131 return [
132 'form' => $form,
133 'error' => BackendStrings::getExceptionStrings()['invalid_request_data'],
134 ];
135 }
136
137 $value = sanitize_text_field($input[$key]);
138 if (strlen($value) > $maxLength) {
139 return [
140 'form' => $form,
141 'error' => sprintf(
142 /* translators: 1: Field name, 2: Max length. */
143 __('%1$s must be at most %2$d characters.', 'ivyforms'),
144 $key,
145 $maxLength
146 ),
147 ];
148 }
149
150 $form[$key] = $value;
151
152 return ['form' => $form, 'error' => null];
153 }
154
155 /**
156 * @param array<string, mixed> $form
157 * @param array<string, mixed> $input
158 * @return array<string, mixed>
159 */
160 private static function mergeValidatedBooleanSettings(array $form, array $input): array
161 {
162 foreach (['published', 'showTitle', 'showDescription', 'storeEntries'] as $boolKey) {
163 if (!array_key_exists($boolKey, $input) || !is_bool($input[$boolKey])) {
164 continue;
165 }
166 $form[$boolKey] = $input[$boolKey];
167 }
168
169 return $form;
170 }
171
172 /**
173 * @param array<string, mixed> $form
174 */
175 private static function formName(array $form): ?string
176 {
177 return is_string($form['name'] ?? null) ? $form['name'] : null;
178 }
179
180 /**
181 * @param array<string, mixed> $form
182 * @return array{0: bool, 1: mixed}
183 */
184 private static function persistFormSettingsUpdate(int $formId, array $form): array
185 {
186 try {
187 $result = McpAbilitiesHelper::restRequest('POST', '/form/update/' . $formId, $form);
188 $success = McpAbilitiesHelper::isRestMutationSuccessful($result);
189 } catch (\Throwable $exception) {
190 $result = [
191 'code' => 'exception',
192 'message' => $exception->getMessage(),
193 ];
194 $success = false;
195 }
196
197 return [$success, $result];
198 }
199 }
200