0) { // Editing existing map - check permission try { $existingMap = getMapById($mapId); if (!$existingMap) { $error = "Map not found."; $mapId = 0; } elseif (!canEdit('map', $mapId)) { // User doesn't have permission to edit this map ob_end_clean(); header('Location: index.php?error=access_denied'); exit; } } catch (Exception $e) { $error = "Failed to load map."; $mapId = 0; } } else { // Creating new map - only admins can create if (!isAdmin()) { ob_end_clean(); header('Location: index.php?error=access_denied'); exit; } } // Get available layers from GeoServer $availableLayers = getAvailableLayers(); // Handle save to database request if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'save') { $title = isset($_POST['map_title']) ? trim($_POST['map_title']) : 'Untitled Map'; $description = isset($_POST['map_description']) ? trim($_POST['map_description']) : ''; $categoryId = isset($_POST['map_category']) && $_POST['map_category'] !== '' ? intval($_POST['map_category']) : null; $basemaps = isset($_POST['basemaps']) ? $_POST['basemaps'] : ['osm']; $layers = isset($_POST['layers']) ? $_POST['layers'] : []; $features = isset($_POST['features']) ? $_POST['features'] : []; // Parse filters from POST (supporting multiple conditions per layer) $filters = []; if (isset($_POST['layer_filters']) && is_array($_POST['layer_filters'])) { foreach ($_POST['layer_filters'] as $layer => $layerFilters) { if (!is_array($layerFilters)) continue; $conditions = []; foreach ($layerFilters as $index => $condition) { if (!empty($condition['attribute']) && !empty($condition['operator']) && isset($condition['value'])) { $conditions[] = [ 'attribute' => $condition['attribute'], 'operator' => $condition['operator'], 'value' => $condition['value'], 'logic' => isset($condition['logic']) ? $condition['logic'] : 'AND' ]; } } if (!empty($conditions)) { $filters[$layer] = $conditions; } } } $initialExtent = [ 'center_lon' => isset($_POST['center_lon']) && $_POST['center_lon'] !== '' ? floatval($_POST['center_lon']) : null, 'center_lat' => isset($_POST['center_lat']) && $_POST['center_lat'] !== '' ? floatval($_POST['center_lat']) : null, 'zoom_level' => isset($_POST['zoom_level']) && $_POST['zoom_level'] !== '' ? floatval($_POST['zoom_level']) : null ]; try { // Get the map ID from POST if updating $editMapId = isset($_POST['map_id']) ? intval($_POST['map_id']) : 0; if ($editMapId == 0) { // Create new map $editMapId = saveMap($title, $description, '', $basemaps, $layers, $features, $initialExtent, $categoryId, $filters); } // Generate the template $template = generateMapTemplate($editMapId, $basemaps, $layers, $features, $initialExtent, null, $filters); // Update existing map updateMap($editMapId, $title, $description, $template, $basemaps, $layers, $features, $initialExtent, $categoryId, $filters); // Clear output buffer and redirect ob_clean(); header('Location: index.php?saved=map'); exit; } catch (Exception $e) { error_log("Error saving map: " . $e->getMessage()); $error = "Failed to save map. Please check database configuration."; } } // Handle map generation request first, before any output if ($_SERVER['REQUEST_METHOD'] === 'POST') { $basemaps = isset($_POST['basemaps']) ? $_POST['basemaps'] : []; $layers = isset($_POST['layers']) ? $_POST['layers'] : []; $features = isset($_POST['features']) ? $_POST['features'] : []; // Parse filters from POST (supporting multiple conditions per layer) $filters = []; if (isset($_POST['layer_filters']) && is_array($_POST['layer_filters'])) { foreach ($_POST['layer_filters'] as $layer => $layerFilters) { if (!is_array($layerFilters)) continue; $conditions = []; foreach ($layerFilters as $index => $condition) { if (!empty($condition['attribute']) && !empty($condition['operator']) && isset($condition['value'])) { $conditions[] = [ 'attribute' => $condition['attribute'], 'operator' => $condition['operator'], 'value' => $condition['value'], 'logic' => isset($condition['logic']) ? $condition['logic'] : 'AND' ]; } } if (!empty($conditions)) { $filters[$layer] = $conditions; } } } // Get initial extent settings $initialExtent = [ 'center_lon' => isset($_POST['center_lon']) && $_POST['center_lon'] !== '' ? floatval($_POST['center_lon']) : null, 'center_lat' => isset($_POST['center_lat']) && $_POST['center_lat'] !== '' ? floatval($_POST['center_lat']) : null, 'zoom_level' => isset($_POST['zoom_level']) && $_POST['zoom_level'] !== '' ? floatval($_POST['zoom_level']) : null ]; // Ensure we have at least one basemap if (empty($basemaps)) { $basemaps = ['osm']; } // Generate the template with initial extent settings $template = generateMapTemplate(0, $basemaps, $layers, $features, $initialExtent, null, $filters); // Clear any previous output ob_clean(); // Set proper headers for HTML content header('Content-Type: text/html; charset=utf-8'); header('Cache-Control: no-cache, must-revalidate'); header('Pragma: no-cache'); // Output the template directly echo $template; exit; } // If we get here, we're displaying the builder interface ?>