-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile.php
212 lines (193 loc) · 10 KB
/
profile.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
<?php
session_start(); // Panggil session_start() di bagian atas file sebelum output HTML dimulai
include "koneksi.php";
// Fungsi untuk mendapatkan informasi profil pengguna
function getUserProfile($conn, $userid)
{
$query = mysqli_query($conn, "SELECT * FROM user WHERE userid = $userid");
return mysqli_fetch_assoc($query);
}
// Fungsi untuk memperbarui informasi profil pengguna
function updateUserProfile($conn, $userid, $username, $fullname, $email, $address, $profile_photo)
{
// Cek apakah ada file foto yang diunggah
if(!empty($profile_photo['name'])) {
// Simpan foto ke direktori tertentu (misalnya, 'uploads/')
$target_dir = "gambar/";
$target_file = $target_dir . basename($profile_photo["name"]);
move_uploaded_file($profile_photo["tmp_name"], $target_file);
// Update informasi profil dengan nama file foto
$query = "UPDATE user SET username='$username', namalengkap='$fullname', email='$email', alamat='$address', profile_photo='$target_file' WHERE userid='$userid'";
mysqli_query($conn, $query);
} else {
// Jika tidak ada foto yang diunggah, lakukan pembaruan tanpa mengubah foto profil
$query = "UPDATE user SET username='$username', namalengkap='$fullname', email='$email', alamat='$address' WHERE userid='$userid'";
mysqli_query($conn, $query);
}
}
// Fungsi untuk menghitung jumlah foto yang diunggah oleh pengguna
function countUserUploadedPhotos($conn, $userid)
{
$query = mysqli_query($conn, "SELECT COUNT(*) AS total FROM foto WHERE userid = $userid");
$result = mysqli_fetch_assoc($query);
return $result['total'];
}
// Proses pembaruan informasi profil jika data dikirimkan melalui POST
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Pastikan pengguna sudah login sebelum mengakses halaman ini
if (!isset($_SESSION['userid'])) {
header("Location: login.php");
exit();
}
// Ambil data dari formulir
$username = $_POST['username'];
$fullname = $_POST['fullname'];
$email = $_POST['email'];
$address = $_POST['address'];
$profile_photo = $_FILES['profile_photo']; // Ambil file foto profil
// Ambil userid dari sesi
$userid = $_SESSION['userid'];
// Update informasi profil di database
updateUserProfile($conn, $userid, $username, $fullname, $email, $address, $profile_photo);
// Simpan informasi pengguna yang diperbarui ke dalam session
$_SESSION['username'] = $username;
$_SESSION['namalengkap'] = $fullname;
$_SESSION['email'] = $email;
$_SESSION['alamat'] = $address;
// Redirect kembali ke halaman profil setelah pembaruan
header("Location: {$_SERVER['PHP_SELF']}");
exit();
}
// Ambil informasi profil pengguna
$user = isset($_SESSION['userid']) ? getUserProfile($conn, $_SESSION['userid']) : null;
// Ambil jumlah foto yang diunggah oleh pengguna
$total_photos = isset($_SESSION['userid']) ? countUserUploadedPhotos($conn, $_SESSION['userid']) : 0;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Profil Pengguna</title>
<link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
<style>
/* Tambahkan CSS untuk popup */
.popup-container {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
z-index: 999;
justify-content: center;
align-items: center;
}
.popup {
background-color: white;
width: 80%;
max-width: 400px;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
</style>
</head>
<body class="bg-gray-100 min-h-screen">
<ul class="flex justify-end">
<?php
// Check user role to determine the redirection link
if (isset($_SESSION['role']) && $_SESSION['role'] === 'admin') {
?>
<?php include 'adminnavbar.php'; ?>
<?php
} else {
?>
<?php include 'navbar.php'; ?>
<?php
}
?>
</ul>
<div class=" p-4">
<h1 class="text-3xl text-center text-gray-800">Profil Pengguna</h1>
<?php if (isset($_SESSION['namalengkap'])): ?>
<p class="text-center mt-2">Selamat datang <b><?= $_SESSION['namalengkap'] ?></b></p>
<?php endif; ?>
</div>
<div class="flex justify-center items-center">
<div class="bg-white w-full max-w-md p-8 rounded-lg shadow-lg mt-8">
<?php if ($user): ?>
<div class="mb-6 text-center">
<label for="profile_photo" class="block text-sm font-medium text-gray-700"></label>
<img src="./<?= $user['profile_photo'] ?>" alt="Foto Profil" class="rounded-full h-24 w-24 mx-auto shadow-lg">
</div>
<div class="mb-6">
<label for="username" class="block text-sm font-medium text-gray-700">Username:</label>
<p id="username-display" class="text-lg font-semibold text-gray-800"><?= $user['username'] ?></p>
</div>
<div class="mb-6">
<label for="fullname" class="block text-sm font-medium text-gray-700">Nama Lengkap:</label>
<p id="fullname-display" class="text-lg font-semibold text-gray-800"><?= $user['namalengkap'] ?></p>
</div>
<div class="mb-6">
<label for="email" class="block text-sm font-medium text-gray-700">Email:</label>
<p id="email-display" class="text-lg font-semibold text-gray-800"><?= $user['email'] ?></p>
</div>
<div class="mb-6">
<label for="address" class="block text-sm font-medium text-gray-700">Alamat:</label>
<p id="address-display" class="text-lg font-semibold text-gray-800"><?= $user['alamat'] ?></p>
</div>
<!-- Tampilkan jumlah foto yang sudah diunggah oleh pengguna -->
<div class="mb-6">
<label for="total-photos" class="block text-sm font-medium text-gray-700">Jumlah Foto Diunggah:</label>
<p id="total-photos-display" class="text-lg font-semibold text-gray-800"><?= $total_photos ?></p>
</div>
<!-- Tombol Edit Profil -->
<button id="edit-button" class="mt-4 w-full bg-blue-500 text-white py-2 px-4 rounded-md hover:bg-blue-600">Edit Profil</button>
<!-- Popup Form Edit Profil -->
<div class="popup-container" id="popup-container">
<div class="popup">
<h2 class="text-2xl font-bold mb-4">Edit Profil</h2>
<form action="<?= $_SERVER['PHP_SELF'] ?>" method="POST" enctype="multipart/form-data">
<div class="mb-4">
<label for="profile_photo" class="block text-sm font-medium text-gray-700">Upload Foto Profil:</label>
<input type="file" id="profile_photo" name="profile_photo" class="mt-1 p-2 block w-full border border-gray-300 rounded-md">
</div>
<div class="mb-4">
<label for="username" class="block text-sm font-medium text-gray-700">Username:</label>
<input type="text" id="username" name="username" value="<?= $user['username'] ?>" class="mt-1 p-2 block w-full border border-gray-300 rounded-md">
</div>
<div class="mb-4">
<label for="fullname" class="block text-sm font-medium text-gray-700">Nama Lengkap:</label>
<input type="text" id="fullname" name="fullname" value="<?= $user['namalengkap'] ?>" class="mt-1 p-2 block w-full border border-gray-300 rounded-md">
</div>
<div class="mb-4">
<label for="email" class="block text-sm font-medium text-gray-700">Email:</label>
<input type="email" id="email" name="email" value="<?= $user['email'] ?>" class="mt-1 p-2 block w-full border border-gray-300 rounded-md">
</div>
<div class="mb-4">
<label for="address" class="block text-sm font-medium text-gray-700">Alamat:</label>
<textarea id="address" name="address" class="mt-1 p-2 block w-full border border-gray-300 rounded-md"><?= $user['alamat'] ?></textarea>
</div>
<button type="submit" class="w-full bg-green-500 text-white py-2 px-4 rounded-md hover:bg-green-600">Simpan Perubahan</button>
</form>
<button id="close-popup" class="mt-4 w-full bg-red-500 text-white py-2 px-4 rounded-md hover:bg-red-600">Tutup</button>
</div>
</div>
<script>
// Tambahkan skrip untuk menampilkan/menyembunyikan popup
document.getElementById('edit-button').addEventListener('click', function () {
document.getElementById('popup-container').style.display = 'flex';
});
document.getElementById('close-popup').addEventListener('click', function () {
document.getElementById('popup-container').style.display = 'none';
});
</script>
<?php else: ?>
<p class="text-center">Silakan <a href="login.php" class="text-blue-500 hover:underline">login</a> untuk mengakses profil Anda.</p>
<?php endif; ?>
</div>
</div>
</body>
</html>