-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_jurusan.php
More file actions
232 lines (214 loc) · 7.75 KB
/
data_jurusan.php
File metadata and controls
232 lines (214 loc) · 7.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
ob_start();
require_once __DIR__ . '/vendor/autoload.php'; // mPDF
// --- Tambah / Edit Data ---
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['simpan_jurusan'])) {
$jur_id = $_POST['jur_id'] ?? '';
$jur_nama = trim($_POST['jur_nama']);
// --- Cek duplikasi ---
if ($jur_id) {
$cek = $koneksi->prepare("SELECT jur_id FROM jurusan
WHERE jur_nama=? AND jur_id<>? AND jur_flag='T' LIMIT 1");
$cek->bind_param("si", $jur_nama, $jur_id);
} else {
$cek = $koneksi->prepare("SELECT jur_id FROM jurusan
WHERE jur_nama=? AND jur_flag='T' LIMIT 1");
$cek->bind_param("s", $jur_nama);
}
$cek->execute();
$res = $cek->get_result();
if ($res->num_rows > 0) {
echo "<script src='https://cdn.jsdelivr.net/npm/sweetalert2@11'></script>
<script>
Swal.fire({
icon: 'warning',
title: 'Duplikasi!',
text: 'Nama Jurusan sudah ada!',
confirmButtonText: 'OK'
}).then(() => {
window.location='dashboard_admin.php?page=data_jurusan';
});
</script>";
exit;
}
// --- Simpan / Update ---
if ($jur_id) {
$stmt = $koneksi->prepare("UPDATE jurusan SET jur_nama=? WHERE jur_id=?");
$stmt->bind_param("si", $jur_nama, $jur_id);
$aksi = "diubah";
} else {
$stmt = $koneksi->prepare("INSERT INTO jurusan (jur_nama, jur_flag) VALUES (?, 'T')");
$stmt->bind_param("s", $jur_nama);
$aksi = "disimpan";
}
$stmt->execute();
echo "<script src='https://cdn.jsdelivr.net/npm/sweetalert2@11'></script>
<script>
Swal.fire({
icon: 'success',
title: 'Berhasil!',
text: 'Data berhasil $aksi!',
showConfirmButton: false,
timer: 1500
}).then(() => {
window.location='dashboard_admin.php?page=data_jurusan';
});
</script>";
exit;
}
// --- Hapus Data (soft delete) ---
if (isset($_GET['hapus'])) {
$jur_id = (int) $_GET['hapus'];
$koneksi->query("UPDATE jurusan SET jur_flag='F' WHERE jur_id=$jur_id");
echo "<script src='https://cdn.jsdelivr.net/npm/sweetalert2@11'></script>
<script>
Swal.fire({
icon: 'success',
title: 'Dihapus!',
text: 'Data berhasil dihapus.',
showConfirmButton: false,
timer: 1500
}).then(() => {
window.location='dashboard_admin.php?page=data_jurusan';
});
</script>";
exit;
}
// --- Cetak PDF ---
if (isset($_GET['cetak'])) {
$result = $koneksi->query("SELECT * FROM jurusan WHERE jur_flag='T' ORDER BY jur_id ASC");
$html = '<h2 style="text-align:center;">Daftar Jurusan</h2>';
$html .= '<table border="1" cellpadding="5" cellspacing="0" width="100%">
<thead>
<tr style="background:#f2f2f2;">
<th width="10%">No</th>
<th>Nama Jurusan</th>
</tr>
</thead>
<tbody>';
$no = 1;
while ($row = $result->fetch_assoc()) {
$html .= '<tr>
<td>'.$no++.'</td>
<td>'.$row['jur_nama'].'</td>
</tr>';
}
$html .= '</tbody></table>';
$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML($html);
$mpdf->Output("data_jurusan.pdf", "I");
exit;
}
// --- Ambil Data untuk List ---
$jurusan = $koneksi->query("SELECT * FROM jurusan WHERE jur_flag='T' ORDER BY jur_id DESC");
ob_end_flush();
?>
<!DOCTYPE html>
<html>
<head>
<title>Manajemen Jurusan</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.datatables.net/1.13.6/css/dataTables.bootstrap5.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/responsive/2.5.0/css/responsive.bootstrap5.min.css">
</head>
<body class="p-4">
<h2 class="text-center">MANAJEMEN JURUSAN</h2>
<button class="btn btn-primary mb-3" data-bs-toggle="modal" data-bs-target="#jurusanModal" onclick="openModal()">+ Tambah Jurusan</button>
<table id="jurusanTable" class="table table-bordered table-striped">
<thead class="table-light">
<tr>
<th></th>
<th width="10%">No</th>
<th>Nama Jurusan</th>
<th width="20%">Aksi</th>
</tr>
</thead>
<tbody>
<?php $no=1; while ($row = $jurusan->fetch_assoc()): ?>
<tr>
<td></td>
<td><?= $no++; ?></td>
<td><?= $row['jur_nama']; ?></td>
<td>
<button class="btn btn-sm btn-warning"
data-bs-toggle="modal"
data-bs-target="#jurusanModal"
onclick='openModal(<?= json_encode($row); ?>)'>✏ Edit</button>
<a href="javascript:void(0)"
class="btn btn-sm btn-danger"
onclick="hapusData(<?= $row['jur_id']; ?>)">🗑 Hapus</a>
</td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
<!-- Modal -->
<div class="modal fade" id="jurusanModal" tabindex="-1" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<form method="POST">
<div class="modal-header">
<h5 class="modal-title">Form Jurusan</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<input type="hidden" name="jur_id" id="jur_id">
<input type="hidden" name="simpan_jurusan" value="1">
<div class="mb-3">
<label>Nama Jurusan</label>
<input type="text" class="form-control" name="jur_nama" id="jur_nama" required>
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary">Simpan</button>
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
</div>
</form>
</div>
</div>
</div>
<!-- JS Libraries -->
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.13.6/js/dataTables.bootstrap5.min.js"></script>
<script>
$(document).ready(function() {
$('#jurusanTable').DataTable({
responsive: { details: { type: 'column', target: 'tr' } },
columnDefs: [{ className: 'dtr-control', orderable: false, targets: 0 }],
order: [1, 'asc'],
language: {
search: "Cari:",
lengthMenu: "Tampilkan _MENU_ data",
info: "Menampilkan _START_ sampai _END_ dari _TOTAL_ data",
paginate: { first: "Awal", last: "Akhir", next: "→", previous: "←" }
}
});
});
// fungsi buka modal edit/tambah
function openModal(data = null) {
document.getElementById('jur_id').value = data ? data.jur_id : '';
document.getElementById('jur_nama').value = data ? data.jur_nama : '';
}
// fungsi hapus dengan SweetAlert konfirmasi
function hapusData(id) {
Swal.fire({
title: 'Yakin ingin menghapus?',
text: "Data akan dihapus dari daftar jurusan.",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#d33',
cancelButtonColor: '#3085d6',
confirmButtonText: 'Ya, hapus!',
cancelButtonText: 'Batal'
}).then((result) => {
if (result.isConfirmed) {
window.location = 'dashboard_admin.php?page=data_jurusan&hapus=' + id;
}
});
}
</script>
</body>
</html>