| @@ -1,18 +1,13 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | /** |
| 4 | - * @copyright © TMS-Plugins. All rights reserved. | |
| 4 | + * @copyright © Melograno Venture Studio. All rights reserved. | |
| 5 | 5 | * @licence See COPYING.md for license details. |
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace IvyForms\Services\Template; |
| 9 | 9 | |
| 10 | -// phpcs:disable PSR1.Files.SideEffects | |
| 11 | -if (!defined('ABSPATH')) { | |
| 12 | - exit; // Exit if accessed directly | |
| 13 | -} | |
| 14 | - | |
| 15 | 10 | use IvyForms\Common\Exceptions\InvalidArgumentException; |
| 16 | 11 | use IvyForms\Services\Translations\BackendStrings; |
| 17 | 12 | |
| 18 | 13 | /** |
| @@ -22,21 +17,21 @@ | ||
| 22 | 17 | */ |
| 23 | 18 | class TemplateService |
| 24 | 19 | { |
| 25 | 20 | /** |
| 26 | - * Get all predefined form templates | |
| 21 | + * Get all template metadata (without form_data) for listing | |
| 27 | 22 | * |
| 28 | 23 | * @return array<string, array<string, mixed>> |
| 29 | 24 | */ |
| 30 | - public function getAllTemplates(): array | |
| 25 | + public function getAllTemplateMetas(): array | |
| 31 | 26 | { |
| 32 | - return TemplateMapper::getAllTemplates(); | |
| 27 | + return TemplateMapper::getAllTemplateMetas(); | |
| 33 | 28 | } |
| 34 | 29 | |
| 35 | 30 | /** |
| 36 | 31 | * Get predefined form template by ID |
| 37 | 32 | * |
| 38 | - * @param string $templateId | |
| 33 | + * @param string $templateId | |
| 39 | 34 | * @return array<string, mixed>|null |
| 40 | 35 | */ |
| 41 | 36 | public function getTemplateById(string $templateId): ?array |
| 42 | 37 | { |
| @@ -45,9 +40,9 @@ | ||
| 45 | 40 | |
| 46 | 41 | /** |
| 47 | 42 | * Get form data from template by ID |
| 48 | 43 | * |
| 49 | - * @param string $templateId | |
| 44 | + * @param string $templateId | |
| 50 | 45 | * @return array<string, mixed> |
| 51 | 46 | * @throws InvalidArgumentException |
| 52 | 47 | */ |
| 53 | 48 | public function getFormDataFromTemplate(string $templateId): array |
| @@ -59,12 +54,72 @@ | ||
| 59 | 54 | BackendStrings::getTemplateStrings()['template_not_found'] |
| 60 | 55 | ); |
| 61 | 56 | } |
| 62 | 57 | |
| 63 | - return $template['form_data']; | |
| 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; | |
| 64 | 68 | } |
| 65 | 69 | |
| 66 | 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 | + /** | |
| 67 | 122 | * Get template categories |
| 68 | 123 | * |
| 69 | 124 | * @return array<string, array<string, string>> |
| 70 | 125 | */ |
| @@ -70,11 +125,11 @@ | ||
| 70 | 125 | */ |
| 71 | 126 | public function getTemplateCategories(): array |
| 72 | 127 | { |
| 73 | 128 | return [ |
| 74 | -// 'basic' => [ | |
| 75 | -// 'name' => '', | |
| 76 | -// 'description' => '' | |
| 77 | -// ] | |
| 129 | + // 'basic' => [ | |
| 130 | + // 'name' => '', | |
| 131 | + // 'description' => '' | |
| 132 | + // ] | |
| 78 | 133 | ]; |
| 79 | 134 | } |
| 80 | 135 | } |