prepare("SELECT * FROM lessons WHERE id = ?"); $stmt->execute([$lesson_id]); $lesson = $stmt->fetch(); if (!$lesson) { set_flash_message('error', 'Lezione non trovata'); header('Location: lessons.php'); exit; } } catch (PDOException $e) { set_flash_message('error', 'Errore durante il caricamento della lezione'); header('Location: lessons.php'); exit; } // Processa il form if ($_SERVER['REQUEST_METHOD'] === 'POST') { $title = sanitize_input($_POST['title'] ?? ''); $description = sanitize_input($_POST['description'] ?? ''); $type = $_POST['type'] ?? 'video'; $level = $_POST['level'] ?? 'principiante'; $category = sanitize_input($_POST['category'] ?? ''); $price = floatval($_POST['price'] ?? 0); $duration = !empty($_POST['duration']) ? intval($_POST['duration']) : null; $video_url = sanitize_input($_POST['video_url'] ?? '') ?: $lesson['video_url']; // Mantieni il vecchio se non specificato $video_platform = $_POST['video_platform'] ?? 'local'; $live_url = sanitize_input($_POST['live_url'] ?? ''); $live_platform = sanitize_input($_POST['live_platform'] ?? ''); $live_date = !empty($_POST['live_date']) ? $_POST['live_date'] : null; $is_demo = isset($_POST['is_demo']) ? 1 : 0; $is_active = isset($_POST['is_active']) ? 1 : 0; // Gestione upload nuovo file locale (opzionale) if ($type === 'video' && $video_platform === 'local' && isset($_FILES['video_file']) && $_FILES['video_file']['error'] === UPLOAD_ERR_OK) { $upload_dir = $is_demo ? '../uploads/lessons/demo/' : '../uploads/lessons/pay/'; // Crea le cartelle se non esistono if (!is_dir($upload_dir)) { mkdir($upload_dir, 0755, true); } // Crea nome file sicuro $file_extension = strtolower(pathinfo($_FILES['video_file']['name'], PATHINFO_EXTENSION)); $allowed_extensions = ['mp4', 'webm', 'ogg', 'mov']; if (!in_array($file_extension, $allowed_extensions)) { $error = 'Formato video non supportato. Usa: mp4, webm, ogg, mov'; } else { $file_name = uniqid('video_') . '_' . time() . '.' . $file_extension; $upload_path = $upload_dir . $file_name; if (move_uploaded_file($_FILES['video_file']['tmp_name'], $upload_path)) { // Elimina il vecchio file se esisteva $old_video_path = ltrim($lesson['video_url'], '/'); if (!empty($lesson['video_url']) && file_exists('../' . $old_video_path)) { @unlink('../' . $old_video_path); } // Imposta il nuovo percorso (senza slash iniziale) $video_url = 'uploads/lessons/' . ($is_demo ? 'demo' : 'pay') . '/' . $file_name; // Estrai durata automaticamente se non specificata if (empty($duration)) { $duration = get_video_duration($upload_path); } } else { $error = 'Errore durante il caricamento del file'; } } } // Validazione if (empty($title)) { $error = 'Il titolo è obbligatorio'; } elseif (empty($description)) { $error = 'La descrizione è obbligatoria'; } elseif ($type === 'live' && empty($live_date)) { $error = 'Per le lezioni live, la data è obbligatoria'; } elseif (!$error) { try { $stmt = $pdo->prepare(" UPDATE lessons SET title = ?, description = ?, type = ?, video_url = ?, video_platform = ?, duration = ?, live_platform = ?, live_url = ?, live_date = ?, level = ?, category = ?, price = ?, is_demo = ?, is_active = ?, updated_at = NOW() WHERE id = ? "); $stmt->execute([ $title, $description, $type, $type === 'video' ? $video_url : null, $type === 'video' ? $video_platform : null, $duration, $type === 'live' ? $live_platform : null, $type === 'live' ? $live_url : null, $live_date, $level, $category, $price, $is_demo, $is_active, $lesson_id ]); set_flash_message('success', 'Lezione aggiornata con successo!'); header('Location: lessons.php'); exit; } catch (PDOException $e) { $error = 'Errore durante l\'aggiornamento della lezione'; } } } ?>
Admin