0) { $stmt = $db->prepare("SELECT * FROM blog_posts WHERE id = :id LIMIT 1"); $stmt->execute([':id' => $postId]); $post = $stmt->fetch(); if (!$post) { header('Location: ' . BLOG_URL . '/admin/'); exit; } } // Load categories $categories = $db->query("SELECT * FROM blog_categories ORDER BY name")->fetchAll(); // Handle form submission if ($_SERVER['REQUEST_METHOD'] === 'POST') { $title = trim($_POST['title'] ?? ''); $slug = trim($_POST['slug'] ?? ''); $excerpt = trim($_POST['excerpt'] ?? ''); $content = $_POST['content'] ?? ''; $categoryId = intval($_POST['category_id'] ?? 0); $author = trim($_POST['author'] ?? 'TRIMAPE'); $status = ($_POST['status'] ?? 'draft') === 'published' ? 'published' : 'draft'; $coverImage = trim($_POST['cover_image'] ?? ''); if ($title === '') { $error = 'El título es obligatorio.'; } else { if ($slug === '') { $slug = slugify($title); } // Ensure unique slug $slugCheck = $db->prepare("SELECT id FROM blog_posts WHERE slug = :slug AND id != :id LIMIT 1"); $slugCheck->execute([':slug' => $slug, ':id' => $postId]); if ($slugCheck->fetch()) { $slug .= '-' . time(); } $publishedAt = $status === 'published' ? ($post['published_at'] ?? date('Y-m-d H:i:s')) : null; if ($post) { // Update $stmt = $db->prepare("UPDATE blog_posts SET title = :title, slug = :slug, excerpt = :excerpt, content = :content, category_id = :cat, author = :author, status = :status, cover_image = :cover, published_at = :pub, updated_at = NOW() WHERE id = :id"); $stmt->execute([ ':title' => $title, ':slug' => $slug, ':excerpt' => $excerpt, ':content' => $content, ':cat' => $categoryId ?: null, ':author' => $author, ':status' => $status, ':cover' => $coverImage, ':pub' => $publishedAt, ':id' => $postId ]); $_SESSION['flash'] = 'Artículo actualizado correctamente.'; } else { // Insert $stmt = $db->prepare("INSERT INTO blog_posts (title, slug, excerpt, content, category_id, author, status, cover_image, published_at) VALUES (:title, :slug, :excerpt, :content, :cat, :author, :status, :cover, :pub)"); $stmt->execute([ ':title' => $title, ':slug' => $slug, ':excerpt' => $excerpt, ':content' => $content, ':cat' => $categoryId ?: null, ':author' => $author, ':status' => $status, ':cover' => $coverImage, ':pub' => $publishedAt ]); $_SESSION['flash'] = 'Artículo creado correctamente.'; } header('Location: ' . BLOG_URL . '/admin/'); exit; } } // Prefill form $formTitle = $post['title'] ?? ($_POST['title'] ?? ''); $formSlug = $post['slug'] ?? ($_POST['slug'] ?? ''); $formExcerpt = $post['excerpt'] ?? ($_POST['excerpt'] ?? ''); $formContent = $post['content'] ?? ($_POST['content'] ?? ''); $formCatId = $post['category_id'] ?? ($_POST['category_id'] ?? 0); $formAuthor = $post['author'] ?? ($_POST['author'] ?? 'TRIMAPE'); $formStatus = $post['status'] ?? ($_POST['status'] ?? 'draft'); $formCover = $post['cover_image'] ?? ($_POST['cover_image'] ?? ''); ?> <?= $post ? 'Editar' : 'Nuevo' ?> artículo - Blog Admin
Dashboard
/blog/
Publicación
Categoría
Autor
Imagen de portada
Sube una imagen o pega una URL Preview Preview