File size: 5.59Kb
<?php
require_once 'db.php';
if (empty($_SESSION['user_id'])) {
header('Location: login.php');
exit;
}
$user_id = (int)$_SESSION['user_id'];
if (function_exists('is_banned') && is_banned($user_id)) {
include 'header.php';
echo '<p>Ваш аккаунт заблокирован.</p>';
include 'footer.php';
exit;
}
/* небольшая функция для обрезки текста */
function cut_text($text, $limit = 60) {
$text = trim((string)$text);
if (strlen($text) <= $limit) return $text;
return substr($text, 0, $limit - 3) . '...';
}
/* 1. Берём все сообщения с участием пользователя */
$q = $mysqli->query("
SELECT id, from_user_id, to_user_id, body, created_at, is_read
FROM messages
WHERE from_user_id = $user_id OR to_user_id = $user_id
ORDER BY created_at DESC, id DESC
");
$dialogs_raw = []; // other_id => ['last'=>row,'unread'=>int]
if ($q) {
while ($row = $q->fetch_assoc()) {
$other_id = ($row['from_user_id'] == $user_id)
? (int)$row['to_user_id']
: (int)$row['from_user_id'];
if (!isset($dialogs_raw[$other_id])) {
$dialogs_raw[$other_id] = [
'last' => $row,
'unread' => 0,
];
}
if ($row['to_user_id'] == $user_id && !$row['is_read']) {
$dialogs_raw[$other_id]['unread']++;
}
}
}
/* 2. Подтягиваем данные пользователей */
$dialogs = [];
if (!empty($dialogs_raw)) {
$ids = implode(',', array_keys($dialogs_raw));
$u = $mysqli->query("SELECT id, name, city, photo FROM users WHERE id IN ($ids)");
$users = [];
while ($row = $u->fetch_assoc()) {
$users[(int)$row['id']] = $row;
}
foreach ($dialogs_raw as $oid => $info) {
if (!isset($users[$oid])) continue;
$dialogs[] = [
'other_id' => $oid,
'user' => $users[$oid],
'last' => $info['last'],
'unread' => $info['unread'],
];
}
}
include 'header.php';
?>
<style>
.dialogs-page { margin-top: 18px; }
.dialog-card {
background:#ffffff;
border-radius:22px;
padding:16px 18px;
box-shadow:0 10px 30px rgba(0,0,0,0.06);
}
.dialog-item{
display:flex;
align-items:center;
justify-content:space-between;
gap:10px;
padding:10px 0;
border-bottom:1px solid rgba(0,0,0,0.04);
}
.dialog-item:last-child{border-bottom:none;}
.dialog-link{
display:flex;
align-items:center;
gap:10px;
text-decoration:none;
color:inherit;
flex:1;
}
.dialog-avatar{
width:46px;height:46px;border-radius:999px;object-fit:cover;
}
.dialog-avatar-placeholder{
width:46px;height:46px;border-radius:999px;
background:linear-gradient(135deg,#ffe5f0,#f5ebff);
display:flex;align-items:center;justify-content:center;font-size:22px;
}
.dialog-name{font-size:14px;font-weight:600;}
.dialog-city{font-size:12px;color:#7b7287;}
.dialog-preview{font-size:12px;color:#4a4257;margin-top:3px;}
.dialog-meta{text-align:right;font-size:11px;color:#a29ab4;}
.dialog-unread{
display:inline-block;
min-width:18px;
padding:0 6px;
height:18px;
border-radius:999px;
background:#ff3b68;
color:#fff;
font-size:11px;
line-height:18px;
text-align:center;
margin-top:4px;
}
@media (max-width:768px){
.dialog-card{margin:0 -4px 60px;border-radius:18px;}
}
</style>
<div class="dialogs-page">
<h1>Сообщения</h1>
<div class="dialog-card">
<?php if (empty($dialogs)): ?>
<p style="font-size:14px;color:#7b7287;">
У вас пока нет диалогов. Напишите кому-нибудь из анкет 🙂
</p>
<?php else: ?>
<?php foreach ($dialogs as $d): ?>
<?php
$other_id = (int)$d['other_id'];
$u = $d['user'];
$last = $d['last'];
$unread = (int)$d['unread'];
?>
<div class="dialog-item">
<a href="messages.php?user_id=<?= $other_id ?>" class="dialog-link">
<?php if (!empty($u['photo'])): ?>
<img src="/assets/img/<?= htmlspecialchars($u['photo']) ?>" class="dialog-avatar" alt="">
<?php else: ?>
<div class="dialog-avatar-placeholder">🙂</div>
<?php endif; ?>
<div>
<div class="dialog-name"><?= htmlspecialchars($u['name'] ?: 'Без имени') ?></div>
<?php if (!empty($u['city'])): ?>
<div class="dialog-city"><?= htmlspecialchars($u['city']) ?></div>
<?php endif; ?>
<div class="dialog-preview">
<?= htmlspecialchars(cut_text($last['body'], 60)) ?>
</div>
</div>
</a>
<div class="dialog-meta">
<div><?= htmlspecialchars(date('d.m H:i', strtotime($last['created_at']))) ?></div>
<?php if ($unread > 0): ?>
<div class="dialog-unread">
<?= $unread > 99 ? '99+' : $unread ?>
</div>
<?php endif; ?>
</div>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
</div>
<?php include 'footer.php'; ?>