2025-02-14 18:15:23 +01:00
|
|
|
<?php
|
|
|
|
require_once '../../includes/config.php';
|
|
|
|
require_once '../../includes/auth.php';
|
|
|
|
require_once '../../includes/stories.php';
|
|
|
|
|
|
|
|
if (!Auth::check()) {
|
|
|
|
http_response_code(401);
|
2025-02-23 19:20:23 +01:00
|
|
|
exit(json_encode(['success' => false, 'error' => 'Non autorisé']));
|
2025-02-14 18:15:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
|
|
$storyId = $input['storyId'] ?? null;
|
|
|
|
$chapterId = $input['chapterId'] ?? null;
|
|
|
|
$title = $input['title'] ?? '';
|
2025-02-23 19:20:23 +01:00
|
|
|
$content = $input['content'] ?? '';
|
|
|
|
$draft = $input['draft'] ?? false;
|
2025-02-14 18:15:23 +01:00
|
|
|
|
|
|
|
try {
|
2025-02-23 19:20:23 +01:00
|
|
|
// Ajout de logs pour déboguer
|
|
|
|
error_log('Données reçues: ' . print_r($input, true));
|
|
|
|
|
2025-02-14 18:15:23 +01:00
|
|
|
$story = Stories::get($storyId);
|
|
|
|
if (!$story) {
|
|
|
|
throw new Exception('Roman non trouvé');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($chapterId) {
|
|
|
|
// Mise à jour d'un chapitre existant
|
2025-02-23 19:20:23 +01:00
|
|
|
$chapterUpdated = false;
|
2025-02-14 18:15:23 +01:00
|
|
|
foreach ($story['chapters'] as &$chapter) {
|
2025-02-23 19:20:23 +01:00
|
|
|
if ($chapter['id'] === $chapterId) {
|
2025-02-14 18:15:23 +01:00
|
|
|
$chapter['title'] = $title;
|
|
|
|
$chapter['content'] = $content;
|
2025-02-23 19:20:23 +01:00
|
|
|
$chapter['draft'] = $draft;
|
2025-02-14 18:15:23 +01:00
|
|
|
$chapter['updated'] = date('Y-m-d');
|
2025-02-23 19:20:23 +01:00
|
|
|
$chapterUpdated = true;
|
2025-02-14 18:15:23 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2025-02-23 19:20:23 +01:00
|
|
|
if (!$chapterUpdated) {
|
|
|
|
throw new Exception('Chapitre non trouvé');
|
|
|
|
}
|
2025-02-14 18:15:23 +01:00
|
|
|
} else {
|
|
|
|
// Nouveau chapitre
|
|
|
|
$story['chapters'][] = [
|
|
|
|
'id' => uniqid(),
|
|
|
|
'title' => $title,
|
|
|
|
'content' => $content,
|
2025-02-23 19:20:23 +01:00
|
|
|
'draft' => $draft,
|
2025-02-14 18:15:23 +01:00
|
|
|
'created' => date('Y-m-d'),
|
|
|
|
'updated' => date('Y-m-d')
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2025-02-23 19:20:23 +01:00
|
|
|
if (!Stories::save($story)) {
|
|
|
|
throw new Exception('Erreur lors de la sauvegarde du roman');
|
|
|
|
}
|
|
|
|
|
2025-02-14 18:15:23 +01:00
|
|
|
echo json_encode(['success' => true]);
|
|
|
|
} catch (Exception $e) {
|
2025-02-23 19:20:23 +01:00
|
|
|
error_log('Erreur dans save-chapter.php: ' . $e->getMessage());
|
2025-02-14 18:15:23 +01:00
|
|
|
http_response_code(500);
|
2025-02-23 19:20:23 +01:00
|
|
|
echo json_encode([
|
|
|
|
'success' => false,
|
|
|
|
'error' => $e->getMessage()
|
|
|
|
]);
|
2025-02-14 18:15:23 +01:00
|
|
|
}
|