Fix outlet selection and usage rules in new connections
This commit is contained in:
@@ -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;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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,
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
$endpointUsage[$endpointType][$endpointId]['total']++;
|
||||||
$typeB = $normalizePortType((string)($row['port_b_type'] ?? ''));
|
if ($endpointType === 'outlet') {
|
||||||
$idB = (int)($row['port_b_id'] ?? 0);
|
if ($otherType === 'patchpanel') {
|
||||||
if ($typeB === $endpointType && $idB === $endpointId) {
|
$endpointUsage[$endpointType][$endpointId]['patchpanel']++;
|
||||||
return true;
|
} 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)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user