Fix outlet selection and usage rules in new connections

This commit is contained in:
2026-02-19 09:16:49 +01:00
parent 9ece132df5
commit 1a51d2507b
2 changed files with 63 additions and 18 deletions

View File

@@ -77,6 +77,9 @@ $isEndpointAllowed = static function (string $type, int $id) use ($occupiedByTyp
if ($id <= 0) { if ($id <= 0) {
return false; return false;
} }
if ($type === 'outlet') {
return true;
}
if ($type === $portAType && $id === $portAId) { if ($type === $portAType && $id === $portAId) {
return true; return true;
} }

View File

@@ -74,31 +74,73 @@ $otherConnections = $sql->get(
[$connId] [$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) { if ($endpointId <= 0) {
return false; return;
} }
foreach ((array)$otherConnections as $row) { if (!isset($endpointUsage[$endpointType][$endpointId])) {
$typeA = $normalizePortType((string)($row['port_a_type'] ?? '')); $endpointUsage[$endpointType][$endpointId] = [
$idA = (int)($row['port_a_id'] ?? 0); 'total' => 0,
if ($typeA === $endpointType && $idA === $endpointId) { 'patchpanel' => 0,
return true; 'other' => 0,
} ];
}
$typeB = $normalizePortType((string)($row['port_b_type'] ?? '')); $endpointUsage[$endpointType][$endpointId]['total']++;
$idB = (int)($row['port_b_id'] ?? 0); if ($endpointType === 'outlet') {
if ($typeB === $endpointType && $idB === $endpointId) { if ($otherType === 'patchpanel') {
return true; $endpointUsage[$endpointType][$endpointId]['patchpanel']++;
} else {
$endpointUsage[$endpointType][$endpointId]['other']++;
} }
} }
return false;
}; };
if ($isEndpointUsed($portAType, $portAId)) { foreach ((array)$otherConnections as $row) {
$errors[] = "Port an Endpunkt A ist bereits in Verwendung"; $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)) { if (!empty($errors)) {