[ 'table' => 'disclaimer_content', 'title' => 'Disclaimer', 'icon' => 'fa-shield-alt' ], 'privacy' => [ 'table' => 'privacy_policy_content', 'title' => 'Privacy Policy', 'icon' => 'fa-user-shield' ], 'refund' => [ 'table' => 'refund_policy_content', 'title' => 'Refund Policy', 'icon' => 'fa-undo-alt' ], 'shipping' => [ 'table' => 'shipping_policy_content', 'title' => 'Shipping Policy', 'icon' => 'fa-truck' ], 'terms' => [ 'table' => 'terms_conditions_content', 'title' => 'Terms & Conditions', 'icon' => 'fa-file-contract' ] ]; // Validate policy type if (!isset($policyConfig[$policy_type])) { $policy_type = 'disclaimer'; } $currentTable = $policyConfig[$policy_type]['table']; $currentTitle = $policyConfig[$policy_type]['title']; try { $db = getDbConnection(); } catch (PDOException $e) { logError("Database connection error: " . $e->getMessage()); $error = "Database error: " . $e->getMessage(); } // 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_title = isset($_POST['section_title']) ? sanitizeInput($_POST['section_title']) : ''; $section_content = isset($_POST['section_content']) ? $_POST['section_content'] : ''; $section_icon = isset($_POST['section_icon']) ? sanitizeInput($_POST['section_icon']) : ''; $sort_order = isset($_POST['sort_order']) ? (int)$_POST['sort_order'] : 1; $status = isset($_POST['status']) ? sanitizeInput($_POST['status']) : 'active'; $type = isset($_POST['policy_type']) ? sanitizeInput($_POST['policy_type']) : $policy_type; if (empty($section_title) || empty($section_content)) { $error = "Section title and content are required."; } else { try { $table = $policyConfig[$type]['table']; if ($formAction === 'add') { $stmt = $db->prepare("INSERT INTO {$table} (section_title, section_content, section_icon, sort_order, status, created_at) VALUES (:title, :content, :icon, :sort_order, :status, NOW())"); $stmt->bindParam(':title', $section_title); $stmt->bindParam(':content', $section_content); $stmt->bindParam(':icon', $section_icon); $stmt->bindParam(':sort_order', $sort_order); $stmt->bindParam(':status', $status); $stmt->execute(); $success = "Section added successfully."; } else { $section_id = isset($_POST['section_id']) ? (int)$_POST['section_id'] : 0; $stmt = $db->prepare("UPDATE {$table} SET section_title = :title, section_content = :content, section_icon = :icon, sort_order = :sort_order, status = :status, updated_at = NOW() WHERE id = :id"); $stmt->bindParam(':title', $section_title); $stmt->bindParam(':content', $section_content); $stmt->bindParam(':icon', $section_icon); $stmt->bindParam(':sort_order', $sort_order); $stmt->bindParam(':status', $status); $stmt->bindParam(':id', $section_id); $stmt->execute(); $success = "Section updated successfully."; } header("Location: policy_management.php?type={$type}&success=" . urlencode($success)); exit; } catch (Exception $e) { $error = "Error: " . $e->getMessage(); logError($error); } } } if ($formAction === 'delete') { $section_id = isset($_POST['section_id']) ? (int)$_POST['section_id'] : 0; $type = isset($_POST['policy_type']) ? sanitizeInput($_POST['policy_type']) : $policy_type; try { $table = $policyConfig[$type]['table']; $stmt = $db->prepare("DELETE FROM {$table} WHERE id = :id"); $stmt->bindParam(':id', $section_id); $stmt->execute(); $success = "Section deleted successfully."; header("Location: policy_management.php?type={$type}&success=" . urlencode($success)); exit; } catch (PDOException $e) { $error = "Database error: " . $e->getMessage(); logError($error); } } if ($formAction === 'toggle') { $section_id = isset($_POST['section_id']) ? (int)$_POST['section_id'] : 0; $type = isset($_POST['policy_type']) ? sanitizeInput($_POST['policy_type']) : $policy_type; try { $table = $policyConfig[$type]['table']; $stmt = $db->prepare("SELECT status FROM {$table} WHERE id = :id"); $stmt->bindParam(':id', $section_id); $stmt->execute(); $currentData = $stmt->fetch(); if (!$currentData) { throw new Exception("Section not found."); } $newStatus = $currentData['status'] === 'active' ? 'inactive' : 'active'; $stmt = $db->prepare("UPDATE {$table} SET status = :status, updated_at = NOW() WHERE id = :id"); $stmt->bindParam(':status', $newStatus); $stmt->bindParam(':id', $section_id); $stmt->execute(); $success = "Section status updated successfully."; header("Location: policy_management.php?type={$type}&success=" . urlencode($success)); exit; } catch (Exception $e) { $error = "Error: " . $e->getMessage(); logError($error); } } } } // Get data for edit if ($action === 'edit' && $id > 0) { try { $stmt = $db->prepare("SELECT * FROM {$currentTable} WHERE id = :id"); $stmt->bindParam(':id', $id); $stmt->execute(); $section = $stmt->fetch(PDO::FETCH_ASSOC); if (!$section) { $error = "Section not found."; $action = 'list'; } } catch (PDOException $e) { logError("Edit query error: " . $e->getMessage()); $error = "Database error: " . $e->getMessage(); $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 {$currentTable}"); $stmt->execute(); $totalRecords = $stmt->fetchColumn(); $stmt = $db->prepare("SELECT * FROM {$currentTable} 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(); $sections = $stmt->fetchAll(PDO::FETCH_ASSOC); $totalPages = ceil($totalRecords / $limit); } catch (PDOException $e) { logError("Database query error: " . $e->getMessage()); $error = "Database error: " . $e->getMessage(); $sections = []; $totalPages = 0; } $pageTitle = ($action === 'add') ? "Add New Section - {$currentTitle}" : (($action === 'edit') ? "Edit Section - {$currentTitle}" : "Policy Management"); include 'includes/header.php'; ?>