prepare('SELECT * FROM articles WHERE id = ?'); $stmt->execute([$id]); $article = $stmt->fetch(); if (!$article) { flash('error', 'Artikel nicht gefunden.'); redirect('/admin/articles.php'); } } $categories = $pdo->query('SELECT id, name FROM categories ORDER BY sort_order, name')->fetchAll(); if ($_SERVER['REQUEST_METHOD'] === 'POST') { csrf_verify(); $title = trim($_POST['title'] ?? ''); $slug = trim($_POST['slug'] ?? ''); $excerpt = trim($_POST['excerpt'] ?? ''); $body = $_POST['body'] ?? ''; $categoryId = !empty($_POST['category_id']) ? (int) $_POST['category_id'] : null; $status = in_array($_POST['status'] ?? '', ['draft', 'published']) ? $_POST['status'] : 'draft'; $publishedAt = trim($_POST['published_at'] ?? ''); // Validierung $errors = []; if ($title === '') { $errors[] = 'Titel ist erforderlich.'; } if ($slug === '') { $slug = slugify($title); } else { $slug = slugify($slug); } if ($slug === '') { $errors[] = 'Slug konnte nicht generiert werden.'; } // Slug-Eindeutigkeit prüfen $slugCheck = $pdo->prepare('SELECT id FROM articles WHERE slug = ? AND id != ?'); $slugCheck->execute([$slug, $id ?? 0]); if ($slugCheck->fetch()) { $errors[] = 'Dieser Slug wird bereits verwendet.'; } // Body sanitieren $body = sanitize_html($body); // Cover-Bild $coverImage = $article['cover_image'] ?? null; if (!empty($_FILES['cover_image']['name'])) { $uploaded = handle_upload($_FILES['cover_image']); if ($uploaded === false) { $errors[] = 'Bild-Upload fehlgeschlagen. Erlaubt: JPG, PNG, GIF, WebP (max. 5 MB).'; } else { $coverImage = $uploaded; } } if (isset($_POST['remove_cover']) && $_POST['remove_cover'] === '1') { $coverImage = null; } if ($status === 'published' && empty($publishedAt)) { $publishedAt = date('Y-m-d H:i:s'); } if (empty($errors)) { if ($id) { $stmt = $pdo->prepare( "UPDATE articles SET title=?, slug=?, excerpt=?, body=?, cover_image=?, category_id=?, status=?, published_at=?, updated_at=datetime('now') WHERE id=?" ); $stmt->execute([$title, $slug, $excerpt, $body, $coverImage, $categoryId, $status, $publishedAt ?: null, $id]); flash('success', 'Artikel aktualisiert.'); } else { $stmt = $pdo->prepare( 'INSERT INTO articles (title, slug, excerpt, body, cover_image, category_id, status, published_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)' ); $stmt->execute([$title, $slug, $excerpt, $body, $coverImage, $categoryId, $status, $publishedAt ?: null]); $id = $pdo->lastInsertId(); flash('success', 'Artikel erstellt.'); } redirect('/admin/article-edit.php?id=' . $id); } else { foreach ($errors as $err) { flash('error', $err); } } } $pageTitle = $article ? 'Artikel bearbeiten' : 'Neuer Artikel'; $currentPage = 'articles'; $extraHead = ' '; ob_start(); ?>
'; include __DIR__ . '/templates/layout.php';