diff --git a/app/modules/connections/edit.php b/app/modules/connections/edit.php index 331eb86..40fe688 100644 --- a/app/modules/connections/edit.php +++ b/app/modules/connections/edit.php @@ -77,6 +77,9 @@ $isEndpointAllowed = static function (string $type, int $id) use ($occupiedByTyp if ($id <= 0) { return false; } + if ($type === 'outlet') { + return true; + } if ($type === $portAType && $id === $portAId) { return true; } diff --git a/app/modules/connections/save.php b/app/modules/connections/save.php index cd001c4..c6f3aa9 100644 --- a/app/modules/connections/save.php +++ b/app/modules/connections/save.php @@ -74,31 +74,73 @@ $otherConnections = $sql->get( [$connId] ); -$isEndpointUsed = static function (string $endpointType, int $endpointId) use ($otherConnections, $normalizePortType): bool { +$endpointUsage = []; +$trackUsage = static function (string $endpointType, int $endpointId, string $otherType) use (&$endpointUsage): void { if ($endpointId <= 0) { - return false; + return; } - foreach ((array)$otherConnections as $row) { - $typeA = $normalizePortType((string)($row['port_a_type'] ?? '')); - $idA = (int)($row['port_a_id'] ?? 0); - if ($typeA === $endpointType && $idA === $endpointId) { - return true; - } - - $typeB = $normalizePortType((string)($row['port_b_type'] ?? '')); - $idB = (int)($row['port_b_id'] ?? 0); - if ($typeB === $endpointType && $idB === $endpointId) { - return true; + if (!isset($endpointUsage[$endpointType][$endpointId])) { + $endpointUsage[$endpointType][$endpointId] = [ + 'total' => 0, + 'patchpanel' => 0, + 'other' => 0, + ]; + } + $endpointUsage[$endpointType][$endpointId]['total']++; + if ($endpointType === 'outlet') { + if ($otherType === 'patchpanel') { + $endpointUsage[$endpointType][$endpointId]['patchpanel']++; + } else { + $endpointUsage[$endpointType][$endpointId]['other']++; } } - return false; }; -if ($isEndpointUsed($portAType, $portAId)) { - $errors[] = "Port an Endpunkt A ist bereits in Verwendung"; +foreach ((array)$otherConnections as $row) { + $typeA = $normalizePortType((string)($row['port_a_type'] ?? '')); + $typeB = $normalizePortType((string)($row['port_b_type'] ?? '')); + $idA = (int)($row['port_a_id'] ?? 0); + $idB = (int)($row['port_b_id'] ?? 0); + + $trackUsage($typeA, $idA, $typeB); + $trackUsage($typeB, $idB, $typeA); } -if ($isEndpointUsed($portBType, $portBId)) { - $errors[] = "Port an Endpunkt B ist bereits in Verwendung"; + +$validateEndpointUsage = static function (string $endpointType, int $endpointId, string $otherType, string $label) use ($endpointUsage): ?string { + if ($endpointId <= 0) { + return null; + } + + $stats = $endpointUsage[$endpointType][$endpointId] ?? ['total' => 0, 'patchpanel' => 0, 'other' => 0]; + if ((int)$stats['total'] <= 0) { + return null; + } + + if ($endpointType !== 'outlet') { + return $label . " ist bereits in Verwendung"; + } + + if ($otherType === 'patchpanel') { + if ((int)$stats['patchpanel'] > 0) { + return $label . " hat bereits eine Patchpanel-Verbindung"; + } + return null; + } + + if ((int)$stats['other'] > 0) { + return $label . " hat bereits eine Endgeraete-Verbindung"; + } + return null; +}; + +$errorA = $validateEndpointUsage($portAType, $portAId, $portBType, 'Port an Endpunkt A'); +if ($errorA !== null) { + $errors[] = $errorA; +} + +$errorB = $validateEndpointUsage($portBType, $portBId, $portAType, 'Port an Endpunkt B'); +if ($errorB !== null) { + $errors[] = $errorB; } if (!empty($errors)) {