267 lines
12 KiB
PHP
267 lines
12 KiB
PHP
<?php
|
|
/**
|
|
* BackOffice - gestion des services.
|
|
*/
|
|
|
|
require_once __DIR__ . '/../includes/Security.php';
|
|
require_once __DIR__ . '/../config/database.php';
|
|
|
|
Security::bootstrap(true, ['no_store' => true]);
|
|
Security::requireAdmin();
|
|
|
|
$db = getDatabase();
|
|
$feedback = '';
|
|
$feedbackType = '';
|
|
$editService = null;
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
Security::verifyCsrf($_POST['csrf_token'] ?? '');
|
|
|
|
$action = (string) ($_POST['action'] ?? '');
|
|
|
|
if ($action === 'create' || $action === 'update') {
|
|
$title = trim((string) ($_POST['title'] ?? ''));
|
|
$description = trim((string) ($_POST['description'] ?? ''));
|
|
$icon = trim((string) ($_POST['icon'] ?? '🚀'));
|
|
$displayOrder = max(0, (int) ($_POST['display_order'] ?? 0));
|
|
$isActive = isset($_POST['is_active']) ? 1 : 0;
|
|
|
|
if ($title === '' || $description === '') {
|
|
$feedback = 'Le titre et la description sont requis.';
|
|
$feedbackType = 'error';
|
|
} else {
|
|
if ($action === 'create') {
|
|
$db->prepare(
|
|
'INSERT INTO services (icon, title, description, display_order, is_active)
|
|
VALUES (:icon, :title, :description, :display_order, :is_active)'
|
|
)->execute([
|
|
'icon' => mb_substr($icon, 0, 10),
|
|
'title' => $title,
|
|
'description' => $description,
|
|
'display_order' => $displayOrder,
|
|
'is_active' => $isActive,
|
|
]);
|
|
$feedback = 'Service cree avec succes.';
|
|
} else {
|
|
$serviceId = (int) ($_POST['service_id'] ?? 0);
|
|
$db->prepare(
|
|
'UPDATE services
|
|
SET icon = :icon, title = :title, description = :description, display_order = :display_order, is_active = :is_active
|
|
WHERE id = :id'
|
|
)->execute([
|
|
'icon' => mb_substr($icon, 0, 10),
|
|
'title' => $title,
|
|
'description' => $description,
|
|
'display_order' => $displayOrder,
|
|
'is_active' => $isActive,
|
|
'id' => $serviceId,
|
|
]);
|
|
$feedback = 'Service mis a jour.';
|
|
}
|
|
|
|
$feedbackType = 'success';
|
|
header('Location: services.php?ok=' . urlencode($feedback));
|
|
exit;
|
|
}
|
|
} elseif ($action === 'toggle') {
|
|
$serviceId = (int) ($_POST['service_id'] ?? 0);
|
|
$db->prepare('UPDATE services SET is_active = NOT is_active WHERE id = :id')
|
|
->execute(['id' => $serviceId]);
|
|
header('Location: services.php?ok=Statut+mis+a+jour');
|
|
exit;
|
|
} elseif ($action === 'delete') {
|
|
$serviceId = (int) ($_POST['service_id'] ?? 0);
|
|
$db->prepare('DELETE FROM services WHERE id = :id')->execute(['id' => $serviceId]);
|
|
header('Location: services.php?ok=Service+supprime');
|
|
exit;
|
|
}
|
|
}
|
|
|
|
$stmt = $db->prepare('SELECT * FROM services WHERE id = :id');
|
|
$stmt->execute(['id' => (int) ($_GET['edit'] ?? 0)]);
|
|
$editService = $stmt->fetch() ?: null;
|
|
|
|
$services = $db->query('SELECT * FROM services ORDER BY display_order ASC, id ASC')->fetchAll();
|
|
|
|
if (isset($_GET['ok'])) {
|
|
$feedback = (string) $_GET['ok'];
|
|
$feedbackType = 'success';
|
|
}
|
|
?>
|
|
<!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>Services - 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">Services</h1>
|
|
<?php if ($editService === null): ?>
|
|
<button type="button" id="toggleServiceForm" class="admin-btn admin-btn--primary">+ Nouveau service</button>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<?php if ($feedback !== ''): ?>
|
|
<div class="admin-alert admin-alert--<?= $feedbackType ?>"><?= Security::e($feedback) ?></div>
|
|
<?php endif; ?>
|
|
|
|
<div id="formNew" class="admin-form <?= $editService ? '' : 'is-hidden' ?>" style="margin-bottom:2rem;">
|
|
<h2 style="font-size:1rem;font-weight:700;margin-bottom:1.5rem;">
|
|
<?= $editService ? 'Modifier le service' : 'Nouveau service' ?>
|
|
</h2>
|
|
|
|
<form method="post">
|
|
<?= Security::csrfInput() ?>
|
|
<input type="hidden" name="action" value="<?= $editService ? 'update' : 'create' ?>">
|
|
<?php if ($editService): ?>
|
|
<input type="hidden" name="service_id" value="<?= (int) $editService['id'] ?>">
|
|
<?php endif; ?>
|
|
|
|
<div class="admin-form__row">
|
|
<div class="admin-form__group">
|
|
<label class="admin-form__label" for="service_icon">Emoji / Icône</label>
|
|
<input type="text" id="service_icon" name="icon" class="admin-form__input" value="<?= Security::e((string) ($editService['icon'] ?? '🚀')) ?>" maxlength="10">
|
|
</div>
|
|
|
|
<div class="admin-form__group">
|
|
<label class="admin-form__label" for="service_order">Ordre d'affichage</label>
|
|
<input type="number" id="service_order" name="display_order" class="admin-form__input" value="<?= (int) ($editService['display_order'] ?? 0) ?>" min="0">
|
|
</div>
|
|
</div>
|
|
|
|
<div class="admin-form__group">
|
|
<label class="admin-form__label" for="service_title">Titre</label>
|
|
<input type="text" id="service_title" name="title" class="admin-form__input" required value="<?= Security::e((string) ($editService['title'] ?? '')) ?>">
|
|
</div>
|
|
|
|
<div class="admin-form__group">
|
|
<label class="admin-form__label" for="service_description">Description</label>
|
|
<textarea id="service_description" name="description" class="admin-form__textarea" rows="3" required><?= Security::e((string) ($editService['description'] ?? '')) ?></textarea>
|
|
</div>
|
|
|
|
<div class="admin-form__group">
|
|
<label style="display:flex;align-items:center;gap:0.5rem;cursor:pointer;">
|
|
<input type="checkbox" name="is_active" value="1" <?= (!$editService || !empty($editService['is_active'])) ? 'checked' : '' ?>>
|
|
<span class="admin-form__label" style="margin:0;">Actif (visible sur le site)</span>
|
|
</label>
|
|
</div>
|
|
|
|
<div class="admin-form__actions">
|
|
<button type="submit" class="admin-btn admin-btn--primary"><?= $editService ? 'Enregistrer' : 'Creer le service' ?></button>
|
|
<a href="services.php" class="admin-btn admin-btn--outline">Annuler</a>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
|
|
<?php if (empty($services)): ?>
|
|
<div class="admin-empty"><p>Aucun service configure.</p></div>
|
|
<?php else: ?>
|
|
<div class="admin-table-wrapper">
|
|
<table class="admin-table">
|
|
<thead>
|
|
<tr><th>Ordre</th><th>Icone</th><th>Titre</th><th>Statut</th><th>Actions</th></tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($services as $service): ?>
|
|
<tr>
|
|
<td><?= (int) $service['display_order'] ?></td>
|
|
<td style="font-size:1.5rem"><?= Security::e((string) $service['icon']) ?></td>
|
|
<td>
|
|
<strong><?= Security::e((string) $service['title']) ?></strong><br>
|
|
<small style="color:var(--admin-text-secondary)"><?= Security::e(mb_substr((string) $service['description'], 0, 80)) ?>…</small>
|
|
</td>
|
|
<td>
|
|
<?php if (!empty($service['is_active'])): ?>
|
|
<span class="admin-badge admin-badge--published">Actif</span>
|
|
<?php else: ?>
|
|
<span class="admin-badge admin-badge--draft">Inactif</span>
|
|
<?php endif; ?>
|
|
</td>
|
|
<td>
|
|
<div style="display:flex;gap:0.5rem;flex-wrap:wrap;">
|
|
<a href="?edit=<?= (int) $service['id'] ?>" class="admin-btn admin-btn--outline admin-btn--sm">Modifier</a>
|
|
|
|
<form method="post" style="display:inline;">
|
|
<?= Security::csrfInput() ?>
|
|
<input type="hidden" name="action" value="toggle">
|
|
<input type="hidden" name="service_id" value="<?= (int) $service['id'] ?>">
|
|
<button type="submit" class="admin-btn admin-btn--outline admin-btn--sm">
|
|
<?= !empty($service['is_active']) ? 'Desactiver' : 'Activer' ?>
|
|
</button>
|
|
</form>
|
|
|
|
<button
|
|
type="button"
|
|
class="admin-btn admin-btn--danger admin-btn--sm"
|
|
data-delete-service
|
|
data-service-id="<?= (int) $service['id'] ?>"
|
|
data-service-name="<?= Security::e((string) $service['title']) ?>">
|
|
Supprimer
|
|
</button>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<?php endif; ?>
|
|
</main>
|
|
</div>
|
|
|
|
<div class="admin-modal-overlay" id="deleteModal">
|
|
<div class="admin-modal">
|
|
<h2 class="admin-modal__title">Supprimer ce service ?</h2>
|
|
<p class="admin-modal__text">Le service "<span id="deleteName"></span>" sera definitivement supprime.</p>
|
|
<div class="admin-modal__actions">
|
|
<button type="button" class="admin-btn admin-btn--outline" id="closeServiceDeleteModal">Annuler</button>
|
|
<form method="post" id="deleteForm">
|
|
<?= Security::csrfInput() ?>
|
|
<input type="hidden" name="action" value="delete">
|
|
<input type="hidden" name="service_id" id="deleteId">
|
|
<button type="submit" class="admin-btn admin-btn--danger">Supprimer</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script nonce="<?= Security::cspNonce() ?>">
|
|
function deleteService(id, name) {
|
|
document.getElementById('deleteId').value = id;
|
|
document.getElementById('deleteName').textContent = name;
|
|
document.getElementById('deleteModal').classList.add('is-active');
|
|
}
|
|
|
|
document.getElementById('deleteModal').addEventListener('click', function (event) {
|
|
if (event.target === this) {
|
|
this.classList.remove('is-active');
|
|
}
|
|
});
|
|
|
|
document.getElementById('closeServiceDeleteModal').addEventListener('click', function () {
|
|
document.getElementById('deleteModal').classList.remove('is-active');
|
|
});
|
|
|
|
var toggleServiceForm = document.getElementById('toggleServiceForm');
|
|
if (toggleServiceForm) {
|
|
toggleServiceForm.addEventListener('click', function () {
|
|
document.getElementById('formNew').classList.toggle('is-hidden');
|
|
});
|
|
}
|
|
|
|
document.querySelectorAll('[data-delete-service]').forEach(function (button) {
|
|
button.addEventListener('click', function () {
|
|
deleteService(button.dataset.serviceId, button.dataset.serviceName);
|
|
});
|
|
});
|
|
</script>
|
|
</body>
|
|
</html>
|