| 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/admin/ |
Upload File : |
<?php
$page_title = 'Manajemen Shortcut';
require_once 'config/database.php';
require_once '../includes/config.php'; // Load BASE_URL
require_once 'includes/init.php';
requireLogin();
// Cek apakah user adalah admin
if (!in_array($_SESSION['role'], ['superadmin','admin'])) {
redirect(BASE_URL . '/admin/dashboard.php');
}
// Handle add
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['add'])) {
$title = sanitize($_POST['title']);
$description = sanitize($_POST['description']);
$icon = sanitize($_POST['icon']);
$link = sanitize($_POST['link']);
$link_type = sanitize($_POST['link_type']);
$target = sanitize($_POST['target']);
$status = sanitize($_POST['status']);
$shortcut_order = (int)$_POST['shortcut_order'];
$image_type = sanitize($_POST['image_type']);
$image_path = '';
// Handle image upload jika tipe gambar
if ($image_type == 'image' && isset($_FILES['shortcut_image']) && $_FILES['shortcut_image']['error'] == 0) {
$target_dir = dirname(__DIR__) . "/uploads/shortcuts/";
if (!file_exists($target_dir)) {
mkdir($target_dir, 0777, true);
}
$file_ext = strtolower(pathinfo($_FILES['shortcut_image']['name'], PATHINFO_EXTENSION));
$allowed = ['jpg', 'jpeg', 'png', 'gif', 'webp'];
if (in_array($file_ext, $allowed)) {
if ($_FILES['shortcut_image']['size'] <= 1 * 1024 * 1024) {
$image_name = time() . '_' . uniqid() . '.' . $file_ext;
$target_file = $target_dir . $image_name;
if (move_uploaded_file($_FILES['shortcut_image']['tmp_name'], $target_file)) {
$image_path = 'uploads/shortcuts/' . $image_name;
}
}
}
}
$query = "INSERT INTO shortcuts (title, description, icon, image, image_type, link, link_type, target, status, shortcut_order)
VALUES ('$title', '$description', '$icon', '$image_path', '$image_type', '$link', '$link_type', '$target', '$status', $shortcut_order)";
if ($conn->query($query)) {
redirect(BASE_URL . '/admin/shortcuts.php?msg=added');
} else {
$error = "Error: " . $conn->error;
}
}
// Handle edit
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['edit'])) {
$id = (int)$_POST['id'];
$title = sanitize($_POST['title']);
$description = sanitize($_POST['description']);
$icon = sanitize($_POST['icon']);
$link = sanitize($_POST['link']);
$link_type = sanitize($_POST['link_type']);
$target = sanitize($_POST['target']);
$status = sanitize($_POST['status']);
$shortcut_order = (int)$_POST['shortcut_order'];
$image_type = sanitize($_POST['image_type']);
// Ambil data lama untuk cek gambar
$old_data = $conn->query("SELECT image FROM shortcuts WHERE id = $id")->fetch_assoc();
$image_path = $old_data['image'];
// Handle image upload jika tipe gambar dan ada file baru
if ($image_type == 'image' && isset($_FILES['shortcut_image']) && $_FILES['shortcut_image']['error'] == 0) {
$target_dir = dirname(__DIR__) . "/uploads/shortcuts/";
$file_ext = strtolower(pathinfo($_FILES['shortcut_image']['name'], PATHINFO_EXTENSION));
$allowed = ['jpg', 'jpeg', 'png', 'gif', 'webp'];
if (in_array($file_ext, $allowed)) {
if ($_FILES['shortcut_image']['size'] <= 1 * 1024 * 1024) {
$image_name = time() . '_' . uniqid() . '.' . $file_ext;
$target_file = $target_dir . $image_name;
if (move_uploaded_file($_FILES['shortcut_image']['tmp_name'], $target_file)) {
// Hapus gambar lama jika ada
$old_file = dirname(__DIR__) . "/" . $old_data['image'];
if (!empty($old_data['image']) && file_exists($old_file)) {
unlink($old_file);
}
$image_path = 'uploads/shortcuts/' . $image_name;
}
}
}
}
$query = "UPDATE shortcuts SET
title = '$title',
description = '$description',
icon = '$icon',
image = '$image_path',
image_type = '$image_type',
link = '$link',
link_type = '$link_type',
target = '$target',
status = '$status',
shortcut_order = $shortcut_order
WHERE id = $id";
if ($conn->query($query)) {
redirect(BASE_URL . '/admin/shortcuts.php?msg=updated');
} else {
$error = "Error: " . $conn->error;
}
}
// Handle delete
if (isset($_GET['delete'])) {
$id = (int)$_GET['delete'];
// Hapus gambar jika ada
$data = $conn->query("SELECT image FROM shortcuts WHERE id = $id")->fetch_assoc();
if ($data && !empty($data['image'])) {
$old_file = dirname(__DIR__) . "/" . $data['image'];
if (file_exists($old_file)) {
unlink($old_file);
}
}
$conn->query("DELETE FROM shortcuts WHERE id = $id");
redirect(BASE_URL . '/admin/shortcuts.php?msg=deleted');
}
// Get all shortcuts
$shortcuts = $conn->query("SELECT * FROM shortcuts ORDER BY shortcut_order ASC, id ASC");
// Get for edit
$edit_shortcut = null;
if (isset($_GET['edit'])) {
$id = (int)$_GET['edit'];
$result = $conn->query("SELECT * FROM shortcuts WHERE id = $id");
$edit_shortcut = $result->fetch_assoc();
}
$msg = '';
if (isset($_GET['msg'])) {
if ($_GET['msg'] == 'added') $msg = '<div class="alert alert-success">Shortcut berhasil ditambahkan!</div>';
if ($_GET['msg'] == 'updated') $msg = '<div class="alert alert-success">Shortcut berhasil diupdate!</div>';
if ($_GET['msg'] == 'deleted') $msg = '<div class="alert alert-success">Shortcut berhasil dihapus!</div>';
}
require_once 'includes/header.php';
?>
<style>
.icon-preview {
font-size: 24px;
margin-right: 10px;
color: #4e73df;
}
.image-preview {
margin-top: 10px;
padding: 10px;
background: #f8f9fc;
border-radius: 8px;
}
.image-preview img {
max-height: 80px;
max-width: 100%;
border-radius: 8px;
}
</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">
<i class="fas fa-th-large me-2"></i>Manajemen Shortcut
</h1>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addModal">
<i class="fas fa-plus me-2"></i>Tambah Shortcut
</button>
</div>
<?php echo $msg; ?>
<?php if (isset($error)) echo '<div class="alert alert-danger">' . $error . '</div>'; ?>
<!-- Shortcuts List -->
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-white">Daftar Shortcut</h6>
</div>
<div class="card-body">
<?php if ($shortcuts && $shortcuts->num_rows > 0): ?>
<div class="table-responsive">
<table class="table table-hover">
<thead>
<tr>
<th width="80">Icon</th>
<th>Judul</th>
<th>Deskripsi</th>
<th>Link</th>
<th>Tipe</th>
<th>Target</th>
<th>Status</th>
<th width="80">Urutan</th>
<th width="120">Aksi</th>
</tr>
</thead>
<tbody>
<?php while($shortcut = $shortcuts->fetch_assoc()): ?>
<tr>
<td class="text-center">
<?php if ($shortcut['image_type'] == 'image' && !empty($shortcut['image'])): ?>
<img src="<?php echo BASE_URL; ?>/uploads/shortcut/<?php echo $shortcut['image']; ?>" alt="<?php echo htmlspecialchars($shortcut['title']); ?>" style="width: 40px; height: 40px; object-fit: contain;">
<?php else: ?>
<i class="<?php echo $shortcut['icon']; ?> fa-2x text-primary"></i>
<?php endif; ?>
</td>
<td><strong><?php echo htmlspecialchars($shortcut['title']); ?></strong></td>
<td><small><?php echo substr(htmlspecialchars($shortcut['description']), 0, 50); ?></small></td>
<td><code><?php echo htmlspecialchars($shortcut['link']); ?></code></td>
<td>
<span class="badge bg-<?php echo $shortcut['link_type'] == 'internal' ? 'info' : 'warning'; ?>">
<?php echo $shortcut['link_type'] == 'internal' ? 'Internal' : 'Eksternal'; ?>
</span>
</td>
<td>
<span class="badge bg-<?php echo $shortcut['target'] == '_blank' ? 'secondary' : 'primary'; ?>">
<?php echo $shortcut['target'] == '_blank' ? 'Tab Baru' : 'Halaman Sama'; ?>
</span>
</td>
<td>
<span class="badge bg-<?php echo $shortcut['status'] == 'active' ? 'success' : 'secondary'; ?>">
<?php echo ucfirst($shortcut['status']); ?>
</span>
</td>
<td><?php echo $shortcut['shortcut_order']; ?></td>
<td>
<div class="btn-group">
<a href="<?php echo BASE_URL; ?>/admin/shortcuts.php?edit=<?php echo $shortcut['id']; ?>" class="btn btn-sm btn-info" title="Edit">
<i class="fas fa-edit"></i>
</a>
<a href="<?php echo BASE_URL; ?>/admin/shortcuts.php?delete=<?php echo $shortcut['id']; ?>" class="btn btn-sm btn-danger" title="Hapus" onclick="return confirm('Yakin ingin menghapus shortcut ini?')">
<i class="fas fa-trash"></i>
</a>
</div>
</td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
</div>
<?php else: ?>
<div class="text-center py-5">
<i class="fas fa-th-large fa-4x text-muted mb-3"></i>
<h5>Belum ada shortcut</h5>
<p class="text-muted">Klik tombol "Tambah Shortcut" untuk menambahkan shortcut pertama.</p>
</div>
<?php endif; ?>
</div>
</div>
</div>
<!-- Add Modal -->
<div class="modal fade" id="addModal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<form method="POST" enctype="multipart/form-data" action="<?php echo BASE_URL; ?>/admin/shortcuts.php">
<div class="modal-header">
<h5 class="modal-title">
<i class="fas fa-plus-circle me-2"></i>Tambah Shortcut Baru
</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<div class="mb-3">
<label class="form-label">Judul <span class="text-danger">*</span></label>
<input type="text" class="form-control" name="title" id="addTitle" required>
</div>
<div class="mb-3">
<label class="form-label">Deskripsi</label>
<textarea class="form-control" name="description" rows="2" id="addDesc"></textarea>
</div>
<div class="mb-3">
<label class="form-label">Tipe Tampilan</label>
<div class="d-flex gap-3">
<div class="form-check">
<input class="form-check-input" type="radio" name="image_type" id="add_type_icon" value="icon" checked>
<label class="form-check-label" for="add_type_icon">
<i class="fas fa-icons me-1"></i> Icon Font Awesome
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="image_type" id="add_type_image" value="image">
<label class="form-check-label" for="add_type_image">
<i class="fas fa-image me-1"></i> Gambar PNG/JPG
</label>
</div>
</div>
</div>
<div class="mb-3" id="add_icon_field">
<label class="form-label">Icon (Font Awesome)</label>
<div class="input-group">
<span class="input-group-text"><i class="fas fa-icons"></i></span>
<input type="text" class="form-control" name="icon" id="addIcon" value="fas fa-external-link-alt">
</div>
<small class="text-muted">Contoh: fas fa-info-circle, fas fa-building, fas fa-envelope</small>
</div>
<div class="mb-3" id="add_image_field" style="display: none;">
<label class="form-label">Gambar (PNG/JPG)</label>
<input type="file" class="form-control" name="shortcut_image" accept="image/*" id="addShortcutImage">
<small class="text-muted">Ukuran ideal: 200x200 pixel, format PNG atau JPG. Maks 1MB.</small>
<div id="addImagePreview" class="image-preview" style="display: none;">
<img src="" alt="Preview" id="addPreviewImg">
</div>
</div>
<div class="mb-3">
<label class="form-label">Link/URL <span class="text-danger">*</span></label>
<input type="text" class="form-control" name="link" id="addLink" required>
<small class="text-muted">Untuk internal: profil, berita | Untuk eksternal: https://example.com</small>
</div>
<div class="row">
<div class="col-md-6 mb-3">
<label class="form-label">Tipe Link</label>
<select class="form-control" name="link_type">
<option value="internal">Internal (Halaman dalam website)</option>
<option value="external">Eksternal (Website lain)</option>
</select>
</div>
<div class="col-md-6 mb-3">
<label class="form-label">Target</label>
<select class="form-control" name="target">
<option value="_self">Halaman yang Sama</option>
<option value="_blank">Tab Baru</option>
</select>
</div>
</div>
<div class="row">
<div class="col-md-6 mb-3">
<label class="form-label">Urutan</label>
<input type="number" class="form-control" name="shortcut_order" value="0" min="0">
</div>
<div class="col-md-6 mb-3">
<label class="form-label">Status</label>
<select class="form-control" name="status">
<option value="active">Aktif</option>
<option value="inactive">Tidak Aktif</option>
</select>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
<button type="submit" name="add" class="btn btn-primary">
<i class="fas fa-save me-2"></i>Simpan
</button>
</div>
</form>
</div>
</div>
</div>
<!-- Edit Modal -->
<?php if ($edit_shortcut): ?>
<div class="modal fade" id="editModal" tabindex="-1" data-bs-backdrop="static">
<div class="modal-dialog">
<div class="modal-content">
<form method="POST" enctype="multipart/form-data" action="<?php echo BASE_URL; ?>/admin/shortcuts.php">
<div class="modal-header">
<h5 class="modal-title">
<i class="fas fa-edit me-2"></i>Edit Shortcut
</h5>
<a href="<?php echo BASE_URL; ?>/admin/shortcuts.php" class="btn-close"></a>
</div>
<div class="modal-body">
<input type="hidden" name="id" value="<?php echo $edit_shortcut['id']; ?>">
<div class="mb-3">
<label class="form-label">Judul <span class="text-danger">*</span></label>
<input type="text" class="form-control" name="title" id="editTitle" value="<?php echo htmlspecialchars($edit_shortcut['title']); ?>" required>
</div>
<div class="mb-3">
<label class="form-label">Deskripsi</label>
<textarea class="form-control" name="description" rows="2" id="editDesc"><?php echo htmlspecialchars($edit_shortcut['description']); ?></textarea>
</div>
<div class="mb-3">
<label class="form-label">Tipe Tampilan</label>
<div class="d-flex gap-3">
<div class="form-check">
<input class="form-check-input" type="radio" name="image_type" id="edit_type_icon" value="icon" <?php echo $edit_shortcut['image_type'] == 'icon' ? 'checked' : ''; ?>>
<label class="form-check-label" for="edit_type_icon">
<i class="fas fa-icons me-1"></i> Icon Font Awesome
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="image_type" id="edit_type_image" value="image" <?php echo $edit_shortcut['image_type'] == 'image' ? 'checked' : ''; ?>>
<label class="form-check-label" for="edit_type_image">
<i class="fas fa-image me-1"></i> Gambar PNG/JPG
</label>
</div>
</div>
</div>
<div class="mb-3" id="edit_icon_field" style="<?php echo $edit_shortcut['image_type'] != 'image' ? 'display: block;' : 'display: none;'; ?>">
<label class="form-label">Icon (Font Awesome)</label>
<div class="input-group">
<span class="input-group-text"><i class="<?php echo $edit_shortcut['icon']; ?>"></i></span>
<input type="text" class="form-control" name="icon" id="editIcon" value="<?php echo $edit_shortcut['icon']; ?>">
</div>
</div>
<div class="mb-3" id="edit_image_field" style="<?php echo $edit_shortcut['image_type'] == 'image' ? 'display: block;' : 'display: none;'; ?>">
<label class="form-label">Gambar (PNG/JPG)</label>
<?php if (!empty($edit_shortcut['image'])): ?>
<div class="image-preview mb-2">
<img src="<?php echo BASE_URL; ?>/uploads/shortcut/<?php echo $edit_shortcut['image']; ?>" alt="<?php echo htmlspecialchars($edit_shortcut['title']); ?>" style="max-height: 80px; border-radius: 8px;">
<small class="text-muted d-block">Gambar saat ini</small>
</div>
<?php endif; ?>
<input type="file" class="form-control" name="shortcut_image" accept="image/*" id="editShortcutImage">
<small class="text-muted">Kosongkan jika tidak ingin mengubah gambar. Maks 1MB.</small>
<div id="editImagePreview" class="image-preview" style="display: none;">
<img src="" alt="Preview" id="editPreviewImg">
</div>
</div>
<div class="mb-3">
<label class="form-label">Link/URL <span class="text-danger">*</span></label>
<input type="text" class="form-control" name="link" id="editLink" value="<?php echo htmlspecialchars($edit_shortcut['link']); ?>" required>
</div>
<div class="row">
<div class="col-md-6 mb-3">
<label class="form-label">Tipe Link</label>
<select class="form-control" name="link_type">
<option value="internal" <?php echo $edit_shortcut['link_type'] == 'internal' ? 'selected' : ''; ?>>Internal</option>
<option value="external" <?php echo $edit_shortcut['link_type'] == 'external' ? 'selected' : ''; ?>>Eksternal</option>
</select>
</div>
<div class="col-md-6 mb-3">
<label class="form-label">Target</label>
<select class="form-control" name="target">
<option value="_self" <?php echo $edit_shortcut['target'] == '_self' ? 'selected' : ''; ?>>Halaman yang Sama</option>
<option value="_blank" <?php echo $edit_shortcut['target'] == '_blank' ? 'selected' : ''; ?>>Tab Baru</option>
</select>
</div>
</div>
<div class="row">
<div class="col-md-6 mb-3">
<label class="form-label">Urutan</label>
<input type="number" class="form-control" name="shortcut_order" value="<?php echo $edit_shortcut['shortcut_order']; ?>" min="0">
</div>
<div class="col-md-6 mb-3">
<label class="form-label">Status</label>
<select class="form-control" name="status">
<option value="active" <?php echo $edit_shortcut['status'] == 'active' ? 'selected' : ''; ?>>Aktif</option>
<option value="inactive" <?php echo $edit_shortcut['status'] == 'inactive' ? 'selected' : ''; ?>>Tidak Aktif</option>
</select>
</div>
</div>
</div>
<div class="modal-footer">
<a href="<?php echo BASE_URL; ?>/admin/shortcuts.php" class="btn btn-secondary">Batal</a>
<button type="submit" name="edit" class="btn btn-primary">
<i class="fas fa-save me-2"></i>Update
</button>
</div>
</form>
</div>
</div>
</div>
<script>
const BASE_URL = '<?php echo BASE_URL; ?>';
// Toggle icon/image field untuk Add Modal
document.querySelectorAll('#addModal input[name="image_type"]').forEach(radio => {
radio.addEventListener('change', function() {
if (this.value === 'icon') {
document.getElementById('add_icon_field').style.display = 'block';
document.getElementById('add_image_field').style.display = 'none';
document.querySelector('#addModal input[name="icon"]').required = true;
document.querySelector('#addModal input[name="shortcut_image"]').required = false;
} else {
document.getElementById('add_icon_field').style.display = 'none';
document.getElementById('add_image_field').style.display = 'block';
document.querySelector('#addModal input[name="icon"]').required = false;
document.querySelector('#addModal input[name="shortcut_image"]').required = true;
}
});
});
// Image preview untuk Add Modal
const addImageInput = document.getElementById('addShortcutImage');
if (addImageInput) {
addImageInput.addEventListener('change', function(e) {
const file = e.target.files[0];
if (file) {
if (file.size > 1 * 1024 * 1024) {
alert('Ukuran file maksimal 1MB!');
this.value = '';
return;
}
const reader = new FileReader();
reader.onload = function(e) {
const preview = document.getElementById('addImagePreview');
document.getElementById('addPreviewImg').src = e.target.result;
preview.style.display = 'block';
}
reader.readAsDataURL(file);
}
});
}
// Toggle icon/image field untuk Edit Modal
document.querySelectorAll('#editModal input[name="image_type"]').forEach(radio => {
radio.addEventListener('change', function() {
if (this.value === 'icon') {
document.getElementById('edit_icon_field').style.display = 'block';
document.getElementById('edit_image_field').style.display = 'none';
document.querySelector('#editModal input[name="icon"]').required = true;
document.querySelector('#editModal input[name="shortcut_image"]').required = false;
} else {
document.getElementById('edit_icon_field').style.display = 'none';
document.getElementById('edit_image_field').style.display = 'block';
document.querySelector('#editModal input[name="icon"]').required = false;
document.querySelector('#editModal input[name="shortcut_image"]').required = true;
}
});
});
// Image preview untuk Edit Modal
const editImageInput = document.getElementById('editShortcutImage');
if (editImageInput) {
editImageInput.addEventListener('change', function(e) {
const file = e.target.files[0];
if (file) {
if (file.size > 1 * 1024 * 1024) {
alert('Ukuran file maksimal 1MB!');
this.value = '';
return;
}
const reader = new FileReader();
reader.onload = function(e) {
const preview = document.getElementById('editImagePreview');
document.getElementById('editPreviewImg').src = e.target.result;
preview.style.display = 'block';
}
reader.readAsDataURL(file);
}
});
}
// Show edit modal automatically
document.addEventListener('DOMContentLoaded', function() {
const editModal = document.getElementById('editModal');
if (editModal) {
new bootstrap.Modal(editModal).show();
}
});
</script>
<?php endif; ?>
<?php require_once 'includes/footer.php'; ?>