WIP: device löschen
This commit is contained in:
@@ -2,13 +2,14 @@
|
||||
/**
|
||||
* app/modules/devices/delete.php
|
||||
*
|
||||
* Löscht ein Gerät inkl. abhängiger Verbindungen.
|
||||
* Loescht ein Geraet. Bei Abhaengigkeiten ist force=1 erforderlich.
|
||||
*/
|
||||
|
||||
$deviceId = (int)($_GET['id'] ?? 0);
|
||||
$forceDelete = (int)($_GET['force'] ?? 0) === 1;
|
||||
|
||||
if ($deviceId <= 0) {
|
||||
$_SESSION['error'] = "Ungültige Geräte-ID";
|
||||
$_SESSION['error'] = "Ungueltige Geraete-ID";
|
||||
header('Location: ?module=devices&action=list');
|
||||
exit;
|
||||
}
|
||||
@@ -20,12 +21,61 @@ $device = $sql->single(
|
||||
);
|
||||
|
||||
if (!$device) {
|
||||
$_SESSION['error'] = "Gerät nicht gefunden";
|
||||
$_SESSION['error'] = "Geraet nicht gefunden";
|
||||
header('Location: ?module=devices&action=list');
|
||||
exit;
|
||||
}
|
||||
|
||||
// Verbindungen auf Ports dieses Geräts entfernen (keine FK auf device_ports in connections).
|
||||
$dependencies = $sql->single(
|
||||
"SELECT
|
||||
(
|
||||
SELECT COUNT(*)
|
||||
FROM device_ports dp
|
||||
WHERE dp.device_id = ?
|
||||
) AS port_count,
|
||||
(
|
||||
SELECT COUNT(*)
|
||||
FROM device_port_modules dpm
|
||||
JOIN device_ports dp2 ON dp2.id = dpm.device_port_id
|
||||
WHERE dp2.device_id = ?
|
||||
) AS module_count,
|
||||
(
|
||||
SELECT COUNT(*)
|
||||
FROM connections c
|
||||
WHERE (c.port_a_type = 'device' AND c.port_a_id IN (
|
||||
SELECT dp3.id FROM device_ports dp3 WHERE dp3.device_id = ?
|
||||
))
|
||||
OR (c.port_b_type = 'device' AND c.port_b_id IN (
|
||||
SELECT dp4.id FROM device_ports dp4 WHERE dp4.device_id = ?
|
||||
))
|
||||
) AS connection_count",
|
||||
"iiii",
|
||||
[$deviceId, $deviceId, $deviceId, $deviceId]
|
||||
);
|
||||
|
||||
$portCount = (int)($dependencies['port_count'] ?? 0);
|
||||
$moduleCount = (int)($dependencies['module_count'] ?? 0);
|
||||
$connectionCount = (int)($dependencies['connection_count'] ?? 0);
|
||||
$hasDependencies = $portCount > 0 || $moduleCount > 0 || $connectionCount > 0;
|
||||
|
||||
if ($hasDependencies && !$forceDelete) {
|
||||
$parts = [];
|
||||
if ($connectionCount > 0) {
|
||||
$parts[] = $connectionCount . ' Verbindungen';
|
||||
}
|
||||
if ($portCount > 0) {
|
||||
$parts[] = $portCount . ' Ports';
|
||||
}
|
||||
if ($moduleCount > 0) {
|
||||
$parts[] = $moduleCount . ' Port-Module';
|
||||
}
|
||||
|
||||
$_SESSION['error'] = "Geraet hat abhaengige Daten (" . implode(', ', $parts) . "). Loeschen bitte bestaetigen.";
|
||||
header('Location: ?module=devices&action=edit&id=' . urlencode((string)$deviceId));
|
||||
exit;
|
||||
}
|
||||
|
||||
// Connections referenzieren device_ports nur logisch, daher manuell entfernen.
|
||||
$sql->set(
|
||||
"DELETE FROM connections
|
||||
WHERE (port_a_type = 'device' AND port_a_id IN (SELECT id FROM device_ports WHERE device_id = ?))
|
||||
@@ -41,9 +91,9 @@ $deleted = $sql->set(
|
||||
);
|
||||
|
||||
if ($deleted > 0) {
|
||||
$_SESSION['success'] = "Gerät gelöscht: " . $device['name'];
|
||||
$_SESSION['success'] = "Geraet geloescht: " . $device['name'];
|
||||
} else {
|
||||
$_SESSION['error'] = "Gerät konnte nicht gelöscht werden";
|
||||
$_SESSION['error'] = "Geraet konnte nicht geloescht werden";
|
||||
}
|
||||
|
||||
header('Location: ?module=devices&action=list');
|
||||
|
||||
Reference in New Issue
Block a user