35311-vm/view_message.php
2025-10-30 00:12:08 +00:00

96 lines
3.6 KiB
PHP

<?php
session_start();
require_once 'db/config.php';
if (!isset($_SESSION['user_id'])) {
header("Location: index.php");
exit;
}
$message_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if ($message_id === 0) {
$redirect_url = ($_SESSION['user_role'] === 'staff') ? 'staff_dashboard.php' : 'partner_dashboard.php';
header("Location: " . $redirect_url);
exit;
}
$pdo = db();
// Fetch message details
$stmt = $pdo->prepare("
SELECT m.*, u.email as sender_email
FROM messages m
JOIN users u ON m.sender_user_id = u.id
WHERE m.id = ?
");
$stmt->execute([$message_id]);
$message = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$message || ($message['recipient_user_id'] != $_SESSION['user_id'] && $message['sender_user_id'] != $_SESSION['user_id'])) {
// Message not found or user is not part of the conversation
$redirect_url = ($_SESSION['user_role'] === 'staff') ? 'staff_dashboard.php' : 'partner_dashboard.php';
header("Location: " . $redirect_url . "?error=not_found");
exit;
}
// Mark as read if the current user is the recipient
if ($message['recipient_user_id'] == $_SESSION['user_id'] && !$message['read_at']) {
$pdo->prepare("UPDATE messages SET read_at = NOW() WHERE id = ?")->execute([$message_id]);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>View Message - Continuum of Healing</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="assets/css/custom.css">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<div class="container-fluid">
<a class="navbar-brand" href="#">Continuum of Healing</a>
<a href="logout.php" class="btn btn-outline-light">Logout</a>
</div>
</nav>
<div class="container mt-4">
<div class="d-flex justify-content-between align-items-center mb-4">
<h1 class="h2">View Message</h1>
<a href="partner_dashboard.php" class="btn btn-secondary">← Back to Dashboard</a>
</div>
<div class="card">
<div class="card-header">
<h5><?php echo htmlspecialchars($message['subject']); ?></h5>
<small class="text-muted">From: <?php echo htmlspecialchars($message['sender_email']); ?> on <?php echo date("M j, Y, g:i a", strtotime($message['created_at'])); ?></small>
</div>
<div class="card-body">
<p><?php echo nl2br(htmlspecialchars($message['body'])); ?></p>
</div>
</div>
<hr class="my-4">
<div class="card">
<div class="card-header">Reply</div>
<div class="card-body">
<form action="send_message.php" method="POST">
<input type="hidden" name="recipient_user_id" value="<?php echo $message['sender_user_id']; ?>">
<input type="hidden" name="subject" value="Re: <?php echo htmlspecialchars($message['subject']); ?>">
<div class="mb-3">
<label for="body" class="form-label">Message</label>
<textarea name="body" id="body" class="form-control" rows="5" required></textarea>
</div>
<button type="submit" class="btn btn-primary-custom">Send Reply</button>
</form>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>