cms/admin/pages.php

352 lines
12 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 'delete':
if (isset($_POST['id'])) {
deletePage($_POST['id']);
logActivity('page_deleted', 'page', $_POST['id']);
$message = 'Strona została usunięta';
}
break;
case 'save':
$data = [
'id' => $_POST['id'] ?? null,
'title' => $_POST['title'],
'slug' => $_POST['slug'],
'content' => $_POST['content'],
'meta_description' => $_POST['meta_description'],
'status' => $_POST['status'],
'template' => $_POST['template'],
'author_id' => $_SESSION['user_id']
];
if (savePage($data)) {
$action = $data['id'] ? 'page_updated' : 'page_created';
logActivity($action, 'page', $data['id']);
$message = 'Strona została zapisana';
} else {
$message = 'Błąd podczas zapisywania strony';
}
break;
}
}
}
// Pobierz wszystkie strony
$stmt = $db->query("
SELECT p.*, u.username as author_name
FROM pages p
LEFT JOIN users u ON p.author_id = u.id
ORDER BY p.created_at DESC
");
$pages = $stmt->fetchAll();
// Tryb edycji
$editPage = null;
if (isset($_GET['edit'])) {
$stmt = $db->prepare("SELECT * FROM pages WHERE id = ?");
$stmt->execute([$_GET['edit']]);
$editPage = $stmt->fetch();
}
?>
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Zarządzanie stronami - 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;
display: flex;
justify-content: space-between;
align-items: center;
}
.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: 200px;
font-family: inherit;
}
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;
}
.status-badge {
display: inline-block;
padding: 4px 10px;
border-radius: 12px;
font-size: 12px;
font-weight: 500;
}
.status-published {
background: #d4edda;
color: #155724;
}
.status-draft {
background: #fff3cd;
color: #856404;
}
.message {
padding: 12px 20px;
background: #d4edda;
color: #155724;
border-radius: 5px;
margin-bottom: 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" class="active">📄 Strony</a>
<a href="media.php">🖼️ Media</a>
<a href="menus.php">🔗 Menu</a>
</nav>
</div>
<div class="main-content">
<h1 style="margin-bottom: 30px;">Zarządzanie stronami</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 $editPage ? 'Edytuj stronę' : 'Dodaj nową stronę'; ?></h2>
</div>
<div class="card-body">
<form method="POST">
<input type="hidden" name="action" value="save">
<?php if ($editPage): ?>
<input type="hidden" name="id" value="<?php echo $editPage['id']; ?>">
<?php endif; ?>
<div class="form-group">
<label>Tytuł strony</label>
<input type="text" name="title" required
value="<?php echo escape($editPage['title'] ?? ''); ?>">
</div>
<div class="form-group">
<label>Slug (adres URL)</label>
<input type="text" name="slug"
value="<?php echo escape($editPage['slug'] ?? ''); ?>">
<small style="color: #666;">Zostaw puste, aby wygenerować automatycznie</small>
</div>
<div class="form-group">
<label>Treść</label>
<textarea name="content"><?php echo escape($editPage['content'] ?? ''); ?></textarea>
</div>
<div class="form-group">
<label>Meta opis</label>
<input type="text" name="meta_description"
value="<?php echo escape($editPage['meta_description'] ?? ''); ?>">
</div>
<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 20px;">
<div class="form-group">
<label>Status</label>
<select name="status">
<option value="draft" <?php echo ($editPage['status'] ?? '') === 'draft' ? 'selected' : ''; ?>>Szkic</option>
<option value="published" <?php echo ($editPage['status'] ?? '') === 'published' ? 'selected' : ''; ?>>Opublikowana</option>
</select>
</div>
<div class="form-group">
<label>Szablon</label>
<select name="template">
<option value="default">Domyślny</option>
<option value="fullwidth">Pełna szerokość</option>
</select>
</div>
</div>
<button type="submit" class="btn">
<?php echo $editPage ? 'Zaktualizuj stronę' : 'Dodaj stronę'; ?>
</button>
<?php if ($editPage): ?>
<a href="pages.php" class="btn" style="background: #6c757d;">Anuluj</a>
<?php endif; ?>
</form>
</div>
</div>
<!-- Lista stron -->
<div class="card">
<div class="card-header">
<h2>Wszystkie strony</h2>
</div>
<div class="card-body">
<table>
<thead>
<tr>
<th>Tytuł</th>
<th>Slug</th>
<th>Autor</th>
<th>Status</th>
<th>Data utworzenia</th>
<th>Akcje</th>
</tr>
</thead>
<tbody>
<?php foreach ($pages as $page): ?>
<tr>
<td><?php echo escape($page['title']); ?></td>
<td><?php echo escape($page['slug']); ?></td>
<td><?php echo escape($page['author_name']); ?></td>
<td>
<span class="status-badge status-<?php echo $page['status']; ?>">
<?php echo $page['status'] === 'published' ? 'Opublikowana' : 'Szkic'; ?>
</span>
</td>
<td><?php echo formatDate($page['created_at'], 'd.m.Y'); ?></td>
<td>
<a href="?edit=<?php echo $page['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 $page['id']; ?>">
<button type="submit" class="btn btn-sm btn-danger"
onclick="return confirm('Czy na pewno chcesz usunąć tę stronę?')">
Usuń
</button>
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>