add author page

This commit is contained in:
mirivlad 2025-11-20 15:34:54 +08:00
parent 91f70ed69c
commit 9404edad6e
4 changed files with 90 additions and 18 deletions

53
author.php Normal file
View File

@ -0,0 +1,53 @@
<?php
require_once 'config/config.php';
require_once 'models/Book.php';
require_once 'views/header.php';
$author_id = (int)($_GET['id'] ?? 0);
if (!$author_id) {
http_response_code(400);
echo "<h2>Неверный запрос</h2>";
include 'views/footer.php';
exit;
}
$stmt = $pdo->prepare("SELECT id, username, display_name FROM users WHERE id = ?");
$stmt->execute([$author_id]);
$author = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$author) {
http_response_code(404);
echo "<h2>Автор не найден</h2>";
include 'views/footer.php';
exit;
}
$bookModel = new Book($pdo);
$books = $bookModel->findByUser($author_id, true); // только опубликованные (нужен параметр в модели)
$page_title = ($author['display_name'] ?: $author['username']) . ' — публичная страница';
include 'views/header.php';
?>
<h1><?= e($author['display_name'] ?: $author['username']) ?></h1>
<?php if (empty($books)): ?>
<p>У этого автора пока нет опубликованных книг.</p>
<?php else: ?>
<div class="grid">
<?php foreach ($books as $b): ?>
<article>
<?php if ($b['cover_image']): ?>
<img src="<?= e($b['cover_image']) ?>" alt="<?= e($b['title']) ?>" style="max-width:100%; height:auto;">
<?php endif; ?>
<h3><?= e($b['title']) ?></h3>
<?php if ($b['description']): ?>
<p><?= nl2br(e($b['description'])) ?></p>
<?php endif; ?>
<a href="view_book.php?share_token=<?= e($b['share_token']) ?>">Читать</a>
</article>
<?php endforeach; ?>
</div>
<?php endif; ?>
<?php include 'views/footer.php'; ?>

View File

@ -39,6 +39,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
'genre' => $genre,
'user_id' => $user_id
];
$data['published'] = isset($_POST['published']) ? 1 : 0;
if ($is_edit) {
$success = $bookModel->update($book_id, $data);
@ -102,7 +103,16 @@ include 'views/header.php';
<textarea id="description" name="description"
placeholder="Краткое описание сюжета или аннотация..."
rows="6"
style="width: 100%;"><?= e($book['description'] ?? $_POST['description'] ?? '') ?></textarea>
style="width: 100%;">
<?= e($book['description'] ?? $_POST['description'] ?? '') ?>
</textarea>
<div>
<label for="published">
<input type="checkbox" id="published" name="published" value="1"
<?= !empty($book['published']) || (!empty($_POST['published']) && $_POST['published']) ? 'checked' : '' ?>>
Опубликовать книгу (показывать на публичной странице автора)
</label>
</div>
</div>
<div style="display: flex; gap: 5px; flex-wrap: wrap; align-items: center;">

View File

@ -20,27 +20,31 @@ class Book {
return $stmt->fetch(PDO::FETCH_ASSOC);
}
public function findByUser($user_id) {
$stmt = $this->pdo->prepare("
SELECT b.*,
COUNT(c.id) as chapter_count,
COALESCE(SUM(c.word_count), 0) as total_words
FROM books b
LEFT JOIN chapters c ON b.id = c.book_id
WHERE b.user_id = ?
GROUP BY b.id
ORDER BY b.created_at DESC
");
public function findByUser($user_id, $only_published = false) {
$sql = "
SELECT b.*,
COUNT(c.id) as chapter_count,
COALESCE(SUM(c.word_count), 0) as total_words
FROM books b
LEFT JOIN chapters c ON b.id = c.book_id
WHERE b.user_id = ?
";
if ($only_published) {
$sql .= " AND b.published = 1 ";
}
$sql .= " GROUP BY b.id ORDER BY b.created_at DESC ";
$stmt = $this->pdo->prepare($sql);
$stmt->execute([$user_id]);
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function create($data) {
$share_token = bin2hex(random_bytes(16));
$published = isset($data['published']) ? (int)$data['published'] : 0;
$stmt = $this->pdo->prepare("
INSERT INTO books (title, description, genre, user_id, series_id, sort_order_in_series, share_token)
VALUES (?, ?, ?, ?, ?, ?, ?)
INSERT INTO books (title, description, genre, user_id, series_id, sort_order_in_series, share_token, published)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
");
return $stmt->execute([
$data['title'],
@ -49,14 +53,17 @@ class Book {
$data['user_id'],
$data['series_id'] ?? null,
$data['sort_order_in_series'] ?? null,
$share_token
$share_token,
$published
]);
}
public function update($id, $data) {
$published = isset($data['published']) ? (int)$data['published'] : 0;
$stmt = $this->pdo->prepare("
UPDATE books
SET title = ?, description = ?, genre = ?, series_id = ?, sort_order_in_series = ?
UPDATE books
SET title = ?, description = ?, genre = ?, series_id = ?, sort_order_in_series = ?, published = ?
WHERE id = ? AND user_id = ?
");
return $stmt->execute([
@ -65,6 +72,7 @@ class Book {
$data['genre'] ?? null,
$data['series_id'] ?? null,
$data['sort_order_in_series'] ?? null,
$published,
$id,
$data['user_id']
]);

View File

@ -78,6 +78,7 @@ include 'views/header.php';
<article>
<h3>Информация об аккаунте</h3>
<p><a href="author.php?id=<?= $_SESSION['user_id'] ?>" target="_blank">Моя публичная страница</a></p>
<p><strong>Дата регистрации:</strong> <?= date('d.m.Y H:i', strtotime($user['created_at'])) ?></p>
<?php if ($user['last_login']): ?>
<p><strong>Последний вход:</strong> <?= date('d.m.Y H:i', strtotime($user['last_login'])) ?></p>