146 lines
5.7 KiB
PHP
146 lines
5.7 KiB
PHP
<?php
|
|
session_start();
|
|
if (!isset($_SESSION['user_id'])) {
|
|
header('Location: login.php');
|
|
exit;
|
|
}
|
|
|
|
require_once 'db/config.php';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
header('Location: info_producto.php');
|
|
exit;
|
|
}
|
|
|
|
$pdo = db();
|
|
|
|
// Diferenciar entre el formulario del banner y el de las tarjetas
|
|
if (isset($_POST['banner_text'])) {
|
|
// --- LÓGICA PARA GUARDAR EL BANNER ---
|
|
|
|
$banner_text = $_POST['banner_text'] ?? '';
|
|
|
|
try {
|
|
// Guardar el texto del banner
|
|
$stmt = $pdo->prepare("INSERT INTO configuracion (clave, valor) VALUES ('banner_text', ?) ON DUPLICATE KEY UPDATE valor = ?");
|
|
$stmt->execute([$banner_text, $banner_text]);
|
|
|
|
// Manejar la subida de la imagen del banner
|
|
if (isset($_FILES['banner_image']) && $_FILES['banner_image']['error'] === UPLOAD_ERR_OK) {
|
|
$file = $_FILES['banner_image'];
|
|
|
|
$allowedTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'];
|
|
if (!in_array($file['type'], $allowedTypes)) {
|
|
$_SESSION['error_message'] = 'Tipo de archivo no permitido para el banner. Sube JPG, PNG, GIF o WEBP.';
|
|
header('Location: info_producto.php');
|
|
exit;
|
|
}
|
|
|
|
$uploadDir = 'assets/uploads/info_images/';
|
|
if (!is_dir($uploadDir)) {
|
|
mkdir($uploadDir, 0775, true);
|
|
}
|
|
|
|
// Obtener y borrar imagen anterior si existe
|
|
$stmt_old = $pdo->prepare("SELECT valor FROM configuracion WHERE clave = 'banner_image'");
|
|
$stmt_old->execute();
|
|
$old_image_name = $stmt_old->fetchColumn();
|
|
if ($old_image_name && file_exists($uploadDir . $old_image_name)) {
|
|
unlink($uploadDir . $old_image_name);
|
|
}
|
|
|
|
$extension = pathinfo($file['name'], PATHINFO_EXTENSION);
|
|
$fileName = 'banner_' . uniqid() . '.' . $extension;
|
|
$uploadPath = $uploadDir . $fileName;
|
|
|
|
if (move_uploaded_file($file['tmp_name'], $uploadPath)) {
|
|
// Guardar el nuevo nombre de la imagen en la BD
|
|
$stmt_img = $pdo->prepare("INSERT INTO configuracion (clave, valor) VALUES ('banner_image', ?) ON DUPLICATE KEY UPDATE valor = ?");
|
|
$stmt_img->execute([$fileName, $fileName]);
|
|
} else {
|
|
$_SESSION['error_message'] = 'No se pudo guardar la nueva imagen del banner.';
|
|
header('Location: info_producto.php');
|
|
exit;
|
|
}
|
|
}
|
|
|
|
$_SESSION['success_message'] = '¡La configuración del banner se ha guardado con éxito!';
|
|
|
|
} catch (PDOException $e) {
|
|
error_log("Database error: " . $e->getMessage());
|
|
$_SESSION['error_message'] = 'Error al guardar la configuración del banner. Por favor, contacta a soporte.';
|
|
}
|
|
|
|
} else {
|
|
// --- LÓGICA EXISTENTE PARA GUARDAR TARJETAS INFORMATIVAS ---
|
|
|
|
$id = filter_input(INPUT_POST, 'id', FILTER_VALIDATE_INT);
|
|
$producto_id = !empty($_POST['producto_id']) ? filter_input(INPUT_POST, 'producto_id', FILTER_VALIDATE_INT) : null;
|
|
$texto_informativo = $_POST['texto_informativo'] ?? '';
|
|
$column_id = filter_input(INPUT_POST, 'column_id', FILTER_VALIDATE_INT);
|
|
$current_imagen = $_POST['current_imagen'] ?? '';
|
|
|
|
$is_new_entry = empty($id);
|
|
$has_new_image = isset($_FILES['imagen']) && $_FILES['imagen']['error'] === UPLOAD_ERR_OK;
|
|
|
|
if (!$column_id || trim($texto_informativo) === '') {
|
|
$_SESSION['error_message'] = 'Por favor, completa la descripción y selecciona una columna.';
|
|
header('Location: info_producto.php');
|
|
exit;
|
|
}
|
|
|
|
|
|
|
|
$imagen_name = $current_imagen;
|
|
|
|
if (isset($_FILES['imagen']) && $_FILES['imagen']['error'] === UPLOAD_ERR_OK) {
|
|
$file = $_FILES['imagen'];
|
|
$allowedTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'];
|
|
if (!in_array($file['type'], $allowedTypes)) {
|
|
$_SESSION['error_message'] = 'Tipo de archivo no permitido. Sube JPG, PNG, GIF o WEBP.';
|
|
header('Location: info_producto.php');
|
|
exit;
|
|
}
|
|
|
|
$uploadDir = 'assets/uploads/info_images/';
|
|
if (!is_dir($uploadDir)) {
|
|
mkdir($uploadDir, 0775, true);
|
|
}
|
|
|
|
if (!empty($current_imagen) && file_exists($current_imagen)) {
|
|
unlink($current_imagen);
|
|
}
|
|
|
|
$extension = pathinfo($file['name'], PATHINFO_EXTENSION);
|
|
$fileName = 'info_' . uniqid() . '.' . $extension;
|
|
$uploadPath = $uploadDir . $fileName;
|
|
|
|
if (move_uploaded_file($file['tmp_name'], $uploadPath)) {
|
|
$imagen_name = $uploadPath;
|
|
} else {
|
|
$_SESSION['error_message'] = 'No se pudo guardar la nueva imagen.';
|
|
header('Location: info_producto.php');
|
|
exit;
|
|
}
|
|
}
|
|
|
|
try {
|
|
if ($id) {
|
|
$stmt = $pdo->prepare('UPDATE info_productos SET producto_id = ?, texto_informativo = ?, column_id = ?, imagen_url = ? WHERE id = ?');
|
|
$stmt->execute([$producto_id, $texto_informativo, $column_id, $imagen_name, $id]);
|
|
$_SESSION['success_message'] = '¡Tarjeta actualizada con éxito!';
|
|
} else {
|
|
$stmt = $pdo->prepare('INSERT INTO info_productos (producto_id, texto_informativo, column_id, imagen_url) VALUES (?, ?, ?, ?)');
|
|
$stmt->execute([$producto_id, $texto_informativo, $column_id, $imagen_name]);
|
|
$_SESSION['success_message'] = '¡Tarjeta creada con éxito!';
|
|
}
|
|
} catch (PDOException $e) {
|
|
error_log("Database error: " . $e->getMessage());
|
|
$_SESSION['error_message'] = 'Error al guardar en la base de datos. Por favor, contacta a soporte.';
|
|
}
|
|
}
|
|
|
|
header('Location: info_producto.php');
|
|
exit;
|
|
?>
|