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

192 lines
8.4 KiB
PHP

<?php
/**
* BackOffice — Gestion des projets
* Portfolio Tony AVERTY
*/
require_once __DIR__ . '/../includes/Security.php';
Security::bootstrap(true, ['no_store' => true]);
Security::requireAdmin();
require_once __DIR__ . '/../config/database.php';
$db = getDatabase();
$feedback = '';
$feedbackType = '';
// ── Actions rapides (publier / dépublier) ─────────────────
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
Security::verifyCsrf($_POST['csrf_token'] ?? '');
$action = $_POST['action'];
$projectId = (int) ($_POST['project_id'] ?? 0);
if ($projectId > 0) {
switch ($action) {
case 'publish':
$db->prepare('UPDATE projects SET status = :s WHERE id = :id')
->execute(['s' => 'published', 'id' => $projectId]);
$feedback = 'Projet publié avec succès.';
$feedbackType = 'success';
break;
case 'unpublish':
$db->prepare('UPDATE projects SET status = :s WHERE id = :id')
->execute(['s' => 'draft', 'id' => $projectId]);
$feedback = 'Projet repassé en brouillon.';
$feedbackType = 'success';
break;
}
}
}
// ── Message après création / modif / suppression ──────────
if (isset($_GET['success'])) {
$msgs = [
'created' => 'Projet créé avec succès.',
'updated' => 'Projet mis à jour avec succès.',
'deleted' => 'Projet supprimé avec succès.',
];
$feedback = $msgs[$_GET['success']] ?? 'Opération réussie.';
$feedbackType = 'success';
}
// ── Liste des projets ─────────────────────────────────────
$projects = $db->query('SELECT * FROM projects ORDER BY display_order ASC, created_at DESC')->fetchAll();
?>
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="robots" content="noindex, nofollow">
<title>Projets — BackOffice Tony AVERTY</title>
<link rel="stylesheet" href="assets/admin.css">
</head>
<body>
<div class="admin-page">
<?php require __DIR__ . '/includes/nav.php'; ?>
<main class="admin-content">
<div class="admin-topbar">
<h1 class="admin-page-title">Projets</h1>
<a href="project-form.php" class="admin-btn admin-btn--primary">+ Nouveau projet</a>
</div>
<?php if ($feedback): ?>
<div class="admin-alert admin-alert--<?= $feedbackType ?>"><?= htmlspecialchars($feedback, ENT_QUOTES, 'UTF-8') ?></div>
<?php endif; ?>
<?php if (empty($projects)): ?>
<div class="admin-table-wrapper">
<div class="admin-empty">
<p>Aucun projet pour le moment.</p>
<a href="project-form.php" class="admin-btn admin-btn--primary">Créer le premier projet</a>
</div>
</div>
<?php else: ?>
<div class="admin-table-wrapper">
<table class="admin-table">
<thead>
<tr>
<th>Ordre</th>
<th>Titre</th>
<th>Statut</th>
<th>Badge</th>
<th>Date</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($projects as $project): ?>
<tr>
<td><?= (int) $project['display_order'] ?></td>
<td><strong><?= htmlspecialchars($project['title'], ENT_QUOTES, 'UTF-8') ?></strong></td>
<td>
<?php if ($project['status'] === 'published'): ?>
<span class="admin-badge admin-badge--published">Publié</span>
<?php else: ?>
<span class="admin-badge admin-badge--draft">Brouillon</span>
<?php endif; ?>
</td>
<td>
<?php if ($project['badge'] === 'production'): ?>
<span class="admin-badge admin-badge--production">En production</span>
<?php else: ?>
<span class="admin-badge admin-badge--development">En développement</span>
<?php endif; ?>
</td>
<td><?= date('d/m/Y', strtotime($project['created_at'])) ?></td>
<td>
<div style="display:flex;gap:0.5rem;flex-wrap:wrap;">
<a href="project-form.php?id=<?= (int) $project['id'] ?>"
class="admin-btn admin-btn--outline admin-btn--sm">Modifier</a>
<?php if ($project['status'] === 'draft'): ?>
<form method="post" style="display:inline;">
<?= Security::csrfInput() ?>
<input type="hidden" name="project_id" value="<?= (int) $project['id'] ?>">
<input type="hidden" name="action" value="publish">
<button type="submit" class="admin-btn admin-btn--outline admin-btn--sm">Publier</button>
</form>
<?php else: ?>
<form method="post" style="display:inline;">
<?= Security::csrfInput() ?>
<input type="hidden" name="project_id" value="<?= (int) $project['id'] ?>">
<input type="hidden" name="action" value="unpublish">
<button type="submit" class="admin-btn admin-btn--outline admin-btn--sm">Dépublier</button>
</form>
<?php endif; ?>
<button type="button" class="admin-btn admin-btn--danger admin-btn--sm"
data-delete-project
data-project-id="<?= (int) $project['id'] ?>"
data-project-name="<?= htmlspecialchars($project['title'], ENT_QUOTES, 'UTF-8') ?>">
Supprimer
</button>
</div>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endif; ?>
</main>
</div>
<!-- Modal suppression -->
<div class="admin-modal-overlay" id="deleteModal">
<div class="admin-modal">
<h2 class="admin-modal__title">Supprimer ce projet ?</h2>
<p class="admin-modal__text">Le projet "<span id="deleteProjectName"></span>" sera définitivement supprimé.</p>
<div class="admin-modal__actions">
<button type="button" class="admin-btn admin-btn--outline" id="closeDeleteModal">Annuler</button>
<form method="post" action="project-delete.php" id="deleteForm">
<?= Security::csrfInput() ?>
<input type="hidden" name="project_id" id="deleteProjectId">
<button type="submit" class="admin-btn admin-btn--danger">Supprimer</button>
</form>
</div>
</div>
</div>
<script nonce="<?= Security::cspNonce() ?>">
function openDeleteModal(id, name) {
document.getElementById('deleteProjectId').value = id;
document.getElementById('deleteProjectName').textContent = name;
document.getElementById('deleteModal').classList.add('is-active');
}
function closeDeleteModal() {
document.getElementById('deleteModal').classList.remove('is-active');
}
document.getElementById('deleteModal').addEventListener('click', function(e) {
if (e.target === this) closeDeleteModal();
});
document.getElementById('closeDeleteModal').addEventListener('click', closeDeleteModal);
document.querySelectorAll('[data-delete-project]').forEach(function (button) {
button.addEventListener('click', function () {
openDeleteModal(button.dataset.projectId, button.dataset.projectName);
});
});
</script>
</body>
</html>