| 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_29052026/admin/ |
Upload File : |
<?php
$page_title = 'Daftar Artikel';
require_once 'config/database.php';
require_once 'includes/header.php';
require_once 'includes/init.php';
// =====================================================
// HANDLE DELETE
// =====================================================
if (isset($_GET['delete'])) {
$id = (int)$_GET['delete'];
// Ambil gambar untuk dihapus
$article = $conn->query("SELECT featured_image FROM articles WHERE id = $id")->fetch_assoc();
if ($article && !empty($article['featured_image']) && file_exists("../" . $article['featured_image'])) {
unlink("../" . $article['featured_image']);
}
$conn->query("DELETE FROM articles WHERE id = $id");
redirect('articles.php?msg=deleted');
}
// =====================================================
// HANDLE BULK ACTION
// =====================================================
if (isset($_POST['bulk_action']) && isset($_POST['selected'])) {
$action = $_POST['bulk_action'];
$selected = $_POST['selected'];
$ids = implode(',', array_map('intval', $selected));
if ($action == 'publish') {
$conn->query("UPDATE articles SET status = 'published', published_at = NOW() WHERE id IN ($ids)");
$msg = '<div class="alert alert-success">Artikel berhasil dipublikasikan!</div>';
} elseif ($action == 'draft') {
$conn->query("UPDATE articles SET status = 'draft' WHERE id IN ($ids)");
$msg = '<div class="alert alert-success">Artikel berhasil diubah ke draft!</div>';
} elseif ($action == 'delete') {
// Hapus gambar-gambar terkait
$articles = $conn->query("SELECT featured_image FROM articles WHERE id IN ($ids)");
while($article = $articles->fetch_assoc()) {
if (!empty($article['featured_image']) && file_exists("../" . $article['featured_image'])) {
unlink("../" . $article['featured_image']);
}
}
$conn->query("DELETE FROM articles WHERE id IN ($ids)");
$msg = '<div class="alert alert-success">Artikel berhasil dihapus!</div>';
}
}
// =====================================================
// PAGINATION SETUP
// =====================================================
$limit = 10; // Jumlah artikel per halaman
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
if ($page < 1) $page = 1;
$offset = ($page - 1) * $limit;
// =====================================================
// SEARCH & FILTER
// =====================================================
$search = isset($_GET['search']) ? sanitize($_GET['search']) : '';
$category_filter = isset($_GET['category']) ? (int)$_GET['category'] : 0;
$status_filter = isset($_GET['status']) ? sanitize($_GET['status']) : '';
$author_filter = isset($_GET['author']) ? (int)$_GET['author'] : 0;
$date_from = isset($_GET['date_from']) ? sanitize($_GET['date_from']) : '';
$date_to = isset($_GET['date_to']) ? sanitize($_GET['date_to']) : '';
// Build WHERE clause
$where = ["1=1"];
if (!empty($search)) {
$where[] = "(a.title LIKE '%$search%' OR a.content LIKE '%$search%' OR a.excerpt LIKE '%$search%')";
}
if ($category_filter > 0) {
$where[] = "a.category_id = $category_filter";
}
if (!empty($status_filter)) {
$where[] = "a.status = '$status_filter'";
}
if ($author_filter > 0) {
$where[] = "a.author_id = $author_filter";
}
if (!empty($date_from)) {
$where[] = "DATE(a.created_at) >= '$date_from'";
}
if (!empty($date_to)) {
$where[] = "DATE(a.created_at) <= '$date_to'";
}
$where_clause = implode(' AND ', $where);
// =====================================================
// GET TOTAL RECORDS FOR PAGINATION
// =====================================================
$total_query = "SELECT COUNT(*) as total FROM articles a WHERE $where_clause";
$total_result = $conn->query($total_query);
$total_records = $total_result->fetch_assoc()['total'];
$total_pages = ceil($total_records / $limit);
// =====================================================
// GET ARTICLES WITH PAGINATION
// =====================================================
$articles = $conn->query("
SELECT a.*, c.name as category_name, u.full_name as author_name
FROM articles a
LEFT JOIN categories c ON a.category_id = c.id
LEFT JOIN users u ON a.author_id = u.id
WHERE $where_clause
ORDER BY a.created_at DESC
LIMIT $offset, $limit
");
// =====================================================
// GET FILTER DATA
// =====================================================
$categories = $conn->query("SELECT id, name FROM categories ORDER BY name");
$authors = $conn->query("SELECT id, full_name FROM users ORDER BY full_name");
// =====================================================
// MESSAGES
// =====================================================
$msg = '';
if (isset($_GET['msg'])) {
if ($_GET['msg'] == 'added') $msg = '<div class="alert alert-success">Artikel berhasil ditambahkan!</div>';
if ($_GET['msg'] == 'updated') $msg = '<div class="alert alert-success">Artikel berhasil diupdate!</div>';
if ($_GET['msg'] == 'deleted') $msg = '<div class="alert alert-success">Artikel berhasil dihapus!</div>';
}
if (isset($msg)) echo $msg;
?>
<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">Daftar Artikel Berita</h1>
<a href="article-add.php" class="btn btn-primary">
<i class="fas fa-plus me-2"></i>Tulis Artikel Baru
</a>
</div>
<?php echo $msg; ?>
<!-- Search and Filter -->
<div class="card shadow mb-4">
<div class="card-body">
<form method="GET" class="row g-3">
<div class="col-md-6">
<div class="input-group">
<input type="text" class="form-control" name="search" placeholder="Cari artikel..." value="<?php echo $search; ?>">
<button class="btn btn-primary" type="submit">
<i class="fas fa-search"></i> Cari
</button>
</div>
</div>
<div class="col-md-6 text-end">
<a href="articles.php" class="btn btn-secondary">
<i class="fas fa-sync-alt"></i> Reset
</a>
</div>
</form>
</div>
</div>
<!-- Articles Table -->
<div class="card shadow mb-4">
<div class="card-body">
<div class="table-responsive">
<table class="table table-hover">
<thead>
<tr>
<th>Judul</th>
<th>Kategori</th>
<th>Penulis</th>
<th>Status</th>
<th>Views</th>
<th>Tanggal</th>
<th>Aksi</th>
</tr>
</thead>
<tbody>
<?php if ($articles->num_rows > 0): ?>
<?php while($article = $articles->fetch_assoc()): ?>
<tr>
<td>
<strong><?php echo $article['title']; ?></strong>
</td>
<td><?php echo $article['category_name'] ?? '-'; ?></td>
<td><?php echo $article['author_name']; ?></td>
<td>
<span class="badge bg-<?php echo $article['status'] == 'published' ? 'success' : 'secondary'; ?>">
<?php echo $article['status']; ?>
</span>
</td>
<td><?php echo number_format($article['views']); ?></td>
<td><?php echo date('d M Y', strtotime($article['created_at'])); ?></td>
<td>
<a href="article-edit.php?id=<?php echo $article['id']; ?>" class="btn btn-sm btn-info">
<i class="fas fa-edit"></i>
</a>
<a href="?delete=<?php echo $article['id']; ?>" class="btn btn-sm btn-danger" onclick="return confirm('Yakin ingin menghapus?')">
<i class="fas fa-trash"></i>
</a>
</td>
</tr>
<?php endwhile; ?>
<?php else: ?>
<tr>
<td colspan="7" class="text-center">Tidak ada data</td>
</tr>
<?php endif; ?>
</tbody>
</table>
</div>
<!-- Pagination -->
<?php if ($total_pages > 1): ?>
<nav>
<ul class="pagination justify-content-center">
<?php for($i = 1; $i <= $total_pages; $i++): ?>
<li class="page-item <?php echo $i == $page ? 'active' : ''; ?>">
<a class="page-link" href="?page=<?php echo $i; ?>&search=<?php echo $search; ?>">
<?php echo $i; ?>
</a>
</li>
<?php endfor; ?>
</ul>
</nav>
<?php endif; ?>
</div>
</div>
</div>
<?php
// Helper function to build query string
function buildQueryString($exclude = []) {
$params = $_GET;
foreach($exclude as $key) {
unset($params[$key]);
}
if(empty($params)) return '';
return '&' . http_build_query($params);
}
require_once 'includes/footer.php';
?>