false, 'message' => '', 'filename' => '']; // Create target directory if it doesn't exist $targetPath = __DIR__ . '/../' . $targetDir; if (!is_dir($targetPath)) { mkdir($targetPath, 0755, true); } // Check if file was uploaded if ($file['error'] !== UPLOAD_ERR_OK) { $result['message'] = 'File upload error.'; return $result; } // Validate image $imageInfo = getimagesize($file['tmp_name']); if ($imageInfo === false) { $result['message'] = 'Invalid image file.'; return $result; } $fileName = basename($file['name']); $fileExt = strtolower(pathinfo($fileName, PATHINFO_EXTENSION)); $newFileName = uniqid() . '.' . $fileExt; $targetFile = $targetPath . '/' . $newFileName; // Validate file type if (!in_array($fileExt, $allowedTypes)) { $result['message'] = 'File type not allowed. Only ' . implode(', ', $allowedTypes) . ' are permitted.'; return $result; } // Validate file size if ($file['size'] > $maxFileSize) { $result['message'] = 'File size must not exceed 5MB.'; return $result; } // Move uploaded file if (move_uploaded_file($file['tmp_name'], $targetFile)) { $result['success'] = true; $result['filename'] = $newFileName; logError("File uploaded successfully: $newFileName to $targetDir"); } else { $result['message'] = 'Failed to upload file.'; } return $result; } // Initialize variables $action = isset($_GET['action']) ? sanitizeInput($_GET['action']) : 'list'; $id = isset($_GET['id']) ? (int)$_GET['id'] : 0; $success = isset($_GET['success']) ? sanitizeInput($_GET['success']) : ''; $error = ''; try { $db = getDbConnection(); // Verify table existence $stmt = $db->query("SHOW TABLES LIKE 'about_content'"); if ($stmt->rowCount() == 0) { logError("Table 'about_content' does not exist in database."); $error = "Table 'about_content' not found in database."; $aboutItems = []; $totalPages = 0; } } catch (PDOException $e) { logError("Database connection error: " . $e->getMessage()); $error = "Database error: " . $e->getMessage(); $aboutItems = []; $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') { $title = isset($_POST['title']) ? sanitizeInput($_POST['title']) : ''; $description = isset($_POST['description']) ? $_POST['description'] : ''; $sort_order = isset($_POST['sort_order']) ? (int)$_POST['sort_order'] : 1; $status = isset($_POST['status']) ? sanitizeInput($_POST['status']) : 'active'; if (empty($title) || empty($description)) { $error = "Title and description are required."; } else { try { $image = null; if (isset($_FILES['image']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) { $uploadResult = uploadFile($_FILES['image'], 'img/about'); if ($uploadResult['success']) { $image = $uploadResult['filename']; } else { throw new Exception($uploadResult['message']); } } if ($formAction === 'add') { $stmt = $db->prepare("INSERT INTO about_content (title, description, image, sort_order, status, created_at) VALUES (:title, :description, :image, :sort_order, :status, NOW())"); $stmt->bindParam(':title', $title); $stmt->bindParam(':description', $description); $stmt->bindParam(':image', $image); $stmt->bindParam(':sort_order', $sort_order); $stmt->bindParam(':status', $status); $stmt->execute(); $success = "About content added successfully."; } else { $about_id = isset($_POST['about_id']) ? (int)$_POST['about_id'] : 0; $stmt = $db->prepare("SELECT image FROM about_content WHERE id = :id"); $stmt->bindParam(':id', $about_id); $stmt->execute(); $currentData = $stmt->fetch(); if (!$currentData) { throw new Exception("Content not found."); } if (empty($image)) { $image = $currentData['image']; } else { if (!empty($currentData['image'])) { $oldImagePath = __DIR__ . '/../img/about/' . $currentData['image']; if (file_exists($oldImagePath)) { unlink($oldImagePath); logError("Old image deleted: " . $currentData['image']); } } } $stmt = $db->prepare("UPDATE about_content SET title = :title, description = :description, image = :image, sort_order = :sort_order, status = :status, updated_at = NOW() WHERE id = :id"); $stmt->bindParam(':title', $title); $stmt->bindParam(':description', $description); $stmt->bindParam(':image', $image); $stmt->bindParam(':sort_order', $sort_order); $stmt->bindParam(':status', $status); $stmt->bindParam(':id', $about_id); $stmt->execute(); $success = "About content updated successfully."; } header("Location: about_content.php?success=" . urlencode($success)); exit; } catch (Exception $e) { $error = "Error: " . $e->getMessage(); logError($error); } } } if ($formAction === 'delete') { $about_id = isset($_POST['about_id']) ? (int)$_POST['about_id'] : 0; try { $stmt = $db->prepare("SELECT image FROM about_content WHERE id = :id"); $stmt->bindParam(':id', $about_id); $stmt->execute(); $data = $stmt->fetch(); $stmt = $db->prepare("DELETE FROM about_content WHERE id = :id"); $stmt->bindParam(':id', $about_id); $stmt->execute(); if (!empty($data['image'])) { $imagePath = __DIR__ . '/../img/about/' . $data['image']; if (file_exists($imagePath)) { unlink($imagePath); logError("Image deleted on content deletion: " . $data['image']); } } $success = "About content deleted successfully."; header("Location: about_content.php?success=" . urlencode($success)); exit; } catch (PDOException $e) { $error = "Database error: " . $e->getMessage(); logError($error); } } if ($formAction === 'toggle') { $about_id = isset($_POST['about_id']) ? (int)$_POST['about_id'] : 0; try { $stmt = $db->prepare("SELECT status FROM about_content WHERE id = :id"); $stmt->bindParam(':id', $about_id); $stmt->execute(); $currentData = $stmt->fetch(); if (!$currentData) { throw new Exception("Content not found."); } $newStatus = $currentData['status'] === 'active' ? 'inactive' : 'active'; $stmt = $db->prepare("UPDATE about_content SET status = :status, updated_at = NOW() WHERE id = :id"); $stmt->bindParam(':status', $newStatus); $stmt->bindParam(':id', $about_id); $stmt->execute(); $success = "About content status updated successfully."; header("Location: about_content.php?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 about_content WHERE id = :id"); $stmt->bindParam(':id', $id); $stmt->execute(); $about = $stmt->fetch(PDO::FETCH_ASSOC); if (!$about) { $error = "Content 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 about_content"); $stmt->execute(); $totalRecords = $stmt->fetchColumn(); $stmt = $db->prepare("SELECT * FROM about_content 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(); $aboutItems = $stmt->fetchAll(PDO::FETCH_ASSOC); $totalPages = ceil($totalRecords / $limit); } catch (PDOException $e) { logError("Database query error for about_content: " . $e->getMessage()); $error = "Database error: " . $e->getMessage(); $aboutItems = []; $totalPages = 0; } // Set page title $pageTitle = ($action === 'add') ? "Add New About Content" : (($action === 'edit') ? "Edit About Content" : "Manage About Content"); include 'includes/header.php'; ?>