Files
netwatch/app/modules/floor_infrastructure/save.php
2026-02-19 10:20:04 +01:00

265 lines
10 KiB
PHP

<?php
/**
* app/modules/floor_infrastructure/save.php
*
* Speichert Patchpanel- und Wandbuchsen-Einträge
*/
$type = $_POST['type'] ?? '';
$id = (int)($_POST['id'] ?? 0);
if ($type === 'patchpanel') {
$fixedPanelWidth = 20;
$fixedPanelHeight = 5;
$name = trim($_POST['name'] ?? '');
$floorId = (int)($_POST['floor_id'] ?? 0);
$posX = (int)($_POST['pos_x'] ?? 0);
$posY = (int)($_POST['pos_y'] ?? 0);
$width = $fixedPanelWidth;
$height = $fixedPanelHeight;
$portCount = (int)($_POST['port_count'] ?? 0);
$comment = trim($_POST['comment'] ?? '');
$errors = [];
if ($name === '') {
$errors[] = 'Name ist erforderlich';
}
if ($floorId <= 0) {
$errors[] = 'Stockwerk ist erforderlich';
}
if ($portCount < 0) {
$errors[] = 'Port-Anzahl darf nicht negativ sein';
}
if (!empty($errors)) {
$_SESSION['error'] = implode(', ', $errors);
$_SESSION['validation_errors'] = $errors;
$redirectUrl = $id > 0 ? "?module=floor_infrastructure&action=edit&type=patchpanel&id=$id" : "?module=floor_infrastructure&action=edit&type=patchpanel";
header("Location: $redirectUrl");
exit;
}
$panelId = $id;
if ($id > 0) {
$sql->set(
"UPDATE floor_patchpanels SET name = ?, floor_id = ?, pos_x = ?, pos_y = ?, width = ?, height = ?, port_count = ?, comment = ? WHERE id = ?",
"siiiiisii",
[$name, $floorId, $posX, $posY, $width, $height, $portCount, $comment, $id]
);
} else {
$panelId = (int)$sql->set(
"INSERT INTO floor_patchpanels (name, floor_id, pos_x, pos_y, width, height, port_count, comment) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
"siiiiiss",
[$name, $floorId, $posX, $posY, $width, $height, $portCount, $comment],
true
);
}
if ($panelId > 0 && $portCount > 0) {
$existingCount = (int)($sql->single(
"SELECT COUNT(*) AS cnt FROM floor_patchpanel_ports WHERE patchpanel_id = ?",
"i",
[$panelId]
)['cnt'] ?? 0);
if ($existingCount < $portCount) {
for ($i = $existingCount + 1; $i <= $portCount; $i++) {
$sql->set(
"INSERT INTO floor_patchpanel_ports (patchpanel_id, name) VALUES (?, ?)",
"is",
[$panelId, 'Port ' . $i]
);
}
}
}
$_SESSION['success'] = $id > 0 ? 'Patchpanel gespeichert' : 'Patchpanel erstellt';
} elseif ($type === 'outlet') {
$name = trim($_POST['name'] ?? '');
$roomId = (int)($_POST['room_id'] ?? 0);
$x = (int)($_POST['x'] ?? 0);
$y = (int)($_POST['y'] ?? 0);
$comment = trim($_POST['comment'] ?? '');
$bindPatchpanelPortId = (int)($_POST['bind_patchpanel_port_id'] ?? 0);
$outletId = $id;
$errors = [];
if ($name === '') {
$errors[] = 'Name ist erforderlich';
}
if ($roomId <= 0) {
$errors[] = 'Raum ist erforderlich';
}
if (!empty($errors)) {
$_SESSION['error'] = implode(', ', $errors);
$_SESSION['validation_errors'] = $errors;
$redirectUrl = $id > 0 ? "?module=floor_infrastructure&action=edit&type=outlet&id=$id" : "?module=floor_infrastructure&action=edit&type=outlet";
header("Location: $redirectUrl");
exit;
}
if ($id > 0) {
$sql->set(
"UPDATE network_outlets SET name = ?, room_id = ?, x = ?, y = ?, comment = ? WHERE id = ?",
"siiisi",
[$name, $roomId, $x, $y, $comment, $id]
);
} else {
$outletId = (int)$sql->set(
"INSERT INTO network_outlets (name, room_id, x, y, comment) VALUES (?, ?, ?, ?, ?)",
"siiis",
[$name, $roomId, $x, $y, $comment],
true
);
}
if ($outletId > 0) {
$existingPortCount = (int)($sql->single(
"SELECT COUNT(*) AS cnt FROM network_outlet_ports WHERE outlet_id = ?",
"i",
[$outletId]
)['cnt'] ?? 0);
if ($existingPortCount === 0) {
$sql->set(
"INSERT INTO network_outlet_ports (outlet_id, name) VALUES (?, 'Port 1')",
"i",
[$outletId]
);
}
if ($bindPatchpanelPortId > 0) {
$roomFloorId = (int)($sql->single(
"SELECT floor_id FROM rooms WHERE id = ?",
"i",
[$roomId]
)['floor_id'] ?? 0);
$patchpanelPort = $sql->single(
"SELECT
fpp.id,
fp.floor_id
FROM floor_patchpanel_ports fpp
JOIN floor_patchpanels fp ON fp.id = fpp.patchpanel_id
WHERE fpp.id = ?",
"i",
[$bindPatchpanelPortId]
);
if (!$patchpanelPort) {
$_SESSION['error'] = 'Gewaehlter Patchpanel-Port existiert nicht';
$_SESSION['validation_errors'] = ['Gewaehlter Patchpanel-Port existiert nicht'];
header('Location: ?module=floor_infrastructure&action=edit&type=outlet&id=' . $outletId);
exit;
}
if ($roomFloorId <= 0 || (int)$patchpanelPort['floor_id'] !== $roomFloorId) {
$_SESSION['error'] = 'Patchpanel-Port und Raum muessen auf demselben Stockwerk liegen';
$_SESSION['validation_errors'] = ['Patchpanel-Port und Raum muessen auf demselben Stockwerk liegen'];
header('Location: ?module=floor_infrastructure&action=edit&type=outlet&id=' . $outletId);
exit;
}
$outletPortId = (int)($sql->single(
"SELECT id
FROM network_outlet_ports
WHERE outlet_id = ?
ORDER BY id
LIMIT 1",
"i",
[$outletId]
)['id'] ?? 0);
if ($outletPortId <= 0) {
$_SESSION['error'] = 'Wandbuchsen-Port konnte nicht ermittelt werden';
$_SESSION['validation_errors'] = ['Wandbuchsen-Port konnte nicht ermittelt werden'];
header('Location: ?module=floor_infrastructure&action=edit&type=outlet&id=' . $outletId);
exit;
}
$existingPatchpanelUsage = $sql->single(
"SELECT
id,
port_a_type,
port_a_id,
port_b_type,
port_b_id
FROM connections
WHERE
((port_a_type = 'patchpanel' OR port_a_type = 'floor_patchpanel_ports') AND port_a_id = ?)
OR
((port_b_type = 'patchpanel' OR port_b_type = 'floor_patchpanel_ports') AND port_b_id = ?)
LIMIT 1",
"ii",
[$bindPatchpanelPortId, $bindPatchpanelPortId]
);
if ($existingPatchpanelUsage) {
$sameOutletConnection = (
(
(($existingPatchpanelUsage['port_a_type'] ?? '') === 'outlet' || ($existingPatchpanelUsage['port_a_type'] ?? '') === 'network_outlet_ports')
&& (int)($existingPatchpanelUsage['port_a_id'] ?? 0) === $outletPortId
)
||
(
(($existingPatchpanelUsage['port_b_type'] ?? '') === 'outlet' || ($existingPatchpanelUsage['port_b_type'] ?? '') === 'network_outlet_ports')
&& (int)($existingPatchpanelUsage['port_b_id'] ?? 0) === $outletPortId
)
);
if (!$sameOutletConnection) {
$_SESSION['error'] = 'Gewaehlter Patchpanel-Port ist bereits verbunden';
$_SESSION['validation_errors'] = ['Gewaehlter Patchpanel-Port ist bereits verbunden'];
header('Location: ?module=floor_infrastructure&action=edit&type=outlet&id=' . $outletId);
exit;
}
}
$sql->set(
"DELETE FROM connections
WHERE
((port_a_type = 'outlet' OR port_a_type = 'network_outlet_ports') AND port_a_id = ? AND (port_b_type = 'patchpanel' OR port_b_type = 'floor_patchpanel_ports'))
OR
((port_b_type = 'outlet' OR port_b_type = 'network_outlet_ports') AND port_b_id = ? AND (port_a_type = 'patchpanel' OR port_a_type = 'floor_patchpanel_ports'))",
"ii",
[$outletPortId, $outletPortId]
);
$connectionTypeId = (int)($sql->single(
"SELECT id FROM connection_types ORDER BY id LIMIT 1",
"",
[]
)['id'] ?? 0);
if ($connectionTypeId <= 0) {
$connectionTypeId = (int)$sql->set(
"INSERT INTO connection_types (name, medium, duplex, line_style, comment) VALUES (?, ?, ?, ?, ?)",
"sssss",
['Default', 'copper', 'custom', 'solid', 'Auto-created by floor_infrastructure/save'],
true
);
}
if ($connectionTypeId <= 0) {
$_SESSION['error'] = 'Kein Verbindungstyp fuer automatische Bindung verfuegbar';
$_SESSION['validation_errors'] = ['Kein Verbindungstyp fuer automatische Bindung verfuegbar'];
header('Location: ?module=floor_infrastructure&action=edit&type=outlet&id=' . $outletId);
exit;
}
$sql->set(
"INSERT INTO connections (connection_type_id, port_a_type, port_a_id, port_b_type, port_b_id, vlan_config, comment)
VALUES (?, 'outlet', ?, 'patchpanel', ?, NULL, ?)",
"iiis",
[$connectionTypeId, $outletPortId, $bindPatchpanelPortId, 'Auto-Link bei Wandbuchsen-Erstellung']
);
}
}
$_SESSION['success'] = $id > 0 ? 'Wandbuchse gespeichert' : 'Wandbuchse erstellt';
} else {
$_SESSION['error'] = 'Ungueltiger Infrastrukturobjekt-Typ';
$_SESSION['validation_errors'] = ['Ungueltiger Infrastrukturobjekt-Typ'];
}
header('Location: ?module=floor_infrastructure&action=list');
exit;