-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_user.php
252 lines (226 loc) · 10.1 KB
/
add_user.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
<?php
session_start();
require_once('include/db_connection.php');
if (!isset($_SESSION['username']) || $_SESSION['role'] !== 'superadmin') {
header("Location: login.php");
exit();
}
$pageTitle = "Tambah Pengguna";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = trim($_POST['username']);
$full_name = trim($_POST['full_name']);
$password = $_POST['password'];
$role = $_POST['role'];
$fotoName = null;
$fotoPath = null;
$error = '';
$success = '';
$compressionRatio = null;
// Validasi input
if (empty($username)) {
$error = 'Username harus diisi.';
} elseif (empty($full_name)) {
$error = 'Nama Lengkap harus diisi.';
} elseif (empty($password)) {
$error = 'Password harus diisi.';
} elseif (empty($role)) {
$error = 'Role harus dipilih.';
}
// Proses upload foto jika ada
if (empty($error) && !empty($_FILES['foto']['name'])) {
$foto = $_FILES['foto'];
$fotoName = strtolower(basename($foto['name']));
$fotoExtension = pathinfo($fotoName, PATHINFO_EXTENSION);
$fotoBaseName = pathinfo($fotoName, PATHINFO_FILENAME);
// Ganti spasi dengan "-"
$fotoName = preg_replace('/\s+/', '-', $fotoBaseName) . '.' . $fotoExtension;
$fotoPath = 'uploads/users/' . $fotoName;
// Cek apakah file sudah ada dan tambahkan suffix jika perlu
$counter = 1;
while (file_exists($fotoPath)) {
$fotoName = preg_replace('/\s+/', '-', $fotoBaseName) . '-' . $counter . '.' . $fotoExtension;
$fotoPath = 'uploads/users/' . $fotoName;
$counter++;
}
// Validasi format dan ukuran file
$allowedTypes = ['image/jpeg', 'image/png', 'image/webp'];
if (!in_array($foto['type'], $allowedTypes)) {
$error = "Format file tidak didukung. Harus JPG, PNG, atau WEBP.";
} elseif ($foto['size'] > 2 * 1024 * 1024) {
$error = "Ukuran file terlalu besar. Maksimal 2MB.";
} else {
if (move_uploaded_file($foto['tmp_name'], $fotoPath)) {
$originalSize = filesize($fotoPath);
compressImage($fotoPath, $fotoPath, 75);
$compressedSize = filesize($fotoPath);
$compressionRatio = ($originalSize - $compressedSize) / $originalSize * 100;
} else {
$error = "Gagal mengupload foto.";
}
}
}
if (empty($error)) {
$passwordHash = password_hash($password, PASSWORD_DEFAULT);
$stmt = $conn->prepare("INSERT INTO users (username, full_name, password, role, foto, created_at) VALUES (?, ?, ?, ?, ?, NOW())");
$fotoParam = $fotoName ? $fotoName : null;
$stmt->bind_param("sssss", $username, $full_name, $passwordHash, $role, $fotoParam);
if ($stmt->execute()) {
$success = 'Pengguna berhasil ditambahkan!';
if ($compressionRatio !== null) {
$success .= " Gambar telah dikompresi sebesar " . round($compressionRatio, 2) . "%.";
}
echo json_encode(['success' => true, 'message' => $success]);
} else {
$error = 'Terjadi kesalahan: ' . $stmt->error;
echo json_encode(['success' => false, 'message' => $error]);
}
$stmt->close();
} else {
echo json_encode(['success' => false, 'message' => $error]);
}
exit();
}
function compressImage($source, $destination, $quality)
{
$info = getimagesize($source);
if ($info['mime'] == 'image/jpeg') {
$image = imagecreatefromjpeg($source);
imagejpeg($image, $destination, $quality);
} elseif ($info['mime'] == 'image/png') {
$image = imagecreatefrompng($source);
imagepng($image, $destination, floor($quality / 10));
} elseif ($info['mime'] == 'image/webp') {
$image = imagecreatefromwebp($source);
imagewebp($image, $destination, $quality);
}
}
?>
<?php include('include/header.php'); ?>
<div class="container-fluid">
<div class="row">
<div class="col-12 col-md-3">
<?php include('include/sidebar.php'); ?>
</div>
<div class="col-12 col-md-9">
<div class="card shadow">
<div class="card-body m-4">
<h3 class="card-title text-center fw-bold">Tambah Pengguna</h3>
<hr class="my-4">
<form id="addUserForm" method="POST" enctype="multipart/form-data">
<div class="mb-4">
<label for="username" class="form-label fw-bold">Username</label>
<div class="input-group">
<span class="input-group-text"><i class="fa-regular fa-user"></i></span>
<input type="text" id="username" name="username" class="form-control" placeholder="Username">
</div>
</div>
<div class="mb-4">
<label for="full_name" class="form-label fw-bold">Nama Lengkap</label>
<div class="input-group">
<span class="input-group-text"><i class="fa-regular fa-id-card"></i></span>
<input type="text" id="full_name" name="full_name" class="form-control" placeholder="Nama Lengkap">
</div>
</div>
<div class="mb-4">
<label for="password" class="form-label fw-bold">Password</label>
<div class="input-group">
<span class="input-group-text"><i class="fa-regular fa-lock"></i></span>
<input type="password" id="password" name="password" class="form-control" placeholder="Password">
</div>
</div>
<div class="mb-4">
<label for="role" class="form-label fw-bold">Pilih Role</label>
<div class="input-group">
<span class="input-group-text"><i class="fa-regular fa-users"></i></span>
<select id="role" name="role" class="form-select">
<option value="" selected disabled>Pilih Role</option>
<option value="user">User</option>
<option value="admin">Admin</option>
</select>
</div>
</div>
<div class="mb-4">
<label for="foto" class="form-label fw-bold">Unggah Foto</label>
<div class="input-group">
<input type="file" id="foto" name="foto" class="form-control" accept="image/*" onchange="previewImage(event)">
<span class="input-group-text"><i class="fa-regular fa-cloud-arrow-up"></i></span>
</div>
<div class="form-text text-muted small">Format gambar yang diperbolehkan: JPG, JPEG, PNG, WEBP. Ukuran maksimal 2MB.</div>
</div>
<div class="d-flex justify-content-center align-items-center">
<img id="imagePreview" src="#" alt="Preview" class="rounded-circle border border-white border-3 shadow" style="display:none; width: 150px; height: 150px; object-fit: cover;">
</div>
<hr class="my-4">
<div class="d-flex justify-content-center">
<button type="submit" class="btn btn-primary">
<i class="fa-regular fa-user-plus me-2"></i>Tambah Pengguna
</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<?php include('include/footer.php'); ?>
<script>
function previewImage(event) {
const imagePreview = document.getElementById('imagePreview');
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function(e) {
imagePreview.src = e.target.result;
imagePreview.style.display = 'block';
}
reader.readAsDataURL(file);
} else {
imagePreview.style.display = 'none';
}
}
document.getElementById('addUserForm').onsubmit = function(event) {
event.preventDefault();
const formData = new FormData(this);
// Validasi input sebelum mengirim
let isValid = true;
let errorMessage = '';
const username = document.getElementById('username').value;
const full_name = document.getElementById('full_name').value;
const password = document.getElementById('password').value;
const role = document.getElementById('role').value;
if (!username) {
isValid = false;
errorMessage += 'Username harus diisi.\n';
}
if (!full_name) {
isValid = false;
errorMessage += 'Nama Lengkap harus diisi.\n';
}
if (!password) {
isValid = false;
errorMessage += 'Password harus diisi.\n';
}
if (!role) {
isValid = false;
errorMessage += 'Role harus dipilih.\n';
}
if (!isValid) {
alert(errorMessage);
return;
}
fetch('add_user.php', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
if (data.success) {
alert(data.message);
window.location.href = 'admin.php';
} else {
alert(data.message);
}
})
.catch(error => console.error('Error:', error));
}
</script>