44 lines
1.0 KiB
PHP
44 lines
1.0 KiB
PHP
<?php
|
|
session_start();
|
|
require_once 'db/config.php';
|
|
require_once 'auth-check.php';
|
|
require_once 'auth-helpers.php';
|
|
|
|
if (!can($_SESSION['user_role_id'], 'location', 'delete')) {
|
|
header('Location: index.php?error=access_denied');
|
|
exit;
|
|
}
|
|
|
|
$location_id = $_GET['id'] ?? null;
|
|
|
|
if (!$location_id) {
|
|
header('Location: locations.php');
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
$pdo = db();
|
|
$pdo->beginTransaction();
|
|
|
|
// Set location_id to NULL for assets associated with this location
|
|
$stmt = $pdo->prepare('UPDATE assets SET location_id = NULL WHERE location_id = ?');
|
|
$stmt->execute([$location_id]);
|
|
|
|
// Delete the location
|
|
$stmt = $pdo->prepare('DELETE FROM locations WHERE id = ?');
|
|
$stmt->execute([$location_id]);
|
|
|
|
$pdo->commit();
|
|
|
|
header("Location: locations.php?success=location_deleted");
|
|
exit;
|
|
|
|
} catch (PDOException $e) {
|
|
if ($pdo->inTransaction()) {
|
|
$pdo->rollBack();
|
|
}
|
|
// In a real app, log this error.
|
|
header("Location: locations.php?error=db_error");
|
|
exit;
|
|
}
|