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

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

253 lines 6.9 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 /**
11 * Page structure helpers for IvyForms MCP multipage operations.
12 */
13 class McpMultiPagePagesHelper
14 {
15 /**
16 * @param array<int, mixed> $pageInputs
17 * @return array<int, array<string, mixed>>
18 */
19 public static function buildPagesFromInput(array $pageInputs): array
20 {
21 $pages = [];
22
23 foreach (array_values($pageInputs) as $index => $pageInput) {
24 if (!is_array($pageInput)) {
25 continue;
26 }
27
28 $label = sanitize_text_field((string) ($pageInput['label'] ?? sprintf('Page %d', $index + 1)));
29 if ($label === '') {
30 $label = sprintf('Page %d', $index + 1);
31 }
32
33 $pageId = sanitize_text_field((string) ($pageInput['id'] ?? sprintf('page_%d', $index + 1)));
34 if ($pageId === '') {
35 $pageId = sprintf('page_%d', $index + 1);
36 }
37
38 $pages[] = [
39 'id' => $pageId,
40 'label' => $label,
41 'closable' => $index > 0 ? (bool) ($pageInput['closable'] ?? true) : false,
42 'fields' => [],
43 ];
44 }
45
46 return $pages;
47 }
48
49 /**
50 * @param array<string, mixed> $form
51 * @param array<string, mixed> $input
52 * @return array<string, mixed>
53 */
54 public static function mergeProgressIndicator(array $form, array $input): array
55 {
56 if (isset($input['progressIndicator']) && is_array($input['progressIndicator'])) {
57 $form['progressIndicator'] = $input['progressIndicator'];
58
59 return $form;
60 }
61
62 if (!isset($form['progressIndicator']) || !is_array($form['progressIndicator'])) {
63 $form['progressIndicator'] = McpMultiPageHelper::defaultProgressIndicator();
64 }
65
66 return $form;
67 }
68
69 /**
70 * @return array<int, array<string, mixed>>
71 */
72 public static function defaultFirstPage(): array
73 {
74 return [[
75 'id' => 'page_1',
76 'label' => 'Page 1',
77 'closable' => false,
78 ]];
79 }
80
81 /**
82 * @param array<string, mixed> $result
83 * @return array<string, mixed>
84 */
85 public static function enrichResultWithAddedPage(array $result, string $label): array
86 {
87 if (empty($result['success']) || !is_array($result['pages'] ?? null)) {
88 return $result;
89 }
90
91 $addedPage = self::findPageByLabel($result['pages'], $label);
92 if ($addedPage !== null) {
93 $result['addedPage'] = $addedPage;
94 }
95
96 return $result;
97 }
98
99 /**
100 * @param array<int, array<string, mixed>> $pages
101 * @return array<int, string>
102 */
103 public static function collectPageIds(array $pages): array
104 {
105 $pageIds = [];
106 foreach ($pages as $page) {
107 if (!is_array($page)) {
108 continue;
109 }
110 $pageId = sanitize_text_field((string) ($page['id'] ?? ''));
111 if ($pageId !== '') {
112 $pageIds[] = $pageId;
113 }
114 }
115
116 return $pageIds;
117 }
118
119 /**
120 * @param array<int, array<string, mixed>> $fields
121 * @param array<int, mixed> $assignments
122 * @param array<int, string> $validPageIds
123 * @return array<int, array<string, mixed>>
124 */
125 public static function applyFieldPageAssignments(
126 array $fields,
127 array $assignments,
128 array $validPageIds,
129 string $fallbackPageId
130 ): array {
131 $assignmentsByFieldId = self::normalizeFieldAssignments($assignments, $validPageIds);
132
133 foreach ($fields as &$field) {
134 if (!is_array($field)) {
135 continue;
136 }
137
138 $fieldId = (int) ($field['id'] ?? 0);
139 $targetPageId = $assignmentsByFieldId[$fieldId]
140 ?? McpMultiPageHelper::extractFieldPageId($field)
141 ?? $fallbackPageId;
142
143 if (!in_array($targetPageId, $validPageIds, true)) {
144 $targetPageId = $fallbackPageId;
145 }
146
147 McpMultiPageHelper::applyPageIdToField($field, $targetPageId);
148 }
149 unset($field);
150
151 return $fields;
152 }
153
154 /**
155 * @param array<int, array<string, mixed>> $pages
156 * @param array<string, mixed> $newPage
157 * @return array<int, array<string, mixed>>
158 */
159 public static function insertPage(array $pages, array $newPage, string $insertAfterPageId): array
160 {
161 if ($insertAfterPageId === '') {
162 $pages[] = $newPage;
163
164 return $pages;
165 }
166
167 $result = [];
168 $inserted = false;
169
170 foreach ($pages as $page) {
171 $result[] = $page;
172 $currentPageId = sanitize_text_field((string) ($page['id'] ?? ''));
173 if ($currentPageId === $insertAfterPageId) {
174 $result[] = $newPage;
175 $inserted = true;
176 }
177 }
178
179 if (!$inserted) {
180 $result[] = $newPage;
181 }
182
183 return $result;
184 }
185
186 /**
187 * @param array<int, mixed> $pages
188 * @return array<int, array<string, mixed>>
189 */
190 public static function stripFieldsFromPages(array $pages): array
191 {
192 $normalized = [];
193
194 foreach ($pages as $page) {
195 if (!is_array($page)) {
196 continue;
197 }
198
199 $normalized[] = [
200 'id' => sanitize_text_field((string) ($page['id'] ?? '')),
201 'label' => sanitize_text_field((string) ($page['label'] ?? '')),
202 'closable' => (bool) ($page['closable'] ?? false),
203 ];
204 }
205
206 return $normalized;
207 }
208
209 /**
210 * @param array<int, mixed> $assignments
211 * @param array<int, string> $validPageIds
212 * @return array<int, string>
213 */
214 private static function normalizeFieldAssignments(array $assignments, array $validPageIds): array
215 {
216 $normalized = [];
217
218 foreach ($assignments as $assignment) {
219 if (!is_array($assignment)) {
220 continue;
221 }
222
223 $fieldId = (int) ($assignment['fieldId'] ?? 0);
224 $pageId = sanitize_text_field((string) ($assignment['pageId'] ?? ''));
225 if ($fieldId <= 0 || $pageId === '' || !in_array($pageId, $validPageIds, true)) {
226 continue;
227 }
228
229 $normalized[$fieldId] = $pageId;
230 }
231
232 return $normalized;
233 }
234
235 /**
236 * @param array<int, array<string, mixed>> $pages
237 * @return array<string, mixed>|null
238 */
239 private static function findPageByLabel(array $pages, string $label): ?array
240 {
241 foreach ($pages as $page) {
242 if (!is_array($page)) {
243 continue;
244 }
245 if (($page['label'] ?? '') === $label) {
246 return $page;
247 }
248 }
249
250 return null;
251 }
252 }
253