Files
2026-10-21 22:17:26 +02:00

198 lines
8.5 KiB
PHP

<?php
/**
* admin/map_layers.php
*
* Gestion des couches cartographiques (visibilite, couleur, ordre
* d'affichage) utilisees par la carte publique/consultation. Priorite
* "Faible" dans la roadmap : fonctionnalites simples de configuration,
* avec CSRF systematique sur toute ecriture et confirmation avant
* suppression d'une couche.
*
* Hypothese de schema :
* map_layers(id, name, layer_key, color, is_visible, sort_order,
* description)
*/
declare(strict_types=1);
require_once __DIR__ . '/admin_auth.php';
require_once __DIR__ . '/../lib/db.php';
require_once __DIR__ . '/_nav.php';
require_admin_auth();
$db = pdo();
$feedback = null;
$feedbackType = 'success';
function sanitize_hex_color(string $color): ?string
{
$color = trim($color);
return preg_match('/^#[0-9a-fA-F]{6}$/', $color) ? $color : null;
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
require_csrf_token();
$action = $_POST['action'] ?? '';
if ($action === 'create_layer') {
$name = trim((string)($_POST['name'] ?? ''));
$layerKey = trim((string)($_POST['layer_key'] ?? ''));
$color = sanitize_hex_color((string)($_POST['color'] ?? '#3388ff'));
$description = trim((string)($_POST['description'] ?? ''));
if ($name === '' || !preg_match('/^[a-z0-9_\-]+$/', $layerKey)) {
$feedback = 'Nom ou clé de couche invalide (clé: lettres minuscules, chiffres, - et _ uniquement).';
$feedbackType = 'error';
} elseif ($color === null) {
$feedback = 'Couleur invalide (format attendu : #rrggbb).';
$feedbackType = 'error';
} else {
try {
// Correction de l'erreur MySQL 1093 avec une table dérivée (sous-requête AS m2)
$stmt = $db->prepare(
'INSERT INTO map_layers (name, layer_key, color, is_visible, sort_order, description)
VALUES (?, ?, ?, 1, (SELECT COALESCE(MAX(m2.sort_order), 0) + 1 FROM (SELECT sort_order FROM map_layers) AS m2), ?)'
);
$stmt->execute([$name, $layerKey, $color, $description]);
$feedback = 'Couche créée.';
} catch (Throwable $e) {
$feedback = 'Erreur lors de la création (clé déjà utilisée ?) : ' . $e->getMessage();
$feedbackType = 'error';
}
}
} elseif ($action === 'toggle_visibility') {
$layerId = filter_input(INPUT_POST, 'layer_id', FILTER_VALIDATE_INT);
if ($layerId) {
$stmt = $db->prepare('UPDATE map_layers SET is_visible = 1 - is_visible WHERE id = ?');
$stmt->execute([$layerId]);
$feedback = 'Visibilité mise à jour.';
}
} elseif ($action === 'update_color') {
$layerId = filter_input(INPUT_POST, 'layer_id', FILTER_VALIDATE_INT);
$color = sanitize_hex_color((string)($_POST['color'] ?? ''));
if ($layerId && $color !== null) {
$stmt = $db->prepare('UPDATE map_layers SET color = ? WHERE id = ?');
$stmt->execute([$color, $layerId]);
$feedback = 'Couleur mise à jour.';
} else {
$feedback = 'Couleur invalide.';
$feedbackType = 'error';
}
} elseif ($action === 'delete_layer') {
$layerId = filter_input(INPUT_POST, 'layer_id', FILTER_VALIDATE_INT);
$confirmed = isset($_POST['confirm']) && $_POST['confirm'] === '1';
if (!$confirmed || !$layerId) {
$feedback = 'Suppression refusée : confirmation manquante.';
$feedbackType = 'error';
} else {
$stmt = $db->prepare('DELETE FROM map_layers WHERE id = ? LIMIT 1');
$stmt->execute([$layerId]);
$feedback = $stmt->rowCount() > 0 ? 'Couche supprimée.' : 'Couche introuvable.';
}
}
}
$layers = $db->query('SELECT id, name, layer_key, color, is_visible, sort_order, description FROM map_layers ORDER BY sort_order ASC')->fetchAll(PDO::FETCH_ASSOC);
$user = current_admin_username();
?>
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<title>Couches cartographiques — NetMonster Admin</title>
<link rel="stylesheet" href="assets/theme.css">
</head>
<body>
<?php render_admin_nav('map_layers.php', $user); ?>
<main class="container">
<h1>Couches cartographiques</h1>
<p>Gérez les couches affichées sur la carte (visibilité, couleur, ordre). Toute suppression demande une confirmation explicite.</p>
<?php if ($feedback !== null): ?>
<div class="alert alert-<?= $feedbackType === 'error' ? 'error' : 'success' ?>">
<?= htmlspecialchars($feedback, ENT_QUOTES, 'UTF-8') ?>
</div>
<?php endif; ?>
<section class="card">
<h2>Nouvelle couche</h2>
<form method="post">
<?= csrf_field() ?>
<input type="hidden" name="action" value="create_layer">
<label>Nom affichée
<input type="text" name="name" required maxlength="100">
</label>
<label>Clé technique
<input type="text" name="layer_key" required pattern="[a-z0-9_\-]+" placeholder="ex: sites_4g">
</label>
<label>Couleur
<input type="color" name="color" value="#3388ff">
</label>
<label>Description
<input type="text" name="description" maxlength="255">
</label>
<button type="submit" class="btn btn-primary">Créer</button>
</form>
</section>
<section class="card">
<h2>Couches existantes (<?= count($layers) ?>)</h2>
<table class="table">
<thead>
<tr>
<th>Ordre</th>
<th>Nom</th>
<th>Clé</th>
<th>Couleur</th>
<th>Visible</th>
<th>Description</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($layers as $layer): ?>
<tr>
<td><?= (int)$layer['sort_order'] ?></td>
<td><?= htmlspecialchars((string)$layer['name'], ENT_QUOTES, 'UTF-8') ?></td>
<td><code><?= htmlspecialchars((string)$layer['layer_key'], ENT_QUOTES, 'UTF-8') ?></code></td>
<td>
<form method="post" style="display:inline-flex;gap:.5rem;align-items:center">
<?= csrf_field() ?>
<input type="hidden" name="action" value="update_color">
<input type="hidden" name="layer_id" value="<?= (int)$layer['id'] ?>">
<input type="color" name="color" value="<?= htmlspecialchars((string)$layer['color'], ENT_QUOTES, 'UTF-8') ?>" onchange="this.form.submit()">
</form>
</td>
<td>
<form method="post">
<?= csrf_field() ?>
<input type="hidden" name="action" value="toggle_visibility">
<input type="hidden" name="layer_id" value="<?= (int)$layer['id'] ?>">
<button type="submit" class="btn btn-secondary">
<?= ((int)$layer['is_visible'] === 1) ? 'Visible' : 'Masquée' ?>
</button>
</form>
</td>
<td><?= htmlspecialchars((string)$layer['description'], ENT_QUOTES, 'UTF-8') ?></td>
<td>
<form method="post" onsubmit="return confirm('Confirmer la suppression de cette couche ?');">
<?= csrf_field() ?>
<input type="hidden" name="action" value="delete_layer">
<input type="hidden" name="confirm" value="1">
<input type="hidden" name="layer_id" value="<?= (int)$layer['id'] ?>">
<button type="submit" class="btn btn-danger">Supprimer</button>
</form>
</td>
</tr>
<?php endforeach; ?>
<?php if (empty($layers)): ?>
<tr><td colspan="7">Aucune couche configurée.</td></tr>
<?php endif; ?>
</tbody>
</table>
</section>
</main>
</body>
</html>