From 7ab890005bc1e1cd6e4cfcda5cfae2e13b69e37b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Pocrnji=C4=8D?= Date: Mon, 18 May 2026 12:37:12 +0200 Subject: [PATCH] fixed the fixed --- app/Services/EmailTemplateRenderer.php | 45 ++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/app/Services/EmailTemplateRenderer.php b/app/Services/EmailTemplateRenderer.php index 6c19829..b991f9b 100644 --- a/app/Services/EmailTemplateRenderer.php +++ b/app/Services/EmailTemplateRenderer.php @@ -188,13 +188,7 @@ protected function buildMap(array $ctx): array $meta = json_decode($meta, true) ?? []; } if (is_array($meta)) { - // Flatten meta entries. Supports two formats: - // - Structured: { "sklic": { "value": "...", "type": "string", ... } } → extract 'value' - // - Plain: { "sklic": "..." } → use as-is - $out['contract']['meta'] = array_map( - fn ($item) => (is_array($item) && array_key_exists('value', $item)) ? $item['value'] : $item, - $meta - ); + $out['contract']['meta'] = $this->flattenMetaForTemplate($meta); } } if (isset($ctx['activity'])) { @@ -225,4 +219,41 @@ protected function buildMap(array $ctx): array return $out; } + + /** + * Flatten a contract meta array so every leaf value is accessible by its bare key. + * + * Handles three formats stored in the wild: + * 1. Numeric wrapper: { "1": { "sklic": "SI00…", "job_days": 1 } } + * → { "sklic": "SI00…", "job_days": 1 } + * 2. Structured entry: { "sklic": { "value": "SI00…", "type": "string" } } + * → { "sklic": "SI00…" } + * 3. Already flat: { "sklic": "SI00…" } + * → { "sklic": "SI00…" } + */ + private function flattenMetaForTemplate(array $meta): array + { + $flat = []; + foreach ($meta as $key => $item) { + if (!is_array($item)) { + // Plain scalar — keep as-is (format 3) + if (!array_key_exists($key, $flat)) { + $flat[$key] = $item; + } + } elseif (array_key_exists('value', $item)) { + // Structured { value, type, title } entry (format 2) + $flat[$key] = $item['value']; + } elseif (is_numeric($key)) { + // Numeric wrapper key — recurse and alias without the prefix (format 1) + foreach ($this->flattenMetaForTemplate($item) as $nk => $nv) { + if (!array_key_exists($nk, $flat)) { + $flat[$nk] = $nv; + } + } + } + // Non-numeric nested arrays without a 'value' key are silently skipped + } + + return $flat; + } }