-
Notifications
You must be signed in to change notification settings - Fork 0
/
song_page.php
400 lines (371 loc) · 13.9 KB
/
song_page.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
<?php
session_start(); // Start the session
include 'db_connection.php';
include 'song.php';
$songID = isset($_GET['id']) ? intval($_GET['id']) : 0;
$song = fetchSongDetails($conn, $songID);
// Check if user is logged in and session variables are set
if (isset($_SESSION['user_name']) && isset($_SESSION['profile_image_url'])) {
$userName = $_SESSION['user_name'];
$profileImageUrl = $_SESSION['profile_image_url'];
} else {
$userName = 'Guest'; // Default value if user is not logged in
$profileImageUrl = 'default-profile-image.jpg'; // Default profile image
}
if (!$song) {
header("Location: error.php");
exit;
}
$userID = isset($_SESSION['user_id']) ? $_SESSION['user_id'] : null;
if (!$userID) {
header("Location: login.php");
exit;
}
// Check if song is already liked by the user
$isLikedQuery = $conn->prepare("SELECT COUNT(*) as count FROM liked_songs WHERE user_id = ? AND song_id = ?");
$isLikedQuery->bind_param("ii", $userID, $songID);
$isLikedQuery->execute();
$result = $isLikedQuery->get_result();
$likeStatus = $result->fetch_assoc()['count'] > 0;
$isLikedQuery->close();
$comments = fetchComments($conn, $songID);
mysqli_close($conn);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Song Page</title>
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&display=swap" rel="stylesheet">
<link rel="stylesheet" href="assets/css/song_page.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
<style>
body::before {
content: "";
background: url('<?php echo htmlspecialchars($song['background_picture_upload']); ?>') no-repeat center center/cover;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: -1;
filter: blur(8px);
}
.container {
background-color: rgba(255, 255, 255, 0.8);
position: relative;
padding: 20px;
}
.close-button {
position: absolute;
top: 10px;
right: 10px;
font-size: 24px;
background: none;
border: none;
cursor: pointer;
outline: none;
}
.like-button {
font-size: 24px;
color: <?php echo $likeStatus ? '#ff0000' : '#ccc'; ?>;
cursor: pointer;
transition: color 0.3s ease;
background: none;
border: none;
padding: 0;
outline: none;
}
.like-button:hover {
color: #ff0000;
}
.like-button.liked .fa-heart {
color: #ff0000;
}
.like-button.unliked .fa-heart {
color: #ccc;
}
.profile-image {
width: 40px;
height: 40px;
border-radius: 50%;
margin-right: 10px;
}
.toast {
visibility: hidden;
min-width: 300px;
margin-left: -150px;
background-color: #d4edda;
color: #155724;
text-align: left;
border-radius: 5px;
padding: 16px;
position: fixed;
z-index: 1001;
left: 50%;
bottom: 30px;
font-size: 17px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
display: flex;
align-items: center;
}
.toast.show {
visibility: visible;
-webkit-animation: fadein 0.5s, fadeout 0.5s 3.5s;
animation: fadein 0.5s, fadeout 0.5s 3.5s;
}
@-webkit-keyframes fadein {
from {bottom: 0; opacity: 0;}
to {bottom: 30px; opacity: 1;}
}
@keyframes fadein {
from {bottom: 0; opacity: 0;}
to {bottom: 30px; opacity: 1;}
}
@-webkit-keyframes fadeout {
from {bottom: 30px; opacity: 1;}
to {bottom: 0; opacity: 0;}
}
@keyframes fadeout {
from {bottom: 30px; opacity: 1;}
to {bottom: 0; opacity: 0;}
}
.toast .icon {
margin-right: 10px;
font-size: 20px;
}
.toast .close {
margin-left: auto;
cursor: pointer;
}
.audio-player {
display: flex;
align-items: center;
justify-content: space-between;
background-color: #fff;
border-radius: 10px;
padding: 10px 20px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
margin-top: 20px;
}
.audio-controls {
display: flex;
align-items: center;
}
.audio-controls button {
background: none;
border: none;
font-size: 24px;
cursor: pointer;
margin: 0 10px;
color: #333;
}
.audio-progress {
flex-grow: 1;
margin: 0 20px;
}
.audio-progress input {
width: 100%;
}
.audio-time {
display: flex;
justify-content: space-between;
width: 50px;
font-size: 14px;
color: #666;
}
.volume-control {
display: flex;
align-items: center;
}
.volume-control input {
width: 100px;
margin-left: 10px;
}
.download-button {
background: none;
border: none;
font-size: 24px;
cursor: pointer;
color: #333;
}
</style>
</head>
<body>
<div class="container">
<button class="close-button" onclick="goBack()">×</button>
<header>
<img src="<?php echo htmlspecialchars($song['profile_picture_upload']); ?>" alt="Song Cover">
<h1><?php echo htmlspecialchars($song['song_title']); ?></h1>
<p><?php echo htmlspecialchars($song['artist_name']); ?></p>
<p><?php echo htmlspecialchars($song['categories']); ?></p>
<form id="like-form" method="POST">
<input type="hidden" name="song_id" value="<?php echo htmlspecialchars($songID); ?>">
<button type="button" id="like-button" class="like-button"><i class="far fa-heart"></i></button>
</form>
</header>
<main>
<div class="audio-player">
<div class="audio-controls">
<button onclick="togglePlayPause()"><i id="play-pause-icon" class="fas fa-play"></i></button>
</div>
<div class="audio-progress">
<input type="range" id="progress-bar" value="0" max="100">
</div>
<div class="audio-time">
<span id="current-time">00:00</span>
</div>
<div class="volume-control">
<i class="fas fa-volume-up"></i>
<input type="range" id="volume-bar" min="0" max="1" step="0.01" value="0.4">
</div>
<a href="<?php echo htmlspecialchars($song['mp3_upload']); ?>" download class="download-button">
<i class="fas fa-download"></i>
</a>
</div>
<audio id="audio-player" volume="0.4">
<source src="<?php echo htmlspecialchars($song['mp3_upload']); ?>" type="audio/mp3">
Your browser does not support the audio element.
</audio>
<section class="comments">
<h2>Comments</h2>
<form id="comment-form">
<textarea id="comment-text" name="comment_text" placeholder="Write a Comment..." required></textarea>
<input type="hidden" name="song_id" value="<?php echo htmlspecialchars($songID); ?>">
<button type="submit" class="send-button">Submit</button>
</form>
<div id="comment-list">
<?php foreach ($comments as $comment) { ?>
<div class="comment">
<div class="comment-header">
<img src="<?php echo htmlspecialchars($comment['profile_image']); ?>" alt="Profile Picture" class="profile-image">
<p><strong><?php echo htmlspecialchars($comment['name']); ?>:</strong> <?php echo htmlspecialchars($comment['comment_text']); ?></p>
</div>
<small><?php echo htmlspecialchars($comment['created_at']); ?></small>
</div>
<?php } ?>
</div>
<?php if (isset($error)) { ?>
<p><?php echo $error; ?></p>
<?php } ?>
</section>
</main>
</div>
<!-- Toast Notification -->
<div id="toast" class="toast">
<span class="icon"><i class="fas fa-check-circle"></i></span>
<span class="message"></span>
<span class="close" onclick="hideToast()">×</span>
</div>
<script>
// Pass PHP variables to JavaScript
const userName = <?php echo json_encode($userName); ?>;
const profileImageUrl = <?php echo json_encode($profileImageUrl); ?>;
// JavaScript code to use these variables
console.log("User Name:", userName);
console.log("Profile Image URL:", profileImageUrl);
const likeButton = document.getElementById('like-button');
const commentForm = document.getElementById('comment-form');
const commentList = document.getElementById('comment-list');
const toast = document.getElementById('toast');
const audioPlayer = document.getElementById('audio-player');
const playPauseIcon = document.getElementById('play-pause-icon');
const progressBar = document.getElementById('progress-bar');
const volumeBar = document.getElementById('volume-bar');
const currentTime = document.getElementById('current-time');
// Handle like button click
likeButton.addEventListener('click', function() {
const songID = document.querySelector('input[name="song_id"]').value;
fetch('ajax_handler.php', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: new URLSearchParams({
action: 'like',
song_id: songID
})
})
.then(response => response.json())
.then(data => {
if (data.success) {
showToast(data.message);
likeButton.classList.toggle('liked');
} else {
showToast(data.message);
}
})
.catch(error => console.error('Error:', error));
});
// Handle comment form submission
commentForm.addEventListener('submit', function(event) {
event.preventDefault();
const formData = new FormData(commentForm);
fetch('ajax_handler.php', {
method: 'POST',
body: formData,
})
.then(response => response.json())
.then(data => {
if (data.success) {
showToast(data.message);
const newComment = document.createElement('div');
newComment.classList.add('comment');
newComment.innerHTML = `
<div class="comment-header">
<img src="${profileImageUrl}" alt="Profile Picture" class="profile-image">
<p><strong>${userName}:</strong> ${document.getElementById('comment-text').value}</p>
</div>
<small>Just now</small>
`;
commentList.appendChild(newComment);
document.getElementById('comment-text').value = '';
} else {
showToast(data.message);
}
})
.catch(error => console.error('Error:', error));
});
// Handle play/pause button click
function togglePlayPause() {
if (audioPlayer.paused) {
audioPlayer.play();
playPauseIcon.classList.replace('fa-play', 'fa-pause');
} else {
audioPlayer.pause();
playPauseIcon.classList.replace('fa-pause', 'fa-play');
}
}
// Update progress bar and time
audioPlayer.addEventListener('timeupdate', () => {
const value = (audioPlayer.currentTime / audioPlayer.duration) * 100;
progressBar.value = value;
currentTime.textContent = formatTime(audioPlayer.currentTime);
});
progressBar.addEventListener('input', () => {
const time = (progressBar.value / 100) * audioPlayer.duration;
audioPlayer.currentTime = time;
});
volumeBar.addEventListener('input', () => {
audioPlayer.volume = volumeBar.value;
});
function formatTime(seconds) {
const minutes = Math.floor(seconds / 60);
const secs = Math.floor(seconds % 60);
return `${String(minutes).padStart(2, '0')}:${String(secs).padStart(2, '0')}`;
}
function showToast(message) {
toast.querySelector('.message').textContent = message;
toast.classList.add('show');
setTimeout(() => toast.classList.remove('show'), 4000);
}
function hideToast() {
toast.classList.remove('show');
}
function goBack() {
window.history.back();
}
</script>
</body>
</html>