| 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/ |
Upload File : |
<?php
/**
* Halaman Daftar Berita
*/
// Load inisialisasi
require_once 'includes/init.php';
// Set page variables
$page_title = 'Berita Terbaru - ' . ($settings['site_title'] ?? 'Website Instansi');
$meta_description = 'Daftar berita dan artikel terbaru dari instansi';
// =====================================================
// HANDLE SEARCH & FILTER KATEGORI
// =====================================================
$search_keyword = isset($_GET['search']) ? trim($_GET['search']) : '';
$category_filter = isset($_GET['category']) ? (int)$_GET['category'] : 0;
$category_slug = isset($_GET['slug']) ? trim($_GET['slug']) : '';
$selected_category_name = '';
// Jika ada slug, cari ID kategori
if (!empty($category_slug)) {
$cat_query = $conn->query("SELECT id, name FROM categories WHERE slug = '$category_slug'");
if ($cat_query && $cat_query->num_rows > 0) {
$cat_data = $cat_query->fetch_assoc();
$category_filter = $cat_data['id'];
$selected_category_name = $cat_data['name'];
}
}
// Sanitasi untuk tampilan
$search_display = htmlspecialchars($search_keyword, ENT_QUOTES, 'UTF-8');
// Pagination
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$limit = 9;
$offset = ($page - 1) * $limit;
// =====================================================
// BUILD WHERE CLAUSE
// =====================================================
$where_clause = "a.status = 'published'";
$params = [];
$types = "";
// Filter berdasarkan kata kunci
if (!empty($search_keyword)) {
$search_param = "%$search_keyword%";
$where_clause .= " AND (a.title LIKE ? OR a.content LIKE ? OR a.excerpt LIKE ?)";
$params[] = $search_param;
$params[] = $search_param;
$params[] = $search_param;
$types .= "sss";
}
// Filter berdasarkan kategori
if ($category_filter > 0) {
$where_clause .= " AND a.category_id = ?";
$params[] = $category_filter;
$types .= "i";
}
// =====================================================
// HITUNG TOTAL ARTICLES
// =====================================================
if (!empty($params)) {
$count_sql = "SELECT COUNT(*) as total FROM articles a WHERE $where_clause";
$count_stmt = $conn->prepare($count_sql);
if ($count_stmt) {
$count_stmt->bind_param($types, ...$params);
$count_stmt->execute();
$total_result = $count_stmt->get_result();
$total = $total_result->fetch_assoc()['total'];
$count_stmt->close();
} else {
$total = 0;
}
} else {
$total_result = $conn->query("SELECT COUNT(*) as total FROM articles a WHERE $where_clause");
$total = $total_result->fetch_assoc()['total'];
}
$total_pages = ceil($total / $limit);
// =====================================================
// AMBIL ARTICLES
// =====================================================
if (!empty($params)) {
$sql = "
SELECT a.*, c.name as category_name, c.slug as category_slug, 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.published_at DESC
LIMIT $offset, $limit
";
$stmt = $conn->prepare($sql);
if ($stmt) {
$stmt->bind_param($types, ...$params);
$stmt->execute();
$articles = $stmt->get_result();
$stmt->close();
} else {
$articles = false;
}
} else {
$articles = $conn->query("
SELECT a.*, c.name as category_name, c.slug as category_slug, 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 a.status = 'published'
ORDER BY a.published_at DESC
LIMIT $offset, $limit
");
}
$search_has_results = ($articles && $articles->num_rows > 0);
// =====================================================
// AMBIL SEMUA KATEGORI UNTUK FILTER (DENGAN JUMLAH ARTIKEL)
// =====================================================
$categories = $conn->query("
SELECT c.*, COUNT(a.id) as article_count
FROM categories c
LEFT JOIN articles a ON a.category_id = c.id AND a.status = 'published'
GROUP BY c.id
ORDER BY c.name ASC
");
// Include header
include 'includes/header.php';
?>
<style>
/* ============================================
BERITA PAGE STYLES
Prefix: art-
============================================ */
/* Search Widget */
.art-search-widget {
background: white;
border-radius: 20px;
padding: 24px;
margin-bottom: 30px;
border: 1px solid #eef2f6;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
}
.art-search-title {
font-size: 1.1rem;
font-weight: 700;
color: #1e293b;
margin-bottom: 20px;
display: flex;
align-items: center;
gap: 10px;
}
.art-search-title i {
color: #3b82f6;
}
.art-search-title .art-title-line {
flex: 1;
height: 2px;
background: linear-gradient(90deg, #e2e8f0, transparent);
}
.art-search-form {
display: flex;
gap: 10px;
flex-wrap: wrap;
}
.art-search-input-group {
flex: 1;
position: relative;
}
.art-search-input-group input,
.art-search-input-group select {
width: 100%;
padding: 12px 16px;
border: 1px solid #e2e8f0;
border-radius: 12px;
font-size: 0.9rem;
transition: all 0.3s;
background: white;
}
.art-search-input-group input:focus,
.art-search-input-group select:focus {
outline: none;
border-color: #3b82f6;
box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}
.art-search-submit {
padding: 0 24px;
background: #3b82f6;
border: none;
border-radius: 12px;
color: white;
cursor: pointer;
transition: all 0.3s;
font-weight: 600;
}
.art-search-submit:hover {
background: #1d4ed8;
}
.art-search-reset {
padding: 0 20px;
background: #f1f5f9;
border: 1px solid #e2e8f0;
border-radius: 12px;
color: #64748b;
cursor: pointer;
transition: all 0.3s;
text-decoration: none;
display: inline-flex;
align-items: center;
gap: 6px;
}
.art-search-reset:hover {
background: #e2e8f0;
color: #1e293b;
}
/* Search Info */
.art-search-info {
background: #eff6ff;
border-radius: 12px;
padding: 12px 20px;
margin-bottom: 24px;
display: flex;
justify-content: space-between;
align-items: center;
flex-wrap: wrap;
gap: 10px;
}
.art-search-info-text {
color: #1d4ed8;
font-size: 0.9rem;
}
.art-search-info-text i {
margin-right: 8px;
}
.art-search-info-text strong {
font-weight: 700;
}
/* Categories Grid (seperti di index.php) */
.art-categories-grid {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 24px;
margin-bottom: 40px;
}
.art-category-card {
background: white;
border-radius: 20px;
padding: 30px 20px;
text-align: center;
transition: all 0.3s ease;
border: 1px solid #eef2f6;
text-decoration: none;
display: block;
}
.art-category-card:hover {
transform: translateY(-6px);
box-shadow: 0 20px 30px -12px rgba(0, 0, 0, 0.12);
border-color: #3b82f6;
}
.art-category-icon {
width: 60px;
height: 60px;
background: linear-gradient(135deg, #dbeafe 0%, #eff6ff 100%);
border-radius: 30px;
display: flex;
align-items: center;
justify-content: center;
margin: 0 auto 16px;
transition: all 0.3s;
}
.art-category-card:hover .art-category-icon {
background: linear-gradient(135deg, #3b82f6 0%, #1d4ed8 100%);
}
.art-category-icon i {
font-size: 1.5rem;
color: #3b82f6;
transition: all 0.3s;
}
.art-category-card:hover .art-category-icon i {
color: white;
}
.art-category-name {
font-size: 1rem;
font-weight: 700;
margin: 0 0 8px 0;
color: #1e293b;
}
.art-category-count {
font-size: 0.75rem;
color: #64748b;
margin: 0;
}
.art-category-card:hover .art-category-name,
.art-category-card:hover .art-category-count {
color: #3b82f6;
}
/* Active Category */
.art-category-card.active {
border-color: #3b82f6;
background: #eff6ff;
}
.art-category-card.active .art-category-icon {
background: linear-gradient(135deg, #3b82f6 0%, #1d4ed8 100%);
}
.art-category-card.active .art-category-icon i {
color: white;
}
.art-category-card.active .art-category-name {
color: #3b82f6;
}
/* News Card */
.news-card {
background: white;
border-radius: 20px;
overflow: hidden;
transition: all 0.3s;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
border: 1px solid #eef2f6;
height: 100%;
}
.news-card:hover {
transform: translateY(-4px);
box-shadow: 0 20px 30px -12px rgba(0, 0, 0, 0.1);
}
.news-image {
position: relative;
height: 200px;
overflow: hidden;
}
.news-image img {
width: 100%;
height: 100%;
object-fit: cover;
transition: transform 0.5s;
}
.news-card:hover .news-image img {
transform: scale(1.05);
}
.news-category {
position: absolute;
top: 15px;
left: 15px;
background: #3b82f6;
color: white;
padding: 4px 12px;
border-radius: 20px;
font-size: 0.7rem;
font-weight: 600;
}
.news-content {
padding: 20px;
}
.news-meta {
font-size: 0.8rem;
color: #6c7a8e;
display: flex;
flex-wrap: wrap;
gap: 12px;
}
.news-title {
font-size: 1.1rem;
font-weight: 700;
line-height: 1.4;
margin: 12px 0;
}
.news-title a {
color: #1a2c3e;
transition: color 0.3s;
text-decoration: none;
}
.news-title a:hover {
color: #3b82f6;
}
.news-excerpt {
font-size: 0.85rem;
color: #64748b;
line-height: 1.5;
margin-bottom: 16px;
}
.btn-link {
color: #3b82f6;
font-weight: 600;
font-size: 0.85rem;
text-decoration: none;
display: inline-flex;
align-items: center;
gap: 6px;
transition: gap 0.3s;
}
.btn-link:hover {
gap: 10px;
color: #1d4ed8;
}
/* Pagination */
.pagination {
gap: 5px;
}
.page-item .page-link {
border-radius: 10px;
color: #4a5568;
border: 1px solid #e2e8f0;
padding: 8px 16px;
}
.page-item.active .page-link {
background: #3b82f6;
border-color: #3b82f6;
color: white;
}
.page-item.disabled .page-link {
color: #cbd5e1;
}
/* Empty State */
.empty-state {
text-align: center;
padding: 60px 20px;
background: #f8fafc;
border-radius: 20px;
}
.empty-state i {
font-size: 4rem;
color: #cbd5e1;
margin-bottom: 20px;
}
.empty-state h3 {
font-size: 1.25rem;
font-weight: 600;
color: #1a2c3e;
margin-bottom: 10px;
}
.empty-state p {
color: #6c7a8e;
}
mark {
background: #fef08a;
padding: 0 2px;
border-radius: 4px;
}
/* Responsive */
@media (max-width: 1024px) {
.art-categories-grid {
grid-template-columns: repeat(2, 1fr);
gap: 16px;
}
}
@media (max-width: 768px) {
.art-search-form {
flex-direction: column;
}
.art-search-submit,
.art-search-reset {
padding: 10px 20px;
justify-content: center;
}
.art-search-info {
flex-direction: column;
text-align: center;
}
.news-image {
height: 180px;
}
.art-categories-grid {
grid-template-columns: repeat(2, 1fr);
gap: 16px;
}
.art-category-card {
padding: 20px 15px;
}
.news-meta {
flex-direction: column;
gap: 4px;
}
}
@media (max-width: 576px) {
.art-categories-grid {
grid-template-columns: repeat(2, 1fr);
gap: 12px;
}
.art-category-icon {
width: 45px;
height: 45px;
}
.art-category-icon i {
font-size: 1.2rem;
}
.art-category-name {
font-size: 0.85rem;
}
.news-title {
font-size: 1rem;
}
.news-excerpt {
font-size: 0.8rem;
}
}
</style>
<div class="art-section">
<!-- Header -->
<div class="art-header-with-lines row mx-auto">
<h2 class="art-title-left">Berita Kita</h2>
<div class="art-line-left-only"></div>
</div>
<!-- ===================================================== -->
<!-- CARD PENCARIAN BERITA (DENGAN FILTER KATEGORI) -->
<!-- ===================================================== -->
<div class="art-search-widget">
<div class="art-search-title">
<i class="fas fa-search"></i>
<span>Cari Berita & Artikel</span>
<div class="art-title-line"></div>
</div>
<form action="berita.php" method="GET" class="art-search-form">
<div class="art-search-input-group" style="flex: 2;">
<input type="text" name="search" placeholder="Cari berita atau artikel..."
value="<?php echo $search_display; ?>">
</div>
<div class="art-search-input-group" style="flex: 1;">
<select name="slug">
<option value="">Semua Kategori</option>
<?php
$categories->data_seek(0);
while($cat = $categories->fetch_assoc()):
$selected = ($category_slug == $cat['slug']) ? 'selected' : '';
?>
<option value="<?php echo $cat['slug']; ?>" <?php echo $selected; ?>>
<?php echo htmlspecialchars($cat['name']); ?> (<?php echo $cat['article_count']; ?>)
</option>
<?php endwhile; ?>
</select>
</div>
<button type="submit" class="art-search-submit">
<i class="fas fa-search me-2"></i>Cari
</button>
<?php if (!empty($search_keyword) || $category_filter > 0): ?>
<a href="berita.php" class="art-search-reset">
<i class="fas fa-times me-1"></i> Reset
</a>
<?php endif; ?>
</form>
</div>
<!-- Search Info (menampilkan hasil pencarian) -->
<?php if (!empty($search_keyword) || $category_filter > 0): ?>
<div class="art-search-info">
<div class="art-search-info-text">
<i class="fas fa-search"></i>
Menampilkan hasil
<?php if (!empty($search_keyword)): ?>
pencarian untuk: <strong>"<?php echo $search_display; ?>"</strong>
<?php endif; ?>
<?php if (!empty($selected_category_name)): ?>
<?php if (!empty($search_keyword)): ?> dan <?php endif; ?>
kategori: <strong><?php echo htmlspecialchars($selected_category_name); ?></strong>
<?php endif; ?>
</div>
<div class="art-search-info-text">
<i class="fas fa-file-alt"></i>
Ditemukan <strong><?php echo $total; ?></strong> berita
</div>
</div>
<?php endif; ?>
<!-- ===================================================== -->
<!-- DAFTAR KATEGORI (FILTER CEPAT) -->
<!-- ===================================================== -->
<!-- Articles List -->
<section class="articles-list">
<div class="container" style="padding: 0; margin-top: -80px">
<?php if ($search_has_results): ?>
<div class="row">
<?php while($article = $articles->fetch_assoc()):
$article_url = article_url($article);
?>
<div class="col-lg-4 col-md-6 mb-4" data-aos="fade-up">
<div class="news-card h-100">
<div class="news-image">
<?php if(!empty($article['featured_image'])): ?>
<img src="<?php echo $article['featured_image']; ?>" alt="<?php echo htmlspecialchars($article['title']); ?>">
<?php else: ?>
<img src="assets/images/news-default.png" alt="Default News Image">
<?php endif; ?>
<div class="news-category"><?php echo htmlspecialchars($article['category_name'] ?? 'Umum'); ?></div>
</div>
<div class="news-content">
<div class="news-meta mb-2">
<span><i class="far fa-calendar me-1"></i> <?php echo tgl_indonesia($article['published_at']); ?></span>
<span><i class="far fa-user me-1"></i> <?php echo htmlspecialchars($article['author_name']); ?></span>
</div>
<h3 class="news-title">
<a href="<?php echo $article_url; ?>">
<?php
$title = htmlspecialchars($article['title']);
if (!empty($search_keyword)) {
$title = preg_replace("/($search_display)/i", "<mark>$1</mark>", $title);
}
echo $title;
?>
</a>
</h3>
<p class="news-excerpt">
<?php
$excerpt = substr($article['excerpt'] ?? strip_tags($article['content']), 0, 100) . '...';
if (!empty($search_keyword)) {
$excerpt = preg_replace("/($search_display)/i", "<mark>$1</mark>", $excerpt);
}
echo $excerpt;
?>
</p>
<a href="<?php echo $article_url; ?>" class="btn-link">
Baca Selengkapnya <i class="fas fa-arrow-right ms-2"></i>
</a>
</div>
</div>
</div>
<?php endwhile; ?>
</div>
<!-- Pagination -->
<?php if ($total_pages > 1): ?>
<nav aria-label="Page navigation" class="mt-5">
<ul class="pagination justify-content-center">
<li class="page-item <?php echo $page <= 1 ? 'disabled' : ''; ?>">
<a class="page-link" href="?page=<?php echo $page-1; ?>&search=<?php echo urlencode($search_keyword); ?>&slug=<?php echo urlencode($category_slug); ?>">
<i class="fas fa-chevron-left"></i> Sebelumnya
</a>
</li>
<?php
$start_page = max(1, $page - 2);
$end_page = min($total_pages, $page + 2);
for($i = $start_page; $i <= $end_page; $i++):
?>
<li class="page-item <?php echo $i == $page ? 'active' : ''; ?>">
<a class="page-link" href="?page=<?php echo $i; ?>&search=<?php echo urlencode($search_keyword); ?>&slug=<?php echo urlencode($category_slug); ?>"><?php echo $i; ?></a>
</li>
<?php endfor; ?>
<li class="page-item <?php echo $page >= $total_pages ? 'disabled' : ''; ?>">
<a class="page-link" href="?page=<?php echo $page+1; ?>&search=<?php echo urlencode($search_keyword); ?>&slug=<?php echo urlencode($category_slug); ?>">
Selanjutnya <i class="fas fa-chevron-right"></i>
</a>
</li>
</ul>
</nav>
<?php endif; ?>
<?php else: ?>
<div class="empty-state">
<?php if (!empty($search_keyword) || $category_filter > 0): ?>
<i class="fas fa-search"></i>
<h3>Tidak Ada Hasil Pencarian</h3>
<p class="text-muted">
<?php if (!empty($search_keyword)): ?>
Tidak ditemukan berita dengan kata kunci <strong>"<?php echo $search_display; ?>"</strong>
<?php endif; ?>
<?php if (!empty($selected_category_name)): ?>
<?php if (!empty($search_keyword)): ?> dan <?php endif; ?>
pada kategori <strong><?php echo htmlspecialchars($selected_category_name); ?></strong>
<?php endif; ?>
</p>
<a href="berita.php" class="btn btn-primary mt-3">
<i class="fas fa-arrow-left me-2"></i>Lihat Semua Berita
</a>
<?php else: ?>
<i class="fas fa-newspaper"></i>
<h3>Belum Ada Berita</h3>
<p class="text-muted">Belum ada berita yang dipublikasikan.</p>
<?php endif; ?>
</div>
<?php endif; ?>
</div>
</section>
</div>
<?php
// Include footer
include 'includes/footer.php';
?>