Erlaube feste Verdrahtung plus Patchkabel fuer Outlet/Patchpanel
closes #26
This commit is contained in:
@@ -90,6 +90,87 @@ function isTopologyPairAllowed(string $typeA, string $typeB): bool
|
||||
return true;
|
||||
}
|
||||
|
||||
function buildEndpointUsageMap($sql, int $excludeConnectionId = 0): array
|
||||
{
|
||||
$usage = [
|
||||
'device' => [],
|
||||
'module' => [],
|
||||
'outlet' => [],
|
||||
'patchpanel' => [],
|
||||
];
|
||||
|
||||
$rows = $sql->get(
|
||||
"SELECT id, port_a_type, port_a_id, port_b_type, port_b_id
|
||||
FROM connections
|
||||
WHERE id <> ?",
|
||||
'i',
|
||||
[$excludeConnectionId]
|
||||
);
|
||||
|
||||
$track = static function (string $endpointType, int $endpointId, string $otherType) use (&$usage): void {
|
||||
if ($endpointId <= 0 || !isset($usage[$endpointType])) {
|
||||
return;
|
||||
}
|
||||
if (!isset($usage[$endpointType][$endpointId])) {
|
||||
$usage[$endpointType][$endpointId] = [
|
||||
'total' => 0,
|
||||
'fixed' => 0,
|
||||
'patch' => 0,
|
||||
];
|
||||
}
|
||||
|
||||
$usage[$endpointType][$endpointId]['total']++;
|
||||
if (in_array($endpointType, ['outlet', 'patchpanel'], true)) {
|
||||
if (in_array($otherType, ['outlet', 'patchpanel'], true)) {
|
||||
$usage[$endpointType][$endpointId]['fixed']++;
|
||||
} elseif (in_array($otherType, ['device', 'module'], true)) {
|
||||
$usage[$endpointType][$endpointId]['patch']++;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
foreach ((array)$rows as $row) {
|
||||
$typeA = normalizeEndpointType((string)($row['port_a_type'] ?? ''));
|
||||
$typeB = normalizeEndpointType((string)($row['port_b_type'] ?? ''));
|
||||
$idA = (int)($row['port_a_id'] ?? 0);
|
||||
$idB = (int)($row['port_b_id'] ?? 0);
|
||||
if ($typeA === null || $typeB === null) {
|
||||
continue;
|
||||
}
|
||||
$track($typeA, $idA, $typeB);
|
||||
$track($typeB, $idB, $typeA);
|
||||
}
|
||||
|
||||
return $usage;
|
||||
}
|
||||
|
||||
function validateEndpointCapacity(array $usage, string $endpointType, int $endpointId, string $otherType, string $label): ?string
|
||||
{
|
||||
if ($endpointId <= 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$stats = $usage[$endpointType][$endpointId] ?? ['total' => 0, 'fixed' => 0, 'patch' => 0];
|
||||
if ((int)$stats['total'] <= 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (in_array($endpointType, ['outlet', 'patchpanel'], true)) {
|
||||
if ((int)$stats['total'] >= 2) {
|
||||
return $label . ' hat bereits die maximale Anzahl von 2 Verbindungen';
|
||||
}
|
||||
if (in_array($otherType, ['outlet', 'patchpanel'], true) && (int)$stats['fixed'] >= 1) {
|
||||
return $label . ' hat bereits eine feste Verdrahtung';
|
||||
}
|
||||
if (in_array($otherType, ['device', 'module'], true) && (int)$stats['patch'] >= 1) {
|
||||
return $label . ' hat bereits ein Patchkabel';
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
return $label . ' ist bereits in Verwendung';
|
||||
}
|
||||
|
||||
function loadConnections($sql): void
|
||||
{
|
||||
$contextType = strtolower(trim((string)($_GET['context_type'] ?? 'all')));
|
||||
@@ -226,9 +307,20 @@ function saveConnection($sql): void
|
||||
$mode = isset($data['mode']) ? (string)$data['mode'] : null;
|
||||
$comment = isset($data['comment']) ? (string)$data['comment'] : null;
|
||||
|
||||
if (!empty($data['id'])) {
|
||||
$id = (int)$data['id'];
|
||||
$existing = $sql->single('SELECT id FROM connections WHERE id = ?', 'i', [$id]);
|
||||
$connectionId = !empty($data['id']) ? (int)$data['id'] : 0;
|
||||
$usage = buildEndpointUsageMap($sql, $connectionId);
|
||||
$capacityErrorA = validateEndpointCapacity($usage, $portAType, $portAId, $portBType, 'Port an Endpunkt A');
|
||||
if ($capacityErrorA !== null) {
|
||||
jsonError($capacityErrorA, 409);
|
||||
}
|
||||
$capacityErrorB = validateEndpointCapacity($usage, $portBType, $portBId, $portAType, 'Port an Endpunkt B');
|
||||
if ($capacityErrorB !== null) {
|
||||
jsonError($capacityErrorB, 409);
|
||||
}
|
||||
|
||||
if ($connectionId > 0) {
|
||||
$id = $connectionId;
|
||||
$existing = $sql->single('SELECT id FROM connections WHERE id = ?', 'i', [$connectionId]);
|
||||
if (!$existing) {
|
||||
jsonError('Verbindung existiert nicht', 404);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user