TIF_NGANJUK_E41212020/admin/profile/update_profile.php

134 lines
4.1 KiB
PHP

<?php include '../template/template1.php'; ?>
<?php
// Ambil data admin yang sedang login
$admin = $_SESSION['user_global_admin'];
$admin_id = $admin->id_admin;
// Tangkap data dari form
$nama_admin = trim($_POST['nama_admin']);
$email = trim($_POST['email']);
$alamat = trim($_POST['alamat']);
// Validasi input tidak boleh kosong
if (empty($nama_admin) || empty($email) || empty($alamat)) {
echo "<script>
Swal.fire({
icon: 'error',
title: 'Oops...',
text: 'Semua field harus diisi!',
}).then(() => window.location.href = '../profile/index.php');
</script>";
exit;
}
// Inisialisasi update foto
$update_foto = "";
$foto_new_name = "";
// Cek apakah ada unggahan foto baru
if (!empty($_FILES['foto']['name'])) {
$foto = $_FILES['foto'];
$foto_name = $foto['name'];
$foto_tmp = $foto['tmp_name'];
$foto_size = $foto['size'];
$foto_ext = strtolower(pathinfo($foto_name, PATHINFO_EXTENSION));
$allowed_ext = ['jpg', 'jpeg', 'png', 'gif'];
// Validasi ekstensi
if (!in_array($foto_ext, $allowed_ext)) {
echo "<script>
Swal.fire({
icon: 'error',
title: 'Oops...',
text: 'Format file tidak diizinkan (hanya JPG, JPEG, PNG, GIF)!',
}).then(() => window.location.href = '../profile/index.php');
</script>";
exit;
}
// Validasi ukuran (maksimal 2MB)
if ($foto_size > 2097152) {
echo "<script>
Swal.fire({
icon: 'error',
title: 'Oops...',
text: 'Ukuran file maksimal 2MB!',
}).then(() => window.location.href = '../profile/index.php');
</script>";
exit;
}
// Tentukan lokasi penyimpanan
$foto_new_name = time() . '_' . basename($foto_name);
$foto_path = "../../assets/img/profile/" . $foto_new_name;
// Hapus foto lama jika ada (kecuali default)
$query_foto = mysqli_query($conn, "SELECT foto FROM admin WHERE id_admin = '$admin_id'");
$data_foto = mysqli_fetch_assoc($query_foto);
if ($data_foto && !empty($data_foto['foto']) && file_exists("../../assets/img/profile/" . $data_foto['foto']) && $data_foto['foto'] !== 'default.jpg') {
unlink("../../assets/img/profile/" . $data_foto['foto']);
}
// Pindahkan file ke folder
if (!move_uploaded_file($foto_tmp, $foto_path)) {
echo "<script>
Swal.fire({
icon: 'error',
title: 'Oops...',
text: 'Gagal mengunggah foto!',
}).then(() => window.location.href = '../profile/index.php');
</script>";
exit;
}
// Update database dengan foto baru
$update_foto = ", foto = ?";
}
// Query update profil dengan Prepared Statements
$query = "UPDATE admin SET nama_admin = ?, email = ?, alamat = ? $update_foto WHERE id_admin = ?";
$stmt = mysqli_prepare($conn, $query);
// Bind parameter
if (!empty($_FILES['foto']['name'])) {
mysqli_stmt_bind_param($stmt, "ssssi", $nama_admin, $email, $alamat, $foto_new_name, $admin_id);
} else {
mysqli_stmt_bind_param($stmt, "sssi", $nama_admin, $email, $alamat, $admin_id);
}
// Eksekusi query
$update = mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
if ($update) {
// Perbarui sesi admin agar perubahan langsung terlihat
$_SESSION['user_global_admin']->nama_admin = $nama_admin;
$_SESSION['user_global_admin']->email = $email;
$_SESSION['user_global_admin']->alamat = $alamat;
if (!empty($_FILES['foto']['name'])) {
$_SESSION['user_global_admin']->foto = $foto_new_name;
}
echo "<script>
Swal.fire({
icon: 'success',
title: 'Berhasil!',
text: 'Profil berhasil diperbarui!',
}).then(() => window.location.href = '../profile/index.php');
</script>";
exit;
} else {
echo "<script>
Swal.fire({
icon: 'error',
title: 'Oops...',
text: 'Terjadi kesalahan, coba lagi!',
}).then(() => window.location.href = '../profile/index.php');
</script>";
}
// Tutup koneksi database
mysqli_close($conn);
?>