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); }