34849-vm/save_marketing_video.php
2026-07-29 18:37:28 +00:00

224 lines
8.3 KiB
PHP

<?php
include 'db/config.php';
session_start();
if (!isset($_SESSION['user_id'])) {
header('Location: login.php');
exit();
}
function marketing_store_video_photo(?array $file, ?string $currentPath = null): ?string
{
if ($file === null || !isset($file['error']) || $file['error'] === UPLOAD_ERR_NO_FILE) {
return $currentPath;
}
if ($file['error'] !== UPLOAD_ERR_OK) {
return $currentPath;
}
$allowedTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'];
$mimeType = $file['type'] ?? '';
if (!in_array($mimeType, $allowedTypes, true)) {
return $currentPath;
}
$target_dir = 'assets/uploads/marketing_images/';
if (!is_dir($target_dir)) {
mkdir($target_dir, 0777, true);
}
$file_extension = strtolower(pathinfo($file['name'] ?? '', PATHINFO_EXTENSION));
if ($file_extension === '') {
$mimeExtensions = [
'image/jpeg' => 'jpg',
'image/png' => 'png',
'image/gif' => 'gif',
'image/webp' => 'webp',
];
$file_extension = $mimeExtensions[$mimeType] ?? 'jpg';
}
$file_name = uniqid('video_', true) . '.' . $file_extension;
$target_file = $target_dir . $file_name;
if (!move_uploaded_file($file['tmp_name'], $target_file)) {
return $currentPath;
}
if (!empty($currentPath) && $currentPath !== $target_file && file_exists($currentPath)) {
@unlink($currentPath);
}
return $target_file;
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$db = db();
$action = $_POST['action'] ?? 'create';
if ($action == 'update') {
// Lógica para actualizar (Editora o Tú)
$id = $_POST['id'];
$estado = $_POST['estado'];
$link_video = '';
if (isset($_POST['link_video'])) {
$link_video = is_array($_POST['link_video']) ? implode(',', array_filter($_POST['link_video'])) : $_POST['link_video'];
}
$link_landing = '';
if (isset($_POST['link_landing'])) {
$link_landing = is_array($_POST['link_landing']) ? implode(',', array_filter($_POST['link_landing'])) : $_POST['link_landing'];
}
$link_flyer = '';
if (isset($_POST['link_flyer'])) {
$link_flyer = is_array($_POST['link_flyer']) ? implode(',', array_filter($_POST['link_flyer'])) : $_POST['link_flyer'];
}
$material = $_POST['material'] ?? '';
$fecha_entrega = !empty($_POST['fecha_entrega']) ? $_POST['fecha_entrega'] : null;
$orden = !empty($_POST['orden']) ? $_POST['orden'] : 0;
$angulo_video = '';
if (isset($_POST['angulo_video'])) {
$angulo_video = is_array($_POST['angulo_video']) ? implode(',', array_filter($_POST['angulo_video'])) : $_POST['angulo_video'];
}
$link_inspiracion_landing = '';
if (isset($_POST['link_inspiracion_landing'])) {
$link_inspiracion_landing = is_array($_POST['link_inspiracion_landing']) ? implode(',', array_filter($_POST['link_inspiracion_landing'])) : $_POST['link_inspiracion_landing'];
}
$link_inspiracion_video = '';
if (isset($_POST['link_inspiracion_video'])) {
$link_inspiracion_video = is_array($_POST['link_inspiracion_video']) ? implode(',', array_filter($_POST['link_inspiracion_video'])) : $_POST['link_inspiracion_video'];
}
$stmt_current = $db->prepare("SELECT foto_producto FROM marketing_videos WHERE id = ?");
$stmt_current->execute([$id]);
$current_photo = $stmt_current->fetchColumn();
$foto_path = marketing_store_video_photo($_FILES['foto_producto'] ?? null, is_string($current_photo) && $current_photo !== '' ? $current_photo : null);
try {
$stmt = $db->prepare("UPDATE marketing_videos SET
estado = ?,
foto_producto = ?,
link_video = ?,
link_landing = ?,
link_flyer = ?,
material = ?,
fecha_entrega = ?,
orden = ?,
angulo_video = ?,
link_inspiracion_landing = ?,
link_inspiracion_video = ?
WHERE id = ?");
$stmt->execute([
$estado,
$foto_path,
$link_video,
$link_landing,
$link_flyer,
$material,
$fecha_entrega,
$orden,
$angulo_video,
$link_inspiracion_landing,
$link_inspiracion_video,
$id
]);
$redirect = $_POST['redirect'] ?? 'marketing_produccion.php?success=updated';
header('Location: ' . $redirect);
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
} else {
// Lógica para crear (Tú)
$producto_id = !empty($_POST['producto_id']) ? $_POST['producto_id'] : null;
$instrucciones = $_POST['instrucciones'] ?? '';
$fecha_entrega = !empty($_POST['fecha_entrega']) ? $_POST['fecha_entrega'] : null;
$orden = !empty($_POST['orden']) ? $_POST['orden'] : 0;
$angulo_video = '';
if (isset($_POST['angulo_video'])) {
$angulo_video = is_array($_POST['angulo_video']) ? implode(',', array_filter($_POST['angulo_video'])) : $_POST['angulo_video'];
}
$link_inspiracion_landing = '';
if (isset($_POST['link_inspiracion_landing'])) {
$link_inspiracion_landing = is_array($_POST['link_inspiracion_landing']) ? implode(',', array_filter($_POST['link_inspiracion_landing'])) : $_POST['link_inspiracion_landing'];
}
$link_inspiracion_video = '';
if (isset($_POST['link_inspiracion_video'])) {
$link_inspiracion_video = is_array($_POST['link_inspiracion_video']) ? implode(',', array_filter($_POST['link_inspiracion_video'])) : $_POST['link_inspiracion_video'];
}
$link_flyer = '';
if (isset($_POST['link_flyer'])) {
$link_flyer = is_array($_POST['link_flyer']) ? implode(',', array_filter($_POST['link_flyer'])) : $_POST['link_flyer'];
}
$link_video = '';
if (isset($_POST['link_video'])) {
$link_video = is_array($_POST['link_video']) ? implode(',', array_filter($_POST['link_video'])) : $_POST['link_video'];
}
$link_landing = '';
if (isset($_POST['link_landing'])) {
$link_landing = is_array($_POST['link_landing']) ? implode(',', array_filter($_POST['link_landing'])) : $_POST['link_landing'];
}
$material = $_POST['material'] ?? '';
$foto_path = marketing_store_video_photo($_FILES['foto_producto'] ?? null, null);
try {
$stmt = $db->prepare("INSERT INTO marketing_videos (
producto_id,
foto_producto,
material,
instrucciones,
estado,
fecha_entrega,
orden,
angulo_video,
link_inspiracion_landing,
link_inspiracion_video,
link_flyer,
link_video,
link_landing
) VALUES (?, ?, ?, ?, 'PENDIENTE', ?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->execute([
$producto_id,
$foto_path,
$material,
$instrucciones,
$fecha_entrega,
$orden,
$angulo_video,
$link_inspiracion_landing,
$link_inspiracion_video,
$link_flyer,
$link_video,
$link_landing
]);
$video_id = $db->lastInsertId();
// Guardar costo inicial si se proporcionó
if (!empty($_POST['costo_producto'])) {
$costo_producto = $_POST['costo_producto'];
$stmt_costo = $db->prepare("INSERT INTO marketing_costos (video_id, costo_producto, inversion_total) VALUES (?, ?, ?)");
$stmt_costo->execute([$video_id, $costo_producto, $costo_producto]);
}
$redirect = $_POST['redirect'] ?? 'marketing_produccion.php?success=created';
header('Location: ' . $redirect);
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
}
}
?>