many fixes in design and add shirt story type for creation book

This commit is contained in:
2026-05-01 20:51:42 +03:00
parent 9294399517
commit ce6fffaa57
24 changed files with 1167 additions and 735 deletions
+103 -9
View File
@@ -36,6 +36,16 @@ class BookController extends BaseController {
$this->redirect('/books/create');
}
$is_short_story = isset($_POST['is_short_story']) ? 1 : 0;
$content = $is_short_story ? ($_POST['content'] ?? '') : null;
// Валидация: для рассказа проверяем, что есть хоть какое-то содержание
if ($is_short_story && empty(trim(strip_tags($content)))) {
$_SESSION['error'] = "Для рассказа необходимо заполнить содержание";
$_SESSION['form_data'] = $_POST;
$this->redirect('/books/create');
}
$bookModel = new Book($this->pdo);
$data = [
'title' => $title,
@@ -44,12 +54,14 @@ class BookController extends BaseController {
'user_id' => $_SESSION['user_id'],
'series_id' => !empty($_POST['series_id']) ? (int)$_POST['series_id'] : null,
'sort_order_in_series' => !empty($_POST['sort_order_in_series']) ? (int)$_POST['sort_order_in_series'] : null,
'published' => isset($_POST['published']) ? 1 : 0
'published' => isset($_POST['published']) ? 1 : 0,
'is_short_story' => $is_short_story,
'content' => $content
];
if ($bookModel->create($data)) {
$new_book_id = $this->pdo->lastInsertId();
// Обработка загрузки обложки
if (isset($_FILES['cover_image']) && $_FILES['cover_image']['error'] === UPLOAD_ERR_OK) {
$cover_result = handleCoverUpload($_FILES['cover_image'], $new_book_id);
@@ -61,9 +73,16 @@ class BookController extends BaseController {
$_SESSION['cover_error'] = $cover_error;
}
}
$_SESSION['success'] = "Книга успешно создана" . ($cover_error ? ", но возникла ошибка с обложкой: " . $cover_error : "");
// Если это рассказ с содержанием - редиректим на редактирование
// Если рассказ без содержания - тоже на редактирование (чтобы добавить текст)
if ($is_short_story) {
$_SESSION['success'] = "Рассказ успешно создан! Теперь можно добавить содержание.";
$this->redirect("/books/{$new_book_id}/edit");
} else {
$_SESSION['success'] = "Книга успешно создана" . ($cover_error ? ", но возникла ошибка с обложкой: " . $cover_error : "");
$this->redirect("/books/{$new_book_id}/chapters/create");
}
} else {
$_SESSION['error'] = "Ошибка при создании книги";
}
@@ -91,18 +110,73 @@ class BookController extends BaseController {
$seriesModel = new Series($this->pdo);
$series = $seriesModel->findByUser($_SESSION['user_id']);
$error = '';
$cover_error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Обработка запроса автосохранения
if (!empty($_POST['autosave']) && $_POST['autosave'] === 'true') {
header('Content-Type: application/json');
// Подавляем ошибки для автосейва
error_reporting(0);
if (!verify_csrf_token($_POST['csrf_token'] ?? '')) {
echo json_encode(['success' => false, 'error' => 'Ошибка безопасности']);
exit;
}
$book = $bookModel->findById($id);
if (!$book || $book['user_id'] != $_SESSION['user_id']) {
echo json_encode(['success' => false, 'error' => 'Книга не найдена']);
exit;
}
$is_short_story = isset($_POST['is_short_story']) ? 1 : 0;
$content = $is_short_story ? ($_POST['content'] ?? '') : null;
// Для autosave обновляем только title и user_id из сессии, остальное получаем из базы
$title = trim($_POST['title'] ?? $book['title']);
$description = trim($_POST['description'] ?? ($book['description'] ?? ''));
$genre = trim($_POST['genre'] ?? ($book['genre'] ?? ''));
$user_id = $_SESSION['user_id'];
$series_id = !empty($_POST['series_id']) ? (int)$_POST['series_id'] : $book['series_id'];
$sort_order_in_series = !empty($_POST['sort_order_in_series']) ? (int)$_POST['sort_order_in_series'] : $book['sort_order_in_series'];
$published = isset($_POST['published']) ? 1 : $book['published'];
$data = [
'title' => $title,
'description' => $description,
'genre' => $genre,
'user_id' => $user_id,
'series_id' => $series_id,
'sort_order_in_series' => $sort_order_in_series,
'published' => $published,
'is_short_story' => $is_short_story,
'content' => $content
];
$success = $bookModel->update($id, $data);
header('Content-Type: application/json');
if ($success) {
echo json_encode(['success' => true]);
} else {
echo json_encode(['success' => false, 'error' => 'Ошибка при сохранении']);
}
exit;
}
if (!verify_csrf_token($_POST['csrf_token'] ?? '')) {
$error = "Ошибка безопасности";
} else {
$title = trim($_POST['title'] ?? '');
if (empty($title)) {
$error = "Название книги обязательно";
} else {
} else {
$is_short_story = isset($_POST['is_short_story']) ? 1 : 0;
$content = $is_short_story ? ($_POST['content'] ?? '') : null;
$data = [
'title' => $title,
'description' => trim($_POST['description'] ?? ''),
@@ -110,7 +184,9 @@ class BookController extends BaseController {
'user_id' => $_SESSION['user_id'],
'series_id' => !empty($_POST['series_id']) ? (int)$_POST['series_id'] : null,
'sort_order_in_series' => !empty($_POST['sort_order_in_series']) ? (int)$_POST['sort_order_in_series'] : null,
'published' => isset($_POST['published']) ? 1 : 0
'published' => isset($_POST['published']) ? 1 : 0,
'is_short_story' => $is_short_story,
'content' => $content
];
// Обработка обложки
@@ -252,7 +328,15 @@ class BookController extends BaseController {
$this->render('errors/404');
return;
}
$chapters = $chapterModel->getPublishedChapters($book['id']);
// Если это рассказ - не загружаем главы
if ($book['is_short_story']) {
$chapters = [];
$is_short_story = true;
} else {
$chapters = $chapterModel->getPublishedChapters($book['id']);
$is_short_story = false;
}
// Получаем информацию об авторе
$stmt = $this->pdo->prepare("SELECT id, username, display_name FROM users WHERE id = ?");
@@ -263,6 +347,7 @@ class BookController extends BaseController {
'book' => $book,
'chapters' => $chapters,
'author' => $author,
'is_short_story' => $is_short_story,
'page_title' => $book['title']
]);
}
@@ -279,7 +364,15 @@ class BookController extends BaseController {
$this->render('errors/404');
return;
}
$chapters = $chapterModel->findByBook($book['id']);
// Если это рассказ - не загружаем главы
if ($book['is_short_story']) {
$chapters = [];
$is_short_story = true;
} else {
$chapters = $chapterModel->findByBook($book['id']);
$is_short_story = false;
}
// Получаем информацию об авторе
$stmt = $this->pdo->prepare("SELECT id, username, display_name FROM users WHERE id = ?");
@@ -290,6 +383,7 @@ class BookController extends BaseController {
'book' => $book,
'chapters' => $chapters,
'author' => $author,
'is_short_story' => $is_short_story,
'page_title' => $book['title']
]);
}