Lectures/admin/api/get-chapter.php

79 lines
2.0 KiB
PHP
Raw Permalink Normal View History

<?php
require_once '../../includes/config.php';
require_once '../../includes/auth.php';
require_once '../../includes/stories.php';
2025-02-15 13:03:10 +01:00
header('Content-Type: application/json');
if (!Auth::check()) {
http_response_code(401);
exit(json_encode(['error' => 'Non autorisé']));
}
$storyId = $_GET['storyId'] ?? '';
$chapterId = $_GET['chapterId'] ?? '';
if (!$storyId || !$chapterId) {
http_response_code(400);
exit(json_encode(['error' => 'Paramètres manquants']));
}
try {
$story = Stories::get($storyId);
if (!$story) {
throw new Exception('Roman non trouvé');
}
$chapter = null;
foreach ($story['chapters'] as $ch) {
if ($ch['id'] === $chapterId) {
$chapter = $ch;
break;
}
}
if (!$chapter) {
throw new Exception('Chapitre non trouvé');
}
2025-02-15 13:03:10 +01:00
// Vérification et correction de la structure du chapitre
if (!isset($chapter['title'])) {
$chapter['title'] = '';
}
// S'assurer que le champ cover existe
if (!isset($chapter['cover'])) {
$chapter['cover'] = null;
}
2025-02-15 13:03:10 +01:00
// Conversion du contenu HTML en format Delta de Quill si nécessaire
if (!isset($chapter['content'])) {
$chapter['content'] = '{"ops":[{"insert":"\n"}]}';
} else {
// Si le contenu est une chaîne HTML
if (is_string($chapter['content']) && !isJson($chapter['content'])) {
// On envoie le HTML brut, Quill le convertira côté client
$chapter['html'] = $chapter['content'];
$chapter['content'] = null;
}
}
echo json_encode($chapter);
} catch (Exception $e) {
2025-02-15 13:03:10 +01:00
error_log('Erreur dans get-chapter.php: ' . $e->getMessage());
http_response_code(500);
2025-02-15 13:03:10 +01:00
echo json_encode([
'error' => $e->getMessage(),
'details' => [
'storyId' => $storyId,
'chapterId' => $chapterId,
'file' => __FILE__,
'line' => __LINE__
]
]);
}
function isJson($string) {
json_decode($string);
return json_last_error() === JSON_ERROR_NONE;
}