devices anlegen
This commit is contained in:
@@ -24,6 +24,10 @@ $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
|
||||
@@ -61,21 +65,27 @@ if (!empty($errors)) {
|
||||
// =========================
|
||||
// In DB speichern
|
||||
// =========================
|
||||
if ($deviceId > 0) {
|
||||
// UPDATE
|
||||
$sql->set(
|
||||
"UPDATE devices SET name = ?, device_type_id = ?, rack_id = ?, rack_position_he = ?, rack_height_he = ?, serial_number = ?, comment = ? WHERE id = ?",
|
||||
"siiiissi",
|
||||
[$name, $deviceTypeId, $rackId, $rackPositionHe, $rackHeightHe, $serialNumber, $comment, $deviceId]
|
||||
$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 {
|
||||
// INSERT
|
||||
// UPDATE
|
||||
$sql->set(
|
||||
"INSERT INTO devices (name, device_type_id, rack_id, rack_position_he, rack_height_he, serial_number, comment) VALUES (?, ?, ?, ?, ?, ?, ?)",
|
||||
"siiiiss",
|
||||
[$name, $deviceTypeId, $rackId, $rackPositionHe, $rackHeightHe, $serialNumber, $comment]
|
||||
"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]
|
||||
);
|
||||
$deviceId = $sql->h->insert_id;
|
||||
}
|
||||
|
||||
if ($isNewDevice && $deviceId > 0) {
|
||||
copyDevicePortsFromType($sql, $deviceId, $deviceTypeId);
|
||||
}
|
||||
|
||||
$_SESSION['success'] = "Gerät gespeichert";
|
||||
@@ -85,3 +95,43 @@ $_SESSION['success'] = "Gerät gespeichert";
|
||||
// =========================
|
||||
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]
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user