| 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:/laragon4/www/bpkd.test_/admin/ |
Upload File : |
<?php
$page_title = 'Tambah Artikel';
require_once 'config/database.php';
require_once 'includes/header.php';
require_once 'includes/init.php';
// Get categories for dropdown
$categories = $conn->query("SELECT * FROM categories ORDER BY name");
// Get tags
$tags = $conn->query("SELECT * FROM tags ORDER BY name");
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Tentukan status berdasarkan tombol yang ditekan
if (isset($_POST['action']) && $_POST['action'] == 'publish') {
$status = 'published';
} else {
$status = isset($_POST['status']) ? sanitize($_POST['status']) : 'draft';
}
$title = sanitize($_POST['title']);
$content = $_POST['content'];
$excerpt = sanitize($_POST['excerpt']);
$category_id = (int)$_POST['category_id'];
$author_id = $_SESSION['user_id'];
$dok_foto = !empty($_POST['dok_foto']) ? sanitize($_POST['dok_foto']) : 'dok. kontributor';
// Generate slug dari judul
$slug = createSlug($title);
// CEK DUPLIKAT SLUG
$check_slug = $conn->query("SELECT id FROM articles WHERE slug = '$slug'");
if ($check_slug->num_rows > 0) {
$slug .= '-' . time();
}
// Generate hash unik
$url_hash = substr(md5(uniqid() . rand() . time()), 0, 12);
$check_hash = $conn->query("SELECT id FROM articles WHERE url_hash = '$url_hash'");
while ($check_hash->num_rows > 0) {
$url_hash = substr(md5(uniqid() . rand() . time()), 0, 12);
$check_hash = $conn->query("SELECT id FROM articles WHERE url_hash = '$url_hash'");
}
// Handle featured image upload
$featured_image = '';
if (isset($_FILES['featured_image']) && $_FILES['featured_image']['error'] == 0) {
$target_dir = "../uploads/articles/";
if (!file_exists($target_dir)) {
mkdir($target_dir, 0777, true);
}
$image_name = time() . '_' . basename($_FILES['featured_image']['name']);
$target_file = $target_dir . $image_name;
if (move_uploaded_file($_FILES['featured_image']['tmp_name'], $target_file)) {
$featured_image = 'uploads/articles/' . $image_name;
}
}
// Published_at handling
$published_at_sql = 'NULL';
if ($status == 'published') {
$published_at_sql = "'" . date('Y-m-d H:i:s') . "'";
}
// ========== PERBAIKAN: GUNAKAN QUERY LANGSUNG (TANPA PREPARED STATEMENT) ==========
$query = "INSERT INTO articles (title, slug, url_hash, content, excerpt, featured_image, category_id, author_id, status, dok_foto, published_at)
VALUES ('$title', '$slug', '$url_hash', '$content', '$excerpt', '$featured_image', $category_id, $author_id, '$status', '$dok_foto', $published_at_sql)";
if ($conn->query($query)) {
$article_id = $conn->insert_id;
// Handle tags
if (isset($_POST['tags']) && is_array($_POST['tags'])) {
foreach ($_POST['tags'] as $tag_id) {
$tag_id = (int)$tag_id;
$conn->query("INSERT INTO article_tags (article_id, tag_id) VALUES ($article_id, $tag_id)");
}
}
redirect('articles.php?msg=added');
} else {
$error = "Error: " . $conn->error;
}
}
// Definisikan variabel $article untuk form
$article = [
'slug' => '',
'url_hash' => '',
'title' => '',
'content' => '',
'excerpt' => '',
'featured_image' => '',
'dok_foto' => 'dok. kontributor'
];
?>
<style>
/* Custom styles untuk form artikel */
.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);
}
/* ========== PERBAIKAN: CSS untuk CKEditor agar tidak menyempit ========== */
.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-editor__main {
width: 100% !important;
}
.ck.ck-toolbar {
border: none !important;
border-bottom: 2px solid #e9ecef !important;
background: #f8f9fc !important;
padding: 10px !important;
}
.ck.ck-button {
border-radius: 8px !important;
transition: all 0.2s !important;
}
.ck.ck-button:hover {
background: #e9ecef !important;
transform: translateY(-2px);
}
/* Pastikan editor tidak menyempit di dalam col-lg-8 */
.col-lg-8 .ck-editor {
width: 100% !important;
max-width: 100% !important;
}
/* Featured image preview */
.featured-image-preview {
position: relative;
border-radius: 15px;
overflow: hidden;
border: 2px solid #e9ecef;
transition: all 0.3s;
}
.featured-image-preview:hover {
border-color: #4e73df;
transform: scale(1.02);
}
.featured-image-preview img {
width: 100%;
height: 200px;
object-fit: cover;
}
.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;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
transition: all 0.3s;
opacity: 0;
}
.featured-image-preview:hover .remove-image {
opacity: 1;
}
.featured-image-preview .remove-image:hover {
background: #dc3545;
transform: scale(1.1);
}
/* Form sidebar */
.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;
transition: all 0.3s;
}
.sidebar-card:hover {
box-shadow: 0 10px 30px rgba(0,0,0,0.1);
}
.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;
font-size: 1rem;
letter-spacing: 0.5px;
}
.sidebar-card .card-body {
padding: 20px;
}
/* Tag container */
.tag-container {
max-height: 200px;
overflow-y: auto;
padding: 10px;
background: #f8f9fc;
border-radius: 10px;
}
.tag-container .form-check {
padding: 8px 12px;
margin: 0;
border-radius: 8px;
transition: all 0.2s;
}
.tag-container .form-check:hover {
background: #e9ecef;
}
.tag-container .form-check-input:checked ~ .form-check-label {
color: #4e73df;
font-weight: 600;
}
/* Publish button */
.btn-publish {
background: linear-gradient(135deg, #1cc88a 0%, #13855c 100%);
color: white;
border: none;
padding: 15px;
font-weight: 600;
letter-spacing: 0.5px;
transition: all 0.3s;
}
.btn-publish:hover {
transform: translateY(-3px);
box-shadow: 0 10px 20px rgba(28, 200, 138, 0.3);
}
.btn-draft {
background: linear-gradient(135deg, #858796 0%, #6b6d7d 100%);
color: white;
border: none;
padding: 15px;
font-weight: 600;
letter-spacing: 0.5px;
transition: all 0.3s;
}
.btn-draft:hover {
transform: translateY(-3px);
box-shadow: 0 10px 20px rgba(133, 135, 150, 0.3);
}
/* Character counter */
.char-counter {
position: absolute;
bottom: 10px;
right: 10px;
background: rgba(78, 115, 223, 0.1);
padding: 5px 10px;
border-radius: 20px;
font-size: 0.8rem;
color: #4e73df;
font-weight: 600;
}
/* Loading overlay */
.loading-overlay {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(255,255,255,0.9);
display: none;
justify-content: center;
align-items: center;
z-index: 9999;
}
.loading-overlay.active {
display: flex;
}
.loading-spinner {
width: 60px;
height: 60px;
border: 5px solid #e9ecef;
border-top-color: #4e73df;
border-radius: 50%;
animation: spin 1s linear infinite;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
/* Sembunyikan textarea asli setelah CKEditor aktif */
textarea#content-editor {
display: none;
}
</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">Tulis Artikel Baru</h1>
<div>
<a href="articles.php" class="btn btn-secondary me-2">
<i class="fas fa-arrow-left me-2"></i>Kembali
</a>
</div>
</div>
<?php if (isset($error)): ?>
<div class="alert alert-danger animate__animated animate__shakeX"><?php echo $error; ?></div>
<?php endif; ?>
<form method="POST" enctype="multipart/form-data" id="articleForm">
<div class="row">
<!-- Main Content Column -->
<div class="col-lg-8">
<div class="card shadow mb-4">
<div class="card-body p-4">
<!-- Title -->
<div class="mb-3">
<label class="form-label fw-bold fs-6">
<i class="fas fa-heading me-2 text-primary"></i>Judul Artikel <span class="text-danger">*</span>
</label>
<input type="text" class="form-control form-control-lg" name="title" id="titleInput"
placeholder="Masukkan judul artikel yang menarik..." required>
<small class="text-muted">Judul yang baik akan menarik lebih banyak pembaca</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 Artikel <span class="text-danger">*</span>
</label>
<div class="editor-wrapper">
<textarea id="content-editor" name="content" rows="20" required></textarea>
</div>
<small class="text-muted mt-2 d-block">
<i class="fas fa-info-circle me-1"></i>Gunakan toolbar di atas untuk memformat konten
</small>
</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>
<div class="position-relative">
<textarea class="form-control" name="excerpt" rows="4"
placeholder="Tulis ringkasan singkat artikel (max 300 karakter)..."
maxlength="300" id="excerpt"></textarea>
<div class="char-counter" id="charCounter">0/300</div>
</div>
<small class="text-muted">Ringkasan akan muncul di halaman daftar artikel dan hasil pencarian</small>
</div>
<!-- Slug field -->
<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']; ?>/artikel/</span>
<input type="text" class="form-control" name="slug" id="slug"
value="<?php echo $article['slug'] ?? ''; ?>"
placeholder="nama-artikel-anda">
</div>
<small class="text-muted">Biarkan kosong untuk generate otomatis dari judul</small>
</div>
</div>
</div>
</div>
<!-- Sidebar Column -->
<div class="col-lg-4">
<div class="form-sidebar">
<!-- Publish Box -->
<div class="sidebar-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" id="status">
<option value="draft">Draft (Simpan sebagai konsep)</option>
<option value="published">Publish (Langsung terbit)</option>
</select>
</div>
<div class="d-grid gap-2">
<button type="button" name="action" value="publish" class="btn btn-publish" id="publishBtn">
<i class="fas fa-paper-plane me-2"></i>Terbitkan Artikel
</button>
<button type="button" class="btn btn-draft" id="draftBtn">
<i class="fas fa-save me-2"></i>Simpan sebagai Draft
</button>
</div>
<hr class="my-3">
<div class="small text-muted">
<i class="fas fa-clock me-1"></i>Terakhir disimpan: -
</div>
</div>
</div>
<!-- Category Box -->
<div class="sidebar-card">
<div class="card-header">
<h6><i class="fas fa-folder me-2"></i>Kategori</h6>
</div>
<div class="card-body">
<select class="form-select" name="category_id" required>
<option value="">Pilih Kategori</option>
<?php while($cat = $categories->fetch_assoc()): ?>
<option value="<?php echo $cat['id']; ?>"><?php echo $cat['name']; ?></option>
<?php endwhile; ?>
</select>
</div>
</div>
<!-- Tags Box -->
<div class="sidebar-card">
<div class="card-header">
<h6><i class="fas fa-tags me-2"></i>Tag</h6>
</div>
<div class="card-body">
<div class="tag-container">
<?php
$tags->data_seek(0);
while($tag = $tags->fetch_assoc()):
?>
<div class="form-check">
<input class="form-check-input" type="checkbox" name="tags[]"
value="<?php echo $tag['id']; ?>" id="tag<?php echo $tag['id']; ?>">
<label class="form-check-label" for="tag<?php echo $tag['id']; ?>">
<?php echo $tag['name']; ?>
</label>
</div>
<?php endwhile; ?>
</div>
</div>
</div>
<!-- Featured Image Box -->
<div class="sidebar-card">
<div class="card-header">
<h6><i class="fas fa-image me-2"></i>Gambar Utama</h6>
</div>
<div class="card-body">
<div class="featured-image-upload">
<input type="file" class="form-control" name="featured_image"
accept="image/*" id="featuredImage">
<small class="text-muted d-block mt-2">
<i class="fas fa-info-circle me-1"></i>Format: JPG, PNG, GIF. Maks 2MB
</small>
</div>
<!-- Preview Image -->
<div class="featured-image-preview mt-3" id="imagePreview" style="display: none;">
<img src="" alt="Preview" id="previewImg">
<button type="button" class="remove-image" onclick="removeImage()">
<i class="fas fa-times"></i>
</button>
</div>
</div>
</div>
<!-- Juru Foto Box -->
<div class="sidebar-card">
<div class="card-header">
<h6><i class="fas fa-user me-2"></i>Juru Foto</h6>
</div>
<div class="card-body">
<input type="text" class="form-control" name="dok_foto" id="dokFoto"
value="dok. kontributor" required>
</div>
</div>
<!-- SEO Preview Box -->
<div class="sidebar-card">
<div class="card-header">
<h6><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">www.instansi.go.id/</div>
<div class="fw-bold text-success" id="seoTitle">Judul Artikel</div>
<div class="small text-muted mt-1" id="seoDesc">Ringkasan artikel akan muncul di sini...</div>
</div>
</div>
</div>
</div>
</div>
</div>
</form>
</div>
<!-- Loading Overlay -->
<div class="loading-overlay" id="loadingOverlay">
<div class="loading-spinner"></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>
// =====================================================
// INISIALISASI CKEDITOR (HANYA SEKALI)
// =====================================================
let editorReady = false;
// Hancurkan editor lama jika ada
if (window.editor) {
try { window.editor.destroy(); } catch(e) {}
window.editor = null;
}
// Hapus elemen CKEditor yang tertinggal
document.querySelectorAll('.ck-editor, .ck-reset, .ck-body').forEach(el => el.remove());
// Inisialisasi CKEditor
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 artikel Anda di sini...',
removePlugins: ['Title', 'ImageUpload', 'CKFinderUploadAdapter', 'EasyImage', 'CKFinder']
})
.then(editor => {
window.editor = editor;
editorReady = true;
// Sinkronisasi otomatis ke textarea
editor.model.document.on('change:data', () => {
document.querySelector('#content-editor').value = editor.getData();
});
console.log('CKEditor siap');
})
.catch(error => {
console.error('CKEditor error:', error);
});
// =====================================================
// HANDLE FORM SUBMIT
// =====================================================
const form = document.getElementById('articleForm');
const publishBtn = document.getElementById('publishBtn');
const draftBtn = document.getElementById('draftBtn');
const statusSelect = document.getElementById('status');
// Publish button
if (publishBtn) {
publishBtn.addEventListener('click', function(e) {
e.preventDefault();
statusSelect.value = 'published';
// Sinkronkan CKEditor
if (window.editor && editorReady) {
document.querySelector('#content-editor').value = window.editor.getData();
}
form.submit();
});
}
// Draft button
if (draftBtn) {
draftBtn.addEventListener('click', function(e) {
e.preventDefault();
statusSelect.value = 'draft';
// Sinkronkan CKEditor
if (window.editor && editorReady) {
document.querySelector('#content-editor').value = window.editor.getData();
}
form.submit();
});
}
// Form submit handler
if (form) {
form.onsubmit = function() {
// Sinkronkan CKEditor ke textarea
if (window.editor && editorReady) {
document.querySelector('#content-editor').value = window.editor.getData();
}
// Tampilkan loading
const loading = document.getElementById('loadingOverlay');
if (loading) loading.classList.add('active');
return true;
};
}
// =====================================================
// FUNGSI LAINNYA
// =====================================================
// Character counter
const excerptInput = document.getElementById('excerpt');
const charCounter = document.getElementById('charCounter');
if (excerptInput && charCounter) {
excerptInput.addEventListener('input', function() {
const len = this.value.length;
charCounter.textContent = len + '/300';
charCounter.style.color = len > 280 ? '#e74a3b' : (len > 250 ? '#f6c23e' : '#4e73df');
});
}
// SEO Preview
const titleInput = document.getElementById('titleInput');
if (titleInput) {
titleInput.addEventListener('input', function() {
document.getElementById('seoTitle').textContent = this.value || 'Judul Artikel';
});
excerptInput.addEventListener('input', function() {
document.getElementById('seoDesc').textContent = this.value || 'Ringkasan artikel akan muncul di sini...';
});
}
// Featured image preview
const featuredImage = document.getElementById('featuredImage');
if (featuredImage) {
featuredImage.addEventListener('change', function(e) {
const file = e.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function(e) {
let preview = document.getElementById('imagePreview');
if (preview) {
document.getElementById('previewImg').src = e.target.result;
preview.style.display = 'block';
}
};
reader.readAsDataURL(file);
}
});
}
window.removeImage = function() {
document.getElementById('featuredImage').value = '';
const preview = document.getElementById('imagePreview');
if (preview) preview.style.display = 'none';
};
// Auto generate slug
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, '');
slugInput.value = slug;
});
}
console.log('✅ Page ready');
</script>
<?php require_once 'includes/footer.php'; ?>