Erlaube feste Verdrahtung plus Patchkabel fuer Outlet/Patchpanel

closes #26
This commit is contained in:
2026-02-19 11:00:48 +01:00
parent b973d2857b
commit dbe977f62c
4 changed files with 142 additions and 17 deletions

View File

@@ -46,7 +46,7 @@ $endpointOptions = [
'patchpanel' => [],
];
$occupiedByType = [
$occupiedStatsByType = [
'device' => [],
'module' => [],
'outlet' => [],
@@ -62,18 +62,24 @@ $occupiedRows = $sql->get(
foreach ((array)$occupiedRows as $row) {
$typeA = $normalizePortType((string)($row['port_a_type'] ?? ''));
$idA = (int)($row['port_a_id'] ?? 0);
if ($idA > 0 && isset($occupiedByType[$typeA])) {
$occupiedByType[$typeA][$idA] = true;
if ($idA > 0 && isset($occupiedStatsByType[$typeA])) {
if (!isset($occupiedStatsByType[$typeA][$idA])) {
$occupiedStatsByType[$typeA][$idA] = ['total' => 0];
}
$occupiedStatsByType[$typeA][$idA]['total']++;
}
$typeB = $normalizePortType((string)($row['port_b_type'] ?? ''));
$idB = (int)($row['port_b_id'] ?? 0);
if ($idB > 0 && isset($occupiedByType[$typeB])) {
$occupiedByType[$typeB][$idB] = true;
if ($idB > 0 && isset($occupiedStatsByType[$typeB])) {
if (!isset($occupiedStatsByType[$typeB][$idB])) {
$occupiedStatsByType[$typeB][$idB] = ['total' => 0];
}
$occupiedStatsByType[$typeB][$idB]['total']++;
}
}
$isEndpointAllowed = static function (string $type, int $id) use ($occupiedByType, $portAType, $portAId, $portBType, $portBId): bool {
$isEndpointAllowed = static function (string $type, int $id) use ($occupiedStatsByType, $portAType, $portAId, $portBType, $portBId): bool {
if ($id <= 0) {
return false;
}
@@ -83,7 +89,10 @@ $isEndpointAllowed = static function (string $type, int $id) use ($occupiedByTyp
if ($type === $portBType && $id === $portBId) {
return true;
}
return empty($occupiedByType[$type][$id]);
$stats = $occupiedStatsByType[$type][$id] ?? ['total' => 0];
$maxConnections = in_array($type, ['outlet', 'patchpanel'], true) ? 2 : 1;
return (int)($stats['total'] ?? 0) < $maxConnections;
};
// Auto-heal: ensure each outlet has at least one selectable port.

View File

@@ -75,16 +75,26 @@ $otherConnections = $sql->get(
);
$endpointUsage = [];
$trackUsage = static function (string $endpointType, int $endpointId) use (&$endpointUsage): void {
$trackUsage = static function (string $endpointType, int $endpointId, string $otherType) use (&$endpointUsage): void {
if ($endpointId <= 0) {
return;
}
if (!isset($endpointUsage[$endpointType][$endpointId])) {
$endpointUsage[$endpointType][$endpointId] = [
'total' => 0,
'fixed' => 0,
'patch' => 0,
];
}
$endpointUsage[$endpointType][$endpointId]['total']++;
if (in_array($endpointType, ['outlet', 'patchpanel'], true)) {
if (in_array($otherType, ['outlet', 'patchpanel'], true)) {
$endpointUsage[$endpointType][$endpointId]['fixed']++;
} elseif (in_array($otherType, ['device', 'module'], true)) {
$endpointUsage[$endpointType][$endpointId]['patch']++;
}
}
};
foreach ((array)$otherConnections as $row) {
@@ -93,29 +103,42 @@ foreach ((array)$otherConnections as $row) {
$idA = (int)($row['port_a_id'] ?? 0);
$idB = (int)($row['port_b_id'] ?? 0);
$trackUsage($typeA, $idA);
$trackUsage($typeB, $idB);
$trackUsage($typeA, $idA, $typeB);
$trackUsage($typeB, $idB, $typeA);
}
$validateEndpointUsage = static function (string $endpointType, int $endpointId, string $label) use ($endpointUsage): ?string {
$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];
$stats = $endpointUsage[$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";
};
$errorA = $validateEndpointUsage($portAType, $portAId, 'Port an Endpunkt A');
$errorA = $validateEndpointUsage($portAType, $portAId, $portBType, 'Port an Endpunkt A');
if ($errorA !== null) {
$errors[] = $errorA;
}
$errorB = $validateEndpointUsage($portBType, $portBId, 'Port an Endpunkt B');
$errorB = $validateEndpointUsage($portBType, $portBId, $portAType, 'Port an Endpunkt B');
if ($errorB !== null) {
$errors[] = $errorB;
}