Files
netwatch/app/modules/devices/save.php

248 lines
6.9 KiB
PHP

<?php
/**
* modules/devices/save.php
*
* Speichert / aktualisiert ein Gerät
* - Basisdaten
* - Rack-Zuordnung
*/
// Nur POST
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
header('Location: ?module=devices&action=list');
exit;
}
// =========================
// Daten einlesen
// =========================
$deviceId = (int)($_POST['id'] ?? 0);
$name = trim($_POST['name'] ?? '');
$deviceTypeId = (int)($_POST['device_type_id'] ?? 0);
$rackId = (int)($_POST['rack_id'] ?? 0);
$rackPositionHe = (int)($_POST['rack_position_he'] ?? 0);
$rackHeightHe = (int)($_POST['rack_height_he'] ?? 1);
$serialNumber = trim($_POST['serial_number'] ?? '');
$comment = trim($_POST['comment'] ?? '');
$webConfigUrl = trim($_POST['web_config_url'] ?? '');
$devicePortRows = is_array($_POST['device_ports'] ?? null) ? $_POST['device_ports'] : [];
if ($webConfigUrl === '') {
$webConfigUrl = null;
}
// =========================
// Validierung
// =========================
$errors = [];
if (empty($name)) {
$errors[] = "Name ist erforderlich";
}
if ($deviceTypeId <= 0) {
$errors[] = "Gerätetyp ist erforderlich";
}
if ($rackId <= 0) {
$errors[] = "Rack ist erforderlich";
}
if ($rackPositionHe <= 0) {
$errors[] = "Rack-Position muss >= 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);
}