From d1dc8dfe7881a97f3250fca77a9ae14678d766d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=9Cmit=20Tun=C3=A7?= Date: Sat, 1 Nov 2025 04:15:10 -0300 Subject: [PATCH] Refactor default data handling in PageForm schema: Implemented a recursive function to flatten nested arrays for default data assignment in both section and footer data fields. This enhancement ensures accurate population of fields by checking existing values, improving data consistency and usability in the form. --- .../Resources/Pages/Schemas/PageForm.php | 87 ++++++++++++++++--- 1 file changed, 76 insertions(+), 11 deletions(-) diff --git a/app/Filament/Admin/Resources/Pages/Schemas/PageForm.php b/app/Filament/Admin/Resources/Pages/Schemas/PageForm.php index daa2377..ea28c9d 100644 --- a/app/Filament/Admin/Resources/Pages/Schemas/PageForm.php +++ b/app/Filament/Admin/Resources/Pages/Schemas/PageForm.php @@ -673,13 +673,41 @@ class PageForm // Mevcut section_data'yı al $existingData = $get('section_data') ?? []; + // Nested array'leri dot notation'a çeviren recursive fonksiyon + $flattenDefaultData = function ($array, $prefix = '') use (&$flattenDefaultData) { + $result = []; + foreach ($array as $key => $value) { + $newKey = $prefix ? "{$prefix}.{$key}" : $key; + if (is_array($value)) { + $result = array_merge($result, $flattenDefaultData($value, $newKey)); + } else { + $result[$newKey] = $value; + } + } + return $result; + }; + + // Default data'yı düzleştir + $flattenedDefaults = $flattenDefaultData($defaultData); + // Her default veri için boş olan alanları doldur - foreach ($defaultData as $key => $defaultValue) { + foreach ($flattenedDefaults as $key => $defaultValue) { + // Dot notation ile nested değeri kontrol et + $keys = explode('.', $key); + $currentValue = $existingData; + + // Nested değere eriş + foreach ($keys as $k) { + $currentValue = $currentValue[$k] ?? null; + if ($currentValue === null) { + break; + } + } + // Eğer bu alan boşsa veya yoksa, default değeri kullan - if (!isset($existingData[$key]) || - $existingData[$key] === null || - $existingData[$key] === '' || - (is_array($existingData[$key]) && empty($existingData[$key]))) { + if ($currentValue === null || + $currentValue === '' || + (is_array($currentValue) && empty($currentValue))) { $set("section_data.{$key}", $defaultValue); } } @@ -742,20 +770,57 @@ class PageForm } $template = FooterTemplate::find($state); - if (!$template || empty($template->default_data)) { + if (!$template) { + return; + } + + // default_data'yı güvenli bir şekilde al + $defaultData = $template->default_data; + if (is_string($defaultData)) { + $defaultData = json_decode($defaultData, true) ?? []; + } + if (!is_array($defaultData) || empty($defaultData)) { return; } // Mevcut footer_data'yı al $existingData = $get('footer_data') ?? []; + // Nested array'leri dot notation'a çeviren recursive fonksiyon + $flattenDefaultData = function ($array, $prefix = '') use (&$flattenDefaultData) { + $result = []; + foreach ($array as $key => $value) { + $newKey = $prefix ? "{$prefix}.{$key}" : $key; + if (is_array($value)) { + $result = array_merge($result, $flattenDefaultData($value, $newKey)); + } else { + $result[$newKey] = $value; + } + } + return $result; + }; + + // Default data'yı düzleştir + $flattenedDefaults = $flattenDefaultData($defaultData); + // Her default veri için boş olan alanları doldur - foreach ($template->default_data as $key => $defaultValue) { + foreach ($flattenedDefaults as $key => $defaultValue) { + // Dot notation ile nested değeri kontrol et + $keys = explode('.', $key); + $currentValue = $existingData; + + // Nested değere eriş + foreach ($keys as $k) { + $currentValue = $currentValue[$k] ?? null; + if ($currentValue === null) { + break; + } + } + // Eğer bu alan boşsa veya yoksa, default değeri kullan - if (!isset($existingData[$key]) || - $existingData[$key] === null || - $existingData[$key] === '' || - (is_array($existingData[$key]) && empty($existingData[$key]))) { + if ($currentValue === null || + $currentValue === '' || + (is_array($currentValue) && empty($currentValue))) { $set("footer_data.{$key}", $defaultValue); } }