446 lines
23 KiB
PHP
446 lines
23 KiB
PHP
<?php
|
|
/**
|
|
* Modifica Lezione
|
|
*
|
|
* Form per modificare una videolezione o lezione live esistente
|
|
*/
|
|
|
|
require_once '../includes/config.php';
|
|
require_once '../includes/functions.php';
|
|
|
|
session_start();
|
|
check_session_timeout();
|
|
require_admin();
|
|
|
|
$error = '';
|
|
$success = false;
|
|
$lesson = null;
|
|
|
|
// Verifica che sia stato passato un ID valido
|
|
$lesson_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
|
|
|
|
if ($lesson_id <= 0) {
|
|
set_flash_message('error', 'ID lezione non valido');
|
|
header('Location: lessons.php');
|
|
exit;
|
|
}
|
|
|
|
$pdo = get_db_connection();
|
|
|
|
// Carica i dati della lezione
|
|
try {
|
|
$stmt = $pdo->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)) {
|
|
// Se il video non è MP4, prova a convertirlo automaticamente
|
|
if ($file_extension !== 'mp4') {
|
|
$converted_path = $upload_dir . pathinfo($file_name, PATHINFO_FILENAME) . '.mp4';
|
|
|
|
// Verifica se FFmpeg è disponibile
|
|
$ffmpeg_check = [];
|
|
exec('ffmpeg -version 2>&1', $ffmpeg_check, $ffmpeg_return);
|
|
|
|
if ($ffmpeg_return === 0) {
|
|
// Converti il video in MP4
|
|
$convert_cmd = sprintf(
|
|
'ffmpeg -i %s -c:v libx264 -preset medium -crf 23 -c:a aac -b:a 128k -movflags +faststart -y %s 2>&1',
|
|
escapeshellarg($upload_path),
|
|
escapeshellarg($converted_path)
|
|
);
|
|
|
|
exec($convert_cmd, $convert_output, $convert_return);
|
|
|
|
if ($convert_return === 0 && file_exists($converted_path)) {
|
|
// Conversione riuscita, elimina il file originale e usa quello convertito
|
|
unlink($upload_path);
|
|
$file_name = pathinfo($file_name, PATHINFO_FILENAME) . '.mp4';
|
|
$upload_path = $converted_path;
|
|
$video_converted = true;
|
|
}
|
|
// Se la conversione fallisce, usa comunque il file originale
|
|
}
|
|
}
|
|
|
|
// 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;
|
|
|
|
// Se è già MP4, considera già convertito
|
|
if ($file_extension === 'mp4') {
|
|
$video_converted = true;
|
|
}
|
|
|
|
// 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 = ?,
|
|
video_converted = ?,
|
|
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,
|
|
$video_converted ?? $lesson['video_converted'] ?? false,
|
|
$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';
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="it">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Modifica Lezione - Admin</title>
|
|
<link rel="stylesheet" href="../assets/css/style.css">
|
|
</head>
|
|
<body>
|
|
<header class="header">
|
|
<div class="container">
|
|
<div class="header-content">
|
|
<?php if (file_exists('../uploads/images/logo.png')): ?>
|
|
<div class="logo">
|
|
<img src="../uploads/images/logo.png" alt="Pilates Studio" class="logo-image">
|
|
<span style="margin-left: 10px; color: var(--primary-color); font-weight: 600;">Admin</span>
|
|
</div>
|
|
<?php else: ?>
|
|
<h1 class="logo">Pilates Studio - Admin</h1>
|
|
<?php endif; ?>
|
|
<nav class="nav">
|
|
<a href="lessons.php" class="btn btn-outline">← Torna alle Lezioni</a>
|
|
<a href="../includes/logout.php" class="btn btn-secondary">Logout</a>
|
|
</nav>
|
|
</div>
|
|
</div>
|
|
</header>
|
|
|
|
<div class="container">
|
|
<div class="dashboard">
|
|
<aside class="sidebar">
|
|
<ul class="sidebar-menu">
|
|
<li><a href="dashboard.php">📊 Dashboard</a></li>
|
|
<li><a href="lessons.php" class="active">🎥 Gestione Lezioni</a></li>
|
|
<li><a href="users.php">👥 Gestione Utenti</a></li>
|
|
<li><a href="purchases.php">💰 Acquisti</a></li>
|
|
<li><a href="profile.php">👤 Profilo</a></li>
|
|
</ul>
|
|
</aside>
|
|
|
|
<main class="main-content">
|
|
<h2 class="section-title" style="text-align: left;">Modifica Lezione</h2>
|
|
|
|
<?php if ($error): ?>
|
|
<div class="alert alert-error"><?php echo htmlspecialchars($error); ?></div>
|
|
<?php endif; ?>
|
|
|
|
<div class="card">
|
|
<form method="POST" action="" enctype="multipart/form-data">
|
|
<!-- Informazioni Base -->
|
|
<h3 style="margin-bottom: 1rem; color: var(--primary-color);">Informazioni Base</h3>
|
|
|
|
<div class="form-group">
|
|
<label for="title" class="form-label">Titolo *</label>
|
|
<input type="text" id="title" name="title" class="form-control" required
|
|
value="<?php echo htmlspecialchars($_POST['title'] ?? $lesson['title']); ?>">
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label for="description" class="form-label">Descrizione *</label>
|
|
<textarea id="description" name="description" class="form-control" required
|
|
rows="4"><?php echo htmlspecialchars($_POST['description'] ?? $lesson['description']); ?></textarea>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label for="type" class="form-label">Tipo Lezione *</label>
|
|
<select id="type" name="type" class="form-control" required onchange="toggleTypeFields()">
|
|
<option value="video" <?php echo ($_POST['type'] ?? $lesson['type']) === 'video' ? 'selected' : ''; ?>>
|
|
Videolezione Registrata
|
|
</option>
|
|
<option value="live" <?php echo ($_POST['type'] ?? $lesson['type']) === 'live' ? 'selected' : ''; ?>>
|
|
Lezione Live
|
|
</option>
|
|
</select>
|
|
</div>
|
|
|
|
<!-- Campi Video -->
|
|
<div id="video-fields" style="display: <?php echo ($_POST['type'] ?? $lesson['type']) === 'video' ? 'block' : 'none'; ?>;">
|
|
<h3 style="margin: 2rem 0 1rem; color: var(--primary-color);">Dettagli Video</h3>
|
|
|
|
<div class="form-group">
|
|
<label for="video_platform" class="form-label">Piattaforma Video</label>
|
|
<select id="video_platform" name="video_platform" class="form-control" onchange="toggleVideoInput()">
|
|
<option value="local" <?php echo ($_POST['video_platform'] ?? $lesson['video_platform']) === 'local' ? 'selected' : ''; ?>>File Locale (Carica dal PC)</option>
|
|
<option value="youtube" <?php echo ($_POST['video_platform'] ?? $lesson['video_platform']) === 'youtube' ? 'selected' : ''; ?>>YouTube</option>
|
|
<option value="vimeo" <?php echo ($_POST['video_platform'] ?? $lesson['video_platform']) === 'vimeo' ? 'selected' : ''; ?>>Vimeo</option>
|
|
<option value="s3" <?php echo ($_POST['video_platform'] ?? $lesson['video_platform']) === 's3' ? 'selected' : ''; ?>>AWS S3</option>
|
|
</select>
|
|
</div>
|
|
|
|
<!-- Upload file locale -->
|
|
<div id="local-upload" class="form-group" style="display: <?php echo ($_POST['video_platform'] ?? $lesson['video_platform']) === 'local' ? 'block' : 'none'; ?>;">
|
|
<?php if (!empty($lesson['video_url'])): ?>
|
|
<div style="margin-bottom: 0.5rem; padding: 0.5rem; background: #e8f4f8; border-radius: 4px;">
|
|
<strong>File attuale:</strong> <?php echo htmlspecialchars(basename($lesson['video_url'])); ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
<label for="video_file" class="form-label">Carica Nuovo Video (opzionale)</label>
|
|
<input type="file" id="video_file" name="video_file" class="form-control"
|
|
accept="video/mp4,video/webm,video/ogg,video/quicktime">
|
|
<small class="text-muted">Formati supportati: MP4, WebM, OGG, MOV. Lascia vuoto per mantenere il file attuale.</small>
|
|
</div>
|
|
|
|
<!-- URL esterno -->
|
|
<div id="external-url" class="form-group" style="display: <?php echo ($_POST['video_platform'] ?? $lesson['video_platform']) === 'local' ? 'none' : 'block'; ?>;">
|
|
<label for="video_url" class="form-label">URL Video</label>
|
|
<input type="text" id="video_url" name="video_url" class="form-control"
|
|
placeholder="es: https://youtube.com/watch?v=..."
|
|
value="<?php echo htmlspecialchars($_POST['video_url'] ?? $lesson['video_url'] ?? ''); ?>">
|
|
<small class="text-muted">Inserisci l'URL del video sulla piattaforma esterna</small>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label for="duration" class="form-label">Durata (minuti)</label>
|
|
<input type="number" id="duration" name="duration" class="form-control"
|
|
min="1" value="<?php echo htmlspecialchars($_POST['duration'] ?? $lesson['duration'] ?? ''); ?>">
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Campi Live -->
|
|
<div id="live-fields" style="display: <?php echo ($_POST['type'] ?? $lesson['type']) === 'live' ? 'block' : 'none'; ?>;">
|
|
<h3 style="margin: 2rem 0 1rem; color: var(--primary-color);">Dettagli Lezione Live</h3>
|
|
|
|
<div class="form-group">
|
|
<label for="live_platform" class="form-label">Piattaforma Live</label>
|
|
<input type="text" id="live_platform" name="live_platform" class="form-control"
|
|
placeholder="es: Zoom, Google Meet, Teams..."
|
|
value="<?php echo htmlspecialchars($_POST['live_platform'] ?? $lesson['live_platform'] ?? ''); ?>">
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label for="live_url" class="form-label">Link Lezione Live</label>
|
|
<input type="text" id="live_url" name="live_url" class="form-control"
|
|
placeholder="es: https://zoom.us/j/..."
|
|
value="<?php echo htmlspecialchars($_POST['live_url'] ?? $lesson['live_url'] ?? ''); ?>">
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label for="live_date" class="form-label">Data e Ora Lezione *</label>
|
|
<input type="datetime-local" id="live_date" name="live_date" class="form-control"
|
|
value="<?php
|
|
$live_date = $_POST['live_date'] ?? $lesson['live_date'] ?? '';
|
|
// Converti il formato della data per datetime-local (YYYY-MM-DDTHH:MM)
|
|
if ($live_date && strpos($live_date, ' ') !== false) {
|
|
$live_date = str_replace(' ', 'T', substr($live_date, 0, 16));
|
|
}
|
|
echo htmlspecialchars($live_date);
|
|
?>">
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Classificazione -->
|
|
<h3 style="margin: 2rem 0 1rem; color: var(--primary-color);">Classificazione</h3>
|
|
|
|
<div class="form-group">
|
|
<label for="level" class="form-label">Livello *</label>
|
|
<select id="level" name="level" class="form-control" required>
|
|
<option value="principiante" <?php echo ($_POST['level'] ?? $lesson['level']) === 'principiante' ? 'selected' : ''; ?>>
|
|
Principiante
|
|
</option>
|
|
<option value="intermedio" <?php echo ($_POST['level'] ?? $lesson['level']) === 'intermedio' ? 'selected' : ''; ?>>
|
|
Intermedio
|
|
</option>
|
|
<option value="avanzato" <?php echo ($_POST['level'] ?? $lesson['level']) === 'avanzato' ? 'selected' : ''; ?>>
|
|
Avanzato
|
|
</option>
|
|
</select>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label for="category" class="form-label">Categoria</label>
|
|
<input type="text" id="category" name="category" class="form-control"
|
|
placeholder="es: Mat Work, Reformer, Stretching..."
|
|
value="<?php echo htmlspecialchars($_POST['category'] ?? $lesson['category'] ?? ''); ?>">
|
|
</div>
|
|
|
|
<!-- Prezzo e Disponibilità -->
|
|
<h3 style="margin: 2rem 0 1rem; color: var(--primary-color);">Prezzo e Disponibilità</h3>
|
|
|
|
<div class="form-group">
|
|
<label for="price" class="form-label">Prezzo (€) *</label>
|
|
<input type="number" id="price" name="price" class="form-control"
|
|
min="0" step="0.01" required
|
|
value="<?php echo htmlspecialchars($_POST['price'] ?? $lesson['price']); ?>">
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label class="form-label">
|
|
<input type="checkbox" name="is_demo" value="1"
|
|
<?php echo ($_POST['is_demo'] ?? $lesson['is_demo']) ? 'checked' : ''; ?>>
|
|
Lezione Demo (gratuita per tutti)
|
|
</label>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label class="form-label">
|
|
<input type="checkbox" name="is_active" value="1"
|
|
<?php echo ($_POST['is_active'] ?? $lesson['is_active']) ? 'checked' : ''; ?>>
|
|
Lezione attiva (visibile agli utenti)
|
|
</label>
|
|
</div>
|
|
|
|
<div class="d-flex gap-1 mt-3">
|
|
<button type="submit" class="btn btn-primary">Salva Modifiche</button>
|
|
<a href="lessons.php" class="btn btn-outline">Annulla</a>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</main>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
// Mostra/nascondi campi in base al tipo di lezione
|
|
function toggleTypeFields() {
|
|
const type = document.getElementById('type').value;
|
|
const videoFields = document.getElementById('video-fields');
|
|
const liveFields = document.getElementById('live-fields');
|
|
|
|
if (type === 'video') {
|
|
videoFields.style.display = 'block';
|
|
liveFields.style.display = 'none';
|
|
} else {
|
|
videoFields.style.display = 'none';
|
|
liveFields.style.display = 'block';
|
|
}
|
|
}
|
|
|
|
// Mostra/nascondi campi upload/URL in base alla piattaforma
|
|
function toggleVideoInput() {
|
|
const platform = document.getElementById('video_platform').value;
|
|
const localUpload = document.getElementById('local-upload');
|
|
const externalUrl = document.getElementById('external-url');
|
|
|
|
if (platform === 'local') {
|
|
localUpload.style.display = 'block';
|
|
externalUrl.style.display = 'none';
|
|
} else {
|
|
localUpload.style.display = 'none';
|
|
externalUrl.style.display = 'block';
|
|
}
|
|
}
|
|
|
|
// Inizializza lo stato al caricamento
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
toggleVideoInput();
|
|
});
|
|
</script>
|
|
<script src="../assets/js/main.js"></script>
|
|
</body>
|
|
</html>
|