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

32 lines
821 B
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.');
}
// The FK is ON DELETE SET NULL, so a delete would silently orphan items:
// refuse while the category is still in use.
$stmt = $pdo->prepare("SELECT COUNT(*) FROM inv_equipment WHERE category_id = ?");
$stmt->execute([$id]);
$inUse = (int)$stmt->fetchColumn();
if ($inUse > 0) {
mnt_json_fail('Impossibile eliminare: la categoria è usata da ' . $inUse . ' attrezzature.');
}
$pdo->prepare("DELETE FROM inv_categories WHERE id = ?")->execute([$id]);
mnt_json_ok();
} catch (Throwable $e) {
mnt_json_fail('Errore: ' . $e->getMessage());
}