0) { // Editing existing dashboard - check permission try { $dashboard = getDashboardById($dashboardId); if ($dashboard) { if (!canEdit('dashboard', $dashboardId)) { // User doesn't have permission to edit this dashboard ob_end_clean(); header('Location: index.php?error=access_denied'); exit; } $dashboardConfig = json_decode($dashboard['config'], true); } } catch (Exception $e) { $error = "Failed to load dashboard."; } } else { // Creating new dashboard - only admins can create if (!isAdmin()) { ob_end_clean(); header('Location: index.php?error=access_denied'); exit; } } // Get available layers from GeoServer $availableLayers = getAvailableLayers(); $geoServerConfig = getGeoServerConfig(); // Handle save request if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) { if ($_POST['action'] === 'save') { $title = isset($_POST['title']) ? trim($_POST['title']) : 'Untitled Dashboard'; $description = isset($_POST['description']) ? trim($_POST['description']) : ''; $categoryId = isset($_POST['category_id']) && $_POST['category_id'] !== '' ? intval($_POST['category_id']) : null; $config = isset($_POST['config']) ? json_decode($_POST['config'], true) : []; try { if ($dashboardId > 0) { // Update existing dashboard updateDashboard($dashboardId, $title, $description, $config, $categoryId); ob_end_clean(); header('Location: index.php?saved=dashboard'); exit; } else { // Create new dashboard $newId = saveDashboard($title, $description, $config, $categoryId); ob_end_clean(); header('Location: index.php?saved=dashboard'); exit; } } catch (Exception $e) { error_log("Error saving dashboard: " . $e->getMessage()); $error = "Failed to save dashboard. Please check database configuration."; } } } // Flush output buffer ob_end_flush(); ?>