PluginProbe
The Innovative Form Builder – IvyForms / 0.8
The Innovative Form Builder – IvyForms v0.8
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 1.2 1.3
ivyforms / backend / src / Services / Template / TemplateService.php

TemplateService.php in The Innovative Form Builder – IvyForms 0.8, at backend/src/Services/Template/TemplateService.php

141 lines 3.6 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\Services\Template;
9
10 // phpcs:disable PSR1.Files.SideEffects
11 if (!defined('ABSPATH')) {
12 exit; // Exit if accessed directly
13 }
14
15 use IvyForms\Common\Exceptions\InvalidArgumentException;
16 use IvyForms\Services\Translations\BackendStrings;
17
18 /**
19 * Class TemplateService
20 *
21 * @package IvyForms\Services\Template
22 */
23 class TemplateService
24 {
25 /**
26 * Get all predefined form templates
27 *
28 * @return array<string, array<string, mixed>>
29 */
30 public function getAllTemplates(): array
31 {
32 return TemplateMapper::getAllTemplates();
33 }
34
35 /**
36 * Get predefined form template by ID
37 *
38 * @param string $templateId
39 * @return array<string, mixed>|null
40 */
41 public function getTemplateById(string $templateId): ?array
42 {
43 return TemplateMapper::getTemplate($templateId);
44 }
45
46 /**
47 * Get form data from template by ID
48 *
49 * @param string $templateId
50 * @return array<string, mixed>
51 * @throws InvalidArgumentException
52 */
53 public function getFormDataFromTemplate(string $templateId): array
54 {
55 $template = $this->getTemplateById($templateId);
56
57 if (!$template) {
58 throw new InvalidArgumentException(
59 BackendStrings::getTemplateStrings()['template_not_found']
60 );
61 }
62
63 $formData = $template['form_data'];
64
65 // Assign sequential rowIndex to each field ONLY if not already specified
66 // This ensures templates without layout get single-column, but templates with
67 // custom multi-column layouts are preserved
68 if (isset($formData['fields']) && is_array($formData['fields'])) {
69 $formData['fields'] = $this->assignFieldLayout($formData['fields']);
70 }
71
72 return $formData;
73 }
74
75 /**
76 * Assign layout properties to fields
77 *
78 * @param array<int, array<string, mixed>> $fields
79 * @return array<int, array<string, mixed>>
80 */
81 private function assignFieldLayout(array $fields): array
82 {
83 $rowCounter = 0;
84 foreach ($fields as &$field) {
85 if ($this->shouldAssignLayout($field)) {
86 $field['rowIndex'] = $rowCounter;
87 $field['columnIndex'] = 0;
88 $field['width'] = 100;
89 $rowCounter++;
90 continue;
91 }
92
93 if ($this->hasCustomLayout($field)) {
94 $rowCounter = max($rowCounter, $field['rowIndex'] + 1);
95 }
96 }
97 unset($field);
98 return $fields;
99 }
100
101 /**
102 * Check if field should get default layout
103 *
104 * @param array<string, mixed> $field
105 * @return bool
106 */
107 private function shouldAssignLayout(array $field): bool
108 {
109 return !isset($field['parentId'])
110 && !isset($field['rowIndex'])
111 && !isset($field['columnIndex'])
112 && !isset($field['width']);
113 }
114
115 /**
116 * Check if field has custom layout
117 *
118 * @param array<string, mixed> $field
119 * @return bool
120 */
121 private function hasCustomLayout(array $field): bool
122 {
123 return !isset($field['parentId']) && isset($field['rowIndex']);
124 }
125
126 /**
127 * Get template categories
128 *
129 * @return array<string, array<string, string>>
130 */
131 public function getTemplateCategories(): array
132 {
133 return [
134 // 'basic' => [
135 // 'name' => '',
136 // 'description' => ''
137 // ]
138 ];
139 }
140 }
141