48 lines
1.3 KiB
PHP
48 lines
1.3 KiB
PHP
|
<?php
|
||
|
require_once '../../includes/config.php';
|
||
|
require_once '../../includes/auth.php';
|
||
|
require_once '../../includes/stories.php';
|
||
|
|
||
|
// Vérification de l'authentification
|
||
|
if (!Auth::check()) {
|
||
|
http_response_code(401);
|
||
|
exit(json_encode(['error' => 'Non autorisé']));
|
||
|
}
|
||
|
|
||
|
$input = json_decode(file_get_contents('php://input'), true);
|
||
|
$storyId = $input['storyId'] ?? '';
|
||
|
$newOrder = $input['chapters'] ?? [];
|
||
|
|
||
|
if (!$storyId || empty($newOrder)) {
|
||
|
http_response_code(400);
|
||
|
exit(json_encode(['error' => 'Paramètres manquants']));
|
||
|
}
|
||
|
|
||
|
try {
|
||
|
$story = Stories::get($storyId);
|
||
|
if (!$story) {
|
||
|
throw new Exception('Roman non trouvé');
|
||
|
}
|
||
|
|
||
|
// Créer un tableau temporaire pour le nouvel ordre
|
||
|
$reorderedChapters = [];
|
||
|
foreach ($newOrder as $item) {
|
||
|
foreach ($story['chapters'] as $chapter) {
|
||
|
if ($chapter['id'] === $item['id']) {
|
||
|
$reorderedChapters[] = $chapter;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Mettre à jour l'ordre des chapitres
|
||
|
$story['chapters'] = $reorderedChapters;
|
||
|
|
||
|
// Sauvegarder les modifications
|
||
|
Stories::save($story);
|
||
|
|
||
|
echo json_encode(['success' => true]);
|
||
|
} catch (Exception $e) {
|
||
|
http_response_code(500);
|
||
|
echo json_encode(['error' => $e->getMessage()]);
|
||
|
}
|