Korjaa laitetilan poisto: siivoa viittaukset ennen poistoa

- dbDeleteLaitetila nollaa devices.laitetila_id, devices.site_id
  ja ipam.site_id ennen laitetilan poistoa
- API: parempi virhekäsittely (\Throwable), logi, tyhjä ID tarkistus
- Tiedostojen poisto: @-suppression ja GLOB_BRACE hidden-tiedostoille

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-12 00:59:03 +02:00
parent 8e9fa76f9c
commit 663c37c7a7
2 changed files with 16 additions and 4 deletions

16
api.php
View File

@@ -4553,6 +4553,11 @@ switch ($action) {
try { try {
$input = json_decode(file_get_contents('php://input'), true); $input = json_decode(file_get_contents('php://input'), true);
$tilaId = $input['id'] ?? ''; $tilaId = $input['id'] ?? '';
if (empty($tilaId)) {
http_response_code(400);
echo json_encode(['error' => 'Laitetilan ID puuttuu']);
break;
}
$tila = dbLoadLaitetila($tilaId); $tila = dbLoadLaitetila($tilaId);
if (!$tila || $tila['company_id'] !== $companyId) { if (!$tila || $tila['company_id'] !== $companyId) {
http_response_code(404); http_response_code(404);
@@ -4562,13 +4567,16 @@ switch ($action) {
// Poista tiedostot levyltä // Poista tiedostot levyltä
$tilaDir = DATA_DIR . '/companies/' . $companyId . '/laitetilat/' . $tilaId; $tilaDir = DATA_DIR . '/companies/' . $companyId . '/laitetilat/' . $tilaId;
if (is_dir($tilaDir)) { if (is_dir($tilaDir)) {
$files = glob($tilaDir . '/*'); $items = glob($tilaDir . '/{,.}*', GLOB_BRACE);
foreach ($files as $f) { if (is_file($f)) unlink($f); } foreach ($items as $item) {
rmdir($tilaDir); if (is_file($item)) @unlink($item);
}
@rmdir($tilaDir);
} }
dbDeleteLaitetila($tilaId); dbDeleteLaitetila($tilaId);
dbAddLog($companyId, currentUser(), 'laitetila_delete', $tilaId, $tila['nimi'] ?? '', 'Poisti laitetilan');
echo json_encode(['success' => true]); echo json_encode(['success' => true]);
} catch (Exception $e) { } catch (\Throwable $e) {
http_response_code(500); http_response_code(500);
echo json_encode(['error' => 'Poisto epäonnistui: ' . $e->getMessage()]); echo json_encode(['error' => 'Poisto epäonnistui: ' . $e->getMessage()]);
} }

4
db.php
View File

@@ -1984,6 +1984,10 @@ function dbSaveLaitetila(string $companyId, array $tila): string {
function dbDeleteLaitetila(string $laitetilaId): ?array { function dbDeleteLaitetila(string $laitetilaId): ?array {
$tila = _dbFetchOne("SELECT id, company_id FROM laitetilat WHERE id = ?", [$laitetilaId]); $tila = _dbFetchOne("SELECT id, company_id FROM laitetilat WHERE id = ?", [$laitetilaId]);
if ($tila) { if ($tila) {
// Nollaa viittaukset laitteissa ja IPAM:ssa
_dbExecute("UPDATE devices SET laitetila_id = NULL WHERE laitetila_id = ?", [$laitetilaId]);
_dbExecute("UPDATE devices SET site_id = NULL WHERE site_id = ?", [$laitetilaId]);
_dbExecute("UPDATE ipam SET site_id = NULL WHERE site_id = ?", [$laitetilaId]);
_dbExecute("DELETE FROM laitetilat WHERE id = ?", [$laitetilaId]); // CASCADE poistaa tiedostot _dbExecute("DELETE FROM laitetilat WHERE id = ?", [$laitetilaId]); // CASCADE poistaa tiedostot
} }
return $tila; return $tila;