190 lines
5.5 KiB
PHP
190 lines
5.5 KiB
PHP
<?php
|
|
require_once 'config.php';
|
|
|
|
// Vérifie la session
|
|
if (!isset($_SESSION['user_id'])) {
|
|
header("Location: login.php");
|
|
exit;
|
|
}
|
|
|
|
$article_id = $_GET['id'] ?? null;
|
|
if (!$article_id) {
|
|
die("❌ Article introuvable.");
|
|
}
|
|
|
|
// Récupération des infos actuelles de l'article
|
|
$stmt = $pdo->prepare("SELECT * FROM posts WHERE id = ?");
|
|
$stmt->execute([$article_id]);
|
|
$post = $stmt->fetch();
|
|
|
|
if (!$post) {
|
|
die("❌ Article non trouvé.");
|
|
}
|
|
|
|
// 🔄 Si on soumet le formulaire
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$title = trim($_POST['title']);
|
|
$content = trim($_POST['content']);
|
|
$imageUrl = $post['image_url'];
|
|
|
|
global $s3Client, $bucketName;
|
|
|
|
// ✅ Suppression de la photo actuelle si demandé
|
|
if (isset($_POST['remove_image'])) {
|
|
if (!empty($post['image_url'])) {
|
|
$oldKey = basename($post['image_url']);
|
|
try {
|
|
$s3Client->deleteObject([
|
|
'Bucket' => $bucketName,
|
|
'Key' => $oldKey
|
|
]);
|
|
} catch (Exception $e) {
|
|
echo "⚠️ Erreur suppression image : " . $e->getMessage();
|
|
}
|
|
}
|
|
$imageUrl = null;
|
|
}
|
|
|
|
// ✅ Nouvelle image uploadée
|
|
elseif (isset($_FILES['image']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) {
|
|
$fileName = time() . '_' . basename($_FILES['image']['name']);
|
|
$tmpPath = $_FILES['image']['tmp_name'];
|
|
|
|
try {
|
|
$s3Client->putObject([
|
|
'Bucket' => $bucketName,
|
|
'Key' => $fileName,
|
|
'SourceFile' => $tmpPath,
|
|
'ACL' => 'public-read'
|
|
]);
|
|
$imageUrl = "http://localhost:9000/$bucketName/$fileName";
|
|
|
|
// Supprime l'ancienne image
|
|
if (!empty($post['image_url'])) {
|
|
$oldKey = basename($post['image_url']);
|
|
$s3Client->deleteObject(['Bucket' => $bucketName, 'Key' => $oldKey]);
|
|
}
|
|
} catch (Exception $e) {
|
|
echo "❌ Erreur upload : " . $e->getMessage();
|
|
exit;
|
|
}
|
|
}
|
|
|
|
// 🔄 Mise à jour SQL
|
|
$stmt = $pdo->prepare("UPDATE posts SET title = ?, content = ?, image_url = ? WHERE id = ?");
|
|
$stmt->execute([$title, $content, $imageUrl, $article_id]);
|
|
|
|
header("Location: index.php");
|
|
exit;
|
|
}
|
|
?>
|
|
|
|
<!DOCTYPE html>
|
|
<html lang="fr">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>✏️ Modifier un article</title>
|
|
<style>
|
|
body {
|
|
font-family: "Poppins", sans-serif;
|
|
background: linear-gradient(120deg, #ffe0ec, #fff6f8);
|
|
color: #333;
|
|
margin: 0;
|
|
}
|
|
.container {
|
|
max-width: 600px;
|
|
margin: 80px auto;
|
|
background: white;
|
|
padding: 40px;
|
|
border-radius: 15px;
|
|
box-shadow: 0 6px 20px rgba(255, 100, 150, 0.2);
|
|
}
|
|
h2 {
|
|
text-align: center;
|
|
color: #ff6f91;
|
|
}
|
|
form {
|
|
display: flex;
|
|
flex-direction: column;
|
|
gap: 15px;
|
|
}
|
|
label {
|
|
font-weight: 600;
|
|
color: #444;
|
|
}
|
|
input[type="text"], textarea {
|
|
padding: 10px 15px;
|
|
border: 1px solid #ddd;
|
|
border-radius: 8px;
|
|
font-size: 1em;
|
|
}
|
|
button {
|
|
background: linear-gradient(45deg, #ff6f91, #ff9671, #ffc75f);
|
|
color: white;
|
|
font-weight: bold;
|
|
padding: 12px;
|
|
border: none;
|
|
border-radius: 50px;
|
|
cursor: pointer;
|
|
transition: 0.3s;
|
|
}
|
|
button:hover {
|
|
transform: translateY(-2px);
|
|
box-shadow: 0 4px 12px rgba(255, 105, 135, 0.4);
|
|
}
|
|
img {
|
|
width: 100%;
|
|
border-radius: 10px;
|
|
}
|
|
.remove-check {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 8px;
|
|
font-size: 0.95em;
|
|
}
|
|
a.retour {
|
|
display: block;
|
|
text-align: center;
|
|
color: #ff6f91;
|
|
text-decoration: none;
|
|
font-weight: bold;
|
|
margin-top: 15px;
|
|
}
|
|
a.retour:hover {
|
|
text-decoration: underline;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="container">
|
|
<h2>✏️ Modifier un article</h2>
|
|
|
|
<form method="POST" enctype="multipart/form-data">
|
|
<label for="title">Titre :</label>
|
|
<input type="text" id="title" name="title" value="<?= htmlspecialchars($post['title']) ?>" required>
|
|
|
|
<label for="content">Contenu :</label>
|
|
<textarea id="content" name="content" rows="6" required><?= htmlspecialchars($post['content']) ?></textarea>
|
|
|
|
<?php if (!empty($post['image_url'])): ?>
|
|
<label>Image actuelle :</label>
|
|
<img src="<?= htmlspecialchars($post['image_url']) ?>" alt="Image actuelle">
|
|
<div class="remove-check">
|
|
<input type="checkbox" name="remove_image" id="remove_image">
|
|
<label for="remove_image">Supprimer la photo actuelle</label>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<label for="image">Nouvelle image (optionnel) :</label>
|
|
<input type="file" name="image" id="image" accept="image/png, image/jpeg">
|
|
|
|
<button type="submit">Mettre à jour</button>
|
|
</form>
|
|
|
|
<a href="dashboard.php" class="retour">⬅ Retour au tableau de bord</a>
|
|
</div>
|
|
</body>
|
|
</html>
|
|
|
|
|