| 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_29052026/admin/ |
Upload File : |
<?php
$page_title = 'Kategori';
require_once 'config/database.php';
require_once 'includes/header.php';
require_once 'includes/init.php';
// Handle add category
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['add'])) {
$name = sanitize($_POST['name']);
$description = sanitize($_POST['description']);
$slug = strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $name)));
$conn->query("INSERT INTO categories (name, slug, description) VALUES ('$name', '$slug', '$description')");
redirect('categories.php?msg=added');
}
// Handle edit category
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['edit'])) {
$id = (int)$_POST['id'];
$name = sanitize($_POST['name']);
$description = sanitize($_POST['description']);
$slug = strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $name)));
$conn->query("UPDATE categories SET name = '$name', slug = '$slug', description = '$description' WHERE id = $id");
redirect('categories.php?msg=updated');
}
// Handle delete
if (isset($_GET['delete'])) {
$id = (int)$_GET['delete'];
$conn->query("DELETE FROM categories WHERE id = $id");
redirect('categories.php?msg=deleted');
}
// Get all categories
$categories = $conn->query("SELECT * FROM categories ORDER BY name");
// Get category for edit
$edit_category = null;
if (isset($_GET['edit'])) {
$id = (int)$_GET['edit'];
$result = $conn->query("SELECT * FROM categories WHERE id = $id");
$edit_category = $result->fetch_assoc();
}
$msg = '';
if (isset($_GET['msg'])) {
if ($_GET['msg'] == 'added') $msg = '<div class="alert alert-success">Kategori berhasil ditambahkan!</div>';
if ($_GET['msg'] == 'updated') $msg = '<div class="alert alert-success">Kategori berhasil diupdate!</div>';
if ($_GET['msg'] == 'deleted') $msg = '<div class="alert alert-success">Kategori berhasil dihapus!</div>';
}
?>
<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">Manajemen Kategori</h1>
</div>
<?php echo $msg; ?>
<div class="row">
<!-- Add Category Form -->
<div class="col-md-4">
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-primary">
<?php echo $edit_category ? 'Edit Kategori' : 'Tambah Kategori Baru'; ?>
</h6>
</div>
<div class="card-body">
<form method="POST">
<?php if ($edit_category): ?>
<input type="hidden" name="id" value="<?php echo $edit_category['id']; ?>">
<?php endif; ?>
<div class="mb-3">
<label class="form-label">Nama Kategori <span class="text-danger">*</span></label>
<input type="text" class="form-control" name="name" value="<?php echo $edit_category['name'] ?? ''; ?>" required>
</div>
<div class="mb-3">
<label class="form-label">Deskripsi</label>
<textarea class="form-control" name="description" rows="3"><?php echo $edit_category['description'] ?? ''; ?></textarea>
</div>
<button type="submit" name="<?php echo $edit_category ? 'edit' : 'add'; ?>" class="btn btn-primary">
<i class="fas fa-save me-2"></i><?php echo $edit_category ? 'Update' : 'Simpan'; ?>
</button>
<?php if ($edit_category): ?>
<a href="categories.php" class="btn btn-secondary">
<i class="fas fa-times me-2"></i>Batal
</a>
<?php endif; ?>
</form>
</div>
</div>
</div>
<!-- Categories List -->
<div class="col-md-8">
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-primary">Daftar Kategori</h6>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-hover">
<thead>
<tr>
<th>Nama</th>
<th>Slug</th>
<th>Deskripsi</th>
<th>Jumlah Artikel</th>
<th>Aksi</th>
</tr>
</thead>
<tbody>
<?php while($cat = $categories->fetch_assoc()):
$count = $conn->query("SELECT COUNT(*) as total FROM articles WHERE category_id = " . $cat['id'])->fetch_assoc()['total'];
?>
<tr>
<td><strong><?php echo $cat['name']; ?></strong></td>
<td><?php echo $cat['slug']; ?></td>
<td><?php echo substr($cat['description'], 0, 50) . '...'; ?></td>
<td><span class="badge bg-info"><?php echo $count; ?></span></td>
<td>
<a href="?edit=<?php echo $cat['id']; ?>" class="btn btn-sm btn-info">
<i class="fas fa-edit"></i>
</a>
<a href="?delete=<?php echo $cat['id']; ?>" class="btn btn-sm btn-danger" onclick="return confirm('Yakin ingin menghapus?')">
<i class="fas fa-trash"></i>
</a>
</td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<?php require_once 'includes/footer.php'; ?>