blog+contacto php listo

This commit is contained in:
2026-09-20 13:28:57 -03:00
parent cd67fca3dc
commit cfb97b486e
24 changed files with 8970 additions and 165 deletions
+294
View File
@@ -0,0 +1,294 @@
<?php
require_once '../config/db.php';
require_once 'auth.php';
verificarAuth();
$admin = getAdminActual();
$post = null;
$modo_edicion = false;
// Si viene ID, es edición
if (isset($_GET['id'])) {
$stmt = $pdo->prepare("SELECT * FROM blog_posts WHERE id = ?");
$stmt->execute([$_GET['id']]);
$post = $stmt->fetch();
if (!$post) {
die('Artículo no encontrado');
}
$modo_edicion = true;
}
// Procesar formulario
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$titulo = trim($_POST['titulo']);
$slug = strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $titulo)));
$extracto = trim($_POST['extracto']);
$contenido = $_POST['contenido'];
$categoria = $_POST['categoria'];
$tags = trim($_POST['tags']);
$destacado = isset($_POST['destacado']) ? 1 : 0;
$estado = $_POST['estado'];
$tiempo_lectura = (int)$_POST['tiempo_lectura'];
$autor_id = $admin['id'];
// Subida de imagen
$imagen = $post['imagen'] ?? '';
if (isset($_FILES['imagen']) && $_FILES['imagen']['error'] === UPLOAD_ERR_OK) {
$allowed = ['image/jpeg', 'image/png', 'image/webp'];
$max_size = 5 * 1024 * 1024; // 5MB
if (!in_array($_FILES['imagen']['type'], $allowed)) {
$error = 'Solo se permiten imágenes JPG, PNG o WebP';
} elseif ($_FILES['imagen']['size'] > $max_size) {
$error = 'La imagen no puede superar los 5MB';
} else {
$extension = pathinfo($_FILES['imagen']['name'], PATHINFO_EXTENSION);
$nombre_imagen = $slug . '-' . time() . '.' . $extension;
$ruta_destino = '../assets/blog/' . $nombre_imagen;
if (!is_dir('../assets/blog')) {
mkdir('../assets/blog', 0755, true);
}
if (move_uploaded_file($_FILES['imagen']['tmp_name'], $ruta_destino)) {
$imagen = 'assets/blog/' . $nombre_imagen;
} else {
$error = 'Error al subir la imagen';
}
}
}
if (!isset($error)) {
if ($modo_edicion) {
$stmt = $pdo->prepare("
UPDATE blog_posts SET
titulo = ?, slug = ?, extracto = ?, contenido = ?,
categoria = ?, tags = ?, imagen = ?, destacado = ?,
estado = ?, tiempo_lectura = ?
WHERE id = ?
");
$stmt->execute([
$titulo, $slug, $extracto, $contenido,
$categoria, $tags, $imagen, $destacado,
$estado, $tiempo_lectura, $post['id']
]);
} else {
$stmt = $pdo->prepare("
INSERT INTO blog_posts
(titulo, slug, extracto, contenido, categoria, tags, imagen, autor_id, destacado, estado, tiempo_lectura)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
");
$stmt->execute([
$titulo, $slug, $extracto, $contenido,
$categoria, $tags, $imagen, $autor_id, $destacado,
$estado, $tiempo_lectura
]);
}
header('Location: dashboard.php');
exit;
}
}
?>
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?= $modo_edicion ? 'Editar' : 'Nuevo' ?> Artículo - Amor y Roma</title>
<script src="https://cdn.tiny.mce.com/1/tinymce/6/tinymce.min.js" referrerpolicy="origin"></script>
<link rel="stylesheet" href="../style/main.css">
<style>
.admin-header {
background: white;
padding: 1.5rem 2rem;
border-bottom: 1px solid rgba(0, 63, 82, 0.12);
display: flex;
justify-content: space-between;
align-items: center;
}
.editor-container {
max-width: 1000px;
margin: 0 auto;
padding: 2rem;
}
.form-group {
margin-bottom: 1.5rem;
}
label {
display: block;
margin-bottom: 0.5rem;
color: #003f52;
font-weight: 600;
}
input[type="text"],
input[type="number"],
select,
textarea {
width: 100%;
padding: 0.75rem;
border: 1.5px solid rgba(0, 63, 82, 0.2);
border-radius: 8px;
font-size: 1rem;
box-sizing: border-box;
font-family: 'Raleway', sans-serif;
}
textarea {
resize: vertical;
min-height: 100px;
}
.form-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 1rem;
}
.checkbox-group {
display: flex;
align-items: center;
gap: 0.5rem;
}
.checkbox-group input[type="checkbox"] {
width: 20px;
height: 20px;
cursor: pointer;
}
.btn-guardar {
background: #ba124d;
color: white;
padding: 0.85rem 2rem;
border: none;
border-radius: 24px;
font-weight: 600;
cursor: pointer;
font-size: 1rem;
}
.btn-guardar:hover {
background: #9c0f40;
}
.btn-cancelar {
background: #eaf0ee;
color: #003f52;
padding: 0.85rem 2rem;
border: none;
border-radius: 24px;
font-weight: 600;
cursor: pointer;
font-size: 1rem;
text-decoration: none;
display: inline-block;
}
.imagen-preview {
margin-top: 1rem;
max-width: 300px;
}
.imagen-preview img {
width: 100%;
border-radius: 8px;
}
.error {
background: #fee;
color: #c00;
padding: 1rem;
border-radius: 8px;
margin-bottom: 1rem;
}
</style>
<script>
tinymce.init({
selector: '#contenido',
height: 500,
plugins: 'anchor autolink charmap codesample emoticons image link lists media searchreplace table visualblocks wordcount',
toolbar: 'undo redo | blocks fontfamily fontsize | bold italic underline strikethrough | link image media table | align lineheight | numlist bullist indent outdent | emoticons charmap | removeformat',
content_style: 'body { font-family: Raleway, sans-serif; font-size: 16px; }',
language: 'es'
});
</script>
</head>
<body>
<div class="admin-header">
<h2 style="margin: 0; font-family: 'Literata', serif;"><?= $modo_edicion ? 'Editar' : 'Nuevo' ?> Artículo</h2>
<a href="dashboard.php" class="btn-cancelar">← Volver al dashboard</a>
</div>
<div class="editor-container">
<?php if (isset($error)): ?>
<div class="error"><?= htmlspecialchars($error) ?></div>
<?php endif; ?>
<form method="POST" enctype="multipart/form-data">
<div class="form-group">
<label for="titulo">Título del artículo *</label>
<input type="text" id="titulo" name="titulo" required value="<?= htmlspecialchars($post['titulo'] ?? '') ?>">
</div>
<div class="form-group">
<label for="extracto">Extracto (resumen corto) *</label>
<textarea id="extracto" name="extracto" required><?= htmlspecialchars($post['extracto'] ?? '') ?></textarea>
</div>
<div class="form-group">
<label for="contenido">Contenido completo *</label>
<textarea id="contenido" name="contenido" required><?= htmlspecialchars($post['contenido'] ?? '') ?></textarea>
</div>
<div class="form-row">
<div class="form-group">
<label for="categoria">Categoría *</label>
<select id="categoria" name="categoria" required>
<option value="">Seleccionar...</option>
<option value="branding" <?= ($post['categoria'] ?? '') === 'branding' ? 'selected' : '' ?>>Branding</option>
<option value="web" <?= ($post['categoria'] ?? '') === 'web' ? 'selected' : '' ?>>Web</option>
<option value="redes" <?= ($post['categoria'] ?? '') === 'redes' ? 'selected' : '' ?>>Redes</option>
<option value="automatizaciones" <?= ($post['categoria'] ?? '') === 'automatizaciones' ? 'selected' : '' ?>>Automatizaciones</option>
<option value="paid" <?= ($post['categoria'] ?? '') === 'paid' ? 'selected' : '' ?>>Paid Media</option>
<option value="estrategia" <?= ($post['categoria'] ?? '') === 'estrategia' ? 'selected' : '' ?>>Estrategia</option>
<option value="casos" <?= ($post['categoria'] ?? '') === 'casos' ? 'selected' : '' ?>>Casos</option>
</select>
</div>
<div class="form-group">
<label for="estado">Estado *</label>
<select id="estado" name="estado" required>
<option value="borrador" <?= ($post['estado'] ?? 'borrador') === 'borrador' ? 'selected' : '' ?>>Borrador</option>
<option value="publicado" <?= ($post['estado'] ?? '') === 'publicado' ? 'selected' : '' ?>>Publicado</option>
</select>
</div>
</div>
<div class="form-row">
<div class="form-group">
<label for="tags">Tags (separados por coma)</label>
<input type="text" id="tags" name="tags" value="<?= htmlspecialchars($post['tags'] ?? '') ?>" placeholder="ej: branding, identidad, logo">
</div>
<div class="form-group">
<label for="tiempo_lectura">Tiempo de lectura (minutos)</label>
<input type="number" id="tiempo_lectura" name="tiempo_lectura" min="1" value="<?= $post['tiempo_lectura'] ?? 5 ?>">
</div>
</div>
<div class="form-group">
<label for="imagen">Imagen destacada *</label>
<input type="file" id="imagen" name="imagen" accept="image/*" <?= !$modo_edicion ? 'required' : '' ?>>
<?php if ($post && $post['imagen']): ?>
<div class="imagen-preview">
<img src="../<?= htmlspecialchars($post['imagen']) ?>" alt="Imagen actual">
<p style="font-size: 0.85rem; color: #666; margin-top: 0.5rem;">Imagen actual: <?= basename($post['imagen']) ?></p>
</div>
<?php endif; ?>
</div>
<div class="form-group">
<div class="checkbox-group">
<input type="checkbox" id="destacado" name="destacado" value="1" <?= ($post['destacado'] ?? 0) ? 'checked' : '' ?>>
<label for="destacado" style="margin: 0;">Marcar como destacado (aparece en la sección "Más leídos")</label>
</div>
</div>
<div style="display: flex; gap: 1rem; margin-top: 2rem;">
<button type="submit" class="btn-guardar"> Guardar Artículo</button>
<a href="dashboard.php" class="btn-cancelar">Cancelar</a>
</div>
</form>
</div>
</body>
</html>