35311-vm/messages.php
2025-10-30 00:25:31 +00:00

96 lines
3.9 KiB
PHP

<?php
session_start();
require_once 'db/config.php';
if (!isset($_SESSION['user_id']) || $_SESSION['user_role'] !== 'resident') {
header("Location: index.php");
exit;
}
$pdo = db();
$user_id = $_SESSION['user_id'];
// Fetch messages for the resident
$stmt = $pdo->prepare("
SELECT m.id, m.subject, m.created_at, m.read_at, u.email as other_party_email
FROM messages m
JOIN users u ON (m.sender_user_id = u.id AND m.recipient_user_id = ?) OR (m.recipient_user_id = u.id AND m.sender_user_id = ?)
WHERE m.sender_user_id = ? OR m.recipient_user_id = ?
ORDER BY m.created_at DESC
");
$stmt->execute([$user_id, $user_id, $user_id, $user_id]);
$messages = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Messages - 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="resident_dashboard.php">Continuum of Healing</a>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link" href="resident_dashboard.php">Dashboard</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="messages.php">Messages</a>
</li>
</ul>
<a href="logout.php" class="btn btn-outline-light">Logout</a>
</div>
</div>
</nav>
<div class="container mt-4">
<div class="d-flex justify-content-between align-items-center mb-4">
<h1 class="h2">My Messages</h1>
<a href="resident_compose_message.php" class="btn btn-primary-custom">New Message</a>
</div>
<div class="card">
<div class="card-body">
<div class="table-responsive">
<table class="table table-hover">
<thead>
<tr>
<th>Subject</th>
<th>From/To</th>
<th>Date</th>
<th></th>
</tr>
</thead>
<tbody>
<?php if (empty($messages)): ?>
<tr>
<td colspan="4" class="text-center">You have no messages.</td>
</tr>
<?php else: ?>
<?php foreach ($messages as $message): ?>
<tr class="<?php echo !$message['read_at'] ? 'fw-bold' : ''; ?>">
<td><?php echo htmlspecialchars($message['subject']); ?></td>
<td><?php echo htmlspecialchars($message['other_party_email']); ?></td>
<td><?php echo date("M j, Y, g:i a", strtotime($message['created_at'])); ?></td>
<td><a href="view_message.php?id=<?php echo $message['id']; ?>" class="btn btn-sm btn-primary">View</a></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>