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; } $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; } 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 'president_message'"); if ($stmt->rowCount() == 0) { error_log("Table 'president_message' does not exist in database."); $error = "Table 'president_message' not found in database."; $messages = []; $totalPages = 0; } } catch (PDOException $e) { error_log("Database connection error: " . $e->getMessage()); $error = "Database error: " . $e->getMessage(); $messages = []; $totalPages = 0; } // Handle form submission if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (isset($_POST['action'])) { $formAction = $_POST['action']; if ($formAction === 'add' || $formAction === 'edit') { $president_name = isset($_POST['president_name']) ? sanitizeInput($_POST['president_name']) : ''; $designation = isset($_POST['designation']) ? sanitizeInput($_POST['designation']) : ''; $message = isset($_POST['message']) ? $_POST['message'] : ''; // Message may contain HTML, so don't sanitize $status = isset($_POST['status']) ? sanitizeInput($_POST['status']) : 'active'; if (empty($president_name) || empty($designation) || empty($message)) { $error = "President's name, designation, and message are required."; } else { try { $image = null; if (isset($_FILES['image']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) { $uploadResult = uploadFile($_FILES['image'], 'img/president'); if ($uploadResult['success']) { $image = $uploadResult['filename']; } else { throw new Exception($uploadResult['message']); } } if ($formAction === 'add') { $stmt = $db->prepare("INSERT INTO president_message (president_name, designation, message, image, status, created_at) VALUES (:president_name, :designation, :message, :image, :status, NOW())"); $stmt->bindParam(':president_name', $president_name); $stmt->bindParam(':designation', $designation); $stmt->bindParam(':message', $message); $stmt->bindParam(':image', $image); $stmt->bindParam(':status', $status); $stmt->execute(); $success = "President's message added successfully."; } else { $message_id = isset($_POST['message_id']) ? (int)$_POST['message_id'] : 0; $stmt = $db->prepare("SELECT image FROM president_message WHERE id = :id"); $stmt->bindParam(':id', $message_id); $stmt->execute(); $currentData = $stmt->fetch(); if (!$currentData) { throw new Exception("Message not found."); } if (empty($image)) { $image = $currentData['image']; } else { if (!empty($currentData['image'])) { $oldImagePath = __DIR__ . '/../img/president/' . $currentData['image']; if (file_exists($oldImagePath)) { unlink($oldImagePath); } } } $stmt = $db->prepare("UPDATE president_message SET president_name = :president_name, designation = :designation, message = :message, image = :image, status = :status WHERE id = :id"); $stmt->bindParam(':president_name', $president_name); $stmt->bindParam(':designation', $designation); $stmt->bindParam(':message', $message); $stmt->bindParam(':image', $image); $stmt->bindParam(':status', $status); $stmt->bindParam(':id', $message_id); $stmt->execute(); $success = "President's message updated successfully."; } header("Location: president_message.php?success=" . urlencode($success)); exit; } catch (Exception $e) { $error = "Error: " . $e->getMessage(); } } } if ($formAction === 'delete') { $message_id = isset($_POST['message_id']) ? (int)$_POST['message_id'] : 0; try { $stmt = $db->prepare("SELECT image FROM president_message WHERE id = :id"); $stmt->bindParam(':id', $message_id); $stmt->execute(); $data = $stmt->fetch(); $stmt = $db->prepare("DELETE FROM president_message WHERE id = :id"); $stmt->bindParam(':id', $message_id); $stmt->execute(); if (!empty($data['image'])) { $imagePath = __DIR__ . '/../img/president/' . $data['image']; if (file_exists($imagePath)) { unlink($imagePath); } } $success = "President's message deleted successfully."; header("Location: president_message.php?success=" . urlencode($success)); exit; } catch (PDOException $e) { $error = "Database error: " . $e->getMessage(); } } if ($formAction === 'toggle') { $message_id = isset($_POST['message_id']) ? (int)$_POST['message_id'] : 0; try { $stmt = $db->prepare("SELECT status FROM president_message WHERE id = :id"); $stmt->bindParam(':id', $message_id); $stmt->execute(); $currentData = $stmt->fetch(); if (!$currentData) { throw new Exception("Message not found."); } $newStatus = $currentData['status'] === 'active' ? 'inactive' : 'active'; $stmt = $db->prepare("UPDATE president_message SET status = :status WHERE id = :id"); $stmt->bindParam(':status', $newStatus); $stmt->bindParam(':id', $message_id); $stmt->execute(); $success = "President's message status updated successfully."; header("Location: president_message.php?success=" . urlencode($success)); exit; } catch (Exception $e) { $error = "Error: " . $e->getMessage(); } } } } // Get data for edit if ($action === 'edit' && $id > 0) { try { $stmt = $db->prepare("SELECT * FROM president_message WHERE id = :id"); $stmt->bindParam(':id', $id); $stmt->execute(); $message = $stmt->fetch(PDO::FETCH_ASSOC); if (!$message) { $error = "Message not found."; $action = 'list'; } } catch (PDOException $e) { error_log("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 president_message"); $stmt->execute(); $totalRecords = $stmt->fetchColumn(); if ($totalRecords == 0) { error_log("No records found in president_message table."); } $stmt = $db->prepare("SELECT * FROM president_message ORDER BY created_at DESC LIMIT :limit OFFSET :offset"); $stmt->bindValue(':limit', $limit, PDO::PARAM_INT); $stmt->bindValue(':offset', $offset, PDO::PARAM_INT); $stmt->execute(); $messages = $stmt->fetchAll(PDO::FETCH_ASSOC); error_log("Fetched " . count($messages) . " records from president_message."); $totalPages = ceil($totalRecords / $limit); } catch (PDOException $e) { error_log("Database query error for president_message: " . $e->getMessage()); $error = "Database error: " . $e->getMessage(); $messages = []; $totalPages = 0; } // Set page title // $pageTitle = ($action === 'add') ? "Add New President's Message" : // (($action === 'edit') ? "Edit President's Message" : "President's Message Management"); include 'includes/header.php'; ?>