@php $defaultSettings = [ 'sections' => [ 'materials' => true, 'verification' => true, 'analytics' => true, ], 'elements' => [ 'materials_summary' => true, 'materials_list' => true, 'verification_summary' => true, 'verification_metrics' => true, 'analytics_table' => true, 'analytics_charts' => true, ], 'participant_lists' => [ 'present' => true, 'absent' => true, ], 'participant_columns' => [ 'position' => true, 'department' => true, 'attendance' => true, 'score' => true, 'passed' => true, 'attempts_checked' => false, 'best_score' => false, ], 'chart_options' => [ 'type' => 'vertical', ], ]; $normalizeBool = function ($value): bool { if (is_bool($value)) { return $value; } if (is_numeric($value)) { return (int) $value === 1; } $normalized = strtolower(trim((string) $value)); return in_array($normalized, ['1', 'true', 'yes', 'on'], true); }; $normalizeChartType = function ($value, $default = 'vertical'): string { $normalized = strtolower(trim((string) $value)); if (!in_array($normalized, ['vertical', 'pie'], true)) { $normalized = (string) $default; } return $normalized; }; $settingsRaw = null; if (\Illuminate\Support\Facades\Schema::hasTable('t051_szkolenia') && \Illuminate\Support\Facades\Schema::hasColumn('t051_szkolenia', 'raport_ustawienia_json')) { $settingsRaw = $data->raport_ustawienia_json ?? null; } if (($settingsRaw === null || trim((string) $settingsRaw) === '') && isset($data->id)) { $settingsPath = 'training/report_settings/'.(int) $data->id.'.json'; if (\Illuminate\Support\Facades\Storage::disk('local')->exists($settingsPath)) { $settingsRaw = \Illuminate\Support\Facades\Storage::disk('local')->get($settingsPath); } } $decodedSettings = null; if (is_string($settingsRaw) && trim($settingsRaw) !== '') { $decoded = json_decode($settingsRaw, true); if (is_array($decoded)) { $decodedSettings = $decoded; } } elseif (is_array($settingsRaw)) { $decodedSettings = $settingsRaw; } $settings = $defaultSettings; if (is_array($decodedSettings)) { foreach ($defaultSettings as $group => $groupDefaults) { $groupInput = is_array($decodedSettings[$group] ?? null) ? $decodedSettings[$group] : []; foreach ($groupDefaults as $key => $defaultValue) { if (is_bool($defaultValue)) { if (array_key_exists($key, $groupInput)) { $settings[$group][$key] = $normalizeBool($groupInput[$key]); } else { $settings[$group][$key] = (bool) $defaultValue; } } else { if ($group === 'chart_options' && $key === 'type') { $settings[$group][$key] = $normalizeChartType( $groupInput[$key] ?? $defaultValue, $defaultValue ); } else { $settings[$group][$key] = $groupInput[$key] ?? $defaultValue; } } } } } @endphp