Files
ecoles/api/folders.php
T
Arnaud 5633969d98 Version initiale — carte des établissements scolaires
Carte Leaflet (annuaire Éducation nationale) + API PHP/MySQL :
fiche établissement (effectifs, IPS, population INSEE, DPE ADEME),
GED documents/photos, commentaires par groupe, gestion utilisateurs
et politique de mots de passe. Imports opendata (effectifs, IPS,
population, DPE, IRIS).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-03 21:35:41 +02:00

90 lines
3.1 KiB
PHP

<?php
/**
* Arborescence globale de dossiers (gérée par le super-admin).
* GET → liste à plat de tous les dossiers (public)
* POST {name, parent_id?} → crée un dossier (super-admin)
* PUT {id, name?, parent_id?, position?} → modifie (super-admin)
* DELETE ?id=N → supprime (et ses sous-dossiers) (super-admin)
*/
declare(strict_types=1);
require __DIR__ . '/db.php';
try {
$method = $_SERVER['REQUEST_METHOD'];
if ($method === 'GET') {
$rows = db()->query('SELECT id, parent_id, name, position FROM folders ORDER BY position, name')->fetchAll();
foreach ($rows as &$r) {
$r['id'] = (int) $r['id'];
$r['parent_id'] = $r['parent_id'] !== null ? (int) $r['parent_id'] : null;
$r['position'] = (int) $r['position'];
}
json_out($rows);
}
require_super();
if ($method === 'POST') {
$b = body_json();
$name = trim((string) ($b['name'] ?? ''));
if ($name === '') {
json_error('Nom de dossier requis.');
}
$parent = isset($b['parent_id']) && $b['parent_id'] !== null && $b['parent_id'] !== ''
? (int) $b['parent_id'] : null;
$pos = (int) ($b['position'] ?? 0);
$st = db()->prepare('INSERT INTO folders (parent_id, name, position) VALUES (?, ?, ?)');
$st->execute([$parent, $name, $pos]);
json_out(['id' => (int) db()->lastInsertId(), 'parent_id' => $parent, 'name' => $name, 'position' => $pos], 201);
}
if ($method === 'PUT') {
$b = body_json();
$id = (int) ($b['id'] ?? 0);
if ($id <= 0) {
json_error('id requis.');
}
$sets = [];
$args = [];
if (array_key_exists('name', $b)) {
$name = trim((string) $b['name']);
if ($name === '') {
json_error('Nom invalide.');
}
$sets[] = 'name = ?';
$args[] = $name;
}
if (array_key_exists('parent_id', $b)) {
$parent = ($b['parent_id'] === null || $b['parent_id'] === '') ? null : (int) $b['parent_id'];
if ($parent === $id) {
json_error('Un dossier ne peut pas être son propre parent.');
}
$sets[] = 'parent_id = ?';
$args[] = $parent;
}
if (array_key_exists('position', $b)) {
$sets[] = 'position = ?';
$args[] = (int) $b['position'];
}
if (!$sets) {
json_error('Rien à modifier.');
}
$args[] = $id;
db()->prepare('UPDATE folders SET ' . implode(', ', $sets) . ' WHERE id = ?')->execute($args);
json_out(['ok' => true]);
}
if ($method === 'DELETE') {
$id = (int) ($_GET['id'] ?? 0);
if ($id <= 0) {
json_error('id requis.');
}
db()->prepare('DELETE FROM folders WHERE id = ?')->execute([$id]);
json_out(['deleted' => $id]);
}
json_error('Méthode non supportée.', 405);
} catch (Throwable $e) {
json_error('Erreur : ' . $e->getMessage(), 500);
}