File size: 7.32Kb
<?php
require_once 'db.php';
if (empty($_SESSION['user_id'])) {
header('Location: login.php');
exit;
}
$from_user_id = (int)$_SESSION['user_id'];
$to_user_id = isset($_GET['user_id']) ? (int)$_GET['user_id'] : 0;
if ($to_user_id <= 0 || $to_user_id === $from_user_id) {
header('Location: browse.php');
exit;
}
if (is_banned($from_user_id)) {
include 'header.php';
echo '<p>Ваш аккаунт заблокирован. Обратитесь в поддержку.</p>';
include 'footer.php';
exit;
}
// получатель
$stmt = $mysqli->prepare("SELECT name, photo FROM users WHERE id = ?");
$stmt->bind_param('i', $to_user_id);
$stmt->execute();
$stmt->bind_result($to_name, $to_photo);
if (!$stmt->fetch()) {
$stmt->close();
header('Location: browse.php');
exit;
}
$stmt->close();
// загружаем подарки
$cats = [];
$res = $mysqli->query("
SELECT c.id AS cat_id, c.name AS cat_name,
g.id AS gift_id, g.title, g.image
FROM gift_categories c
LEFT JOIN gifts g ON g.category_id = c.id
ORDER BY c.name, g.title
");
while ($row = $res->fetch_assoc()) {
if (!isset($cats[$row['cat_id']])) {
$cats[$row['cat_id']] = [
'name' => $row['cat_name'],
'gifts' => []
];
}
if ($row['gift_id']) {
$cats[$row['cat_id']]['gifts'][] = $row;
}
}
$res->close();
$errors = [];
$success = false;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$gift_id = (int)($_POST['gift_id'] ?? 0);
$msg = trim($_POST['gift_message'] ?? '');
if ($gift_id <= 0) {
$errors[] = 'Выберите подарок.';
} else {
// проверяем, что подарок существует
$stmt = $mysqli->prepare("SELECT id FROM gifts WHERE id = ?");
$stmt->bind_param('i', $gift_id);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows === 0) {
$errors[] = 'Подарок не найден.';
}
$stmt->close();
}
if (!$errors) {
$stmt = $mysqli->prepare("
INSERT INTO user_gifts (from_user_id, to_user_id, gift_id, message)
VALUES (?, ?, ?, ?)
");
$stmt->bind_param('iiis', $from_user_id, $to_user_id, $gift_id, $msg);
$stmt->execute();
$stmt->close();
$success = true;
}
}
include 'header.php';
?>
<style>
.send-gift-page {
margin-top: 18px;
}
.send-gift-layout {
display: grid;
grid-template-columns: 260px minmax(0, 1fr);
gap: 20px;
}
.send-gift-recipient {
text-align: center;
background: #ffffff;
border-radius: 22px;
padding: 18px 18px;
box-shadow: 0 10px 30px rgba(0,0,0,0.06);
}
.send-gift-avatar {
width: 110px;
height: 110px;
border-radius: 999px;
object-fit: cover;
margin-bottom: 8px;
}
.send-gift-main {
background: #ffffff;
border-radius: 22px;
padding: 18px 18px;
box-shadow: 0 10px 30px rgba(0,0,0,0.06);
}
.gift-cats {
margin-top: 6px;
max-height: 380px;
overflow-y: auto;
}
.gift-cat-title {
font-weight: 600;
margin: 10px 0 4px;
font-size: 14px;
}
.gift-list {
display: flex;
flex-wrap: wrap;
gap: 8px;
}
.gift-option {
position: relative;
cursor: pointer;
}
.gift-radio {
position: absolute;
top: 4px;
left: 4px;
}
.gift-img {
width: 80px;
height: 80px;
border-radius: 16px;
object-fit: contain;
background: #fff7fb;
box-shadow: 0 4px 12px rgba(0,0,0,0.06);
}
.gift-title-small {
font-size: 11px;
text-align: center;
margin-top: 2px;
}
@media (max-width: 768px) {
.send-gift-layout {
grid-template-columns: 1fr;
}
.send-gift-recipient,
.send-gift-main {
margin: 0 -4px;
border-radius: 18px;
}
}
</style>
<div class="send-gift-page">
<h1>Отправка подарка</h1>
<?php if ($success): ?>
<div class="alert alert-success">
Подарок для <?= htmlspecialchars($to_name) ?> отправлен 🎁
</div>
<?php endif; ?>
<?php if ($errors): ?>
<div class="alert alert-error">
<?php foreach ($errors as $e): ?>
<p><?= htmlspecialchars($e) ?></p>
<?php endforeach; ?>
</div>
<?php endif; ?>
<div class="send-gift-layout">
<aside class="send-gift-recipient">
<?php if ($to_photo): ?>
<img src="/assets/img/<?= htmlspecialchars($to_photo) ?>" class="send-gift-avatar" alt="">
<?php else: ?>
<div class="profile-avatar-placeholder" style="width:110px;height:110px;margin:0 auto 8px;">🙂</div>
<?php endif; ?>
<div style="font-weight:600;font-size:16px;">
<?= htmlspecialchars($to_name) ?>
</div>
<div style="margin-top:10px;font-size:13px;color:#7b7287;">
Сделайте их день немного теплее ❤️
</div>
</aside>
<section class="send-gift-main">
<?php if (!$cats): ?>
<p>Администратор ещё не добавил подарки. Попробуйте позже.</p>
<?php else: ?>
<form method="post">
<div class="gift-cats">
<?php foreach ($cats as $cat): ?>
<div class="gift-cat-title"><?= htmlspecialchars($cat['name']) ?></div>
<?php if (empty($cat['gifts'])): ?>
<p style="font-size:12px;color:#a29ab4;">В этой категории пока нет подарков.</p>
<?php else: ?>
<div class="gift-list">
<?php foreach ($cat['gifts'] as $gift): ?>
<label class="gift-option">
<input type="radio" name="gift_id"
value="<?= (int)$gift['gift_id'] ?>"
class="gift-radio" required>
<img src="/assets/gifts/<?= htmlspecialchars($gift['image']) ?>" class="gift-img" alt="">
<div class="gift-title-small"><?= htmlspecialchars($gift['title']) ?></div>
</label>
<?php endforeach; ?>
</div>
<?php endif; ?>
<?php endforeach; ?>
</div>
<div class="form-row" style="margin-top:12px;">
<label>Сообщение к подарку (необязательно)</label>
<input type="text" name="gift_message" maxlength="255"
placeholder="Например: «пусть этот день будет мягким и тёплым»">
</div>
<button type="submit" class="btn-primary" style="margin-top:12px;">
Отправить подарок
</button>
</form>
<?php endif; ?>
</section>
</div>
</div>
<?php include 'footer.php'; ?>