38873-vm/messages.php
Flatlogic Bot fc3fe3f0a0 v15
2026-02-28 17:48:09 +00:00

423 lines
22 KiB
PHP

<?php
session_start();
if (!isset($_SESSION['user_id'])) {
header("Location: login.php");
exit;
}
require_once __DIR__ . '/db/config.php';
$user_id = $_SESSION['user_id'];
$stmt = db()->prepare("SELECT * FROM users WHERE id = ?");
$stmt->execute([$user_id]);
$user = $stmt->fetch();
$platformName = defined('PLATFORM_NAME') ? PLATFORM_NAME : 'Gatsby';
// Handle Actions (Accept/Reject/Unmatch/Block)
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && isset($_POST['other_user_id'])) {
$other_id = (int)$_POST['other_user_id'];
$action = $_POST['action'];
if ($action === 'accept') {
$stmt = db()->prepare("UPDATE messages SET status = 'accepted' WHERE sender_id = ? AND receiver_id = ? AND status = 'pending'");
$stmt->execute([$other_id, $user_id]);
$stmt = db()->prepare("INSERT INTO notifications (user_id, content) VALUES (?, ?)");
$stmt->execute([$other_id, "Your message request to " . $user['full_name'] . " has been accepted."]);
header("Location: messages.php?chat_with=$other_id");
exit;
} elseif ($action === 'reject') {
$stmt = db()->prepare("UPDATE messages SET status = 'rejected' WHERE sender_id = ? AND receiver_id = ? AND status = 'pending'");
$stmt->execute([$other_id, $user_id]);
header("Location: messages.php");
exit;
} elseif ($action === 'unmatch') {
$stmt = db()->prepare("UPDATE matches SET status = 'unmatched' WHERE (user1_id = ? AND user2_id = ?) OR (user1_id = ? AND user2_id = ?)");
$stmt->execute([$user_id, $other_id, $other_id, $user_id]);
header("Location: messages.php");
exit;
} elseif ($action === 'block') {
$stmt = db()->prepare("INSERT IGNORE INTO blocked_users (blocker_id, blocked_id) VALUES (?, ?)");
$stmt->execute([$user_id, $other_id]);
// Also unmatch if they were matched
$stmt = db()->prepare("UPDATE matches SET status = 'unmatched' WHERE (user1_id = ? AND user2_id = ?) OR (user1_id = ? AND user2_id = ?)");
$stmt->execute([$user_id, $other_id, $other_id, $user_id]);
header("Location: messages.php");
exit;
}
}
// Fetch Blocked Users to filter them out
$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 m.*,
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)
ORDER BY (SELECT MAX(created_at) FROM messages WHERE (sender_id = m.sender_id AND receiver_id = m.receiver_id) OR (sender_id = m.receiver_id AND receiver_id = m.sender_id)) DESC
";
$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;
$startup_id = isset($_GET['startup_id']) ? (int)$_GET['startup_id'] : null;
$active_chat_user = null;
$chat_messages = [];
$can_reply = true;
$needs_acceptance = false;
$is_currently_matched = false;
if ($active_chat_id) {
// Check if blocked
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]);
$has_pending_from_other = (bool)$stmt->fetchColumn();
if (!$has_accepted && !$is_currently_matched && $has_pending_from_other) {
$can_reply = false;
$needs_acceptance = true;
}
}
}
// Handle sending message
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['content']) && $active_chat_id && $can_reply) {
$content = trim($_POST['content']);
if (!empty($content)) {
$status = 'accepted';
$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_match = (bool)$stmt->fetchColumn();
$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]);
$is_accepted = (bool)$stmt->fetchColumn();
if (!$is_match && !$is_accepted) {
if ($startup_id) {
$stmt = db()->prepare("SELECT status, name FROM startups WHERE id = ?");
$stmt->execute([$startup_id]);
$s = $stmt->fetch();
if ($s && $s['status'] === 'private') {
$status = 'pending';
$stmt = db()->prepare("INSERT INTO notifications (user_id, content) VALUES (?, ?)");
$stmt->execute([$active_chat_id, "New message request from " . $user['full_name'] . " regarding " . $s['name'] . "."]);
}
}
}
$stmt = db()->prepare("INSERT INTO messages (sender_id, receiver_id, content, status, startup_id) VALUES (?, ?, ?, ?, ?)");
$stmt->execute([$user_id, $active_chat_id, $content, $status, $startup_id]);
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 href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&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>
.chat-container {
display: grid;
grid-template-columns: 350px 1fr;
gap: 0;
height: calc(100vh - 150px);
background: var(--card-bg);
border-radius: 20px;
overflow: hidden;
border: 1px solid var(--border-color);
}
.chat-sidebar {
border-right: 1px solid var(--border-color);
display: flex;
flex-direction: column;
background: rgba(255,255,255,0.02);
}
.chat-main {
display: flex;
flex-direction: column;
background: transparent;
}
.conv-item {
padding: 15px 20px;
border-bottom: 1px solid var(--border-color);
cursor: pointer;
transition: all 0.2s;
display: flex;
align-items: center;
gap: 12px;
position: relative;
}
.conv-item:hover { background: rgba(255,255,255,0.05); }
.conv-item.active { background: rgba(0, 122, 255, 0.1); border-left: 3px solid var(--accent-blue); }
.avatar-sm {
width: 40px; height: 40px; border-radius: 12px; background: var(--gradient-primary);
display: flex; align-items: center; justify-content: center; font-weight: 700; color: #fff;
}
.chat-header { padding: 15px 25px; border-bottom: 1px solid var(--border-color); display: flex; align-items: center; justify-content: space-between; }
.chat-messages { flex: 1; overflow-y: auto; padding: 25px; display: flex; flex-direction: column; gap: 15px; }
.msg-bubble {
max-width: 70%; padding: 12px 18px; border-radius: 18px; font-size: 14px; line-height: 1.5;
position: relative;
}
.msg-sent { align-self: flex-end; background: var(--accent-blue); color: #fff; border-bottom-right-radius: 4px; }
.msg-received { align-self: flex-start; background: var(--surface-color); border: 1px solid var(--border-color); border-bottom-left-radius: 4px; }
.chat-input { padding: 20px; border-top: 1px solid var(--border-color); display: flex; gap: 15px; }
.chat-input input {
flex: 1; padding: 12px 20px; border-radius: 12px; background: var(--surface-color); border: 1px solid var(--border-color); color: #fff;
}
.status-badge {
font-size: 10px; padding: 2px 6px; border-radius: 4px; font-weight: 700; text-transform: uppercase;
}
.status-pending { background: #ffcc00; color: #000; }
.request-panel {
padding: 20px; background: rgba(255, 204, 0, 0.1); border-top: 1px solid rgba(255, 204, 0, 0.2);
text-align: center;
}
/* Action Menu */
.action-menu { position: relative; }
.action-menu-content {
display: none; position: absolute; right: 0; top: 100%;
background: var(--surface-color); border: 1px solid var(--border-color);
border-radius: 12px; box-shadow: 0 10px 30px rgba(0,0,0,0.5);
z-index: 100; min-width: 150px;
}
.action-menu:hover .action-menu-content { display: block; }
.action-link {
display: block; padding: 10px 15px; color: var(--text-secondary);
text-decoration: none; font-size: 13px; transition: all 0.2s;
background: none; border: none; width: 100%; text-align: left; cursor: pointer;
}
.action-link:hover { background: rgba(255,255,255,0.05); color: #fff; }
.action-link.danger { color: #ff3b30; }
.action-link.danger:hover { background: rgba(255, 59, 48, 0.1); }
</style>
</head>
<body>
<header>
<div class="container" style="display: flex; justify-content: space-between; align-items: center; width: 100%;">
<div class="logo"><?= htmlspecialchars($platformName) ?></div>
<nav class="nav-links">
<?php if ($user['role'] === 'founder'): ?>
<a href="startups.php">My Startups</a>
<a href="discover.php">Find Partners</a>
<?php else: ?>
<a href="startups.php">Browse Startups</a>
<a href="portfolio.php">Portfolio</a>
<?php endif; ?>
<a href="messages.php" class="active">Messages</a>
<a href="notifications.php">Notifications</a>
</nav>
<div style="display: flex; align-items: center; gap: 15px;">
<a href="dashboard.php" style="color: var(--text-secondary);"><i class="fas fa-th-large"></i></a>
<a href="logout.php" class="btn btn-secondary" style="padding: 8px 16px;">Log Out</a>
</div>
</div>
</header>
<main class="container" style="padding-top: 30px;">
<div class="chat-container">
<div class="chat-sidebar">
<div style="padding: 20px; font-weight: 700; border-bottom: 1px solid var(--border-color);">Conversations</div>
<?php if ($user['role'] === 'founder' && !empty($matches)): ?>
<div style="padding: 10px 20px; font-size: 11px; text-transform: uppercase; color: var(--text-secondary); letter-spacing: 1px;">New Matches</div>
<?php foreach ($matches as $match): ?>
<?php
$hasConv = false;
foreach ($conversations as $c) { if ($c['other_user_id'] == $match['match_id']) $hasConv = true; }
if ($hasConv) continue;
?>
<div class="conv-item <?= $active_chat_id == $match['match_id'] ? 'active' : '' ?>" onclick="window.location.href='messages.php?chat_with=<?= $match['match_id'] ?>'">
<div class="avatar-sm" style="background: var(--accent-blue);"><?= substr($match['match_name'], 0, 1) ?></div>
<div>
<div style="font-weight: 600; font-size: 14px;"><?= htmlspecialchars($match['match_name']) ?></div>
<div style="font-size: 11px; color: var(--accent-blue);">New Match! Say hi!</div>
</div>
</div>
<?php endforeach; ?>
<?php endif; ?>
<div style="padding: 15px 20px 10px; font-size: 11px; text-transform: uppercase; color: var(--text-secondary); letter-spacing: 1px;">Recent Chats</div>
<?php if (empty($conversations)): ?>
<div style="padding: 20px; text-align: center; color: var(--text-secondary); font-size: 13px;">No active chats.</div>
<?php else: ?>
<?php foreach ($conversations as $conv): ?>
<?php
$stmt = db()->prepare("SELECT 1 FROM messages WHERE sender_id = ? AND receiver_id = ? AND status = 'pending'");
$stmt->execute([$conv['other_user_id'], $user_id]);
$isPending = (bool)$stmt->fetchColumn();
?>
<div class="conv-item <?= $active_chat_id == $conv['other_user_id'] ? 'active' : '' ?>" onclick="window.location.href='messages.php?chat_with=<?= $conv['other_user_id'] ?>'">
<div class="avatar-sm"><?= substr($conv['other_user_name'], 0, 1) ?></div>
<div>
<div style="font-weight: 600; font-size: 14px;"><?= htmlspecialchars($conv['other_user_name']) ?></div>
<div style="font-size: 12px; color: var(--text-secondary);">
<?= ucfirst($conv['other_role']) ?>
<?php if ($isPending): ?>
<span class="status-badge status-pending">Request</span>
<?php endif; ?>
</div>
</div>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
<div class="chat-main">
<?php if ($active_chat_user): ?>
<div class="chat-header">
<div style="display: flex; align-items: center; gap: 15px;">
<div class="avatar-sm"><?= substr($active_chat_user['full_name'], 0, 1) ?></div>
<div>
<div style="font-weight: 700;"><?= htmlspecialchars($active_chat_user['full_name']) ?></div>
<div style="font-size: 12px; color: var(--text-secondary);"><?= ucfirst($active_chat_user['role']) ?></div>
</div>
</div>
<div class="action-menu">
<button class="btn btn-secondary" style="padding: 5px 12px;"><i class="fas fa-ellipsis-v"></i></button>
<div class="action-menu-content">
<form method="POST" onsubmit="return confirm('Unmatch with this founder?')">
<input type="hidden" name="other_user_id" value="<?= $active_chat_id ?>">
<input type="hidden" name="action" value="unmatch">
<button type="submit" class="action-link">Unmatch</button>
</form>
<form method="POST" onsubmit="return confirm('Block this user? They will not be able to see you or message you.')">
<input type="hidden" name="other_user_id" value="<?= $active_chat_id ?>">
<input type="hidden" name="action" value="block">
<button type="submit" class="action-link danger">Block User</button>
</form>
</div>
</div>
</div>
<div class="chat-messages" id="chat-box">
<?php if (empty($chat_messages)): ?>
<div style="text-align: center; color: var(--text-secondary); margin-top: 50px;">
<i class="fas fa-hand-sparkles" style="font-size: 40px; margin-bottom: 20px; opacity: 0.3;"></i>
<p>This is the start of your conversation with <?= htmlspecialchars(explode(' ', $active_chat_user['full_name'])[0]) ?>.</p>
</div>
<?php else: ?>
<?php foreach ($chat_messages as $msg): ?>
<div class="msg-bubble <?= $msg['sender_id'] == $user_id ? 'msg-sent' : 'msg-received' ?>">
<?= htmlspecialchars($msg['content']) ?>
<?php if ($msg['status'] === 'pending'): ?>
<div style="font-size: 9px; opacity: 0.8; margin-top: 5px;"><i class="fas fa-clock"></i> Pending Request</div>
<?php endif; ?>
<div style="font-size: 10px; opacity: 0.6; margin-top: 5px; text-align: right;">
<?= date('H:i', strtotime($msg['created_at'])) ?>
</div>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
<?php if ($needs_acceptance): ?>
<div class="request-panel">
<p style="margin-bottom: 15px; font-weight: 600;">Would you like to accept this message request?</p>
<form method="POST" style="display: flex; gap: 10px; justify-content: center;">
<input type="hidden" name="other_user_id" value="<?= $active_chat_id ?>">
<button type="submit" name="action" value="accept" class="btn btn-primary" style="padding: 8px 25px;">Accept</button>
<button type="submit" name="action" value="reject" class="btn btn-secondary" style="padding: 8px 25px;">Reject</button>
</form>
</div>
<?php elseif ($can_reply): ?>
<form class="chat-input" method="POST">
<input type="text" name="content" placeholder="Type a message..." required autocomplete="off">
<button type="submit" class="btn btn-primary" style="padding: 0 25px;"><i class="fas fa-paper-plane"></i></button>
</form>
<?php else: ?>
<div style="padding: 20px; text-align: center; color: var(--text-secondary);">
Waiting for <?= htmlspecialchars($active_chat_user['full_name']) ?> to accept your request.
</div>
<?php endif; ?>
<?php else: ?>
<div style="flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; opacity: 0.5;">
<i class="fas fa-comments" style="font-size: 100px; margin-bottom: 30px;"></i>
<h3>Select a conversation to start chatting</h3>
<p>Find co-founders in the matching section.</p>
</div>
<?php endif; ?>
</div>
</div>
</main>
<script>
const chatBox = document.getElementById('chat-box');
if (chatBox) chatBox.scrollTop = chatBox.scrollHeight;
</script>
</body>
</html>