%PDF- <> %âãÏÓ endobj 2 0 obj <> endobj 3 0 obj <>/ExtGState<>/ProcSet[/PDF/Text/ImageB/ImageC/ImageI] >>/Annots[ 28 0 R 29 0 R] /MediaBox[ 0 0 595.5 842.25] /Contents 4 0 R/Group<>/Tabs/S>> endobj ºaâÚÎΞ-ÌE1ÍØÄ÷{òò2ÿ ÛÖ^ÔÀá TÎ{¦?§®¥kuµùÕ5sLOšuY>endobj 2 0 obj<>endobj 2 0 obj<>endobj 2 0 obj<>endobj 2 0 obj<> endobj 2 0 obj<>endobj 2 0 obj<>es 3 0 R>> endobj 2 0 obj<> ox[ 0.000000 0.000000 609.600000 935.600000]/Fi endobj 3 0 obj<> endobj 7 1 obj<>/ProcSet[/PDF/Text/ImageB/ImageC/ImageI]>>/Subtype/Form>> stream
<?php
/**
* Admin: form tambah / edit Galeri Video (judul + tautan YouTube).
*/
require_once dirname(__DIR__) . '/config/config.php';
require_once dirname(__DIR__) . '/includes/auth.php';
require_once dirname(__DIR__) . '/includes/queries.php';
require_central();
$id = (int) get_param('id', '0');
$isEdit = $id > 0;
$errors = [];
$data = ['judul' => '', 'youtube' => '', 'tanggal' => date('Y-m-d')];
$existing = null;
if ($isEdit) {
$stmt = db()->prepare('SELECT * FROM galeri_video WHERE id = ?');
$stmt->execute([$id]);
$existing = $stmt->fetch();
if (!$existing) {
set_flash('error', 'Video tidak ditemukan.');
redirect('admin/galeri-video.php');
}
$data = [
'judul' => $existing['judul'],
'youtube' => 'https://www.youtube.com/watch?v=' . $existing['youtube_id'],
'tanggal' => date('Y-m-d', strtotime($existing['tanggal'] ?: $existing['created_at'])),
];
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
csrf_verify();
$data = [
'judul' => post_param('judul'),
'youtube' => post_param('youtube'),
'tanggal' => post_param('tanggal'),
];
$ytId = youtube_id_from_url($data['youtube']);
if ($data['judul'] === '') {
$errors['judul'] = 'Judul video wajib diisi.';
}
if ($ytId === '') {
$errors['youtube'] = 'Tautan YouTube tidak valid. Contoh: https://www.youtube.com/watch?v=XXXXXXXXXXX';
}
if ($data['tanggal'] === '') {
$data['tanggal'] = date('Y-m-d');
}
$dtCek = DateTime::createFromFormat('Y-m-d', $data['tanggal']);
if (!$dtCek || $dtCek->format('Y-m-d') !== $data['tanggal']) {
$errors['tanggal'] = 'Tanggal tidak valid.';
}
if (empty($errors)) {
if ($isEdit) {
db()->prepare('UPDATE galeri_video SET judul = ?, youtube_id = ?, tanggal = ? WHERE id = ?')
->execute([$data['judul'], $ytId, $data['tanggal'], $id]);
set_flash('success', 'Video berhasil diperbarui.');
} else {
db()->prepare('INSERT INTO galeri_video (judul, youtube_id, tanggal, created_by) VALUES (?, ?, ?, ?)')
->execute([$data['judul'], $ytId, $data['tanggal'], current_admin()['id']]);
set_flash('success', 'Video berhasil ditambahkan.');
}
redirect('admin/galeri-video.php');
}
}
$pageTitle = ($isEdit ? 'Edit' : 'Tambah') . ' Galeri Video — CMS PPID';
$activeMenu = 'galeri-video';
require __DIR__ . '/partials/head.php';
$val = static fn (string $k) => e($data[$k] ?? '');
$previewId = $isEdit && $existing ? $existing['youtube_id'] : '';
?>
<div class="mb-6">
<a href="<?= e(url('admin/galeri-video.php')) ?>" class="text-sm text-slate-500 hover:text-mateng-green inline-flex items-center gap-2 mb-3"><i class="fas fa-arrow-left"></i> Kembali ke daftar</a>
<h2 class="text-2xl font-bold text-slate-900"><?= $isEdit ? 'Edit Galeri Video' : 'Tambah Galeri Video' ?></h2>
</div>
<form method="post" class="max-w-3xl bg-white border border-slate-100 rounded-xl shadow-sm p-6 sm:p-8 space-y-5">
<?= csrf_field() ?>
<div>
<label class="block text-sm font-medium text-slate-700 mb-1.5">Judul Video <span class="text-red-500">*</span></label>
<input type="text" name="judul" value="<?= $val('judul') ?>" placeholder="mis. Sosialisasi Keterbukaan Informasi Publik" class="w-full rounded-lg border px-4 py-2.5 outline-none focus:ring-4 <?= isset($errors['judul']) ? 'border-red-300 focus:ring-red-100' : 'border-slate-300 focus:border-mateng-green focus:ring-green-100' ?>">
<?php if (isset($errors['judul'])): ?><p class="text-xs text-red-500 mt-1"><?= e($errors['judul']) ?></p><?php endif; ?>
</div>
<div>
<label class="block text-sm font-medium text-slate-700 mb-1.5">Tautan YouTube <span class="text-red-500">*</span></label>
<input type="text" name="youtube" id="ytUrl" value="<?= $val('youtube') ?>" placeholder="https://www.youtube.com/watch?v=XXXXXXXXXXX" class="w-full rounded-lg border px-4 py-2.5 outline-none focus:ring-4 <?= isset($errors['youtube']) ? 'border-red-300 focus:ring-red-100' : 'border-slate-300 focus:border-mateng-green focus:ring-green-100' ?>">
<?php if (isset($errors['youtube'])): ?><p class="text-xs text-red-500 mt-1"><?= e($errors['youtube']) ?></p><?php endif; ?>
<p class="text-xs text-slate-400 mt-1">Tempel tautan video YouTube. Mendukung format watch, youtu.be, embed, atau shorts.</p>
<div id="ytPreview" class="mt-3 <?= $previewId ? '' : 'hidden' ?>">
<div class="relative w-full max-w-sm rounded-xl overflow-hidden shadow-sm bg-slate-900" style="aspect-ratio:16/9;">
<img id="ytThumb" src="<?= $previewId ? 'https://img.youtube.com/vi/' . e($previewId) . '/hqdefault.jpg' : '' ?>" class="w-full h-full object-cover" alt="">
<span class="absolute inset-0 flex items-center justify-center pointer-events-none"><i class="fab fa-youtube text-red-600 text-4xl drop-shadow"></i></span>
</div>
</div>
</div>
<div>
<label class="block text-sm font-medium text-slate-700 mb-1.5">Tanggal</label>
<input type="date" name="tanggal" value="<?= $val('tanggal') ?>" class="w-full sm:w-56 rounded-lg border px-4 py-2.5 outline-none focus:ring-4 <?= isset($errors['tanggal']) ? 'border-red-300 focus:ring-red-100' : 'border-slate-300 focus:border-mateng-green focus:ring-green-100' ?>">
<?php if (isset($errors['tanggal'])): ?><p class="text-xs text-red-500 mt-1"><?= e($errors['tanggal']) ?></p><?php endif; ?>
</div>
<div class="flex gap-3 pt-2">
<button type="submit" class="bg-mateng-green hover:bg-green-700 text-white font-bold px-6 py-3 rounded-lg transition-colors flex items-center gap-2"><i class="fas fa-save"></i> <?= $isEdit ? 'Perbarui' : 'Simpan' ?></button>
<a href="<?= e(url('admin/galeri-video.php')) ?>" class="bg-slate-100 hover:bg-slate-200 text-slate-700 font-semibold px-6 py-3 rounded-lg transition-colors">Batal</a>
</div>
</form>
<script>
// Pratinjau thumbnail YouTube saat mengetik tautan.
(function () {
var inp = document.getElementById('ytUrl');
var box = document.getElementById('ytPreview');
var thumb = document.getElementById('ytThumb');
if (!inp || !box || !thumb) return;
function extractId(v) {
v = (v || '').trim();
if (/^[A-Za-z0-9_-]{11}$/.test(v)) return v;
var m = v.match(/(?:youtube\.com\/(?:watch\?(?:.*&)?v=|embed\/|shorts\/|live\/)|youtu\.be\/)([A-Za-z0-9_-]{11})/);
return m ? m[1] : '';
}
inp.addEventListener('input', function () {
var id = extractId(inp.value);
if (id) { thumb.src = 'https://img.youtube.com/vi/' + id + '/hqdefault.jpg'; box.classList.remove('hidden'); }
else { box.classList.add('hidden'); }
});
})();
</script>
<?php require __DIR__ . '/partials/foot.php'; ?>