Portfolio_pero/admin/project-delete.php
2026-09-03 13:27:06 +02:00

58 lines
1.6 KiB
PHP

<?php
/**
* BackOffice — Suppression d'un projet
* Portfolio Tony AVERTY
*/
require_once __DIR__ . '/../includes/Security.php';
Security::bootstrap(true, ['no_store' => true]);
Security::requireAdmin();
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
header('Location: index.php');
exit;
}
Security::verifyCsrf($_POST['csrf_token'] ?? '');
require_once __DIR__ . '/../config/database.php';
$projectId = (int) ($_POST['project_id'] ?? 0);
if ($projectId > 0) {
try {
$db = getDatabase();
// Récupérer l'image pour la supprimer du disque
$stmt = $db->prepare('SELECT image FROM projects WHERE id = :id LIMIT 1');
$stmt->execute(['id' => $projectId]);
$project = $stmt->fetch();
if ($project) {
// Supprimer l'image si elle existe
if (!empty($project['image'])) {
$uploadDir = realpath(__DIR__ . '/../assets/img/projects');
if ($uploadDir !== false) {
$imagePath = $uploadDir . DIRECTORY_SEPARATOR . basename((string) $project['image']);
if (is_file($imagePath)) {
unlink($imagePath);
}
}
}
// Supprimer le projet
$stmt = $db->prepare('DELETE FROM projects WHERE id = :id');
$stmt->execute(['id' => $projectId]);
}
header('Location: index.php?success=deleted');
exit;
} catch (PDOException $e) {
error_log('Erreur suppression projet : ' . $e->getMessage());
header('Location: index.php');
exit;
}
}
header('Location: index.php');
exit;