> */ public function getAllTemplates(): array { return TemplateMapper::getAllTemplates(); } /** * Get predefined form template by ID * * @param string $templateId * @return array|null */ public function getTemplateById(string $templateId): ?array { return TemplateMapper::getTemplate($templateId); } /** * Get form data from template by ID * * @param string $templateId * @return array * @throws InvalidArgumentException */ public function getFormDataFromTemplate(string $templateId): array { $template = $this->getTemplateById($templateId); if (!$template) { throw new InvalidArgumentException( BackendStrings::getTemplateStrings()['template_not_found'] ); } $formData = $template['form_data']; // Assign sequential rowIndex to each field ONLY if not already specified // This ensures templates without layout get single-column, but templates with // custom multi-column layouts are preserved if (isset($formData['fields']) && is_array($formData['fields'])) { $formData['fields'] = $this->assignFieldLayout($formData['fields']); } return $formData; } /** * Assign layout properties to fields * * @param array> $fields * @return array> */ private function assignFieldLayout(array $fields): array { $rowCounter = 0; foreach ($fields as &$field) { if ($this->shouldAssignLayout($field)) { $field['rowIndex'] = $rowCounter; $field['columnIndex'] = 0; $field['width'] = 100; $rowCounter++; continue; } if ($this->hasCustomLayout($field)) { $rowCounter = max($rowCounter, $field['rowIndex'] + 1); } } unset($field); return $fields; } /** * Check if field should get default layout * * @param array $field * @return bool */ private function shouldAssignLayout(array $field): bool { return !isset($field['parentId']) && !isset($field['rowIndex']) && !isset($field['columnIndex']) && !isset($field['width']); } /** * Check if field has custom layout * * @param array $field * @return bool */ private function hasCustomLayout(array $field): bool { return !isset($field['parentId']) && isset($field['rowIndex']); } /** * Get template categories * * @return array> */ public function getTemplateCategories(): array { return [ // 'basic' => [ // 'name' => '', // 'description' => '' // ] ]; } }