prepare("SELECT * FROM users WHERE id = ?");
$stmt->execute([$user_id]);
$user = $stmt->fetch();
if (!$user) { header('Location: login.php'); exit; }
$platformName = defined('PLATFORM_NAME') ? PLATFORM_NAME : 'Gatsby';
// Fetch Blocked Users
$stmt = db()->prepare("SELECT blocked_id FROM blocked_users WHERE blocker_id = ?");
$stmt->execute([$user_id]);
$blocked_ids = $stmt->fetchAll(PDO::FETCH_COLUMN);
$stmt = db()->prepare("SELECT blocker_id FROM blocked_users WHERE blocked_id = ?");
$stmt->execute([$user_id]);
$blocked_by_ids = $stmt->fetchAll(PDO::FETCH_COLUMN);
$all_blocked = array_unique(array_merge($blocked_ids, $blocked_by_ids));
$placeholders = empty($all_blocked) ? "0" : implode(',', array_fill(0, count($all_blocked), '?'));
// Fetch Matches for Founders (active only, not blocked)
$matches = [];
if ($user['role'] === 'founder') {
$sql = "
SELECT u.full_name as match_name,
u.id as match_id,
u.university,
u.degree_program
FROM matches m
JOIN users u ON (m.user1_id = u.id OR m.user2_id = u.id)
WHERE (m.user1_id = ? OR m.user2_id = ?)
AND u.id != ?
AND m.status = 'active'
AND u.id NOT IN ($placeholders)
";
$stmt = db()->prepare($sql);
$params = array_merge([$user_id, $user_id, $user_id], $all_blocked ?: []);
$stmt->execute($params);
$matches = $stmt->fetchAll();
}
// Fetch Conversations (accepted or pending from others, excluding blocked)
$sql = "
SELECT DISTINCT u.id as other_user_id, u.full_name as other_user_name, u.role as other_role
FROM messages m
JOIN users u ON (m.sender_id = u.id OR m.receiver_id = u.id)
WHERE ((m.sender_id = ? OR m.receiver_id = ?) AND u.id != ?)
AND (m.status = 'accepted' OR (m.status = 'pending' AND m.receiver_id = ?))
AND u.id NOT IN ($placeholders)
";
$stmt = db()->prepare($sql);
$params = array_merge([$user_id, $user_id, $user_id, $user_id], $all_blocked ?: []);
$stmt->execute($params);
$conversations = $stmt->fetchAll();
$active_chat_id = isset($_GET['chat_with']) ? (int)$_GET['chat_with'] : null;
$active_chat_user = null;
$chat_messages = [];
$can_reply = true;
$needs_acceptance = false;
$is_currently_matched = false;
if ($active_chat_id) {
if (in_array($active_chat_id, $all_blocked)) { header("Location: messages.php"); exit; }
$stmt = db()->prepare("SELECT id, full_name, role FROM users WHERE id = ?");
$stmt->execute([$active_chat_id]);
$active_chat_user = $stmt->fetch();
if ($active_chat_user) {
$stmt = db()->prepare("SELECT * FROM messages WHERE ((sender_id = ? AND receiver_id = ?) OR (sender_id = ? AND receiver_id = ?)) AND status != 'rejected' ORDER BY created_at ASC");
$stmt->execute([$user_id, $active_chat_id, $active_chat_id, $user_id]);
$chat_messages = $stmt->fetchAll();
$stmt = db()->prepare("SELECT 1 FROM messages WHERE ((sender_id = ? AND receiver_id = ?) OR (sender_id = ? AND receiver_id = ?)) AND status = 'accepted'");
$stmt->execute([$user_id, $active_chat_id, $active_chat_id, $user_id]);
$has_accepted = (bool)$stmt->fetchColumn();
$stmt = db()->prepare("SELECT 1 FROM matches WHERE ((user1_id = ? AND user2_id = ?) OR (user1_id = ? AND user2_id = ?)) AND status = 'active'");
$stmt->execute([$user_id, $active_chat_id, $active_chat_id, $user_id]);
$is_currently_matched = (bool)$stmt->fetchColumn();
$stmt = db()->prepare("SELECT 1 FROM messages WHERE sender_id = ? AND receiver_id = ? AND status = 'pending'");
$stmt->execute([$active_chat_id, $user_id]);
if (!$has_accepted && !$is_currently_matched && (bool)$stmt->fetchColumn()) { $can_reply = false; $needs_acceptance = true; }
}
}
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['content']) && $active_chat_id && $can_reply) {
$content = trim($_POST['content']);
if (!empty($content)) {
$status = ($is_currently_matched || (isset($has_accepted) && $has_accepted)) ? 'accepted' : 'pending';
$stmt = db()->prepare("INSERT INTO messages (sender_id, receiver_id, content, status) VALUES (?, ?, ?, ?)");
$stmt->execute([$user_id, $active_chat_id, $content, $status]);
header("Location: messages.php?chat_with=" . $active_chat_id);
exit;
}
}
?>
Messages — = htmlspecialchars($platformName) ?>
Your Conversations
Select a person from the sidebar to start chatting or view your match history.
= substr($active_chat_user['full_name'], 0, 1) ?>
= htmlspecialchars($active_chat_user['full_name']) ?>
Online
Accept this message request to reply.