Files
zibo-dashboard/public/userarea/manutenzioni/ajax/delete_maintenance.php
T
2026-08-12 21:29:55 +03:00

54 lines
1.7 KiB
PHP

<?php
require_once __DIR__ . '/auth_check.php';
mnt_require_permission('maintenance.manage');
try {
$pdo = mnt_pdo();
$id = (int)($_POST['id'] ?? 0);
if ($id <= 0) {
mnt_json_fail('ID non valido.');
}
$stmt = $pdo->prepare("SELECT id, equipment_id, title FROM maint_maintenances WHERE id = ?");
$stmt->execute([$id]);
$maintenance = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$maintenance) {
mnt_json_fail('Manutenzione non trovata.');
}
// Files of the maintenance and of its interventions (both cascade in DB)
$stmt = $pdo->prepare("SELECT stored_name FROM maint_maintenance_files WHERE maintenance_id = ?");
$stmt->execute([$id]);
$storedNames = $stmt->fetchAll(PDO::FETCH_COLUMN);
$stmt = $pdo->prepare("
SELECT f.stored_name
FROM maint_intervention_files f
INNER JOIN maint_interventions i ON i.id = f.intervention_id
WHERE i.maintenance_id = ?
");
$stmt->execute([$id]);
$storedNames = array_merge($storedNames, $stmt->fetchAll(PDO::FETCH_COLUMN));
$stmt = $pdo->prepare("SELECT signature_path FROM maint_interventions WHERE maintenance_id = ? AND signature_path IS NOT NULL");
$stmt->execute([$id]);
$storedNames = array_merge($storedNames, $stmt->fetchAll(PDO::FETCH_COLUMN));
$pdo->prepare("DELETE FROM maint_maintenances WHERE id = ?")->execute([$id]);
foreach (array_filter($storedNames) as $storedName) {
mnt_delete_stored_file($storedName);
}
mnt_log($pdo, 'maintenance_deleted', (int)$maintenance['equipment_id'], null, null, $currentUserId, null, $maintenance['title']);
mnt_json_ok();
} catch (Throwable $e) {
mnt_json_fail('Errore: ' . $e->getMessage());
}