Files
netwatch/app/modules/connections/list.php

460 lines
19 KiB
PHP

<?php
/**
* app/modules/connections/list.php
*
* Uebersicht der Netzwerkverbindungen
*/
$search = trim($_GET['search'] ?? '');
$deviceId = (int)($_GET['device_id'] ?? 0);
$selectedConnectionId = (int)($_GET['connection_id'] ?? 0);
$endpointUnionSql = "
SELECT
'device' AS endpoint_type,
dp.id AS endpoint_id,
dp.name AS port_name,
d.name AS owner_name,
d.id AS owner_device_id
FROM device_ports dp
JOIN devices d ON d.id = dp.device_id
UNION ALL
SELECT
'module' AS endpoint_type,
mp.id AS endpoint_id,
mp.name AS port_name,
CONCAT(IFNULL(MIN(d.name), 'Unzugeordnet'), ' / ', m.name) AS owner_name,
MIN(d.id) AS owner_device_id
FROM module_ports mp
JOIN modules m ON m.id = mp.module_id
LEFT JOIN device_port_modules dpm ON dpm.module_id = m.id
LEFT JOIN device_ports dp ON dp.id = dpm.device_port_id
LEFT JOIN devices d ON d.id = dp.device_id
GROUP BY mp.id, mp.name, m.name
UNION ALL
SELECT
'outlet' AS endpoint_type,
nop.id AS endpoint_id,
nop.name AS port_name,
CONCAT(o.name, ' / ', IFNULL(r.name, ''), ' / ', IFNULL(f.name, '')) AS owner_name,
NULL AS owner_device_id
FROM network_outlet_ports nop
JOIN network_outlets o ON o.id = nop.outlet_id
LEFT JOIN rooms r ON r.id = o.room_id
LEFT JOIN floors f ON f.id = r.floor_id
UNION ALL
SELECT
'floor_patchpanel' AS endpoint_type,
fpp.id AS endpoint_id,
fpp.name AS port_name,
CONCAT(fp.name, ' / ', IFNULL(f.name, '')) AS owner_name,
NULL AS owner_device_id
FROM floor_patchpanel_ports fpp
JOIN floor_patchpanels fp ON fp.id = fpp.patchpanel_id
LEFT JOIN floors f ON f.id = fp.floor_id
";
$where = [];
$types = '';
$params = [];
if ($search !== '') {
$where[] = "(e1.owner_name LIKE ? OR e2.owner_name LIKE ? OR e1.port_name LIKE ? OR e2.port_name LIKE ?)";
$types .= 'ssss';
$params[] = "%$search%";
$params[] = "%$search%";
$params[] = "%$search%";
$params[] = "%$search%";
}
if ($deviceId > 0) {
$where[] = "(e1.owner_device_id = ? OR e2.owner_device_id = ?)";
$types .= 'ii';
$params[] = $deviceId;
$params[] = $deviceId;
}
$whereSql = $where ? 'WHERE ' . implode(' AND ', $where) : '';
$connections = $sql->get(
"SELECT
c.id,
c.port_a_type, c.port_a_id, c.port_b_type, c.port_b_id,
e1.owner_name AS endpoint_a_name,
e2.owner_name AS endpoint_b_name,
e1.port_name AS port_a_name,
e2.port_name AS port_b_name,
c.vlan_config,
c.comment
FROM connections c
LEFT JOIN ($endpointUnionSql) e1
ON c.port_a_id = e1.endpoint_id
AND (
c.port_a_type = e1.endpoint_type
OR (c.port_a_type = 'device_ports' AND e1.endpoint_type = 'device')
OR (c.port_a_type = 'module_ports' AND e1.endpoint_type = 'module')
OR (c.port_a_type = 'network_outlet_ports' AND e1.endpoint_type = 'outlet')
OR (c.port_a_type = 'floor_patchpanel_ports' AND e1.endpoint_type = 'floor_patchpanel')
OR (c.port_a_type = 'patchpanel' AND e1.endpoint_type = 'floor_patchpanel')
)
LEFT JOIN ($endpointUnionSql) e2
ON c.port_b_id = e2.endpoint_id
AND (
c.port_b_type = e2.endpoint_type
OR (c.port_b_type = 'device_ports' AND e2.endpoint_type = 'device')
OR (c.port_b_type = 'module_ports' AND e2.endpoint_type = 'module')
OR (c.port_b_type = 'network_outlet_ports' AND e2.endpoint_type = 'outlet')
OR (c.port_b_type = 'floor_patchpanel_ports' AND e2.endpoint_type = 'floor_patchpanel')
OR (c.port_b_type = 'patchpanel' AND e2.endpoint_type = 'floor_patchpanel')
)
$whereSql
ORDER BY e1.owner_name, e2.owner_name",
$types,
$params
);
$devices = $sql->get('SELECT id, name FROM devices ORDER BY name', '', []);
$selectedConnection = null;
if ($selectedConnectionId > 0) {
foreach ((array)$connections as $entry) {
if ((int)($entry['id'] ?? 0) === $selectedConnectionId) {
$selectedConnection = $entry;
break;
}
}
}
if ($selectedConnection === null && !empty($connections)) {
$selectedConnection = $connections[0];
$selectedConnectionId = (int)($selectedConnection['id'] ?? 0);
}
$selectedDevice = null;
$selectedDevicePorts = [];
$selectedDeviceVlans = [];
if ($deviceId > 0) {
$selectedDevice = $sql->single(
"SELECT d.id, d.name, dt.name AS type_name
FROM devices d
LEFT JOIN device_types dt ON d.device_type_id = dt.id
WHERE d.id = ?",
'i',
[$deviceId]
);
if ($selectedDevice) {
$selectedDevice['port_count'] = (int)($sql->single(
"SELECT COUNT(*) AS cnt
FROM (
SELECT dp.id
FROM device_ports dp
WHERE dp.device_id = ?
UNION ALL
SELECT DISTINCT mp.id
FROM module_ports mp
JOIN modules m ON m.id = mp.module_id
JOIN device_port_modules dpm ON dpm.module_id = m.id
JOIN device_ports dp ON dp.id = dpm.device_port_id
WHERE dp.device_id = ?
) p",
'ii',
[$deviceId, $deviceId]
)['cnt'] ?? 0);
$selectedDevice['connection_count'] = (int)($sql->single(
"SELECT COUNT(DISTINCT c.id) AS cnt
FROM connections c
LEFT JOIN ($endpointUnionSql) e1
ON c.port_a_id = e1.endpoint_id
AND (
c.port_a_type = e1.endpoint_type
OR (c.port_a_type = 'device_ports' AND e1.endpoint_type = 'device')
OR (c.port_a_type = 'module_ports' AND e1.endpoint_type = 'module')
OR (c.port_a_type = 'network_outlet_ports' AND e1.endpoint_type = 'outlet')
OR (c.port_a_type = 'floor_patchpanel_ports' AND e1.endpoint_type = 'floor_patchpanel')
OR (c.port_a_type = 'patchpanel' AND e1.endpoint_type = 'floor_patchpanel')
)
LEFT JOIN ($endpointUnionSql) e2
ON c.port_b_id = e2.endpoint_id
AND (
c.port_b_type = e2.endpoint_type
OR (c.port_b_type = 'device_ports' AND e2.endpoint_type = 'device')
OR (c.port_b_type = 'module_ports' AND e2.endpoint_type = 'module')
OR (c.port_b_type = 'network_outlet_ports' AND e2.endpoint_type = 'outlet')
OR (c.port_b_type = 'floor_patchpanel_ports' AND e2.endpoint_type = 'floor_patchpanel')
OR (c.port_b_type = 'patchpanel' AND e2.endpoint_type = 'floor_patchpanel')
)
WHERE e1.owner_device_id = ? OR e2.owner_device_id = ?",
'ii',
[$deviceId, $deviceId]
)['cnt'] ?? 0);
$selectedDevicePorts = $sql->get(
"SELECT name, vlan_config
FROM (
SELECT dp.name, dp.vlan_config, dp.id AS sort_id
FROM device_ports dp
WHERE dp.device_id = ?
UNION ALL
SELECT DISTINCT CONCAT(m.name, ' / ', mp.name) AS name, NULL AS vlan_config, (1000000 + mp.id) AS sort_id
FROM module_ports mp
JOIN modules m ON m.id = mp.module_id
JOIN device_port_modules dpm ON dpm.module_id = m.id
JOIN device_ports dp ON dp.id = dpm.device_port_id
WHERE dp.device_id = ?
) p
ORDER BY sort_id
LIMIT 12",
'ii',
[$deviceId, $deviceId]
);
foreach ($selectedDevicePorts as $port) {
if (empty($port['vlan_config'])) {
continue;
}
$vlans = json_decode($port['vlan_config'], true);
foreach ((array)$vlans as $vlan) {
$vlan = trim((string)$vlan);
if ($vlan !== '') {
$selectedDeviceVlans[$vlan] = true;
}
}
}
$selectedDeviceVlans = array_keys($selectedDeviceVlans);
natcasesort($selectedDeviceVlans);
}
}
$buildListUrl = static function (array $extra = []) use ($search, $deviceId): string {
$query = ['module' => 'connections', 'action' => 'list'];
if ($search !== '') {
$query['search'] = $search;
}
if ($deviceId > 0) {
$query['device_id'] = $deviceId;
}
foreach ($extra as $key => $value) {
if ($value === null || $value === '') {
continue;
}
$query[$key] = $value;
}
return '?' . http_build_query($query);
};
?>
<div class="connections-layout">
<div class="connections-container">
<h1>Netzwerkverbindungen</h1>
<div class="filter-form">
<form method="GET">
<input type="hidden" name="module" value="connections">
<input type="hidden" name="action" value="list">
<input type="text" name="search" placeholder="Suche nach Geraet oder Port..."
value="<?php echo htmlspecialchars($search); ?>" class="search-input">
<select name="device_id">
<option value="">- Alle Geraete -</option>
<?php foreach ($devices as $device): ?>
<option value="<?php echo (int)$device['id']; ?>"
<?php echo ((int)$device['id'] === $deviceId) ? 'selected' : ''; ?>>
<?php echo htmlspecialchars((string)$device['name']); ?>
</option>
<?php endforeach; ?>
</select>
<button type="submit" class="button">Filter</button>
<a href="?module=connections&action=list" class="button">Reset</a>
<a href="?module=connections&action=edit" class="button button-primary">+ Neue Verbindung</a>
</form>
</div>
<?php if (!empty($connections)): ?>
<table class="connections-list">
<thead>
<tr>
<th>Von (Geraet -> Port)</th>
<th>Nach (Geraet -> Port)</th>
<th>VLANs</th>
<th>Beschreibung</th>
<th>Status</th>
<th>Aktionen</th>
</tr>
</thead>
<tbody>
<?php foreach ($connections as $conn): ?>
<?php
$connId = (int)($conn['id'] ?? 0);
$comment = trim((string)($conn['comment'] ?? ''));
$hasMissingInfo = empty($conn['endpoint_a_name']) || empty($conn['endpoint_b_name'])
|| empty($conn['port_a_name']) || empty($conn['port_b_name']);
$commentLower = mb_strtolower($comment, 'UTF-8');
$warningFromComment = preg_match('/warn|achtung|critical/', $commentLower) === 1;
$hasWarning = $hasMissingInfo || $warningFromComment;
$rowClass = $connId === $selectedConnectionId ? 'connection-row-selected' : '';
$vlanList = [];
if (!empty($conn['vlan_config'])) {
$vlanList = (array)json_decode((string)$conn['vlan_config'], true);
}
?>
<tr class="<?php echo $rowClass; ?>">
<td>
<strong><?php echo htmlspecialchars((string)($conn['endpoint_a_name'] ?? 'N/A')); ?></strong><br>
<small><?php echo htmlspecialchars((string)($conn['port_a_name'] ?? '-')); ?></small>
</td>
<td>
<strong><?php echo htmlspecialchars((string)($conn['endpoint_b_name'] ?? 'N/A')); ?></strong><br>
<small><?php echo htmlspecialchars((string)($conn['port_b_name'] ?? '-')); ?></small>
</td>
<td>
<small><?php echo !empty($vlanList) ? htmlspecialchars(implode(', ', $vlanList)) : '-'; ?></small>
</td>
<td>
<small><?php echo htmlspecialchars($comment); ?></small>
</td>
<td class="status-cell">
<?php if ($hasWarning): ?>
<span class="status-badge status-badge-warning" title="Unvollstaendige oder kritische Verbindung">Warnung</span>
<?php else: ?>
<span class="status-badge status-badge-ok" title="Verbindung vollstaendig">OK</span>
<?php endif; ?>
</td>
<td class="actions">
<a href="<?php echo htmlspecialchars($buildListUrl(['connection_id' => $connId])); ?>" class="button button-small">Details</a>
<a href="?module=connections&action=edit&id=<?php echo $connId; ?>" class="button button-small">Bearbeiten</a>
<a href="?module=connections&action=swap&id=<?php echo $connId; ?>" class="button button-small" onclick="return confirm('Von/Nach fuer diese Verbindung vertauschen?');">Von/Nach tauschen</a>
<button
type="button"
class="button button-small button-danger js-connection-delete"
data-connection-id="<?php echo $connId; ?>">
Loeschen
</button>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php else: ?>
<div class="empty-state">
<p>Keine Verbindungen gefunden.</p>
<p>
<a href="?module=connections&action=edit" class="button button-primary">
Erste Verbindung anlegen
</a>
</p>
</div>
<?php endif; ?>
</div>
<aside class="connections-sidebar">
<section class="sidebar-card">
<h3>Ausgewaehlte Verbindung</h3>
<?php if ($selectedConnection): ?>
<?php
$selectedConnId = (int)($selectedConnection['id'] ?? 0);
$selectedVlans = [];
if (!empty($selectedConnection['vlan_config'])) {
$selectedVlans = (array)json_decode((string)$selectedConnection['vlan_config'], true);
}
?>
<p><strong>ID:</strong> #<?php echo $selectedConnId; ?></p>
<p><strong>Von:</strong><br>
<?php echo htmlspecialchars((string)($selectedConnection['endpoint_a_name'] ?? 'N/A')); ?><br>
<small><?php echo htmlspecialchars((string)($selectedConnection['port_a_name'] ?? '-')); ?></small>
</p>
<p><strong>Nach:</strong><br>
<?php echo htmlspecialchars((string)($selectedConnection['endpoint_b_name'] ?? 'N/A')); ?><br>
<small><?php echo htmlspecialchars((string)($selectedConnection['port_b_name'] ?? '-')); ?></small>
</p>
<p><strong>VLANs:</strong> <?php echo !empty($selectedVlans) ? htmlspecialchars(implode(', ', $selectedVlans)) : '-'; ?></p>
<p><strong>Kommentar:</strong> <?php echo htmlspecialchars((string)($selectedConnection['comment'] ?? '-')); ?></p>
<div class="sidebar-actions">
<a href="?module=connections&action=edit&id=<?php echo $selectedConnId; ?>" class="button button-small">Bearbeiten</a>
<a href="?module=connections&action=swap&id=<?php echo $selectedConnId; ?>" class="button button-small" onclick="return confirm('Von/Nach fuer diese Verbindung vertauschen?');">Tauschen</a>
<button
type="button"
class="button button-small button-danger js-connection-delete"
data-connection-id="<?php echo $selectedConnId; ?>">
Loeschen
</button>
</div>
<?php else: ?>
<p><em>Keine Verbindung ausgewaehlt.</em></p>
<?php endif; ?>
</section>
<section class="sidebar-card">
<?php if ($selectedDevice): ?>
<h3>Ausgewaehltes Geraet</h3>
<p><strong><?php echo htmlspecialchars((string)$selectedDevice['name']); ?></strong></p>
<p>Typ: <?php echo htmlspecialchars((string)($selectedDevice['type_name'] ?? '-')); ?></p>
<p>Ports: <?php echo (int)$selectedDevice['port_count']; ?></p>
<p>Verbindungen: <?php echo (int)$selectedDevice['connection_count']; ?></p>
<p>
VLANs:
<?php if (!empty($selectedDeviceVlans)): ?>
<?php echo htmlspecialchars(implode(', ', $selectedDeviceVlans)); ?>
<?php else: ?>
-
<?php endif; ?>
</p>
<?php if (!empty($selectedDevicePorts)): ?>
<h4>Ports (max. 12)</h4>
<ul>
<?php foreach ($selectedDevicePorts as $port): ?>
<li><?php echo htmlspecialchars((string)$port['name']); ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<?php else: ?>
<h3>Ausgewaehltes Geraet</h3>
<p><em>Bitte ein Geraet im Filter auswaehlen.</em></p>
<?php endif; ?>
</section>
</aside>
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
document.querySelectorAll('.js-connection-delete').forEach((button) => {
button.addEventListener('click', () => {
const id = Number(button.dataset.connectionId || '0');
if (id <= 0) {
return;
}
if (!confirm('Diese Verbindung wirklich loeschen?')) {
return;
}
fetch('?module=connections&action=delete', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' },
body: 'id=' + encodeURIComponent(id)
})
.then((response) => response.json())
.then((data) => {
if (data && data.success) {
window.location.reload();
return;
}
alert((data && data.message) ? data.message : 'Loeschen fehlgeschlagen');
})
.catch(() => alert('Loeschen fehlgeschlagen'));
});
});
});
</script>