query("SHOW TABLES LIKE 'footer_settings'"); if ($stmt->rowCount() == 0) { logError("Table 'footer_settings' does not exist in database."); $error = "Table 'footer_settings' not found in the database."; $footerItems = []; $totalPages = 0; } } catch (PDOException $e) { logError("Database connection error: " . $e->getMessage()); $error = "Database error: " . $e->getMessage(); $footerItems = []; $totalPages = 0; } // Handle form submission if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (!verifyCSRF($_POST['csrf_token'] ?? '')) { $error = "Invalid CSRF token."; logError($error); } elseif (isset($_POST['action'])) { $formAction = $_POST['action']; if ($formAction === 'add' || $formAction === 'edit') { $section_name = isset($_POST['section_name']) ? sanitizeInput($_POST['section_name']) : ''; $title = isset($_POST['title']) ? sanitizeInput($_POST['title']) : ''; $content = isset($_POST['content']) ? $_POST['content'] : ''; // Content is HTML, so don't sanitize $sort_order = isset($_POST['sort_order']) ? (int)$_POST['sort_order'] : 0; $status = isset($_POST['status']) ? sanitizeInput($_POST['status']) : 'active'; if (empty($section_name) || empty($title)) { $error = "Section name and title are required."; logError($error); } else { try { if ($formAction === 'add') { $stmt = $db->prepare("INSERT INTO footer_settings (section_name, title, content, sort_order, status, created_at) VALUES (:section_name, :title, :content, :sort_order, :status, NOW())"); $stmt->bindParam(':section_name', $section_name); $stmt->bindParam(':title', $title); $stmt->bindParam(':content', $content); $stmt->bindParam(':sort_order', $sort_order); $stmt->bindParam(':status', $status); $stmt->execute(); $success = "Footer setting successfully added."; } else { $footer_id = isset($_POST['footer_id']) ? (int)$_POST['footer_id'] : 0; $stmt = $db->prepare("SELECT id FROM footer_settings WHERE id = :id"); $stmt->bindParam(':id', $footer_id); $stmt->execute(); if (!$stmt->fetch()) { throw new Exception("Setting not found."); } $stmt = $db->prepare("UPDATE footer_settings SET section_name = :section_name, title = :title, content = :content, sort_order = :sort_order, status = :status, updated_at = NOW() WHERE id = :id"); $stmt->bindParam(':section_name', $section_name); $stmt->bindParam(':title', $title); $stmt->bindParam(':content', $content); $stmt->bindParam(':sort_order', $sort_order); $stmt->bindParam(':status', $status); $stmt->bindParam(':id', $footer_id); $stmt->execute(); $success = "Footer setting successfully updated."; } header("Location: footer-settings.php?success=" . urlencode($success)); exit; } catch (Exception $e) { $error = "Error: " . htmlspecialchars($e->getMessage(), ENT_QUOTES, 'UTF-8'); logError($error); } } } if ($formAction === 'delete') { $footer_id = isset($_POST['footer_id']) ? (int)$_POST['footer_id'] : 0; try { $stmt = $db->prepare("SELECT id FROM footer_settings WHERE id = :id"); $stmt->bindParam(':id', $footer_id); $stmt->execute(); if (!$stmt->fetch()) { throw new Exception("Setting not found."); } $stmt = $db->prepare("DELETE FROM footer_settings WHERE id = :id"); $stmt->bindParam(':id', $footer_id); $stmt->execute(); $success = "Footer setting successfully deleted."; header("Location: footer-settings.php?success=" . urlencode($success)); exit; } catch (PDOException $e) { $error = "Database error: " . htmlspecialchars($e->getMessage(), ENT_QUOTES, 'UTF-8'); logError($error); } } if ($formAction === 'toggle') { $footer_id = isset($_POST['footer_id']) ? (int)$_POST['footer_id'] : 0; try { $stmt = $db->prepare("SELECT status FROM footer_settings WHERE id = :id"); $stmt->bindParam(':id', $footer_id); $stmt->execute(); $currentData = $stmt->fetch(); if (!$currentData) { throw new Exception("Setting not found."); } $newStatus = $currentData['status'] === 'active' ? 'inactive' : 'active'; $stmt = $db->prepare("UPDATE footer_settings SET status = :status, updated_at = NOW() WHERE id = :id"); $stmt->bindParam(':status', $newStatus); $stmt->bindParam(':id', $footer_id); $stmt->execute(); $success = "Footer setting status successfully updated."; header("Location: footer-settings.php?success=" . urlencode($success)); exit; } catch (Exception $e) { $error = "Error: " . htmlspecialchars($e->getMessage(), ENT_QUOTES, 'UTF-8'); logError($error); } } } } // Get data for edit if ($action === 'edit' && $id > 0) { try { $stmt = $db->prepare("SELECT * FROM footer_settings WHERE id = :id"); $stmt->bindParam(':id', $id); $stmt->execute(); $footer = $stmt->fetch(PDO::FETCH_ASSOC); if (!$footer) { $error = "Setting not found."; $action = 'list'; } } catch (PDOException $e) { logError("Edit query error: " . $e->getMessage()); $error = "Database error: " . htmlspecialchars($e->getMessage(), ENT_QUOTES, 'UTF-8'); $action = 'list'; } } // Get data for list with pagination $page = isset($_GET['page']) ? (int)$_GET['page'] : 1; $limit = 12; $offset = ($page - 1) * $limit; try { $stmt = $db->prepare("SELECT COUNT(*) FROM footer_settings"); $stmt->execute(); $totalRecords = $stmt->fetchColumn(); $stmt = $db->prepare("SELECT * FROM footer_settings ORDER BY sort_order ASC LIMIT :limit OFFSET :offset"); $stmt->bindValue(':limit', $limit, PDO::PARAM_INT); $stmt->bindValue(':offset', $offset, PDO::PARAM_INT); $stmt->execute(); $footerItems = $stmt->fetchAll(PDO::FETCH_ASSOC); $totalPages = ceil($totalRecords / $limit); } catch (PDOException $e) { logError("Database query error for footer_settings: " . $e->getMessage()); $error = "Database error: " . htmlspecialchars($e->getMessage(), ENT_QUOTES, 'UTF-8'); $footerItems = []; $totalPages = 0; } // Set page title $pageTitle = ($action === 'add') ? "Add New Footer Setting" : (($action === 'edit') ? "Edit Footer Setting" : "Manage Footer Settings"); include 'includes/header.php'; ?>