Files
netwatch/app/modules/devices/save.php
2026-02-11 15:46:50 +01:00

138 lines
3.6 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'] ?? '');
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);
$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);
}
$_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) {
$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]
);
}
}
}