38873-vm/messages.php
Flatlogic Bot 844a54704c v4
2026-02-28 16:01:57 +00:00

246 lines
12 KiB
PHP

<?php
session_start();
if (!isset($_SESSION['user_id'])) {
header("Location: login.php");
exit;
}
require_once __DIR__ . '/db/config.php';
$stmt = db()->prepare("SELECT * FROM users WHERE id = ?");
$stmt->execute([$_SESSION['user_id']]);
$user = $stmt->fetch();
$platformName = defined('PLATFORM_NAME') ? PLATFORM_NAME : 'Gatsby';
// Fetch Matches for Founders
$matches = [];
if ($user['role'] === 'founder') {
$stmt = db()->prepare("
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 != ?
");
$stmt->execute([$_SESSION['user_id'], $_SESSION['user_id'], $_SESSION['user_id']]);
$matches = $stmt->fetchAll();
}
// Fetch Conversations
$stmt = db()->prepare("
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 != ?
");
$stmt->execute([$_SESSION['user_id'], $_SESSION['user_id'], $_SESSION['user_id']]);
$conversations = $stmt->fetchAll();
$active_chat_id = isset($_GET['chat_with']) ? (int)$_GET['chat_with'] : null;
$active_chat_user = null;
$chat_messages = [];
if ($active_chat_id) {
$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 = ?)
ORDER BY created_at ASC
");
$stmt->execute([$_SESSION['user_id'], $active_chat_id, $active_chat_id, $_SESSION['user_id']]);
$chat_messages = $stmt->fetchAll();
}
}
// Handle sending message
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['content']) && $active_chat_id) {
$content = trim($_POST['content']);
if (!empty($content)) {
$stmt = db()->prepare("INSERT INTO messages (sender_id, receiver_id, content) VALUES (?, ?, ?)");
$stmt->execute([$_SESSION['user_id'], $active_chat_id, $content]);
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;
}
.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; gap: 15px; }
.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;
}
.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;
}
</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
// Check if already has conversation
$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): ?>
<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']) ?></div>
</div>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
<div class="chat-main">
<?php if ($active_chat_user): ?>
<div class="chat-header">
<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="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'] == $_SESSION['user_id'] ? 'msg-sent' : 'msg-received' ?>">
<?= htmlspecialchars($msg['content']) ?>
<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>
<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="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>