134 lines
5.5 KiB
PHP
134 lines
5.5 KiB
PHP
<?php
|
|
require_once __DIR__ . '/../includes/init.php';
|
|
require_role('admin');
|
|
|
|
// Handle form submission for adding a new key
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_key'])) {
|
|
$name = trim($_POST['name']);
|
|
$error = '';
|
|
|
|
if (empty($name)) {
|
|
$error = 'Nazwa klucza nie może być pusta.';
|
|
}
|
|
|
|
if (empty($error)) {
|
|
try {
|
|
$stmt = db()->prepare("INSERT INTO attribute_keys (name) VALUES (?)");
|
|
$stmt->execute([$name]);
|
|
$_SESSION['success_message'] = 'Klucz atrybutu został pomyślnie dodany.';
|
|
} catch (PDOException $e) {
|
|
if ($e->errorInfo[1] == 1062) { // Duplicate entry
|
|
$error = 'Klucz atrybutu o tej nazwie już istnieje.';
|
|
} else {
|
|
$error = 'Wystąpił błąd podczas dodawania klucza atrybutu: ' . $e->getMessage();
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!empty($error)) {
|
|
$_SESSION['error_message'] = $error;
|
|
}
|
|
header("Location: attribute_keys.php");
|
|
exit();
|
|
}
|
|
|
|
// Handle deletion
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete_key'])) {
|
|
$key_id = $_POST['id'];
|
|
if ($key_id) {
|
|
try {
|
|
// It is better to check for usage first to provide a user-friendly error message.
|
|
$stmt_check = db()->prepare("SELECT COUNT(*) FROM product_attributes WHERE attribute_key_id = ?");
|
|
$stmt_check->execute([$key_id]);
|
|
|
|
if ($stmt_check->fetchColumn() > 0) {
|
|
$_SESSION['error_message'] = 'Nie można usunąć klucza, ponieważ jest on używany przez produkty. Zmień atrybuty produktów przed usunięciem klucza.';
|
|
} else {
|
|
// No product attributes use this key, safe to delete.
|
|
$stmt = db()->prepare("DELETE FROM attribute_keys WHERE id = ?");
|
|
$stmt->execute([$key_id]);
|
|
$_SESSION['success_message'] = 'Klucz atrybutu został pomyślnie usunięty.';
|
|
}
|
|
} catch (PDOException $e) {
|
|
$_SESSION['error_message'] = 'Wystąpił błąd podczas usuwania klucza atrybutu: ' . $e->getMessage();
|
|
}
|
|
} else {
|
|
$_SESSION['error_message'] = 'Nieprawidłowe żądanie usunięcia.';
|
|
}
|
|
header("Location: attribute_keys.php");
|
|
exit();
|
|
}
|
|
|
|
|
|
// Fetch all attribute keys
|
|
$keys_stmt = db()->query("SELECT * FROM attribute_keys ORDER BY name");
|
|
$attribute_keys = $keys_stmt->fetchAll();
|
|
|
|
$page_title = "Klucze atrybutów";
|
|
|
|
?>
|
|
<?php require_once __DIR__ . '/../includes/html_head.php'; ?>
|
|
<body>
|
|
<?php include 'menu.php'; ?>
|
|
|
|
<main class="container my-5">
|
|
<div class="d-flex justify-content-between align-items-center mb-4">
|
|
<h1 class="h2"><?php echo $page_title; ?></h1>
|
|
</div>
|
|
|
|
<?php
|
|
if (isset($_SESSION['error_message'])) {
|
|
echo '<div class="alert alert-danger"><p class="mb-0">' . htmlspecialchars($_SESSION['error_message']) . '</p></div>';
|
|
unset($_SESSION['error_message']);
|
|
}
|
|
if (isset($_SESSION['success_message'])) {
|
|
echo '<div class="alert alert-success"><p class="mb-0">' . htmlspecialchars($_SESSION['success_message']) . '</p></div>';
|
|
unset($_SESSION['success_message']);
|
|
}
|
|
?>
|
|
|
|
<div class="card">
|
|
<div class="card-body">
|
|
<div class="row">
|
|
<div class="col-md-8">
|
|
<h4>Istniejące klucze</h4>
|
|
<table class="table table-striped table-hover">
|
|
<thead>
|
|
<tr>
|
|
<th>ID</th>
|
|
<th>Nazwa</th>
|
|
<th style="width: 100px;">Akcje</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($attribute_keys as $key): ?>
|
|
<tr>
|
|
<td><?php echo htmlspecialchars($key['id']); ?></td>
|
|
<td><?php echo htmlspecialchars($key['name']); ?></td>
|
|
<td>
|
|
<form method="POST" action="" onsubmit="return confirm('Czy na pewno chcesz usunąć ten klucz?');">
|
|
<input type="hidden" name="id" value="<?php echo $key['id']; ?>">
|
|
<button type="submit" name="delete_key" class="btn btn-sm btn-danger">Usuń</button>
|
|
</form>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<div class="col-md-4">
|
|
<h4>Dodaj nowy klucz</h4>
|
|
<form method="POST" action="">
|
|
<div class="mb-3">
|
|
<label for="name" class="form-label">Nazwa klucza</label>
|
|
<input type="text" name="name" id="name" class="form-control" required>
|
|
</div>
|
|
<button type="submit" name="add_key" class="btn btn-primary">Dodaj klucz</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</main>
|
|
|
|
<?php include __DIR__ . '/../includes/footer.php'; ?>
|