Files
pilates-platform/admin/lesson_create.php
Francesco Picone ca86649914 fix
2025-12-06 18:50:57 +01:00

361 lines
18 KiB
PHP

<?php
/**
* Crea Nuova Lezione
*
* Form per creare una nuova videolezione o lezione live
*/
require_once '../includes/config.php';
require_once '../includes/functions.php';
session_start();
check_session_timeout();
require_admin();
$error = '';
$success = false;
// 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'] ?? '');
$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 file locale
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)) {
// Imposta il percorso relativo per il database
$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 ($type === 'video' && $video_platform === 'local' && empty($video_url)) {
$error = 'Devi caricare un file video per le lezioni locali';
} elseif (!$error) {
$pdo = get_db_connection();
try {
$stmt = $pdo->prepare("
INSERT INTO lessons (
title, description, type, video_url, video_platform,
duration, live_platform, live_url, live_date,
level, category, price, is_demo, is_active,
created_by, created_at
) VALUES (
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW()
)
");
$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,
$_SESSION['user_id']
]);
set_flash_message('success', 'Lezione creata con successo!');
header('Location: lessons.php');
exit;
} catch (PDOException $e) {
$error = 'Errore durante la creazione della lezione';
}
}
}
?>
<!DOCTYPE html>
<html lang="it">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Nuova 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>
</ul>
</aside>
<main class="main-content">
<h2 class="section-title" style="text-align: left;">Crea Nuova 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'] ?? ''); ?>">
</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'] ?? ''); ?></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'] ?? 'video') === 'video' ? 'selected' : ''; ?>>
Videolezione Registrata
</option>
<option value="live" <?php echo ($_POST['type'] ?? '') === 'live' ? 'selected' : ''; ?>>
Lezione Live
</option>
</select>
</div>
<!-- Campi Video -->
<div id="video-fields" style="display: <?php echo ($_POST['type'] ?? 'video') === '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">File Locale (Carica dal PC)</option>
<option value="youtube">YouTube</option>
<option value="vimeo">Vimeo</option>
<option value="s3">AWS S3</option>
</select>
</div>
<!-- Upload file locale -->
<div id="local-upload" class="form-group">
<label for="video_file" class="form-label">Carica Video *</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. Il file verrà salvato automaticamente nella cartella demo/pay.</small>
</div>
<!-- URL esterno -->
<div id="external-url" class="form-group" style="display: none;">
<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'] ?? ''); ?>">
<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'] ?? ''); ?>">
</div>
</div>
<!-- Campi Live -->
<div id="live-fields" style="display: <?php echo ($_POST['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'] ?? ''); ?>">
</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'] ?? ''); ?>">
</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 echo htmlspecialchars($_POST['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'] ?? 'principiante') === 'principiante' ? 'selected' : ''; ?>>
Principiante
</option>
<option value="intermedio" <?php echo ($_POST['level'] ?? '') === 'intermedio' ? 'selected' : ''; ?>>
Intermedio
</option>
<option value="avanzato" <?php echo ($_POST['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'] ?? ''); ?>">
</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'] ?? '0'); ?>">
</div>
<div class="form-group">
<label class="form-label">
<input type="checkbox" name="is_demo" value="1"
<?php echo isset($_POST['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 !isset($_POST['is_active']) || $_POST['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">Crea Lezione</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');
const videoFileInput = document.getElementById('video_file');
if (platform === 'local') {
localUpload.style.display = 'block';
externalUrl.style.display = 'none';
videoFileInput.required = true;
} else {
localUpload.style.display = 'none';
externalUrl.style.display = 'block';
videoFileInput.required = false;
}
}
// Inizializza lo stato al caricamento
document.addEventListener('DOMContentLoaded', function() {
toggleVideoInput();
});
</script>
<script src="../assets/js/main.js"></script>
</body>
</html>