cms/admin/categories.php

384 lines
14 KiB
PHP

<?php
require_once __DIR__ . '/../includes/auth.php';
require_once __DIR__ . '/../includes/functions.php';
requireLogin();
$message = '';
$db = getDB();
// Obsługa akcji
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['action'])) {
switch ($_POST['action']) {
case 'save':
$slug = !empty($_POST['slug']) ? $_POST['slug'] : createSlug($_POST['name']);
if (isset($_POST['id']) && $_POST['id']) {
// Aktualizacja
$stmt = $db->prepare("
UPDATE categories
SET name = ?, slug = ?, description = ?, parent_id = ?
WHERE id = ?
");
$parentId = !empty($_POST['parent_id']) ? $_POST['parent_id'] : null;
if ($stmt->execute([$_POST['name'], $slug, $_POST['description'], $parentId, $_POST['id']])) {
$message = 'Kategoria została zaktualizowana';
logActivity('category_updated', 'category', $_POST['id']);
}
} else {
// Nowa kategoria
$stmt = $db->prepare("
INSERT INTO categories (name, slug, description, parent_id)
VALUES (?, ?, ?, ?)
");
$parentId = !empty($_POST['parent_id']) ? $_POST['parent_id'] : null;
if ($stmt->execute([$_POST['name'], $slug, $_POST['description'], $parentId])) {
$message = 'Kategoria została dodana';
logActivity('category_created', 'category', $db->lastInsertId());
}
}
break;
case 'delete':
$stmt = $db->prepare("DELETE FROM categories WHERE id = ?");
if ($stmt->execute([$_POST['id']])) {
$message = 'Kategoria została usunięta';
logActivity('category_deleted', 'category', $_POST['id']);
}
break;
}
}
}
// Pobierz wszystkie kategorie
$stmt = $db->query("
SELECT c.*, parent.name as parent_name,
(SELECT COUNT(*) FROM page_categories WHERE category_id = c.id) as page_count
FROM categories c
LEFT JOIN categories parent ON c.parent_id = parent.id
ORDER BY c.name
");
$categories = $stmt->fetchAll();
// Tryb edycji
$editCategory = null;
if (isset($_GET['edit'])) {
$stmt = $db->prepare("SELECT * FROM categories WHERE id = ?");
$stmt->execute([$_GET['edit']]);
$editCategory = $stmt->fetch();
}
?>
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Kategorie - Panel CMS</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
background: #f5f5f5;
}
.header {
background: #2c3e50;
color: white;
padding: 0 20px;
display: flex;
justify-content: space-between;
align-items: center;
height: 60px;
}
.sidebar {
position: fixed;
left: 0;
top: 60px;
width: 250px;
background: white;
height: calc(100vh - 60px);
border-right: 1px solid #e0e0e0;
overflow-y: auto;
}
.sidebar nav a {
display: block;
padding: 15px 20px;
color: #333;
text-decoration: none;
border-left: 3px solid transparent;
}
.sidebar nav a:hover, .sidebar nav a.active {
background: #f0f4ff;
border-left-color: #667eea;
color: #667eea;
}
.main-content {
margin-left: 250px;
padding: 30px;
}
.btn {
display: inline-block;
padding: 10px 20px;
background: #667eea;
color: white;
text-decoration: none;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 14px;
}
.btn:hover { background: #5568d3; }
.btn-sm { padding: 6px 12px; font-size: 13px; }
.btn-danger { background: #e74c3c; }
.btn-danger:hover { background: #c0392b; }
.card {
background: white;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.05);
margin-bottom: 20px;
}
.card-header {
padding: 20px;
border-bottom: 1px solid #e0e0e0;
}
.card-body { padding: 20px; }
.form-group {
margin-bottom: 20px;
}
.form-group label {
display: block;
margin-bottom: 8px;
font-weight: 500;
color: #333;
}
.form-group input, .form-group textarea, .form-group select {
width: 100%;
padding: 10px;
border: 2px solid #e1e8ed;
border-radius: 5px;
font-size: 14px;
}
.form-group textarea {
min-height: 100px;
font-family: inherit;
}
.form-group small {
display: block;
margin-top: 5px;
color: #666;
font-size: 13px;
}
table {
width: 100%;
border-collapse: collapse;
}
table th, table td {
padding: 12px;
text-align: left;
border-bottom: 1px solid #e0e0e0;
}
table th {
background: #f8f9fa;
font-weight: 600;
}
.message {
padding: 12px 20px;
background: #d4edda;
color: #155724;
border-radius: 5px;
margin-bottom: 20px;
}
.badge {
display: inline-block;
padding: 4px 10px;
background: #667eea;
color: white;
border-radius: 12px;
font-size: 12px;
font-weight: 500;
}
.category-tree {
padding-left: 20px;
}
</style>
</head>
<body>
<div class="header">
<h1>Panel CMS</h1>
<a href="index.php" class="btn btn-sm">← Powrót</a>
</div>
<div class="sidebar">
<nav>
<a href="index.php">📊 Dashboard</a>
<a href="pages.php">📄 Strony</a>
<a href="media.php">🖼️ Media</a>
<a href="menus.php">🔗 Menu</a>
<a href="categories.php" class="active">📁 Kategorie</a>
<?php if (hasRole('admin')): ?>
<a href="users.php">👥 Użytkownicy</a>
<a href="settings.php">⚙️ Ustawienia</a>
<?php endif; ?>
</nav>
</div>
<div class="main-content">
<h1 style="margin-bottom: 30px;">Zarządzanie kategoriami</h1>
<?php if ($message): ?>
<div class="message"><?php echo escape($message); ?></div>
<?php endif; ?>
<!-- Formularz dodawania/edycji -->
<div class="card">
<div class="card-header">
<h2><?php echo $editCategory ? 'Edytuj kategorię' : 'Dodaj nową kategorię'; ?></h2>
</div>
<div class="card-body">
<form method="POST">
<input type="hidden" name="action" value="save">
<?php if ($editCategory): ?>
<input type="hidden" name="id" value="<?php echo $editCategory['id']; ?>">
<?php endif; ?>
<div class="form-group">
<label>Nazwa kategorii *</label>
<input type="text" name="name" required
value="<?php echo escape($editCategory['name'] ?? ''); ?>">
</div>
<div class="form-group">
<label>Slug (adres URL)</label>
<input type="text" name="slug"
value="<?php echo escape($editCategory['slug'] ?? ''); ?>">
<small>Zostaw puste, aby wygenerować automatycznie z nazwy</small>
</div>
<div class="form-group">
<label>Opis</label>
<textarea name="description"><?php echo escape($editCategory['description'] ?? ''); ?></textarea>
<small>Opcjonalny opis kategorii</small>
</div>
<div class="form-group">
<label>Kategoria nadrzędna</label>
<select name="parent_id">
<option value="">-- Brak (kategoria główna) --</option>
<?php foreach ($categories as $cat): ?>
<?php if (!$editCategory || $cat['id'] != $editCategory['id']): ?>
<option value="<?php echo $cat['id']; ?>"
<?php echo ($editCategory['parent_id'] ?? '') == $cat['id'] ? 'selected' : ''; ?>>
<?php echo escape($cat['name']); ?>
</option>
<?php endif; ?>
<?php endforeach; ?>
</select>
<small>Wybierz kategorię nadrzędną, aby utworzyć podkategorię</small>
</div>
<button type="submit" class="btn">
<?php echo $editCategory ? 'Zaktualizuj kategorię' : 'Dodaj kategorię'; ?>
</button>
<?php if ($editCategory): ?>
<a href="categories.php" class="btn" style="background: #6c757d;">Anuluj</a>
<?php endif; ?>
</form>
</div>
</div>
<!-- Lista kategorii -->
<div class="card">
<div class="card-header">
<h2>Wszystkie kategorie (<?php echo count($categories); ?>)</h2>
</div>
<div class="card-body">
<?php if (empty($categories)): ?>
<p style="color: #999; text-align: center; padding: 20px;">
Brak kategorii. Dodaj pierwszą kategorię powyżej.
</p>
<?php else: ?>
<table>
<thead>
<tr>
<th>Nazwa</th>
<th>Slug</th>
<th>Kategoria nadrzędna</th>
<th>Liczba stron</th>
<th>Data utworzenia</th>
<th>Akcje</th>
</tr>
</thead>
<tbody>
<?php foreach ($categories as $category): ?>
<tr>
<td>
<?php if ($category['parent_id']): ?>
<span style="color: #999;">↳</span>
<?php endif; ?>
<strong><?php echo escape($category['name']); ?></strong>
</td>
<td>
<code style="font-size: 12px; background: #f5f5f5; padding: 2px 6px; border-radius: 3px;">
<?php echo escape($category['slug']); ?>
</code>
</td>
<td>
<?php if ($category['parent_name']): ?>
<?php echo escape($category['parent_name']); ?>
<?php else: ?>
<span style="color: #999;">--</span>
<?php endif; ?>
</td>
<td>
<?php if ($category['page_count'] > 0): ?>
<span class="badge"><?php echo $category['page_count']; ?></span>
<?php else: ?>
<span style="color: #999;">0</span>
<?php endif; ?>
</td>
<td><?php echo formatDate($category['created_at'], 'd.m.Y'); ?></td>
<td>
<a href="?edit=<?php echo $category['id']; ?>" class="btn btn-sm">Edytuj</a>
<form method="POST" style="display: inline;">
<input type="hidden" name="action" value="delete">
<input type="hidden" name="id" value="<?php echo $category['id']; ?>">
<button type="submit" class="btn btn-sm btn-danger"
onclick="return confirm('Czy na pewno chcesz usunąć tę kategorię?')">
Usuń
</button>
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
</div>
</div>
</div>
</body>
</html>