| 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 : D:/bpkd_/admin/ |
Upload File : |
<?php
$page_title = 'Tambah Halaman Baru';
// Enable error reporting untuk debug (hapus nanti)
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once 'config/database.php';
require_once '../includes/config.php';
require_once 'includes/init.php';
requireLogin();
// Cek apakah user adalah admin atau editor
if (!in_array($_SESSION['role'], ['superadmin', 'admin'])) {
redirect(BASE_URL . '/admin/dashboard');
}
$error = '';
$success = '';
// Proses simpan data
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['add'])) {
$title = sanitize($_POST['title']);
$content = $_POST['content'];
$excerpt = sanitize($_POST['excerpt']);
$slug = !empty($_POST['slug']) ? createSlug(sanitize($_POST['slug'])) : createSlug($title);
$status = sanitize($_POST['status']);
$author_id = $_SESSION['user_id'];
$meta_title = sanitize($_POST['meta_title']);
$meta_description = sanitize($_POST['meta_description']);
// Validasi
if (empty($title)) {
$error = "Judul halaman wajib diisi";
} elseif (empty($content)) {
$error = "Konten halaman wajib diisi";
}
// Cek duplikat slug
if (empty($error)) {
$check_slug = $conn->query("SELECT id FROM pages WHERE slug = '$slug'");
if ($check_slug->num_rows > 0) {
$slug .= '-' . time();
}
}
// Handle featured image upload
$featured_image = '';
if (empty($error) && isset($_FILES['featured_image']) && $_FILES['featured_image']['error'] == 0) {
$target_dir = dirname(__DIR__) . "/uploads/pages/";
if (!file_exists($target_dir)) {
mkdir($target_dir, 0777, true);
}
$file_extension = strtolower(pathinfo($_FILES['featured_image']['name'], PATHINFO_EXTENSION));
$allowed_types = ['jpg', 'jpeg', 'png', 'gif', 'webp'];
if (!in_array($file_extension, $allowed_types)) {
$error = "Format gambar tidak diizinkan. Gunakan: JPG, JPEG, PNG, GIF, WEBP";
} elseif ($_FILES['featured_image']['size'] > 2000000) {
$error = "Ukuran gambar maksimal 2MB";
} else {
$image_name = time() . '_' . uniqid() . '.' . $file_extension;
$target_file = $target_dir . $image_name;
if (move_uploaded_file($_FILES['featured_image']['tmp_name'], $target_file)) {
$featured_image = 'uploads/pages/' . $image_name;
} else {
$error = "Gagal mengupload gambar";
}
}
}
// Simpan ke database
if (empty($error)) {
$query = "INSERT INTO pages (title, slug, content, excerpt, featured_image, status, author_id, meta_title, meta_description, created_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, NOW())";
$stmt = $conn->prepare($query);
$stmt->bind_param("ssssssiss",
$title, $slug, $content, $excerpt, $featured_image, $status, $author_id, $meta_title, $meta_description
);
if ($stmt->execute()) {
// 🔥 PERBAIKAN: Redirect tanpa .php dan tanpa query string
redirect(BASE_URL . '/admin/pages?msg=added');
exit;
} else {
$error = "Error: " . $stmt->error;
}
$stmt->close();
}
}
include 'includes/header.php';
?>
<style>
.article-form-container {
max-width: 1400px;
margin: 0 auto;
}
.editor-wrapper {
border: 2px solid #e9ecef;
border-radius: 15px;
overflow: hidden;
transition: all 0.3s;
background: #fff;
}
.editor-wrapper:focus-within {
border-color: #4e73df;
box-shadow: 0 0 0 4px rgba(78, 115, 223, 0.15);
}
.ck-editor__editable_inline {
min-height: 500px !important;
max-height: 700px !important;
font-family: 'Inter', sans-serif !important;
font-size: 16px !important;
line-height: 1.8 !important;
padding: 30px !important;
}
.ck.ck-editor {
width: 100% !important;
}
.ck.ck-toolbar {
border: none !important;
border-bottom: 2px solid #e9ecef !important;
background: #f8f9fc !important;
padding: 10px !important;
}
.form-sidebar {
position: sticky;
top: 30px;
}
.sidebar-card {
border: none;
border-radius: 15px;
box-shadow: 0 5px 20px rgba(0,0,0,0.05);
margin-bottom: 20px;
}
.sidebar-card .card-header {
background: linear-gradient(135deg, #4e73df 0%, #224abe 100%);
color: white;
border-radius: 15px 15px 0 0 !important;
padding: 15px 20px;
}
.sidebar-card .card-header h6 {
margin: 0;
font-weight: 600;
}
.featured-image-preview {
position: relative;
margin-top: 15px;
display: none;
}
.featured-image-preview img {
width: 100%;
max-height: 200px;
object-fit: cover;
border-radius: 12px;
border: 2px solid #e9ecef;
}
.featured-image-preview .remove-image {
position: absolute;
top: 10px;
right: 10px;
background: rgba(220, 53, 69, 0.9);
color: white;
border: none;
border-radius: 50%;
width: 30px;
height: 30px;
cursor: pointer;
}
/* Loading Overlay */
#loadingOverlay {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0,0,0,0.5);
z-index: 9999;
justify-content: center;
align-items: center;
}
#loadingOverlay.active {
display: flex;
}
.spinner-border {
width: 3rem;
height: 3rem;
}
</style>
<div class="container-fluid article-form-container">
<div class="d-sm-flex align-items-center justify-content-between mb-4">
<h1 class="h3 mb-0 text-gray-800">
<i class="fas fa-file-alt me-2"></i> Tambah Halaman Baru
</h1>
<a href="<?php echo BASE_URL; ?>/admin/pages" class="btn btn-secondary">
<i class="fas fa-arrow-left me-2"></i> Kembali
</a>
</div>
<?php if (!empty($error)): ?>
<div class="alert alert-danger alert-dismissible fade show">
<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; ?>
<?php if (!empty($success)): ?>
<div class="alert alert-success alert-dismissible fade show">
<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; ?>
<form method="POST" enctype="multipart/form-data" id="pageForm">
<div class="row">
<!-- MAIN CONTENT (KIRI) -->
<div class="col-lg-8">
<div class="card shadow mb-4">
<div class="card-body p-4">
<!-- Judul -->
<div class="mb-3">
<label class="form-label fw-bold fs-6">
<i class="fas fa-heading me-2 text-primary"></i>Judul Halaman <span class="text-danger">*</span>
</label>
<input type="text" class="form-control form-control-lg" name="title" id="titleInput"
placeholder="Masukkan judul halaman..." required>
</div>
<!-- Slug -->
<div class="mb-3">
<label class="form-label">Slug (URL)</label>
<div class="input-group">
<span class="input-group-text"><?php echo BASE_URL; ?>/halaman/</span>
<input type="text" class="form-control" name="slug" id="slug"
placeholder="otomatis-dari-judul">
</div>
<small class="text-muted">Biarkan kosong untuk generate otomatis dari judul</small>
</div>
<!-- Content with CKEditor -->
<div class="mb-3">
<label class="form-label fw-bold fs-6">
<i class="fas fa-edit me-2 text-primary"></i>Konten Halaman <span class="text-danger">*</span>
</label>
<div class="editor-wrapper">
<textarea id="content-editor" name="content" rows="20" required></textarea>
</div>
</div>
<!-- Excerpt -->
<div class="mb-3">
<label class="form-label fw-bold fs-6">
<i class="fas fa-paragraph me-2 text-primary"></i>Ringkasan (Excerpt)
</label>
<textarea class="form-control" name="excerpt" rows="4"
placeholder="Tulis ringkasan singkat halaman (untuk SEO dan meta description)..."></textarea>
</div>
<!-- SEO Fields -->
<div class="row">
<div class="col-md-6 mb-3">
<label class="form-label">Meta Title (SEO)</label>
<input type="text" class="form-control" name="meta_title"
placeholder="Judul untuk SEO (kosongkan akan menggunakan judul halaman)">
<small class="text-muted">Optimal 50-60 karakter</small>
</div>
<div class="col-md-6 mb-3">
<label class="form-label">Meta Description (SEO)</label>
<input type="text" class="form-control" name="meta_description"
placeholder="Deskripsi untuk SEO">
<small class="text-muted">Optimal 150-160 karakter</small>
</div>
</div>
</div>
</div>
</div>
<!-- SIDEBAR (KANAN) -->
<div class="col-lg-4">
<div class="form-sidebar">
<!-- Publish Box -->
<div class="sidebar-card card">
<div class="card-header">
<h6><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-select" name="status">
<option value="draft">Draft (Simpan sebagai konsep)</option>
<option value="published" selected>Publish (Langsung terbit)</option>
</select>
</div>
<button type="submit" name="add" class="btn btn-primary w-100" id="submitBtn">
<i class="fas fa-save me-2"></i> Simpan Halaman
</button>
</div>
</div>
<!-- Featured Image Box -->
<div class="sidebar-card card">
<div class="card-header">
<h6><i class="fas fa-image me-2"></i> Gambar Unggulan</h6>
</div>
<div class="card-body">
<input type="file" class="form-control" name="featured_image" id="featuredImage"
accept="image/*">
<small class="text-muted d-block mt-2">
<i class="fas fa-info-circle me-1"></i> Format: JPG, PNG, GIF, WEBP. Maks 2MB
</small>
<div class="featured-image-preview mt-3" id="imagePreview">
<img src="" alt="Preview" id="previewImg">
<button type="button" class="remove-image" onclick="removeImage()">×</button>
</div>
</div>
</div>
</div>
</div>
</div>
</form>
</div>
<!-- Loading Overlay -->
<div id="loadingOverlay">
<div class="text-center text-white">
<div class="spinner-border" role="status">
<span class="visually-hidden">Loading...</span>
</div>
<p class="mt-3 fs-5">Menyimpan halaman...</p>
</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>
const BASE_URL = '<?php echo BASE_URL; ?>';
let editorReady = false;
// 🔥 PERBAIKAN: Hapus editor yang sudah ada sebelumnya
document.querySelectorAll('.ck-editor, .ck-reset, .ck-body').forEach(el => el.remove());
// 🔥 PERBAIKAN: Hanya initialize sekali
if (typeof window.editorInstance !== 'undefined') {
try { window.editorInstance.destroy(); } catch(e) {}
window.editorInstance = null;
}
// 🔥 PERBAIKAN: Inisialisasi CKEditor dengan cara yang benar
ClassicEditor
.create(document.querySelector('#content-editor'), {
language: 'id',
toolbar: {
items: [
'heading', '|',
'bold', 'italic', 'underline', 'strikethrough', '|',
'bulletedList', 'numberedList', '|',
'alignment', '|',
'indent', 'outdent', '|',
'link', 'blockQuote', 'insertTable', 'mediaEmbed', '|',
'undo', 'redo'
]
},
placeholder: 'Mulai menulis halaman Anda di sini...',
removePlugins: ['Title', 'ImageUpload', 'CKFinderUploadAdapter', 'EasyImage', 'CKFinder']
})
.then(editor => {
window.editorInstance = editor;
editorReady = true;
console.log('CKEditor initialized successfully');
})
.catch(error => {
console.error('CKEditor error:', error);
});
// 🔥 PERBAIKAN: Form submit handler
const form = document.getElementById('pageForm');
if (form) {
form.addEventListener('submit', function(e) {
// Ambil data dari editor
if (window.editorInstance && editorReady) {
const data = window.editorInstance.getData();
document.querySelector('#content-editor').value = data;
console.log('Content length:', data.length);
}
// Tampilkan loading
const loading = document.getElementById('loadingOverlay');
if (loading) {
loading.classList.add('active');
}
// Disable submit button
const submitBtn = document.getElementById('submitBtn');
if (submitBtn) {
submitBtn.disabled = true;
submitBtn.innerHTML = '<i class="fas fa-spinner fa-spin me-2"></i> Menyimpan...';
}
return true;
});
}
// 🔥 PERBAIKAN: Auto generate slug
const titleInput = document.getElementById('titleInput');
const slugInput = document.getElementById('slug');
if (titleInput && slugInput) {
titleInput.addEventListener('keyup', function() {
const slug = this.value.toLowerCase()
.replace(/[^a-z0-9\s]/g, '')
.replace(/\s+/g, '-')
.replace(/-+/g, '-')
.replace(/^-|-$/g, '');
if (slugInput.value === '' || slugInput.value === slugInput.defaultValue) {
slugInput.value = slug;
}
});
}
// Featured image preview
const featuredImage = document.getElementById('featuredImage');
const imagePreview = document.getElementById('imagePreview');
const previewImg = document.getElementById('previewImg');
if (featuredImage) {
featuredImage.addEventListener('change', function(e) {
const file = e.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function(e) {
previewImg.src = e.target.result;
imagePreview.style.display = 'block';
};
reader.readAsDataURL(file);
}
});
}
function removeImage() {
const featuredImage = document.getElementById('featuredImage');
const imagePreview = document.getElementById('imagePreview');
const previewImg = document.getElementById('previewImg');
if (featuredImage) featuredImage.value = '';
if (imagePreview) imagePreview.style.display = 'none';
if (previewImg) previewImg.src = '';
}
</script>
<?php require_once 'includes/footer.php'; ?>