38873-vm/messages.php
Flatlogic Bot 3575e4a095 v70
2026-02-28 23:27:55 +00:00

294 lines
17 KiB
PHP

<?php
require_once 'db/config.php';
session_start();
$user_id = $_SESSION['user_id'] ?? null;
if (!$user_id) { header('Location: login.php'); exit; }
$stmt = db()->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';
// Action: Accept message request
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'accept' && isset($_POST['sender_id'])) {
$sender_id = (int)$_POST['sender_id'];
$stmt = db()->prepare("UPDATE messages SET status = 'accepted' WHERE sender_id = ? AND receiver_id = ? AND status = 'pending'");
$stmt->execute([$sender_id, $user_id]);
header("Location: messages.php?chat_with=" . $sender_id);
exit;
}
// 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, 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 != 'rejected'
AND u.id NOT IN ($placeholders)
";
$stmt = db()->prepare($sql);
$params = array_merge([$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;
}
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Messages — <?= htmlspecialchars($platformName) ?></title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800;900&display=swap" rel="stylesheet">
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
.profile-avatar-link {
text-decoration: none;
transition: opacity 0.2s;
}
.profile-avatar-link:hover {
opacity: 0.8;
}
</style>
</head>
<body>
<header>
<div class="container" style="display: flex; justify-content: space-between; align-items: center; width: 100%;">
<a href="dashboard.php" class="logo-container">
<img src="assets/images/logo.svg?v=<?php echo time(); ?>" alt="<?= htmlspecialchars($platformName) ?> Logo" class="logo-img">
<span class="logo-text"><?= htmlspecialchars($platformName) ?></span>
</a>
<nav class="nav-links">
<?php if ($user['role'] === 'founder'): ?>
<a href="startups.php">My Startups</a>
<a href="partners.php">Find Partners</a>
<?php else: ?>
<a href="funding_rounds.php">Founding Rounds</a>
<a href="portfolio.php">Portfolio</a>
<a href="discover.php">Discovery Hub</a>
<?php endif; ?>
<a href="messages.php" class="active">Messages</a>
</nav>
<div style="display: flex; align-items: center; gap: 15px;">
<a href="notifications.php" style="color: var(--text-secondary); position: relative; font-size: 18px;">
<i class="fas fa-bell"></i>
</a>
<div style="display: flex; align-items: center; gap: 10px; padding: 5px 12px; background: rgba(255,255,255,0.05); border-radius: 50px; border: 1px solid var(--border-color);">
<a href="profile.php?id=<?= $user['id'] ?>" style="text-decoration: none; display: flex; align-items: center; gap: 10px;">
<div style="width: 24px; height: 24px; background: var(--gradient-primary); border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 10px; color: #fff; font-weight: 800;">
<?= substr($user['full_name'], 0, 1) ?>
</div>
<span style="font-size: 13px; font-weight: 600; color: #fff;"><?= htmlspecialchars(explode(' ', $user['full_name'])[0]) ?></span>
</a>
</div>
<a href="logout.php" class="btn btn-secondary" style="padding: 8px 16px; font-size: 12px; border-radius: 10px;">Log Out</a>
</div>
</div>
</header>
<main class="container" style="padding-top: 30px; padding-bottom: 50px; height: calc(100vh - 100px); display: flex; gap: 30px;">
<div style="width: 350px; flex-shrink: 0; background: var(--card-bg); border: 1px solid var(--border-color); border-radius: 24px; display: flex; flex-direction: column; overflow: hidden;">
<div style="padding: 25px; border-bottom: 1px solid var(--border-color);">
<h2 style="margin: 0; font-size: 24px;">Inbox</h2>
</div>
<?php if ($user['role'] === 'founder' && !empty($matches)): ?>
<div style="padding: 15px 25px; font-size: 12px; text-transform: uppercase; letter-spacing: 1px; color: var(--text-secondary); font-weight: 700;">New Matches</div>
<div style="padding: 0 15px 15px; display: flex; gap: 10px; overflow-x: auto; scrollbar-width: none;">
<?php foreach ($matches as $match): ?>
<a href="messages.php?chat_with=<?= $match['match_id'] ?>" style="flex-shrink: 0; width: 60px; height: 60px; background: var(--surface-color); border: 1px solid var(--accent-blue); border-radius: 18px; display: flex; align-items: center; justify-content: center; font-size: 20px; font-weight: 800; color: #fff; text-decoration: none;">
<?= substr($match['match_name'], 0, 1) ?>
</a>
<?php endforeach; ?>
</div>
<?php endif; ?>
<div style="flex: 1; overflow-y: auto; padding: 10px;">
<?php if (empty($conversations)): ?>
<div style="text-align: center; padding: 40px 20px; color: var(--text-secondary);">
<i class="fas fa-comments" style="font-size: 32px; opacity: 0.2; margin-bottom: 15px;"></i>
<p style="font-size: 14px;">No active conversations yet.</p>
</div>
<?php else: ?>
<?php foreach ($conversations as $conv): ?>
<div style="display: flex; align-items: center; gap: 5px; margin-bottom: 5px;">
<a href="messages.php?chat_with=<?= $conv['other_user_id'] ?>" style="display: flex; align-items: center; gap: 15px; padding: 15px; border-radius: 16px; text-decoration: none; color: inherit; transition: background 0.2s; flex: 1; <?= $active_chat_id == $conv['other_user_id'] ? 'background: rgba(255,255,255,0.06); border: 1px solid var(--border-color);' : '' ?>">
<div style="width: 48px; height: 48px; border-radius: 14px; background: var(--surface-color); display: flex; align-items: center; justify-content: center; font-weight: 800; border: 1px solid var(--border-color);">
<?= substr($conv['other_user_name'], 0, 1) ?>
</div>
<div style="flex: 1; overflow: hidden;">
<div style="font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;"><?= htmlspecialchars($conv['other_user_name']) ?></div>
<div style="font-size: 12px; color: var(--text-secondary);"><?= ucfirst($conv['other_role']) ?></div>
</div>
</a>
<a href="profile.php?id=<?= $conv['other_user_id'] ?>" title="View Profile" style="padding: 10px; color: var(--text-secondary);"><i class="fas fa-user-circle"></i></a>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
</div>
<div style="flex: 1; background: var(--card-bg); border: 1px solid var(--border-color); border-radius: 24px; display: flex; flex-direction: column; overflow: hidden; position: relative;">
<?php if (!$active_chat_user): ?>
<div style="flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; color: var(--text-secondary); text-align: center; padding: 40px;">
<div style="width: 100px; height: 100px; background: var(--surface-color); border-radius: 30px; display: flex; align-items: center; justify-content: center; font-size: 40px; margin-bottom: 30px; border: 1px solid var(--border-color);">
<i class="fas fa-paper-plane" style="opacity: 0.1;"></i>
</div>
<h3>Your Conversations</h3>
<p style="max-width: 300px;">Select a person from the sidebar to start chatting or view your match history.</p>
</div>
<?php else: ?>
<div style="padding: 20px 30px; border-bottom: 1px solid var(--border-color); display: flex; justify-content: space-between; align-items: center; background: rgba(255,255,255,0.02);">
<div style="display: flex; align-items: center; gap: 15px;">
<a href="profile.php?id=<?= $active_chat_user['id'] ?>" class="profile-avatar-link">
<div style="width: 40px; height: 40px; border-radius: 12px; background: var(--surface-color); display: flex; align-items: center; justify-content: center; font-weight: 800; border: 1px solid var(--border-color);">
<?= substr($active_chat_user['full_name'], 0, 1) ?>
</div>
</a>
<div>
<a href="profile.php?id=<?= $active_chat_user['id'] ?>" style="text-decoration: none; color: inherit;"><div style="font-weight: 700;"><?= htmlspecialchars($active_chat_user['full_name']) ?></div></a>
<div style="font-size: 12px; color: var(--accent-blue); font-weight: 600;">Online</div>
</div>
</div>
<a href="profile.php?id=<?= $active_chat_user['id'] ?>" class="btn btn-outline" style="padding: 8px 16px; font-size: 12px;">View Profile</a>
</div>
<div style="flex: 1; overflow-y: auto; padding: 30px; display: flex; flex-direction: column; gap: 15px;" id="chat-scroller">
<?php if (empty($chat_messages)): ?>
<div style="text-align: center; padding: 40px; color: var(--text-secondary);">
<p style="font-size: 14px; font-style: italic;">No messages yet. Say hi!</p>
</div>
<?php else: ?>
<?php foreach ($chat_messages as $msg):
$is_me = ($msg['sender_id'] == $user_id);
?>
<div style="display: flex; justify-content: <?= $is_me ? 'flex-end' : 'flex-start' ?>;">
<div style="max-width: 70%; padding: 14px 20px; border-radius: 20px; font-size: 15px; line-height: 1.5; <?= $is_me ? 'background: var(--gradient-primary); color: #fff; border-bottom-right-radius: 4px;' : 'background: var(--surface-color); border: 1px solid var(--border-color); border-bottom-left-radius: 4px;' ?>">
<?= nl2br(htmlspecialchars($msg['content'])) ?>
<div style="font-size: 10px; margin-top: 8px; opacity: 0.6; text-align: right;">
<?= date('H:i', strtotime($msg['created_at'])) ?>
<?php if ($msg['status'] === 'pending' && $is_me): ?>
<i class="fas fa-clock" title="Pending acceptance" style="margin-left: 5px;"></i>
<?php endif; ?>
</div>
</div>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
<?php if ($needs_acceptance): ?>
<div style="padding: 20px 30px; background: rgba(0, 242, 255, 0.05); border-top: 1px solid var(--border-color); text-align: center;">
<p style="font-size: 14px; margin-bottom: 15px;">Accept this message request to reply.</p>
<form method="POST">
<input type="hidden" name="action" value="accept">
<input type="hidden" name="sender_id" value="<?= $active_chat_id ?>">
<button type="submit" class="btn btn-primary">Accept & Reply</button>
</form>
</div>
<?php else: ?>
<div style="padding: 25px 30px; border-top: 1px solid var(--border-color);">
<form method="POST" style="display: flex; gap: 15px;">
<input type="text" name="content" placeholder="Type your message..." style="flex: 1; background: var(--surface-color); border: 1px solid var(--border-color); color: #fff; padding: 15px 20px; border-radius: 15px;" required autocomplete="off">
<button type="submit" class="btn btn-primary" style="width: 50px; height: 50px; border-radius: 15px; padding: 0; display: flex; align-items: center; justify-content: center;">
<i class="fas fa-paper-plane"></i>
</button>
</form>
</div>
<?php endif; ?>
<?php endif; ?>
</div>
</main>
<script>
const scroller = document.getElementById('chat-scroller');
if (scroller) scroller.scrollTop = scroller.scrollHeight;
</script>
</body>
</html>