-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit_user.php
234 lines (209 loc) · 9.51 KB
/
edit_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
<?php
session_start();
require_once('./include/db_connection.php');
if (!isset($_SESSION['username']) || $_SESSION['role'] !== 'superadmin') {
header("Location: login.php");
exit();
}
$id = $_GET['id'] ?? '';
if (empty($id)) {
header("Location: admin.php");
exit();
}
$stmt = $conn->prepare("SELECT id, username, full_name, role, foto FROM users WHERE id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$stmt->store_result();
$user = [];
$stmt->bind_result($userId, $username, $fullName, $role, $foto);
if ($stmt->fetch()) {
$user = [
'id' => $userId,
'username' => $username,
'full_name' => $fullName,
'role' => $role,
'foto' => $foto
];
}
$stmt->close();
if (empty($user)) {
header("Location: admin.php");
exit();
}
$pageTitle = "Edit Data " . htmlspecialchars($user['full_name']);
$error = '';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$full_name = $_POST['full_name'] ?? '';
$role = $_POST['role'] ?? '';
$foto = $_FILES['foto'];
$allowedFormats = ['jpg', 'jpeg', 'png', 'gif'];
$fileSizeLimit = 2 * 1024 * 1024; // 2MB
// Debugging
// var_dump($foto);
if ($role !== 'admin' && $role !== 'user') {
$error = "Role hanya bisa diubah menjadi Admin atau User.";
} else {
// Cek apakah ada perubahan pada full_name, role, atau foto
$isDataChanged = ($full_name !== $user['full_name']) || ($role !== $user['role']) || !empty($foto['name']);
if (!$isDataChanged) {
echo "<script>
Swal.fire({
icon: 'info',
title: 'Tidak ada perubahan!',
text: 'Tidak ada data yang diubah.',
confirmButtonText: 'OK'
}).then(() => {
window.history.back();
});
</script>";
} else {
$foto_name = $user['foto']; // Default to old photo if no new photo is uploaded
if (!empty($foto['name'])) {
$fotoExtension = strtolower(pathinfo($foto['name'], PATHINFO_EXTENSION));
if (!in_array($fotoExtension, $allowedFormats)) {
$error = "Format gambar tidak valid. Hanya format JPG, JPEG, PNG, dan GIF yang diperbolehkan.";
} elseif ($foto['size'] > $fileSizeLimit) {
$error = "Ukuran gambar maksimal adalah 2MB.";
} else {
// Membuat nama file menjadi lowercase dan mengganti spasi dengan "-"
$foto_name = strtolower(pathinfo($foto['name'], PATHINFO_FILENAME));
$foto_name = preg_replace('/\s+/', '-', $foto_name) . '.' . $fotoExtension;
$target_dir = "./uploads/users/";
// Menghapus foto lama jika ada
if (!empty($user['foto']) && file_exists($target_dir . $user['foto'])) {
unlink($target_dir . $user['foto']);
}
$target_file = $target_dir . $foto_name;
move_uploaded_file($foto['tmp_name'], $target_file);
}
}
if (empty($error)) {
$updateSql = "UPDATE users SET full_name = ?, role = ?";
if (!empty($foto['name'])) {
$updateSql .= ", foto = ?";
}
$updateSql .= " WHERE id = ?";
$updateStmt = $conn->prepare($updateSql);
if (!empty($foto['name'])) {
$updateStmt->bind_param("sssi", $full_name, $role, $foto_name, $id);
} else {
$updateStmt->bind_param("ssi", $full_name, $role, $id);
}
if ($updateStmt->execute()) {
$_SESSION['notification'] = "User updated successfully.";
header("Location: admin.php");
exit();
} else {
$error = "Error updating user: " . $conn->error;
}
}
}
}
}
?>
<?php include('./include/header.php'); ?>
<div class="container my-4">
<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 p-4">
<div class="card-body">
<h4 class="text-center">Edit Data <?php echo htmlspecialchars($user['full_name']); ?></h4>
<hr class="my-4">
<?php if ($error): ?>
<div class="alert alert-danger"><?php echo $error; ?></div>
<?php endif; ?>
<form id="editUserForm" method="POST" enctype="multipart/form-data">
<div class="text-center mb-4">
<img src="<?php echo !empty($user['foto']) ? 'uploads/users/' . htmlspecialchars($user['foto']) : './assets/images/logo.png'; ?>" alt="User Photo" class="rounded-circle" width="150" height="150">
</div>
<div class="mb-4">
<label for="username" class="form-label fw-bold">Username</label>
<div class="position-relative">
<i class="fa-regular fa-user position-absolute" style="left: 10px; top: 10px;"></i>
<input type="text" class="form-control ps-5" id="username" name="username" value="<?php echo htmlspecialchars($user['username']); ?>" readonly>
</div>
</div>
<div class="mb-4">
<label for="full_name" class="form-label fw-bold">Nama Lengkap</label>
<div class="position-relative">
<i class="fa-regular fa-id-card position-absolute" style="left: 10px; top: 10px;"></i>
<input type="text" class="form-control ps-5" id="full_name" name="full_name" value="<?php echo htmlspecialchars($user['full_name']); ?>" required>
</div>
</div>
<div class="mb-4">
<label for="role" class="form-label fw-bold">Role</label>
<select class="form-select" id="role" name="role" required>
<option value="admin" <?php echo ($user['role'] === 'admin') ? 'selected' : ''; ?>>Admin</option>
<option value="user" <?php echo ($user['role'] === 'user') ? 'selected' : ''; ?>>User</option>
</select>
</div>
<div class="mb-4">
<label for="foto" class="form-label fw-bold">Ganti Foto Profil</label>
<div class="input-group">
<input type="file" class="form-control" id="foto" name="foto">
<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, GIF. Ukuran maksimal 2MB.</div>
</div>
<hr class="my-4">
<div class="d-flex justify-content-between">
<a href="admin.php" class="btn btn-secondary">
<i class="fa-regular fa-arrow-left me-2"></i>Kembali
</a>
<button type="submit" class="btn btn-success" id="saveButton">
Simpan<i class="fa-regular fa-save ms-2"></i>
</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<?php include('./include/footer.php'); ?>
<script type="text/javascript">
document.getElementById('editUserForm').onsubmit = function(event) {
event.preventDefault();
const fullName = document.getElementById('full_name').value;
const role = document.getElementById('role').value;
const fileInput = document.getElementById('foto');
const file = fileInput.files[0];
// Debugging: Cek file foto
console.log('Full Name:', fullName);
console.log('Role:', role);
console.log('File:', file);
// Periksa apakah ada perubahan pada data
const isDataChanged = (fullName !== "<?php echo htmlspecialchars($user['full_name']); ?>") ||
(role !== "<?php echo htmlspecialchars($user['role']); ?>") ||
(file !== undefined);
if (!isDataChanged) {
Swal.fire({
icon: 'info',
title: 'Tidak ada perubahan!',
text: 'Tidak ada data yang diubah.',
confirmButtonText: 'OK'
});
} else {
Swal.fire({
title: 'Konfirmasi',
text: "Apakah Anda yakin ingin menyimpan perubahan ini?",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Ya, simpan!',
cancelButtonText: 'Batal'
}).then((result) => {
if (result.isConfirmed) {
this.submit();
}
});
}
};
</script>
<?php
$conn->close();
?>