feat: Formular-URLs in edit.php und save.php aktualisiert, Filter- und Validierungslogik in list.php hinzugefügt
This commit is contained in:
@@ -1,22 +1,16 @@
|
||||
<?php
|
||||
/**
|
||||
* app/devices/save.php
|
||||
* modules/devices/save.php
|
||||
*
|
||||
* Speichert:
|
||||
* - Basisdaten (Name, Beschreibung)
|
||||
* - Gerätetyp-Zuordnung
|
||||
* - Standort (Floor, Rack, Rack-Position)
|
||||
* - Ports (vom Device-Type übernommen)
|
||||
* - SVG-Positionen
|
||||
* Speichert / aktualisiert ein Gerät
|
||||
* - Basisdaten
|
||||
* - Rack-Zuordnung
|
||||
* - Ports (automatisch aus Device-Type oder manuell)
|
||||
*
|
||||
* POST JSON oder multipart/form-data
|
||||
* Erwartet POST (form-data ODER JSON)
|
||||
*/
|
||||
|
||||
// TODO: bootstrap laden
|
||||
// require_once __DIR__ . '/../../bootstrap.php';
|
||||
|
||||
// TODO: Auth erzwingen
|
||||
// requireAuth();
|
||||
require_once __DIR__ . '/../../bootstrap.php';
|
||||
|
||||
// =========================
|
||||
// Request prüfen
|
||||
@@ -24,75 +18,197 @@
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||
http_response_code(405);
|
||||
echo json_encode(['error' => '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
|
||||
|
||||
Reference in New Issue
Block a user