Lectures/admin/story-edit.php

157 lines
5.9 KiB
PHP

<?php
require_once '../includes/config.php';
require_once '../includes/auth.php';
require_once '../includes/stories.php';
require_once 'upload-handler.php';
if (!Auth::check()) {
header('Location: login.php');
exit;
}
$story = null;
$error = '';
$success = '';
// Chargement du roman existant si ID fourni
if (isset($_GET['id'])) {
$story = Stories::get($_GET['id']);
if (!$story) {
header('Location: index.php');
exit;
}
}
// Traitement de la sauvegarde
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
try {
$storyData = [
'id' => $_POST['id'] ?? generateSlug($_POST['title']),
'title' => $_POST['title'],
'description' => $_POST['description'],
'cover' => $story['cover'] ?? '',
'created' => $story['created'] ?? date('Y-m-d'),
'updated' => date('Y-m-d'),
'chapters' => $story['chapters'] ?? []
];
// Gestion de l'upload de couverture
if (isset($_FILES['cover']) && $_FILES['cover']['error'] !== UPLOAD_ERR_NO_FILE) {
$uploadHandler = new CoverUploadHandler();
$storyData['cover'] = $uploadHandler->handleUpload($_FILES['cover'], $storyData['id']);
}
Stories::save($storyData);
$success = 'Roman sauvegardé avec succès';
$story = $storyData;
} catch (Exception $e) {
$error = 'Erreur lors de la sauvegarde : ' . $e->getMessage();
}
}
function generateSlug($title) {
$slug = strtolower($title);
$slug = preg_replace('/[^a-z0-9-]/', '-', $slug);
$slug = preg_replace('/-+/', '-', $slug);
return trim($slug, '-');
}
?>
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?= $story ? 'Modifier' : 'Nouveau' ?> roman - Administration</title>
<link rel="stylesheet" href="../assets/css/main.css">
<link href="https://cdn.quilljs.com/1.3.6/quill.snow.css" rel="stylesheet">
</head>
<body>
<nav class="admin-nav">
<div class="nav-brand">Administration</div>
<div class="nav-menu">
<a href="index.php" class="button">Retour</a>
</div>
</nav>
<main class="admin-main">
<h1><?= $story ? 'Modifier' : 'Nouveau' ?> roman</h1>
<?php if ($error): ?>
<div class="error-message"><?= htmlspecialchars($error) ?></div>
<?php endif; ?>
<?php if ($success): ?>
<div class="success-message"><?= htmlspecialchars($success) ?></div>
<?php endif; ?>
<form method="POST" enctype="multipart/form-data" class="story-form">
<?php if ($story): ?>
<input type="hidden" name="id" value="<?= htmlspecialchars($story['id']) ?>">
<?php endif; ?>
<div class="form-group">
<label for="title">Titre</label>
<input type="text" id="title" name="title" required
value="<?= htmlspecialchars($story['title'] ?? '') ?>">
</div>
<div class="form-group">
<label for="description">Description</label>
<textarea id="description" name="description" rows="4" required><?=
htmlspecialchars($story['description'] ?? '')
?></textarea>
</div>
<div class="form-group">
<label for="cover">Image de couverture</label>
<?php if (isset($story['cover'])): ?>
<img src="<?= htmlspecialchars('../' . $story['cover']) ?>"
alt="Couverture actuelle" class="current-cover">
<?php endif; ?>
<input type="file" id="cover" name="cover" accept="image/*">
</div>
<button type="submit" class="button">Enregistrer</button>
</form>
<?php if ($story): ?>
<section class="chapters-section">
<h2>Chapitres</h2>
<button type="button" id="addChapter" class="button">Ajouter un chapitre</button>
<div id="chaptersList" class="chapters-list">
<?php foreach ($story['chapters'] ?? [] as $index => $chapter): ?>
<div class="chapter-item" data-id="<?= $chapter['id'] ?>">
<div class="chapter-header">
<span class="chapter-number"><?= $index + 1 ?></span>
<input type="text" class="chapter-title"
value="<?= htmlspecialchars($chapter['title']) ?>">
<button type="button" class="button delete-chapter">Supprimer</button>
</div>
<div class="chapter-content">
<div class="editor"><?= $chapter['content'] ?></div>
</div>
</div>
<?php endforeach; ?>
</div>
</section>
<div id="chapterEditor" class="modal" style="display: none;">
<div class="modal-content">
<h2>Éditer le chapitre</h2>
<input type="text" id="chapterTitle" placeholder="Titre du chapitre">
<div id="editor"></div>
<div class="modal-actions">
<button type="button" class="button" id="saveChapter">Enregistrer</button>
<button type="button" class="button" id="cancelEdit">Annuler</button>
</div>
</div>
</div>
<?php endif; ?>
</main>
<script src="https://cdn.quilljs.com/1.3.6/quill.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sortablejs@1.15.0/Sortable.min.js"></script>
<script src="../assets/js/story-edit.js"></script>
</body>
</html>