| 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 : /laragon4/www/bpkd_/admin/ |
Upload File : |
<?php
$page_title = 'Manajemen Menu';
require_once 'config/database.php';
require_once 'includes/init.php';
requireLogin();
// Cek apakah user adalah admin
if (!in_array($_SESSION['role'], ['superadmin','admin'])) {
redirect('dashboard.php');
}
// Get all menus
$menus = $conn->query("SELECT * FROM menus");
// Set active menu
$active_menu = isset($_GET['menu_id']) ? (int)$_GET['menu_id'] : 1;
// Cek apakah menu_id valid
if ($active_menu < 1) {
$active_menu = 1;
}
// Handle add menu item
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['add_item'])) {
$menu_id = $active_menu;
$title = sanitize($_POST['title']);
$url = sanitize($_POST['url']);
$parent_id = (int)$_POST['parent_id'];
$target = sanitize($_POST['target']);
$icon = sanitize($_POST['icon']);
$query = "INSERT INTO menu_items (menu_id, parent_id, title, url, target, icon)
VALUES ($menu_id, $parent_id, '$title', '$url', '$target', '$icon')";
if ($conn->query($query)) {
redirect("menus.php?menu_id=$menu_id&msg=added");
} else {
$error = "Error: " . $conn->error;
}
}
// Handle update menu order
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['update_order'])) {
if (isset($_POST['item']) && is_array($_POST['item'])) {
foreach ($_POST['item'] as $order => $id) {
$id = (int)$id;
$conn->query("UPDATE menu_items SET menu_order = $order WHERE id = $id");
}
redirect("menus.php?menu_id=$active_menu&msg=ordered");
}
}
// Handle delete
if (isset($_GET['delete'])) {
$id = (int)$_GET['delete'];
$conn->query("DELETE FROM menu_items WHERE id = $id");
redirect("menus.php?menu_id=$active_menu&msg=deleted");
}
// Handle edit menu item
$edit_item = null;
if (isset($_GET['edit'])) {
$edit_id = (int)$_GET['edit'];
$result = $conn->query("SELECT * FROM menu_items WHERE id = $edit_id");
if ($result) {
$edit_item = $result->fetch_assoc();
}
}
// Handle update menu item
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['update_item'])) {
$id = (int)$_POST['id'];
$title = sanitize($_POST['title']);
$url = sanitize($_POST['url']);
$parent_id = (int)$_POST['parent_id'];
$target = sanitize($_POST['target']);
$icon = sanitize($_POST['icon']);
$query = "UPDATE menu_items SET
title = '$title',
url = '$url',
parent_id = $parent_id,
target = '$target',
icon = '$icon'
WHERE id = $id";
if ($conn->query($query)) {
redirect("menus.php?menu_id=$active_menu&msg=updated");
} else {
$error = "Error: " . $conn->error;
}
}
// Get menu items for active menu
$menu_items = $conn->query("SELECT * FROM menu_items WHERE menu_id = $active_menu ORDER BY menu_order, id");
// Build menu tree
$items = [];
if ($menu_items && $menu_items->num_rows > 0) {
while($item = $menu_items->fetch_assoc()) {
$items[$item['parent_id']][] = $item;
}
}
// Get all menu items for parent dropdown
$all_menu_items = $conn->query("SELECT * FROM menu_items WHERE menu_id = $active_menu ORDER BY menu_order");
// Messages
$msg = '';
if (isset($_GET['msg'])) {
if ($_GET['msg'] == 'added') $msg = '<div class="alert alert-success">Menu berhasil ditambahkan!</div>';
if ($_GET['msg'] == 'updated') $msg = '<div class="alert alert-success">Menu berhasil diupdate!</div>';
if ($_GET['msg'] == 'deleted') $msg = '<div class="alert alert-success">Menu berhasil dihapus!</div>';
if ($_GET['msg'] == 'ordered') $msg = '<div class="alert alert-success">Urutan menu berhasil disimpan!</div>';
}
require_once 'includes/header.php';
?>
<style>
/* Menu item styles */
.sortable-menu {
list-style: none;
padding: 0;
margin: 0;
}
.sortable-menu ol {
list-style: none;
padding-left: 30px;
margin: 5px 0;
}
.sortable-menu li {
margin-bottom: 5px;
cursor: move;
}
.sortable-menu .menu-item {
border: 1px solid #e3e6f0;
transition: all 0.3s;
background: #fff;
}
.sortable-menu .menu-item:hover {
background: #f8f9fc !important;
border-color: #4e73df;
}
.sortable-menu .menu-item.drag-over {
border: 2px dashed #4e73df;
background: #e8f0fe;
}
.handle {
cursor: move;
opacity: 0.5;
transition: opacity 0.3s;
}
.handle:hover {
opacity: 1;
}
/* Sortable ghost */
.sortable-ghost {
opacity: 0.4;
background: #e3e6f0;
border: 1px dashed #4e73df;
}
.sortable-drag {
opacity: 0.8;
transform: rotate(2deg);
}
/* Icon picker styles */
.icon-preview {
position: absolute;
left: 10px;
top: 50%;
transform: translateY(-50%);
font-size: 1.2rem;
color: #4e73df;
pointer-events: none;
z-index: 5;
}
.icon-input {
padding-left: 35px !important;
}
.icon-grid {
display: grid;
grid-template-columns: repeat(8, 1fr);
gap: 10px;
max-height: 300px;
overflow-y: auto;
padding: 10px;
border: 1px solid #e3e6f0;
border-radius: 8px;
background: #fff;
margin-top: 5px;
}
.icon-grid-item {
display: flex;
flex-direction: column;
align-items: center;
padding: 8px;
cursor: pointer;
border-radius: 8px;
transition: all 0.2s;
border: 1px solid transparent;
}
.icon-grid-item:hover {
background: #f8f9fc;
border-color: #4e73df;
transform: scale(1.05);
}
.icon-grid-item i {
font-size: 1.5rem;
color: #4e73df;
margin-bottom: 5px;
}
.icon-grid-item span {
font-size: 0.7rem;
color: #666;
}
.icon-grid-item.selected {
background: #4e73df;
border-color: #4e73df;
}
.icon-grid-item.selected i,
.icon-grid-item.selected span {
color: white;
}
</style>
<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 Menu</h1>
</div>
<?php if (isset($error)): ?>
<div class="alert alert-danger"><?php echo $error; ?></div>
<?php endif; ?>
<?php echo $msg; ?>
<div class="row">
<!-- Menu Selector -->
<div class="col-12 mb-4">
<div class="card shadow">
<div class="card-body">
<div class="btn-group">
<?php
if ($menus && $menus->num_rows > 0) {
$menus->data_seek(0);
while($menu = $menus->fetch_assoc()):
?>
<a href="?menu_id=<?php echo $menu['id']; ?>"
class="btn btn-<?php echo $active_menu == $menu['id'] ? 'primary' : 'outline-primary'; ?>">
<?php echo htmlspecialchars($menu['name']); ?>
</a>
<?php
endwhile;
} else {
echo '<p class="text-muted">Belum ada menu. Tambahkan menu di database.</p>';
}
?>
</div>
</div>
</div>
</div>
<div class="col-md-5">
<!-- Add/Edit Menu Item Form -->
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-white">
<?php echo isset($edit_item) ? 'Edit Item Menu' : 'Tambah Item Menu Baru'; ?>
</h6>
</div>
<div class="card-body">
<form method="POST" id="menuForm">
<?php if (isset($edit_item)): ?>
<input type="hidden" name="id" value="<?php echo $edit_item['id']; ?>">
<?php endif; ?>
<div class="mb-3">
<label class="form-label">Judul Menu <span class="text-danger">*</span></label>
<input type="text" class="form-control" name="title"
value="<?php echo isset($edit_item) ? htmlspecialchars($edit_item['title']) : ''; ?>"
required>
</div>
<div class="mb-3">
<label class="form-label">URL <span class="text-danger">*</span></label>
<input type="text" class="form-control" name="url"
value="<?php echo isset($edit_item) ? htmlspecialchars($edit_item['url']) : ''; ?>"
placeholder="/profil atau https://example.com"
required>
<small class="text-muted">
Gunakan / untuk link internal (contoh: /profil, /berita)<br>
Gunakan http:// atau https:// untuk link eksternal (contoh: https://google.com)
</small>
</div>
<div class="mb-3">
<label class="form-label">Icon Menu</label>
<div class="position-relative">
<div class="icon-preview">
<i id="iconPreview" class="<?php echo isset($edit_item) && !empty($edit_item['icon']) ? htmlspecialchars($edit_item['icon']) : 'fas fa-link'; ?>"></i>
</div>
<input type="text" class="form-control icon-input" name="icon" id="iconInput"
value="<?php echo isset($edit_item) ? htmlspecialchars($edit_item['icon']) : ''; ?>"
placeholder="fas fa-home">
</div>
<small class="text-muted">Contoh: fas fa-home, fas fa-newspaper, fas fa-envelope</small>
<button type="button" class="btn btn-sm btn-secondary mt-2" onclick="toggleIconPicker()">
<i class="fas fa-icons me-1"></i>Pilih Icon
</button>
<!-- Icon Picker Grid -->
<div id="iconPicker" style="display: none;" class="mt-2">
<div class="icon-grid">
<?php
$icons = [
'fas fa-home', 'fas fa-building', 'fas fa-newspaper', 'fas fa-bullhorn',
'fas fa-images', 'fas fa-envelope', 'fas fa-info-circle', 'fas fa-phone',
'fas fa-hand-holding-heart', 'fas fa-file-alt', 'fas fa-comments', 'fas fa-question-circle',
'fas fa-calendar-alt', 'fas fa-user', 'fas fa-users', 'fas fa-cog',
'fas fa-chart-line', 'fas fa-download', 'fas fa-upload', 'fas fa-search',
'fas fa-edit', 'fas fa-trash', 'fas fa-plus', 'fas fa-minus',
'fas fa-check', 'fas fa-times', 'fas fa-star', 'fas fa-heart',
'fas fa-book', 'fas fa-graduation-cap', 'fas fa-chalkboard-teacher', 'fas fa-school',
'fas fa-hospital', 'fas fa-heartbeat', 'fas fa-ambulance', 'fas fa-medkit',
'fas fa-shopping-cart', 'fas fa-store', 'fas fa-truck', 'fas fa-box',
'fas fa-map-marker-alt', 'fas fa-location-dot', 'fas fa-directions', 'fas fa-road',
'fab fa-facebook', 'fab fa-twitter', 'fab fa-instagram', 'fab fa-youtube',
'fab fa-whatsapp', 'fab fa-telegram', 'fab fa-linkedin', 'fab fa-github'
];
foreach($icons as $icon):
?>
<div class="icon-grid-item" onclick="selectIcon('<?php echo $icon; ?>')">
<i class="<?php echo $icon; ?>"></i>
<span><?php echo substr($icon, strpos($icon, ' ') + 1); ?></span>
</div>
<?php endforeach; ?>
</div>
</div>
</div>
<div class="mb-3">
<label class="form-label">Induk Menu</label>
<select class="form-control" name="parent_id" id="parent_id">
<option value="0">Tidak Ada (Menu Utama)</option>
<?php
if ($all_menu_items && $all_menu_items->num_rows > 0) {
$all_menu_items->data_seek(0);
while($item = $all_menu_items->fetch_assoc()):
if (isset($edit_item) && $item['id'] == $edit_item['id']) continue;
?>
<option value="<?php echo $item['id']; ?>"
<?php echo (isset($edit_item) && $edit_item['parent_id'] == $item['id']) ? 'selected' : ''; ?>>
<?php echo str_repeat('- ', $item['menu_order']) . htmlspecialchars($item['title']); ?>
</option>
<?php
endwhile;
}
?>
</select>
</div>
<div class="mb-3">
<label class="form-label">Target</label>
<select class="form-control" name="target">
<option value="_self" <?php echo (isset($edit_item) && $edit_item['target'] == '_self') ? 'selected' : ''; ?>>
Halaman yang Sama
</option>
<option value="_blank" <?php echo (isset($edit_item) && $edit_item['target'] == '_blank') ? 'selected' : ''; ?>>
Tab Baru
</option>
</select>
</div>
<div class="d-flex gap-2">
<button type="submit" name="<?php echo isset($edit_item) ? 'update_item' : 'add_item'; ?>"
class="btn btn-primary">
<i class="fas fa-save me-2"></i><?php echo isset($edit_item) ? 'Update Menu' : 'Tambah Menu'; ?>
</button>
<?php if (isset($edit_item)): ?>
<a href="menus.php?menu_id=<?php echo $active_menu; ?>" class="btn btn-secondary">
<i class="fas fa-times me-2"></i>Batal
</a>
<?php endif; ?>
</div>
</form>
</div>
</div>
</div>
<div class="col-md-7">
<!-- Menu Items List -->
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-white">Struktur Menu (Drag & Drop untuk mengurutkan)</h6>
</div>
<div class="card-body">
<?php if (!empty($items)): ?>
<form method="POST" id="menu-order-form">
<div id="sortable-menu-container">
<?php echo renderMenuItems(0, $items); ?>
</div>
<button type="submit" name="update_order" class="btn btn-success mt-3" id="saveOrderBtn">
<i class="fas fa-save me-2"></i>Simpan Urutan
</button>
</form>
<?php else: ?>
<div class="text-center py-5">
<i class="fas fa-bars fa-4x text-muted mb-3"></i>
<h5>Belum ada item menu</h5>
<p class="text-muted">Tambahkan item menu menggunakan form di samping</p>
</div>
<?php endif; ?>
</div>
</div>
</div>
</div>
</div>
<!-- Sortable JS -->
<script src="https://cdn.jsdelivr.net/npm/sortablejs@latest/Sortable.min.js"></script>
<script>
// Global variable untuk Sortable instance
let sortableInstance = null;
// Fungsi untuk menginisialisasi Sortable
function initSortable() {
const container = document.getElementById('sortable-menu-container');
if (!container) return;
// Cari elemen sortable-menu (ul pertama di dalam container)
const sortableMenu = container.querySelector('#sortable-menu');
if (sortableMenu) {
// Destroy instance sebelumnya jika ada
if (sortableInstance) {
sortableInstance.destroy();
}
// Buat instance Sortable baru
sortableInstance = new Sortable(sortableMenu, {
handle: '.handle',
animation: 150,
ghostClass: 'sortable-ghost',
dragClass: 'sortable-drag',
onEnd: function(evt) {
// Update hidden inputs setelah drag selesai
updateOrderInputs();
}
});
console.log('Sortable initialized');
} else {
console.log('Sortable menu not found, retrying...');
setTimeout(initSortable, 500);
}
}
// Fungsi untuk mengupdate hidden inputs
function updateOrderInputs() {
const container = document.getElementById('sortable-menu-container');
if (!container) return;
const items = container.querySelectorAll('li[data-id]');
const form = document.getElementById('menu-order-form');
// Hapus input lama
const oldInputs = form.querySelectorAll('input[name="item[]"]');
oldInputs.forEach(input => input.remove());
// Tambahkan input baru dengan urutan yang benar
items.forEach(function(item, index) {
const id = item.dataset.id;
const input = document.createElement('input');
input.type = 'hidden';
input.name = 'item[]';
input.value = id;
form.appendChild(input);
});
console.log('Order updated with ' + items.length + ' items');
}
// Fungsi untuk toggle icon picker
function toggleIconPicker() {
const picker = document.getElementById('iconPicker');
picker.style.display = picker.style.display === 'none' ? 'block' : 'none';
}
// Fungsi untuk select icon
function selectIcon(iconClass) {
document.getElementById('iconInput').value = iconClass;
document.getElementById('iconPreview').className = iconClass;
document.getElementById('iconPicker').style.display = 'none';
}
// Inisialisasi saat halaman dimuat
document.addEventListener('DOMContentLoaded', function() {
// Inisialisasi Sortable
initSortable();
// Preview icon
const iconInput = document.getElementById('iconInput');
if (iconInput) {
iconInput.addEventListener('input', function() {
document.getElementById('iconPreview').className = this.value || 'fas fa-link';
});
}
});
// Re-inisialisasi setelah modal edit (jika ada)
if (document.getElementById('editModal')) {
document.getElementById('editModal').addEventListener('shown.bs.modal', function() {
setTimeout(initSortable, 100);
});
}
</script>
<?php
// Function to render menu items recursively (perbaikan)
function renderMenuItems($parent_id = 0, $items, $level = 0) {
if (!isset($items[$parent_id])) return '';
$html = '<ol class="sortable-menu"';
if ($level == 0) $html .= ' id="sortable-menu"';
$html .= '>';
foreach ($items[$parent_id] as $item) {
$html .= '<li data-id="' . $item['id'] . '">';
$html .= '<div class="menu-item d-flex justify-content-between align-items-center p-2 mb-2 bg-light rounded">';
$html .= '<div>';
$html .= '<i class="fas fa-grip-vertical me-2 text-muted handle"></i>';
// Tampilkan icon jika ada
if (!empty($item['icon'])) {
$html .= '<i class="' . htmlspecialchars($item['icon']) . ' me-2 text-primary"></i>';
}
$html .= '<strong>' . htmlspecialchars($item['title']) . '</strong>';
$html .= ' <small class="text-muted">(' . htmlspecialchars($item['url']) . ')</small>';
if ($item['target'] == '_blank') {
$html .= ' <span class="badge bg-info">New Tab</span>';
}
$html .= '</div>';
$html .= '<div>';
$html .= '<a href="?edit=' . $item['id'] . '&menu_id=' . $GLOBALS['active_menu'] . '" class="btn btn-sm btn-info me-1" title="Edit"><i class="fas fa-edit"></i></a>';
$html .= '<a href="?delete=' . $item['id'] . '&menu_id=' . $GLOBALS['active_menu'] . '" class="btn btn-sm btn-danger" onclick="return confirm(\'Yakin ingin menghapus menu ini?\')" title="Hapus"><i class="fas fa-trash"></i></a>';
$html .= '</div>';
$html .= '</div>';
// Recursively render children
$child_html = renderMenuItems($item['id'], $items, $level + 1);
if ($child_html) {
$html .= $child_html;
}
$html .= '</li>';
}
$html .= '</ol>';
return $html;
}
?>
<?php require_once 'includes/footer.php'; ?>