192 lines
8.7 KiB
PHP
192 lines
8.7 KiB
PHP
<?php
|
|
require_once '../includes/db.php';
|
|
require_once '../includes/auth.php';
|
|
requireEditor();
|
|
|
|
$id = (int)($_GET['id'] ?? 0);
|
|
$stmt = $pdo->prepare("SELECT * FROM videos WHERE id = ?");
|
|
$stmt->execute([$id]);
|
|
$video = $stmt->fetch();
|
|
|
|
if (!$video) {
|
|
die("Video not found.");
|
|
}
|
|
|
|
$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'];
|
|
$video_url = $video['video_url'];
|
|
$thumbnail_url = $video['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)) {
|
|
if ($video['thumbnail_url'] && strpos($video['thumbnail_url'], 'uploads/') === 0) {
|
|
@unlink('../' . $video['thumbnail_url']);
|
|
}
|
|
$thumbnail_url = 'uploads/' . $t_filename;
|
|
}
|
|
} else {
|
|
$error = "Invalid thumbnail format.";
|
|
}
|
|
}
|
|
|
|
if (!$error && $source_type === 'upload' && 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)) {
|
|
if ($video['source_type'] === 'upload') @unlink('../' . $video['video_url']);
|
|
$video_url = 'uploads/' . $filename;
|
|
}
|
|
} else {
|
|
$error = "Invalid video format.";
|
|
}
|
|
} elseif (!$error && $source_type === 'link') {
|
|
$video_url = formatVideoUrl(trim($_POST['external_url']));
|
|
}
|
|
|
|
$stmt = $pdo->prepare("UPDATE videos SET title = ?, description = ?, release_date = ?, source_type = ?, video_url = ?, thumbnail_url = ? WHERE id = ?");
|
|
if ($stmt->execute([$title, $description, $release_date, $source_type, $video_url, $thumbnail_url, $id])) {
|
|
// Update tags
|
|
$pdo->prepare("DELETE FROM video_tags WHERE video_id = ?")->execute([$id]);
|
|
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([$id, $tag_id]);
|
|
}
|
|
}
|
|
cleanupTags();
|
|
$success = "Video updated successfully!";
|
|
// Refresh video data
|
|
$stmt = $pdo->prepare("SELECT * FROM videos WHERE id = ?");
|
|
$stmt->execute([$id]);
|
|
$video = $stmt->fetch();
|
|
} else {
|
|
$error = "Update failed.";
|
|
}
|
|
}
|
|
|
|
// Get current tags
|
|
$stmt = $pdo->prepare("SELECT t.name FROM tags t JOIN video_tags vt ON t.id = vt.tag_id WHERE vt.video_id = ?");
|
|
$stmt->execute([$id]);
|
|
$current_tags = $stmt->fetchAll(PDO::FETCH_COLUMN);
|
|
$tags_str = implode(', ', $current_tags);
|
|
|
|
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;">Edit Sermon: <?= htmlspecialchars($video['title']) ?></h2>
|
|
|
|
<?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" value="<?= htmlspecialchars($video['title']) ?>" required>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label class="form-label">Release Date</label>
|
|
<input type="date" name="release_date" class="form-control" value="<?= $video['release_date'] ?>" required>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label class="form-label">Description</label>
|
|
<textarea name="description" class="form-control" style="min-height: 120px;"><?= htmlspecialchars($video['description']) ?></textarea>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label class="form-label">Tags (comma separated)</label>
|
|
<input type="text" name="tags" class="form-control" value="<?= htmlspecialchars($tags_str) ?>" 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" <?= $video['source_type'] === 'upload' ? 'checked' : '' ?> onclick="toggleSource('upload')"> Upload File
|
|
</label>
|
|
<label style="cursor: pointer;">
|
|
<input type="radio" name="source_type" value="link" <?= $video['source_type'] === 'link' ? 'checked' : '' ?> onclick="toggleSource('link')"> External Link
|
|
</label>
|
|
</div>
|
|
</div>
|
|
|
|
<div id="source_upload" class="form-group" style="display: <?= $video['source_type'] === 'upload' ? 'block' : 'none' ?>;">
|
|
<label class="form-label">Replace Video File (Optional)</label>
|
|
<input type="file" name="video_file" class="form-control" accept="video/*">
|
|
<small style="color: var(--text-muted);">Current: <?= htmlspecialchars($video['video_url']) ?></small>
|
|
</div>
|
|
|
|
<div id="source_link" class="form-group" style="display: <?= $video['source_type'] === 'link' ? 'block' : 'none' ?>;">
|
|
<label class="form-label">External Video URL</label>
|
|
<input type="url" name="external_url" class="form-control" value="<?= $video['source_type'] === 'link' ? htmlspecialchars($video['video_url']) : '' ?>">
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label class="form-label">Thumbnail</label>
|
|
<?php if ($video['thumbnail_url']): ?>
|
|
<div style="margin-bottom: 10px;">
|
|
<img src="../<?= htmlspecialchars($video['thumbnail_url']) ?>" style="width: 120px; border-radius: 8px;">
|
|
</div>
|
|
<?php endif; ?>
|
|
<input type="file" name="thumbnail_file" class="form-control" accept="image/*">
|
|
<small style="color: var(--text-muted);">Leave empty to keep current thumbnail.</small>
|
|
</div>
|
|
|
|
<div style="margin-top: 32px; display: flex; gap: 16px;">
|
|
<button type="submit" class="btn btn-primary" style="flex-grow: 1;">Update Sermon</button>
|
|
<a href="index.php" class="btn" style="background: var(--glass);">Back to Dashboard</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'; ?>
|