churchtube/admin/add_video.php

180 lines
7.9 KiB
PHP

<?php
require_once '../includes/db.php';
require_once '../includes/auth.php';
requireEditor();
$error = '';
$success = '';
function formatVideoUrl($url) {
if (strpos($url, 'drive.google.com') !== false) {
$url = preg_replace('/\/view(\?.*)?$/', '/preview', $url);
if (strpos($url, '/preview') === false && strpos($url, '/file/d/') !== false) {
$url = rtrim($url, '/') . '/preview';
}
}
return $url;
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$title = trim($_POST['title']);
$description = trim($_POST['description']);
$release_date = $_POST['release_date'];
$source_type = $_POST['source_type'];
$thumbnail_url = '';
// Handle Thumbnail Upload
if (isset($_FILES['thumbnail_file']) && $_FILES['thumbnail_file']['error'] === 0) {
$t_ext = strtolower(pathinfo($_FILES['thumbnail_file']['name'], PATHINFO_EXTENSION));
$allowed_img = ['jpg', 'jpeg', 'png', 'webp'];
if (in_array($t_ext, $allowed_img)) {
$t_filename = 'thumb_' . uniqid() . '.' . $t_ext;
if (move_uploaded_file($_FILES['thumbnail_file']['tmp_name'], '../uploads/' . $t_filename)) {
$thumbnail_url = 'uploads/' . $t_filename;
}
} else {
$error = "Invalid thumbnail format. Use JPG, PNG or WebP.";
}
}
if (!$error && $source_type === 'upload') {
if (isset($_FILES['video_file']) && $_FILES['video_file']['error'] === 0) {
$ext = strtolower(pathinfo($_FILES['video_file']['name'], PATHINFO_EXTENSION));
$allowed_vid = ['mp4', 'webm', 'ogg'];
if (in_array($ext, $allowed_vid)) {
$filename = uniqid() . '.' . $ext;
$upload_path = '../uploads/' . $filename;
if (move_uploaded_file($_FILES['video_file']['tmp_name'], $upload_path)) {
$video_url = 'uploads/' . $filename;
} else {
$error = "Failed to move uploaded file.";
}
} else {
$error = "Invalid video format. Use MP4, WebM or OGG.";
}
} else {
$error = "Please select a valid video file.";
}
} elseif (!$error) {
$video_url = formatVideoUrl(trim($_POST['external_url']));
if (empty($video_url)) {
$error = "Please provide an external video URL.";
}
}
if (!$error) {
$stmt = $pdo->prepare("INSERT INTO videos (title, description, release_date, source_type, video_url, thumbnail_url, uploader_id) VALUES (?, ?, ?, ?, ?, ?, ?)");
if ($stmt->execute([$title, $description, $release_date, $source_type, $video_url, $thumbnail_url, $_SESSION['user_id']])) {
$video_id = $pdo->lastInsertId();
if (!empty($_POST['tags'])) {
$tags = explode(',', $_POST['tags']);
foreach ($tags as $tag_name) {
$tag_name = trim($tag_name);
if (!$tag_name) continue;
$pdo->prepare("INSERT IGNORE INTO tags (name) VALUES (?)")->execute([$tag_name]);
$tag_stmt = $pdo->prepare("SELECT id FROM tags WHERE name = ?");
$tag_stmt->execute([$tag_name]);
$tag_id = $tag_stmt->fetchColumn();
$pdo->prepare("INSERT IGNORE INTO video_tags (video_id, tag_id) VALUES (?, ?)")->execute([$video_id, $tag_id]);
}
}
$success = "Video added successfully!";
} else {
$error = "Database error. Could not add video.";
}
}
}
// Reuse header by adjusting paths
ob_start();
require_once '../includes/header.php';
$header = ob_get_clean();
echo str_replace(['assets/', 'index.php', 'login.php', 'logout.php', 'admin/'], ['../assets/', '../index.php', '../login.php', '../logout.php', './'], $header);
?>
<div style="max-width: 800px; margin: 40px auto; padding: 0 24px;">
<div style="background: var(--bg-card); padding: 32px; border-radius: 16px; border: 1px solid var(--glass-border);">
<h2 style="margin-bottom: 24px;"><i class="fas fa-upload" style="margin-right: 12px;"></i> Add New Sermon</h2>
<?php if ($error): ?>
<div style="background: rgba(255,64,129,0.1); color: #ff4081; padding: 16px; border-radius: 8px; margin-bottom: 24px; border: 1px solid rgba(255,64,129,0.2);">
<?= $error ?>
</div>
<?php endif; ?>
<?php if ($success): ?>
<div style="background: rgba(76,175,80,0.1); color: #4caf50; padding: 16px; border-radius: 8px; margin-bottom: 24px; border: 1px solid rgba(76,175,80,0.2);">
<?= $success ?>
</div>
<?php endif; ?>
<form method="POST" enctype="multipart/form-data">
<div class="form-group">
<label class="form-label">Video Title</label>
<input type="text" name="title" class="form-control" placeholder="e.g. The Path to Salvation" required>
</div>
<div class="form-group">
<label class="form-label">Release Date</label>
<input type="date" name="release_date" class="form-control" value="<?= date('Y-m-d') ?>" required>
</div>
<div class="form-group">
<label class="form-label">Description</label>
<textarea name="description" class="form-control" style="min-height: 120px;" placeholder="What is this sermon about?"></textarea>
</div>
<div class="form-group">
<label class="form-label">Tags (comma separated)</label>
<input type="text" name="tags" class="form-control" placeholder="God, Love, Salvation">
</div>
<div class="form-group">
<label class="form-label">Video Source</label>
<div style="display: flex; gap: 20px; margin-top: 8px;">
<label style="cursor: pointer;">
<input type="radio" name="source_type" value="upload" checked onclick="toggleSource('upload')"> Upload File
</label>
<label style="cursor: pointer;">
<input type="radio" name="source_type" value="link" onclick="toggleSource('link')"> External Link (NAS/Cloud)
</label>
</div>
</div>
<div id="source_upload" class="form-group">
<label class="form-label">Select Video File</label>
<input type="file" name="video_file" class="form-control" accept="video/*">
<small style="color: var(--text-muted);">Max size: <?= ini_get('upload_max_filesize') ?></small>
</div>
<div id="source_link" class="form-group" style="display: none;">
<label class="form-label">External Video URL</label>
<input type="url" name="external_url" class="form-control" placeholder="https://nas.example.com/videos/sermon.mp4">
</div>
<div class="form-group">
<label class="form-label">Upload Thumbnail</label>
<input type="file" name="thumbnail_file" class="form-control" accept="image/*">
<small style="color: var(--text-muted);">Recommended: 1280x720 (16:9)</small>
</div>
<div style="margin-top: 32px; display: flex; gap: 16px;">
<button type="submit" class="btn btn-primary" style="flex-grow: 1;">Publish Sermon</button>
<a href="index.php" class="btn" style="background: var(--glass);">Cancel</a>
</div>
</form>
</div>
</div>
<script>
function toggleSource(type) {
document.getElementById('source_upload').style.display = (type === 'upload' ? 'block' : 'none');
document.getElementById('source_link').style.display = (type === 'link' ? 'block' : 'none');
}
</script>
<?php require_once '../includes/footer.php'; ?>