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