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:
16
api.php
16
api.php
@@ -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
4
db.php
@@ -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;
|
||||||
|
|||||||
Reference in New Issue
Block a user