From 2f341bff9fbe52f6ee11a017e9c483b5c224d84c Mon Sep 17 00:00:00 2001 From: Troy Grunt Date: Sat, 7 Feb 2026 23:16:40 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20Formular-URLs=20in=20edit.php=20und=20s?= =?UTF-8?q?ave.php=20aktualisiert,=20Filter-=20und=20Validierungslogik=20i?= =?UTF-8?q?n=20list.php=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/modules/devices/edit.php | 2 +- app/modules/devices/list.php | 257 ++++++++++++++++++++++++++--------- app/modules/devices/save.php | 224 ++++++++++++++++++++++-------- 3 files changed, 365 insertions(+), 118 deletions(-) diff --git a/app/modules/devices/edit.php b/app/modules/devices/edit.php index c729d3d..c4ad625 100644 --- a/app/modules/devices/edit.php +++ b/app/modules/devices/edit.php @@ -36,7 +36,7 @@ $ports = []; // TODO: Ports vorbereiten

Gerät bearbeiten

-
+ + + + -
- - + - + - + - + - + + + + + + + + - - - - - - - - - - +
Vorschau NameGerätetypTyp Standort RackRack-PosHE Ports Aktionen
- + + + + + — + - - Gerät XY +
+ +
+
+ +
+ + + – + - + / +
+ frei
- - - - - - - - - Bearbeiten - + + Ports + Bearbeiten + + Löschen +
- + -
-

Noch keine Geräte angelegt.

-

Erstes Gerät anlegen

+

Keine Geräte gefunden.

- + + diff --git a/app/modules/devices/save.php b/app/modules/devices/save.php index f3d1bdc..b2383c6 100644 --- a/app/modules/devices/save.php +++ b/app/modules/devices/save.php @@ -1,22 +1,16 @@ 'Invalid request method']); exit; } // ========================= -// Daten aus POST / JSON +// Daten einlesen (JSON oder POST) // ========================= -// TODO: Prüfen, ob multipart/form-data oder JSON -// $data = json_decode(file_get_contents('php://input'), true); +$contentType = $_SERVER['CONTENT_TYPE'] ?? ''; +if (str_contains($contentType, 'application/json')) { + $data = json_decode(file_get_contents('php://input'), true) ?? []; +} else { + $data = $_POST; +} + +// ========================= // Basisfelder -// $deviceId = $data['id'] ?? null; -// $name = $data['name'] ?? ''; -// $description = $data['description'] ?? ''; -// $deviceTypeId = $data['device_type_id'] ?? null; -// $floorId = $data['floor_id'] ?? null; -// $rackId = $data['rack_id'] ?? null; -// $rackPosition = $data['rack_position'] ?? null; -// $svgPositions = $data['svg_positions'] ?? []; -// $portsData = $data['ports'] ?? []; +// ========================= + +$deviceId = isset($data['id']) ? (int)$data['id'] : null; +$name = trim($data['name'] ?? ''); +$comment = trim($data['comment'] ?? ''); +$deviceTypeId = (int)($data['device_type_id'] ?? 0); +$rackId = isset($data['rack_id']) ? (int)$data['rack_id'] : null; +$rackPositionHe = isset($data['rack_position_he']) ? (int)$data['rack_position_he'] : null; +$rackHeightHe = isset($data['rack_height_he']) ? (int)$data['rack_height_he'] : null; +$serialNumber = trim($data['serial_number'] ?? ''); +$portsData = $data['ports'] ?? null; // ========================= // Validierung // ========================= -// TODO: -// - Name nicht leer -// - Device-Type existiert -// - Floor / Rack gültig -// - Ports valide +$errors = []; -// ========================= -// Bild / SVG Upload (optional) -// ========================= +if ($name === '') { + $errors[] = 'Name darf nicht leer sein'; +} -// TODO: $_FILES['image'] prüfen -// - Upload in /uploads/devices -// - SVG prüfen / sanitizen -// - Pfad speichern +$deviceType = $sql->single( + "SELECT * FROM device_types WHERE id = ?", + "i", + [$deviceTypeId] +); + +if (!$deviceType) { + $errors[] = 'Ungültiger Gerätetyp'; +} + +if ($rackId !== null) { + $rack = $sql->single( + "SELECT * FROM racks WHERE id = ?", + "i", + [$rackId] + ); + + if (!$rack) { + $errors[] = 'Ungültiges Rack'; + } elseif ($rackHeightHe !== null && $rackPositionHe !== null) { + if ($rackPositionHe < 1 || ($rackPositionHe + $rackHeightHe - 1) > $rack['height_he']) { + $errors[] = 'Gerät passt nicht ins Rack'; + } + } +} + +if ($errors) { + http_response_code(400); + echo json_encode([ + 'status' => 'error', + 'errors' => $errors + ]); + exit; +} // ========================= // Device speichern // ========================= -if (!empty($deviceId)) { - // TODO: UPDATE devices SET ... - // $rows = $sql->set("UPDATE ...", "???", [...]); +if ($deviceId) { + + $sql->set( + " + UPDATE devices SET + name = ?, + comment = ?, + device_type_id = ?, + rack_id = ?, + rack_position_he = ?, + rack_height_he = ?, + serial_number = ? + WHERE id = ? + ", + "ssiiii si", + [ + $name, + $comment, + $deviceTypeId, + $rackId, + $rackPositionHe, + $rackHeightHe, + $serialNumber, + $deviceId + ] + ); + } else { - // TODO: INSERT INTO devices ... - // $deviceId = $sql->set("INSERT ...", "???", [...], true); + + $deviceId = $sql->set( + " + INSERT INTO devices + (name, comment, device_type_id, rack_id, rack_position_he, rack_height_he, serial_number) + VALUES + (?, ?, ?, ?, ?, ?, ?) + ", + "ssiiiii", + [ + $name, + $comment, + $deviceTypeId, + $rackId, + $rackPositionHe, + $rackHeightHe, + $serialNumber + ], + true + ); + + // ========================= + // Ports vom Device-Type übernehmen (nur bei NEU) + // ========================= + + $typePorts = $sql->get( + " + SELECT name, port_type_id + FROM device_type_ports + WHERE device_type_id = ? + ORDER BY id + ", + "i", + [$deviceTypeId] + ); + + foreach ($typePorts as $tp) { + $sql->set( + " + INSERT INTO device_ports + (device_id, name, port_type_id) + VALUES + (?, ?, ?) + ", + "isi", + [ + $deviceId, + $tp['name'], + $tp['port_type_id'] + ] + ); + } } // ========================= -// Ports speichern / übernehmen vom Device-Type +// Ports aktualisieren (optional, z. B. VLAN / Mode) // ========================= -// TODO: -// - $portsData iterieren -// - Positionen aus SVG übernehmen -// - port_type_id, name, VLAN, Modus speichern +if (is_array($portsData)) { + foreach ($portsData as $portId => $port) { -// ========================= -// SVG-Positionen speichern -// ========================= + $status = $port['status'] ?? 'active'; + $mode = $port['mode'] ?? null; + $vlan = isset($port['vlan_config']) ? json_encode($port['vlan_config']) : null; -// TODO: -// - device_positions Tabelle? -// - x/y Koordinaten -// - Rotation / Zoom (optional) + $sql->set( + " + UPDATE device_ports SET + status = ?, + mode = ?, + vlan_config = ? + WHERE id = ? AND device_id = ? + ", + "sssii", + [ + $status, + $mode, + $vlan, + (int)$portId, + $deviceId + ] + ); + } +} // ========================= // Antwort