117 lines
3.8 KiB
PHP
117 lines
3.8 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', 'update')) {
|
|
header('Location: index.php?error=access_denied');
|
|
exit;
|
|
}
|
|
|
|
$error_message = '';
|
|
$location_id = $_GET['id'] ?? null;
|
|
|
|
if (!$location_id) {
|
|
header('Location: locations.php');
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare('SELECT id, name FROM locations WHERE id = ?');
|
|
$stmt->execute([$location_id]);
|
|
$location = $stmt->fetch();
|
|
|
|
if (!$location) {
|
|
header('Location: locations.php');
|
|
exit;
|
|
}
|
|
} catch (PDOException $e) {
|
|
$error_message = 'Database error: ' . $e->getMessage();
|
|
}
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$name = $_POST['name'] ?? '';
|
|
|
|
if (empty($name)) {
|
|
$error_message = 'Please fill in the location name.';
|
|
} else {
|
|
try {
|
|
$pdo = db();
|
|
|
|
$stmt = $pdo->prepare('SELECT id FROM locations WHERE name = ? AND id != ?');
|
|
$stmt->execute([$name, $location_id]);
|
|
if ($stmt->fetch()) {
|
|
$error_message = 'A location with this name already exists.';
|
|
} else {
|
|
$sql = "UPDATE locations SET name = ? WHERE id = ?";
|
|
$stmt = $pdo->prepare($sql);
|
|
$stmt->execute([$name, $location_id]);
|
|
|
|
header("Location: locations.php?success=location_updated");
|
|
exit;
|
|
}
|
|
} catch (PDOException $e) {
|
|
$error_message = 'Database error: ' . $e->getMessage();
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Edit Location - IC-Inventory</title>
|
|
<meta name="description" content="Edit an existing location.">
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
|
|
<link rel="preconnect" href="https://fonts.googleapis.com">
|
|
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
|
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
|
|
<script src="https://unpkg.com/feather-icons"></script>
|
|
</head>
|
|
<body>
|
|
|
|
<div class="wrapper">
|
|
<?php require_once 'templates/sidebar.php'; ?>
|
|
|
|
<main id="content">
|
|
<div class="header">
|
|
<h1>Edit Location</h1>
|
|
<div class="theme-switcher" id="theme-switcher">
|
|
<i data-feather="moon"></i>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="surface p-4">
|
|
<?php if ($error_message): ?>
|
|
<div class="alert alert-danger"><?php echo htmlspecialchars($error_message); ?></div>
|
|
<?php endif; ?>
|
|
|
|
<?php if ($location): ?>
|
|
<form action="edit-location.php?id=<?php echo $location['id']; ?>" method="post">
|
|
<div class="row">
|
|
<div class="col-md-6 mb-3">
|
|
<label for="name" class="form-label">Location Name*</label>
|
|
<input type="text" class="form-control" id="name" name="name" value="<?php echo htmlspecialchars($location['name']); ?>" required>
|
|
</div>
|
|
</div>
|
|
|
|
<button type="submit" class="btn btn-primary">Update Location</button>
|
|
<a href="locations.php" class="btn btn-secondary">Cancel</a>
|
|
</form>
|
|
<?php endif; ?>
|
|
</div>
|
|
</main>
|
|
</div>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
|
|
<script src="assets/js/main.js?v=<?php echo time(); ?>"></script>
|
|
<script>
|
|
feather.replace();
|
|
</script>
|
|
</body>
|
|
</html>
|