| Server IP : 127.0.0.1 / Your IP : 192.168.99.1 Web Server : Apache/2.4.35 (Win64) OpenSSL/1.1.1b mod_fcgid/2.3.9 System : Windows NT WIN-JEJMB9EN0L0 10.0 build 14393 (Windows Server 2016) AMD64 User : Administrator ( 0) PHP Version : 7.4.33 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /bpkd_/admin/ |
Upload File : |
<?php
$page_title = 'Tambah Halaman';
require_once 'config/database.php';
require_once 'includes/header.php';
require_once 'includes/init.php';
requireLogin();
// Aktifkan error reporting untuk debugging
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Get parent pages for dropdown
$parent_pages = $conn->query("SELECT id, title FROM pages WHERE parent_id = 0 ORDER BY title");
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$title = sanitize($_POST['title']);
$content = $_POST['content'];
$excerpt = sanitize($_POST['excerpt']);
$parent_id = (int)$_POST['parent_id'];
$template = sanitize($_POST['template']);
$status = sanitize($_POST['status']);
$author_id = $_SESSION['user_id'];
// Handle slug
$slug = sanitize($_POST['slug']);
if (empty($slug)) {
$slug = createSlug($title);
}
// Cek apakah slug sudah ada
$check_slug = $conn->query("SELECT id FROM pages WHERE slug = '$slug'");
if ($check_slug->num_rows > 0) {
$slug = $slug . '-' . rand(100, 999);
}
// Handle file upload (support gambar dan PDF)
$featured_file = '';
$file_type = '';
if (isset($_FILES['featured_file']) && $_FILES['featured_file']['error'] == 0) {
$target_dir = "../uploads/pages/";
if (!file_exists($target_dir)) {
mkdir($target_dir, 0777, true);
}
$file_ext = strtolower(pathinfo($_FILES['featured_file']['name'], PATHINFO_EXTENSION));
$allowed = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'pdf']; // Tambahkan PDF
if (in_array($file_ext, $allowed)) {
if ($_FILES['featured_file']['size'] <= 5 * 1024 * 1024) { // 5MB untuk PDF
$file_name = time() . '_' . uniqid() . '.' . $file_ext;
$target_file = $target_dir . $file_name;
if (move_uploaded_file($_FILES['featured_file']['tmp_name'], $target_file)) {
$featured_file = 'uploads/pages/' . $file_name;
$file_type = $file_ext;
} else {
$error = "Gagal mengupload file!";
}
} else {
$error = "Ukuran file maksimal 5MB!";
}
} else {
$error = "Format file tidak diizinkan! Gunakan JPG, PNG, GIF, WEBP, atau PDF.";
}
}
if (!isset($error)) {
$query = "INSERT INTO pages (title, slug, content, excerpt, featured_image, file_type, parent_id, template, status, author_id, menu_order)
VALUES ('$title', '$slug', '$content', '$excerpt', '$featured_file', '$file_type', $parent_id, '$template', '$status', $author_id, 0)";
if ($conn->query($query)) {
redirect('pages.php?msg=added');
} else {
$error = "Error: " . $conn->error;
}
}
}
?>
<div class="container-fluid">
<div class="d-sm-flex align-items-center justify-content-between mb-4">
<h1 class="h3 mb-0 text-gray-800">Tambah Halaman Baru</h1>
<a href="pages.php" class="btn btn-secondary">
<i class="fas fa-arrow-left me-2"></i>Kembali
</a>
</div>
<?php if (isset($success)): ?>
<div class="alert alert-success alert-dismissible fade show" role="alert">
<i class="fas fa-check-circle me-2"></i><?php echo $success; ?>
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>
<?php if (isset($error)): ?>
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<i class="fas fa-exclamation-circle me-2"></i><?php echo $error; ?>
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>
<div class="card shadow mb-4">
<div class="card-body">
<form method="POST" enctype="multipart/form-data" id="pageForm">
<div class="row">
<div class="col-md-8">
<!-- Title -->
<div class="mb-3">
<label class="form-label">Judul Halaman <span class="text-danger">*</span></label>
<input type="text" class="form-control" name="title" id="title" required>
</div>
<!-- Slug / URL -->
<div class="mb-3">
<label class="form-label">Slug (URL)</label>
<div class="input-group">
<span class="input-group-text"><?php echo $_SERVER['HTTP_HOST']; ?>/halaman/</span>
<input type="text" class="form-control" name="slug" id="slug"
placeholder="nama-halaman-anda">
</div>
<small class="text-muted">
Biarkan kosong untuk generate otomatis dari judul
</small>
</div>
<!-- Content -->
<div class="mb-3">
<label class="form-label">Konten <span class="text-danger">*</span></label>
<textarea id="content-editor" name="content" rows="15"></textarea>
</div>
<!-- Excerpt -->
<div class="mb-3">
<label class="form-label">Ringkasan (Excerpt)</label>
<textarea class="form-control" name="excerpt" rows="3" placeholder="Ringkasan singkat halaman untuk SEO..."></textarea>
<small class="text-muted">Max 160 karakter untuk meta description</small>
</div>
</div>
<div class="col-md-4">
<!-- Publish Status -->
<div class="card mb-3">
<div class="card-header">
<h6 class="mb-0"><i class="fas fa-globe me-2"></i>Publish</h6>
</div>
<div class="card-body">
<div class="mb-3">
<label class="form-label">Status</label>
<select class="form-control" name="status">
<option value="published">Published</option>
<option value="draft">Draft</option>
</select>
</div>
<button type="submit" name="save_page" class="btn btn-primary w-100" id="saveBtn">
<i class="fas fa-save me-2"></i>Simpan Halaman
</button>
</div>
</div>
<!-- Page Settings -->
<div class="card mb-3">
<div class="card-header">
<h6 class="mb-0"><i class="fas fa-cog me-2"></i>Pengaturan Halaman</h6>
</div>
<div class="card-body">
<div class="mb-3">
<label class="form-label">Induk Halaman</label>
<select class="form-control" name="parent_id">
<option value="0">Tidak Ada (Halaman Utama)</option>
<?php
$parent_pages->data_seek(0);
while($parent = $parent_pages->fetch_assoc()):
?>
<option value="<?php echo $parent['id']; ?>"><?php echo $parent['title']; ?></option>
<?php endwhile; ?>
</select>
</div>
<div class="mb-3">
<label class="form-label">Template</label>
<select class="form-control" name="template">
<option value="default">Default</option>
<option value="fullwidth">Full Width</option>
<option value="sidebar">Dengan Sidebar</option>
<option value="contact">Kontak</option>
</select>
</div>
</div>
</div>
<!-- Featured File (Image or PDF) -->
<div class="card mb-3">
<div class="card-header">
<h6 class="mb-0"><i class="fas fa-paperclip me-2"></i>File Utama (Gambar/PDF)</h6>
</div>
<div class="card-body">
<input type="file" class="form-control" name="featured_file"
accept="image/*,.pdf" id="featuredFile">
<small class="text-muted d-block mt-2">
<i class="fas fa-info-circle me-1"></i>Format: JPG, PNG, GIF, WEBP, PDF. Maks 5MB
</small>
<small class="text-muted">Ukuran ideal gambar: 1200 x 630 pixel</small>
<!-- Preview untuk Gambar -->
<div class="featured-image-preview mt-3" id="imagePreview" style="display: none;">
<img src="" alt="Preview" id="previewImg" class="img-fluid rounded">
</div>
<!-- Preview untuk PDF -->
<div class="pdf-preview mt-3" id="pdfPreview" style="display: none;">
<div class="alert alert-info p-3">
<i class="fas fa-file-pdf fa-2x me-2 text-danger"></i>
<strong>File PDF:</strong> <span id="pdfName"></span>
<br>
<small class="text-muted">PDF akan ditampilkan sebagai link download</small>
</div>
</div>
</div>
</div>
<!-- SEO Preview -->
<div class="card mb-3">
<div class="card-header">
<h6 class="mb-0"><i class="fas fa-search me-2"></i>Pratinjau SEO</h6>
</div>
<div class="card-body">
<div class="seo-preview p-3 bg-light rounded">
<div class="text-primary small"><?php echo $_SERVER['HTTP_HOST']; ?>/halaman/</div>
<div class="fw-bold text-success" id="seoUrl">nama-halaman</div>
<div class="fw-bold text-primary mt-2" id="seoTitle">Judul Halaman</div>
<div class="small text-muted mt-1" id="seoDesc">Ringkasan halaman akan muncul di sini...</div>
</div>
</div>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
<!-- CKEditor 5 -->
<script src="https://cdn.ckeditor.com/ckeditor5/39.0.1/classic/ckeditor.js"></script>
<script src="https://cdn.ckeditor.com/ckeditor5/39.0.1/classic/translations/id.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function() {
let editorInstance = null;
let isSubmitting = false;
// Inisialisasi CKEditor dengan toolbar yang lebih sederhana
ClassicEditor
.create(document.querySelector('#content-editor'), {
language: 'id',
toolbar: {
items: [
'heading', '|',
'bold', 'italic', 'underline', 'strikethrough', '|',
'bulletedList', 'numberedList', '|',
'alignment', '|',
'indent', 'outdent', '|',
'link', 'blockQuote', 'insertTable', '|',
'undo', 'redo', '|',
'fontFamily', 'fontSize', 'fontColor', 'fontBackgroundColor', '|',
'codeBlock', 'subscript', 'superscript', '|',
'horizontalLine', 'pageBreak', '|',
'specialCharacters'
]
},
placeholder: 'Mulai menulis konten halaman di sini...',
removePlugins: ['Title']
})
.then(editor => {
window.editor = editor;
})
.catch(error => {
console.error(error);
});
// Generate slug from title
document.getElementById('title').addEventListener('keyup', function() {
const slugInput = document.getElementById('slug');
if (slugInput.value === '') {
const slug = this.value.toLowerCase()
.replace(/[^a-z0-9\s]/g, '')
.replace(/\s+/g, '-')
.replace(/-+/g, '-')
.replace(/^-|-$/g, '');
slugInput.value = slug;
updateSEOPreview();
}
});
// Update SEO preview
function updateSEOPreview() {
const title = document.getElementById('title').value || 'Judul Halaman';
const slug = document.getElementById('slug').value || 'nama-halaman';
const excerpt = document.querySelector('textarea[name="excerpt"]').value || 'Ringkasan halaman akan muncul di sini...';
document.getElementById('seoTitle').textContent = title;
document.getElementById('seoUrl').textContent = slug;
document.getElementById('seoDesc').textContent = excerpt;
}
// File preview - support gambar dan PDF
document.getElementById('featuredFile').addEventListener('change', function(e) {
const file = e.target.files[0];
if (!file) return;
// Validasi ukuran
if (file.size > 5 * 1024 * 1024) {
alert('Ukuran file maksimal 5MB!');
this.value = '';
return;
}
const fileExt = file.name.split('.').pop().toLowerCase();
const isImage = ['jpg', 'jpeg', 'png', 'gif', 'webp'].includes(fileExt);
const isPdf = fileExt === 'pdf';
if (isImage) {
// Preview gambar
const reader = new FileReader();
reader.onload = function(e) {
document.getElementById('imagePreview').style.display = 'block';
document.getElementById('pdfPreview').style.display = 'none';
document.getElementById('previewImg').src = e.target.result;
}
reader.readAsDataURL(file);
} else if (isPdf) {
// Preview PDF
document.getElementById('imagePreview').style.display = 'none';
document.getElementById('pdfPreview').style.display = 'block';
document.getElementById('pdfName').textContent = file.name;
} else {
alert('Format file tidak didukung! Gunakan gambar atau PDF.');
this.value = '';
}
});
// Input events for SEO preview
document.getElementById('title').addEventListener('input', updateSEOPreview);
document.getElementById('slug').addEventListener('input', updateSEOPreview);
document.querySelector('textarea[name="excerpt"]').addEventListener('input', updateSEOPreview);
});
</script>
<style>
.featured-image-preview img {
max-width: 100%;
max-height: 200px;
object-fit: cover;
border: 1px solid #e9ecef;
border-radius: 5px;
}
.seo-preview {
border: 1px solid #e9ecef;
}
.ck-editor__editable {
min-height: 300px !important;
}
</style>
<?php require_once 'includes/footer.php'; ?>