= 1 sein"; } if ($rackHeightHe < 1) { $errors[] = "Höhe muss >= 1 sein"; } // Falls Fehler: zurück zum Edit-Formular if (!empty($errors)) { $_SESSION['error'] = implode(', ', $errors); $_SESSION['validation_errors'] = $errors; $redirectUrl = $deviceId ? "?module=devices&action=edit&id=$deviceId" : "?module=devices&action=edit"; header("Location: $redirectUrl"); exit; } // ========================= // In DB speichern // ========================= $isNewDevice = $deviceId <= 0; if ($isNewDevice) { // INSERT $deviceId = $sql->set( "INSERT INTO devices (name, device_type_id, rack_id, rack_position_he, rack_height_he, serial_number, comment, web_config_url) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", "siiiisss", [$name, $deviceTypeId, $rackId, $rackPositionHe, $rackHeightHe, $serialNumber, $comment, $webConfigUrl], true ); } else { // UPDATE $sql->set( "UPDATE devices SET name = ?, device_type_id = ?, rack_id = ?, rack_position_he = ?, rack_height_he = ?, serial_number = ?, comment = ?, web_config_url = ? WHERE id = ?", "siiiisssi", [$name, $deviceTypeId, $rackId, $rackPositionHe, $rackHeightHe, $serialNumber, $comment, $webConfigUrl, $deviceId] ); } if ($isNewDevice && $deviceId > 0) { copyDevicePortsFromType($sql, $deviceId, $deviceTypeId); } if ($deviceId > 0 && !$isNewDevice && !empty($devicePortRows)) { syncDevicePorts($sql, $deviceId, $devicePortRows); } $_SESSION['success'] = "Gerät gespeichert"; // ========================= // Redirect // ========================= header('Location: ?module=devices&action=list'); exit; function copyDevicePortsFromType($sql, $deviceId, $deviceTypeId) { if ($deviceId <= 0 || $deviceTypeId <= 0) { return; } $ports = $sql->get( "SELECT name, port_type_id FROM device_type_ports WHERE device_type_id = ? ORDER BY id", "i", [$deviceTypeId] ); if (empty($ports)) { return; } foreach ($ports as $index => $port) { $name = trim($port['name'] ?? ''); if ($name === '') { $name = 'Port ' . ($index + 1); } $portTypeId = isset($port['port_type_id']) ? (int)$port['port_type_id'] : null; if ($portTypeId !== null && $portTypeId <= 0) { $portTypeId = null; } if ($portTypeId === null) { $sql->set( "INSERT INTO device_ports (device_id, name) VALUES (?, ?)", "is", [$deviceId, $name] ); } else { $sql->set( "INSERT INTO device_ports (device_id, name, port_type_id) VALUES (?, ?, ?)", "isi", [$deviceId, $name, $portTypeId] ); } } } function syncDevicePorts($sql, $deviceId, array $rows) { $ports = $sql->get( "SELECT id, name FROM device_ports WHERE device_id = ?", "i", [$deviceId] ); if (empty($ports)) { return; } $allowedIds = []; $currentNames = []; foreach ($ports as $port) { $portId = (int)($port['id'] ?? 0); if ($portId <= 0) { continue; } $allowedIds[$portId] = true; $currentNames[$portId] = (string)($port['name'] ?? ''); } foreach ($rows as $portIdRaw => $row) { $portId = (int)$portIdRaw; if ($portId <= 0 || !isset($allowedIds[$portId]) || !is_array($row)) { continue; } $name = trim((string)($row['name'] ?? '')); if ($name === '') { $name = $currentNames[$portId] ?? ('Port ' . $portId); } $status = trim((string)($row['status'] ?? 'active')); if (!in_array($status, ['active', 'disabled'], true)) { $status = 'active'; } $mode = trim((string)($row['mode'] ?? '')); $mode = $mode !== '' ? $mode : null; $vlanJson = normalizeVlanConfig($row['vlan_config'] ?? ''); if ($mode !== null && $vlanJson !== null) { $sql->set( "UPDATE device_ports SET name = ?, status = ?, mode = ?, vlan_config = ? WHERE id = ? AND device_id = ?", "ssssii", [$name, $status, $mode, $vlanJson, $portId, $deviceId] ); } elseif ($mode !== null) { $sql->set( "UPDATE device_ports SET name = ?, status = ?, mode = ?, vlan_config = NULL WHERE id = ? AND device_id = ?", "sssii", [$name, $status, $mode, $portId, $deviceId] ); } elseif ($vlanJson !== null) { $sql->set( "UPDATE device_ports SET name = ?, status = ?, mode = NULL, vlan_config = ? WHERE id = ? AND device_id = ?", "sssii", [$name, $status, $vlanJson, $portId, $deviceId] ); } else { $sql->set( "UPDATE device_ports SET name = ?, status = ?, mode = NULL, vlan_config = NULL WHERE id = ? AND device_id = ?", "ssii", [$name, $status, $portId, $deviceId] ); } } } function normalizeVlanConfig($raw) { $value = trim((string)$raw); if ($value === '') { return null; } $parts = preg_split('/[\s,;]+/', $value); $normalized = []; foreach ((array)$parts as $part) { $entry = trim((string)$part); if ($entry === '') { continue; } $normalized[$entry] = true; } if (empty($normalized)) { return null; } return json_encode(array_keys($normalized), JSON_UNESCAPED_UNICODE); }