-
Notifications
You must be signed in to change notification settings - Fork 0
/
adm_dsh_SliderProduk.php
253 lines (233 loc) · 10.3 KB
/
adm_dsh_SliderProduk.php
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php
session_start();
// Koneksi ke database
$conn = new mysqli("localhost", "root", "", "sliderDB");
if (!isset($_SESSION['session_username'])) {
header("Location: LebungFarm/login.php");
exit();
}
$toast_message = ''; // Untuk menyimpan pesan toast
// Menambahkan gambar
if (isset($_POST['add'])) {
$group = $_POST['slider_group'];
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["image"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
$check = getimagesize($_FILES["image"]["tmp_name"]);
if ($check === false) {
$toast_message = 'File bukan gambar.'; // Set pesan toast
$uploadOk = 0;
}
if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif") {
$toast_message = 'Hanya file JPG, JPEG, PNG, dan GIF yang diizinkan.'; // Set pesan toast
$uploadOk = 0;
}
if ($uploadOk == 1) {
if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) {
$conn->query("INSERT INTO sliders (slider_group, image_url) VALUES ('$group', '$target_file')");
$toast_message = 'Gambar berhasil ditambah.'; // Set pesan toast
} else {
$toast_message = 'Terjadi kesalahan saat mengunggah file.';
}
}
}
// Mengubah gambar
if (isset($_POST['update'])) {
$id = $_POST['id'];
$group = $_POST['slider_group'];
$uploadOk = 1;
if (!empty($_FILES["image"]["name"])) {
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["image"]["name"]);
$imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
$check = getimagesize($_FILES["image"]["tmp_name"]);
if ($check === false) {
$toast_message = 'File bukan gambar.'; // Set pesan toast
$uploadOk = 0;
}
if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif") {
$toast_message = 'Hanya file JPG, JPEG, PNG, dan GIF yang diizinkan.'; // Set pesan toast
$uploadOk = 0;
}
if ($uploadOk == 1 && move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) {
$conn->query("UPDATE sliders SET image_url='$target_file', slider_group='$group' WHERE id='$id'");
$toast_message = 'Gambar berhasil diupdate.'; // Set pesan toast
}
} else {
$conn->query("UPDATE sliders SET slider_group='$group' WHERE id='$id'");
$toast_message = 'Gambar berhasil diupdate.'; // Set pesan toast tanpa mengubah gambar
}
}
if (isset($_POST['delete'])) {
$id = $_POST['id'];
$result = $conn->query("SELECT image_url FROM sliders WHERE id='$id'");
if ($result) {
$row = $result->fetch_assoc();
if (file_exists($row['image_url'])) {
unlink($row['image_url']);
}
}
$conn->query("DELETE FROM sliders WHERE id='$id'");
$toast_message = 'Gambar berhasil dihapus.';
}
$images = $conn->query("SELECT * FROM sliders");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dashboard</title>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css" rel="stylesheet">
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.0/js/bootstrap.min.js"></script>
<link href="styles.css" rel="stylesheet">
</head>
<body>
<!-- Header -->
<header class="header" style="display: flex;">
<img src="images/Logo_lamsel.png">
<img src="images/Logo_kemendes.png">
<div class="title-wrap text-left">
<p class="judul">LEBUNGFARM</p>
<p class="subjudul" style="color:gray;">Desa Lebung Sari</p>
</div>
<!-- Menu Burger untuk tampilan mobile -->
<button class="burger-menu" onclick="toggleSidebar()" style="color: black;">☰</button>
</header>
<!-- Sidebar -->
<div class="sidebar" id="sidebar">
<h2 style="margin-top: 5px;">Beranda</h2>
<ul>
<a href="#"><li>Slide Pertanian & Peternakan</li></a>
<a href="#"><li>Produk Unggulan Desa</li></a>
<a href="#"><li>Deskripsi Desa</li></a>
<a href="#"><li>Produk Favorit Desa</li></a>
</ul>
<h2>Tentang Kami</h2>
<ul>
<a href="#"><li>Sejarah Desa</li></a>
<a href="#"><li>Nama Kepala Desa / PJS Desa</li></a>
<a href="#"><li>Visi dan Misi Desa</li></a>
<a href="#"><li>Pemerintahan Desa</li></a>
<a href="#"><li>Sektor Pertanian Desa</li></a>
<a href="#"><li>Sektor Peternakan Desa</li></a>
</ul>
<h2>Produk / Layanan</h2>
<ul>
<a href="#top"><li>Slide Produk</li></a>
<a href="adm_dsh_KatalogProduk.php"><li>Katalog Produk</li></a>
<a href="#"><li>Ulasan Produk</li></a>
</ul>
<h2>Kontak Kami</h2>
<ul>
<a href="#"><li>Kontak</li></a>
</ul>
<a href="logout.php" style="display: grid; margin-right:auto;" class="logout_bt"><div>Log Out</div></a>
</div>
<!-- Main Content -->
<div class="content" id="top">
<h1 class="text-center" style="font-weight: bold;">Slide Produk</h1>
<h2>Tambah Gambar</h2>
<form method="POST" enctype="multipart/form-data">
<div class="form-group">
<label for="image">Gambar:</label>
<input type="file" name="image" class="form-control-file" required>
</div>
<div class="form-group">
<label for="slider_group">Grup Slider:</label>
<select name="slider_group" class="form-control" style="max-width:125px" required>
<option value="1">Slider 1</option>
<option value="2">Slider 2</option>
</select>
</div>
<button type="submit" name="add" class="btn btn-primary">Tambah</button>
</form>
<h2 class="mt-4">Daftar Gambar</h2>
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Gambar</th>
<th>Grup Slider</th>
<th>Aksi</th>
</tr>
</thead>
<tbody class="data_slider">
<?php while ($row = $images->fetch_assoc()): ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><img src="<?php echo $row['image_url']; ?>" alt="Image" width="100"></td>
<td><?php echo $row['slider_group']; ?></td>
<td>
<button type="button" class="btn btn-warning edit-button">Update</button>
<form method="POST" style="display:inline;">
<input type="hidden" name="id" value="<?php echo $row['id']; ?>">
<button type="submit" name="delete" class="btn btn-danger">Delete</button>
</form>
</td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
</div>
<!-- Modal Edit Produk -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<span class="modal-title" id="editModalLabel">Edit Gambar</span>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form method="POST" enctype="multipart/form-data">
<div class="modal-body">
<input type="hidden" name="id" id="editId">
<div class="form-group">
<label for="editSliderGroup">Grup Slider:</label>
<select name="slider_group" id="editSliderGroup" class="form-control" required>
<option value="1">Slider 1</option>
<option value="2">Slider 2</option>
</select>
</div>
<div class="form-group">
<label for="editImage">Ganti Gambar:</label>
<input type="file" name="image" id="editImage" class="form-control-file">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Batal</button>
<button type="submit" name="update" class="btn btn-primary">Simpan Perubahan</button>
</div>
</form>
</div>
</div>
</div>
<footer>
Copyright © 2024 UMKM Lebung Sari | All rights reserved.
</footer>
<script>
$(document).ready(function() {
$('.edit-button').click(function() {
var row = $(this).closest('tr');
var id = row.find('td:eq(0)').text(); // Ambil ID dari kolom pertama
var sliderGroup = row.find('td:eq(2)').text(); // Ambil grup slider dari kolom ketiga
var imageUrl = row.find('img').attr('src'); // Ambil URL gambar
// Set nilai untuk modal
$('#editId').val(id);
$('#editSliderGroup').val(sliderGroup);
// Tampilkan modal
$('#editModal').modal('show');
});
});
</script>
<script>
function toggleSidebar() {
const sidebar = document.getElementById('sidebar');
sidebar.classList.toggle('show-sidebar');
}
</script>
</body>
</html>