-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofile.php
More file actions
781 lines (689 loc) · 35.1 KB
/
Copy pathprofile.php
File metadata and controls
781 lines (689 loc) · 35.1 KB
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
<?php
// Enable error reporting for development
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Start output buffering at the very beginning of the file
ob_start();
// Include header (which now includes init.php)
include 'includes/header.php';
// At the top of your file, ensure that functions.php is included
// This might be done through another include like header.php or init.php
// If not, add this near the top of the file:
require_once 'includes/functions.php';
// Near the top of the file, add the badge level definitions
$badge_levels = get_badge_levels();
// Handle post deletion
if (isset($_GET['delete']) && is_logged_in()) {
$post_id = (int)$_GET['delete'];
// Verify post belongs to logged-in user
$check_query = "SELECT * FROM posts WHERE post_id = ? AND user_id = ?";
$check_stmt = mysqli_prepare($conn, $check_query);
if (!$check_stmt) {
die("Prepare failed: " . mysqli_error($conn));
}
mysqli_stmt_bind_param($check_stmt, "ii", $post_id, $_SESSION['user_id']);
if (!mysqli_stmt_execute($check_stmt)) {
die("Execute failed: " . mysqli_stmt_error($check_stmt));
}
$check_result = mysqli_stmt_get_result($check_stmt);
if (mysqli_num_rows($check_result) > 0) {
// Delete the post
$delete_query = "DELETE FROM posts WHERE post_id = ?";
$delete_stmt = mysqli_prepare($conn, $delete_query);
if (!$delete_stmt) {
die("Prepare failed: " . mysqli_error($conn));
}
mysqli_stmt_bind_param($delete_stmt, "i", $post_id);
if (mysqli_stmt_execute($delete_stmt)) {
$_SESSION['message'] = "Post deleted successfully.";
} else {
$_SESSION['message'] = "Error deleting post: " . mysqli_stmt_error($delete_stmt);
}
mysqli_stmt_close($delete_stmt);
} else {
$_SESSION['message'] = "Post not found or you don't have permission to delete it.";
}
mysqli_stmt_close($check_stmt);
// Redirect to current profile page
$redirect_url = "profile.php";
if (isset($_GET['user'])) {
$redirect_url .= "?user=" . urlencode($_GET['user']);
}
safe_redirect("profile.php");
exit;
}
// Check if viewing own profile or someone else's
if (isset($_GET['user'])) {
if (!preg_match('/^[a-zA-Z0-9_]{3,20}$/', $_GET['user'])) {
header("Location: index.php");
exit;
}
$username = sanitize_input($_GET['user']);
$user_query = "SELECT * FROM users WHERE username = ?";
$stmt = mysqli_prepare($conn, $user_query);
if (!$stmt) {
die("Database error: " . mysqli_error($conn));
}
mysqli_stmt_bind_param($stmt, "s", $username);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
if (mysqli_num_rows($result) == 0) {
header("Location: index.php");
exit;
}
$user = mysqli_fetch_assoc($result);
$page_title = htmlspecialchars($user['username']) . "'s Profile";
$viewing_own = is_logged_in() && $_SESSION['username'] === $username;
mysqli_stmt_close($stmt);
} else {
if (!is_logged_in()) {
header("Location: login.php");
exit;
}
$username = $_SESSION['username'];
$user_query = "SELECT * FROM users WHERE user_id = ?";
$stmt = mysqli_prepare($conn, $user_query);
if (!$stmt) {
die("Database error: " . mysqli_error($conn));
}
mysqli_stmt_bind_param($stmt, "i", $_SESSION['user_id']);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$user = mysqli_fetch_assoc($result);
$page_title = "My Profile";
$viewing_own = true;
mysqli_stmt_close($stmt);
}
$profile_image = get_avatar_url($user['profile_picture'] ?? ($user['profile_image'] ?? null));
// Find the user's badge based on karma
$user_badge = get_user_badge($user['karma']);
// Query to get user's posts
$posts_query = "SELECT p.*, COUNT(c.comment_id) as comment_count
FROM posts p
LEFT JOIN comments c ON p.post_id = c.post_id
WHERE p.user_id = ?
GROUP BY p.post_id, p.title, p.content, p.user_id, p.category, p.created_at, p.upvotes, p.downvotes
ORDER BY p.created_at DESC
LIMIT 10";
$stmt = mysqli_prepare($conn, $posts_query);
if (!$stmt) {
die("Database error: " . mysqli_error($conn));
}
mysqli_stmt_bind_param($stmt, "i", $user['user_id']);
mysqli_stmt_execute($stmt);
$posts_result = mysqli_stmt_get_result($stmt);
// Query to get user's comments
$comments_query = "SELECT c.*, p.title as post_title, p.post_id,
(SELECT COUNT(*) FROM votes WHERE comment_id = c.comment_id AND vote_type = 1) as upvotes,
(SELECT COUNT(*) FROM votes WHERE comment_id = c.comment_id AND vote_type = -1) as downvotes
FROM comments c
JOIN posts p ON c.post_id = p.post_id
WHERE c.user_id = ?
ORDER BY c.created_at DESC
LIMIT 20";
$comments_stmt = mysqli_prepare($conn, $comments_query);
mysqli_stmt_bind_param($comments_stmt, "i", $user['user_id']);
mysqli_stmt_execute($comments_stmt);
$comments_result = mysqli_stmt_get_result($comments_stmt);
// Query to get user's upvoted posts
$upvoted_query = "SELECT p.*, u.username, COUNT(c.comment_id) as comment_count,
(SELECT COUNT(*) FROM votes WHERE post_id = p.post_id AND vote_type = 1) as upvotes,
(SELECT COUNT(*) FROM votes WHERE post_id = p.post_id AND vote_type = -1) as downvotes
FROM votes v
JOIN posts p ON v.post_id = p.post_id
JOIN users u ON p.user_id = u.user_id
LEFT JOIN comments c ON p.post_id = c.post_id
WHERE v.user_id = ? AND v.vote_type = 1 AND v.post_id IS NOT NULL
GROUP BY p.post_id, p.title, p.content, p.user_id, u.username, p.category, p.created_at
ORDER BY v.created_at DESC
LIMIT 10";
$upvoted_stmt = mysqli_prepare($conn, $upvoted_query);
mysqli_stmt_bind_param($upvoted_stmt, "i", $user['user_id']);
mysqli_stmt_execute($upvoted_stmt);
$upvoted_result = mysqli_stmt_get_result($upvoted_stmt);
?>
<link rel="stylesheet" href="css/profile.css">
<main class="container">
<?php if (!empty($_SESSION['message'])): ?>
<div class="alert alert-success">
<i class="fas fa-check-circle"></i>
<?php echo htmlspecialchars($_SESSION['message']); unset($_SESSION['message']); ?>
</div>
<?php endif; ?>
<div class="profile-container">
<!-- Enhanced profile header with gradient background -->
<div class="profile-header">
<div class="profile-cover">
<?php if ($viewing_own): ?>
<button class="edit-cover-btn" title="Change cover image">
<i class="fas fa-camera"></i>
</button>
<?php endif; ?>
</div>
<div class="profile-header-content">
<div class="profile-avatar">
<img src="<?php echo htmlspecialchars($profile_image); ?>" alt="<?php echo htmlspecialchars($user['username']); ?>">
<?php if ($viewing_own): ?>
<div class="change-avatar-overlay">
<i class="fas fa-camera"></i>
<span>Change</span>
</div>
<?php endif; ?>
</div>
<div class="profile-info">
<h1><?php echo htmlspecialchars($user['username']); ?></h1>
<span class="badge user-badge" style="background-color: <?php echo $user_badge['color']; ?>">
<?php echo $user_badge['name']; ?>
</span>
<div class="user-badge-container">
<?php if (!empty($user['is_admin']) && $user['is_admin']): ?>
<span class="user-badge admin-badge">
<i class="fas fa-shield-alt"></i> Admin
</span>
<?php endif; ?>
<span class="user-badge joined-badge">
<i class="fas fa-calendar-check"></i>
Joined <?php echo date('M Y', strtotime($user['created_at'])); ?>
</span>
<!-- Last Login Badge -->
<?php if (!empty($user['last_login'])): ?>
<span class="user-badge last-login-badge">
<i class="fas fa-clock"></i>
Last seen <?php echo time_elapsed_string($user['last_login']); ?>
</span>
<?php endif; ?>
</div>
<div class="profile-stats">
<div class="stat-item">
<div class="stat-icon karma-icon">
<i class="fas fa-bolt"></i>
</div>
<div class="stat-details">
<div class="stat-value"><?php echo htmlspecialchars($user['karma']); ?></div>
<div class="stat-label">Karma</div>
</div>
</div>
<div class="stat-item">
<div class="stat-icon posts-icon">
<i class="fas fa-file-alt"></i>
</div>
<div class="stat-details">
<div class="stat-value"><?php echo mysqli_num_rows($posts_result); ?></div>
<div class="stat-label">Posts</div>
</div>
</div>
<div class="stat-item">
<div class="stat-icon member-icon">
<i class="fas fa-user"></i>
</div>
<div class="stat-details">
<div class="stat-value">
<?php echo time_elapsed_string($user['created_at']); ?>
</div>
<div class="stat-label">Member</div>
</div>
</div>
</div>
<?php if ($viewing_own): ?>
<div class="profile-actions">
<a href="edit-profile.php" class="btn btn-primary">
<i class="fas fa-edit"></i> Edit Profile
</a>
<a href="settings.php" class="btn btn-outline">
<i class="fas fa-cog"></i> Settings
</a>
</div>
<?php endif; ?>
</div>
</div>
</div>
<!-- Profile navigation tabs -->
<div class="profile-tabs">
<a href="#posts" class="tab-item active">
<i class="fas fa-file-alt"></i> Posts
</a>
<a href="#comments" class="tab-item">
<i class="fas fa-comments"></i> Comments
</a>
<a href="#upvoted" class="tab-item">
<i class="fas fa-arrow-up"></i> Upvoted
</a>
<?php if ($viewing_own): ?>
<a href="#saved" class="tab-item">
<i class="fas fa-bookmark"></i> Saved
</a>
<?php endif; ?>
</div>
<!-- Profile content area -->
<div id="posts" class="profile-content tab-content active">
<div class="content-header">
<h2 class="section-title">
<i class="fas fa-file-alt"></i>
Recent Posts
</h2>
<?php if ($viewing_own): ?>
<a href="create-post.php" class="create-btn">
<i class="fas fa-plus"></i>
New Post
</a>
<?php endif; ?>
</div>
<?php if (mysqli_num_rows($posts_result) > 0): ?>
<div class="posts-grid">
<?php mysqli_data_seek($posts_result, 0); ?>
<?php while ($post = mysqli_fetch_assoc($posts_result)): ?>
<div class="post-card" data-post-id="<?php echo htmlspecialchars($post['post_id']); ?>">
<div class="vote-column">
<button class="vote-btn upvote <?php if (isset($_SESSION['vote_post_'.$post['post_id']]) && $_SESSION['vote_post_'.$post['post_id']] == 1) echo 'active'; ?>">
<i class="fas fa-arrow-up"></i>
</button>
<div class="vote-count"><?php echo (int)($post['upvotes'] - $post['downvotes']); ?></div>
<button class="vote-btn downvote <?php if (isset($_SESSION['vote_post_'.$post['post_id']]) && $_SESSION['vote_post_'.$post['post_id']] == -1) echo 'active'; ?>">
<i class="fas fa-arrow-down"></i>
</button>
</div>
<div class="post-content">
<div class="post-header">
<div class="post-category">
<a href="index.php?category=<?php echo urlencode($post['category']); ?>">
<i class="fas fa-tag"></i>
<?php echo htmlspecialchars($post['category']); ?>
</a>
</div>
<div class="post-meta">
<i class="far fa-clock"></i>
<?php echo time_elapsed_string($post['created_at']); ?>
</div>
</div>
<h3 class="post-title">
<a href="post.php?id=<?php echo htmlspecialchars($post['post_id']); ?>">
<?php echo htmlspecialchars($post['title']); ?>
</a>
</h3>
<div class="post-body">
<?php
$content = nl2br(htmlspecialchars($post['content']));
echo (strlen($content) > 150) ? substr($content, 0, 150) . '...' : $content;
?>
</div>
<div class="post-footer">
<a href="post.php?id=<?php echo htmlspecialchars($post['post_id']); ?>" class="post-action">
<i class="fas fa-comment"></i>
<span><?php echo (int)$post['comment_count']; ?> Comments</span>
</a>
<a href="post.php?id=<?php echo htmlspecialchars($post['post_id']); ?>" class="post-action">
<i class="fas fa-external-link-alt"></i>
<span>View Post</span>
</a>
<?php if ($viewing_own): ?>
<a href="edit-post.php?id=<?php echo htmlspecialchars($post['post_id']); ?>" class="post-action edit-action">
<i class="fas fa-edit"></i>
<span>Edit</span>
</a>
<a href="profile.php?<?php echo isset($_GET['user']) ? 'user='.urlencode($_GET['user']).'&' : ''; ?>delete=<?php echo htmlspecialchars($post['post_id']); ?>"
class="post-action delete-action"
onclick="return confirm('Are you sure you want to delete this post? This action cannot be undone.');">
<i class="fas fa-trash-alt"></i>
<span>Delete</span>
</a>
<?php endif; ?>
</div>
</div>
</div>
<?php endwhile; ?>
</div>
<div class="pagination-container">
<a href="#" class="pagination-btn disabled">
<i class="fas fa-chevron-left"></i> Previous
</a>
<span class="page-indicator">Page 1</span>
<a href="#" class="pagination-btn">
Next <i class="fas fa-chevron-right"></i>
</a>
</div>
<?php else: ?>
<div class="no-posts-message">
<div class="empty-state">
<div class="empty-icon">
<i class="fas fa-file-alt"></i>
</div>
<h3>No posts yet</h3>
<p>When posts are created, they'll appear here.</p>
<?php if ($viewing_own): ?>
<a href="create-post.php" class="btn btn-primary">
<i class="fas fa-plus"></i> Create your first post
</a>
<?php endif; ?>
</div>
</div>
<?php endif; ?>
</div>
<!-- Hidden tab content (other tabs are not implemented yet) -->
<div id="comments" class="profile-content tab-content">
<div class="content-header">
<h2 class="section-title">
<i class="fas fa-comments"></i>
My Comments
</h2>
</div>
<?php if (mysqli_num_rows($comments_result) > 0): ?>
<div class="comments-container">
<?php while ($comment = mysqli_fetch_assoc($comments_result)): ?>
<div class="comment-card" data-comment-id="<?php echo $comment['comment_id']; ?>">
<div class="comment-header">
<div class="comment-post-title">
On post: <a href="post.php?id=<?php echo htmlspecialchars($comment['post_id']); ?>#comment-<?php echo $comment['comment_id']; ?>">
<?php echo htmlspecialchars($comment['post_title']); ?>
</a>
</div>
<div class="comment-meta">
<i class="far fa-clock"></i>
<?php echo time_elapsed_string($comment['created_at']); ?>
</div>
</div>
<div class="comment-body">
<?php echo nl2br(htmlspecialchars($comment['content'])); ?>
</div>
<div class="comment-footer">
<div class="comment-votes">
<button class="vote-btn vote-up <?php echo (is_logged_in() && get_user_vote($_SESSION['user_id'], null, $comment['comment_id']) == 1) ? 'upvoted' : ''; ?>" data-vote="1">
<i class="fas fa-arrow-up"></i>
</button>
<span class="vote-count"><?php echo $comment['upvotes'] - $comment['downvotes']; ?></span>
<button class="vote-btn vote-down <?php echo (is_logged_in() && get_user_vote($_SESSION['user_id'], null, $comment['comment_id']) == -1) ? 'downvoted' : ''; ?>" data-vote="-1">
<i class="fas fa-arrow-down"></i>
</button>
</div>
<?php if ($viewing_own): ?>
<div class="comment-actions">
<a href="post.php?id=<?php echo htmlspecialchars($comment['post_id']); ?>#comment-<?php echo $comment['comment_id']; ?>" class="comment-action">
<i class="fas fa-external-link-alt"></i> View
</a>
<button class="comment-action delete-btn" data-id="<?php echo $comment['comment_id']; ?>">
<i class="fas fa-trash"></i> Delete
</button>
</div>
<?php endif; ?>
</div>
</div>
<?php endwhile; ?>
</div>
<?php else: ?>
<div class="no-comments-message">
<div class="empty-state">
<div class="empty-icon">
<i class="fas fa-comments"></i>
</div>
<h3>No comments yet</h3>
<p>When you comment on posts, they'll appear here.</p>
</div>
</div>
<?php endif; ?>
</div>
<div id="upvoted" class="profile-content tab-content">
<div class="content-header">
<h2 class="section-title">
<i class="fas fa-arrow-up"></i>
Upvoted Posts
</h2>
</div>
<?php if (mysqli_num_rows($upvoted_result) > 0): ?>
<div class="posts-grid">
<?php while ($post = mysqli_fetch_assoc($upvoted_result)): ?>
<div class="post-card" data-post-id="<?php echo htmlspecialchars($post['post_id']); ?>">
<div class="vote-column">
<button class="vote-btn vote-up upvoted" data-vote="1">
<i class="fas fa-arrow-up"></i>
</button>
<div class="vote-count"><?php echo (int)($post['upvotes'] - $post['downvotes']); ?></div>
<button class="vote-btn vote-down" data-vote="-1">
<i class="fas fa-arrow-down"></i>
</button>
</div>
<div class="post-content">
<div class="post-header">
<div class="post-category">
<a href="index.php?category=<?php echo urlencode($post['category']); ?>">
<i class="fas fa-tag"></i>
<?php echo htmlspecialchars($post['category']); ?>
</a>
</div>
<div class="post-meta">
Posted by <a href="profile.php?user=<?php echo urlencode($post['username']); ?>">
<?php echo htmlspecialchars($post['username']); ?>
</a>
<i class="far fa-clock"></i>
<?php echo time_elapsed_string($post['created_at']); ?>
</div>
</div>
<h3 class="post-title">
<a href="post.php?id=<?php echo htmlspecialchars($post['post_id']); ?>">
<?php echo htmlspecialchars($post['title']); ?>
</a>
</h3>
<div class="post-body">
<?php
$content = nl2br(htmlspecialchars($post['content']));
echo (strlen($content) > 150) ? substr($content, 0, 150) . '...' : $content;
?>
</div>
<div class="post-footer">
<a href="post.php?id=<?php echo htmlspecialchars($post['post_id']); ?>" class="post-action">
<i class="fas fa-comment"></i>
<span><?php echo (int)$post['comment_count']; ?> Comments</span>
</a>
<a href="post.php?id=<?php echo htmlspecialchars($post['post_id']); ?>" class="post-action">
<i class="fas fa-external-link-alt"></i>
<span>View Post</span>
</a>
</div>
</div>
</div>
<?php endwhile; ?>
</div>
<?php else: ?>
<div class="no-upvoted-message">
<div class="empty-state">
<div class="empty-icon">
<i class="fas fa-arrow-up"></i>
</div>
<h3>No upvoted posts yet</h3>
<p>The posts you upvote will appear here.</p>
</div>
</div>
<?php endif; ?>
</div>
<?php if ($viewing_own): ?>
<div id="saved" class="profile-content tab-content">
<!-- Saved posts will be loaded here -->
<div class="empty-state">
<div class="empty-icon">
<i class="fas fa-bookmark"></i>
</div>
<h3>Saved Posts</h3>
<p>This feature is coming soon!</p>
</div>
</div>
<?php endif; ?>
</div>
</main>
<script>
// Profile tab navigation
document.addEventListener('DOMContentLoaded', function() {
const tabItems = document.querySelectorAll('.tab-item');
const tabContents = document.querySelectorAll('.tab-content');
tabItems.forEach(tab => {
tab.addEventListener('click', function(e) {
e.preventDefault();
// Remove active class from all tabs
tabItems.forEach(item => item.classList.remove('active'));
tabContents.forEach(content => content.classList.remove('active'));
// Add active class to current tab
this.classList.add('active');
// Show corresponding content
const target = this.getAttribute('href').substring(1);
document.getElementById(target).classList.add('active');
});
});
// Post menu toggle
document.querySelectorAll('.post-menu-toggle').forEach(toggle => {
toggle.addEventListener('click', function() {
const menu = this.nextElementSibling;
document.querySelectorAll('.post-menu').forEach(m => {
if (m !== menu) m.classList.remove('active');
});
menu.classList.toggle('active');
});
});
// Close post menus when clicking outside
document.addEventListener('click', function(e) {
if (!e.target.closest('.post-management')) {
document.querySelectorAll('.post-menu').forEach(menu => {
menu.classList.remove('active');
});
}
});
// Handle vote buttons for comments
const commentVoteButtons = document.querySelectorAll('.comment-votes .vote-btn');
commentVoteButtons.forEach(button => {
button.addEventListener('click', function() {
const commentCard = this.closest('.comment-card');
const commentId = commentCard.dataset.commentId;
const voteType = this.dataset.vote;
const voteCount = commentCard.querySelector('.vote-count');
const upvoteBtn = commentCard.querySelector('.vote-up');
const downvoteBtn = commentCard.querySelector('.vote-down');
// Call AJAX to vote on comment
fetch('ajax/vote.php', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: `comment_id=${commentId}&vote_type=${voteType}`
})
.then(response => response.json())
.then(data => {
if (data.success) {
// Update vote count
voteCount.textContent = data.vote_count || data.voteCount;
// Update button states
upvoteBtn.classList.remove('upvoted');
downvoteBtn.classList.remove('downvoted');
if (data.user_vote === 1 || data.newVoteType === 1) {
upvoteBtn.classList.add('upvoted');
} else if (data.user_vote === -1 || data.newVoteType === -1) {
downvoteBtn.classList.add('downvoted');
}
showNotification('Vote recorded successfully', 'success');
}
})
.catch(error => {
console.error('Error:', error);
});
});
});
// Handle delete buttons for comments
const deleteButtons = document.querySelectorAll('.comment-action.delete-btn');
deleteButtons.forEach(button => {
button.addEventListener('click', function() {
if (confirm('Are you sure you want to delete this comment? This action cannot be undone.')) {
const commentId = this.dataset.id;
fetch('ajax/delete_comment.php', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: `comment_id=${commentId}`
})
.then(response => response.json())
.then(data => {
if (data.success) {
// Remove comment from DOM with animation
const commentCard = this.closest('.comment-card');
commentCard.style.opacity = '0';
setTimeout(() => {
commentCard.remove();
showNotification('Comment deleted successfully', 'success');
}, 300);
} else {
showNotification(data.message || 'Failed to delete comment', 'error');
}
})
.catch(error => {
console.error('Error:', error);
showNotification('An error occurred', 'error');
});
}
});
});
// Post vote buttons in upvoted tab
const postVoteButtons = document.querySelectorAll('.post-card .vote-btn');
postVoteButtons.forEach(button => {
button.addEventListener('click', function() {
const postCard = this.closest('.post-card');
const postId = postCard.dataset.postId;
const voteType = this.dataset.vote;
const voteCount = postCard.querySelector('.vote-count');
const upvoteBtn = postCard.querySelector('.vote-up');
const downvoteBtn = postCard.querySelector('.vote-down');
// Call AJAX to vote on post
fetch('ajax/vote.php', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: `post_id=${postId}&vote_type=${voteType}`
})
.then(response => response.json())
.then(data => {
if (data.success) {
// Update vote count
voteCount.textContent = data.vote_count || data.voteCount;
// Update button states
upvoteBtn.classList.remove('upvoted');
downvoteBtn.classList.remove('downvoted');
if (data.user_vote === 1 || data.newVoteType === 1) {
upvoteBtn.classList.add('upvoted');
} else if (data.user_vote === -1 || data.newVoteType === -1) {
downvoteBtn.classList.add('downvoted');
}
showNotification('Vote recorded successfully', 'success');
}
})
.catch(error => {
console.error('Error:', error);
});
});
});
// Simple notification function
window.showNotification = function(message, type = 'info') {
const notification = document.createElement('div');
notification.className = `notification ${type}`;
notification.innerHTML = `
<div class="notification-content">
<i class="fas ${type === 'success' ? 'fa-check-circle' : 'fa-info-circle'}"></i>
<span>${message}</span>
</div>
`;
document.body.appendChild(notification);
// Animate in
setTimeout(() => {
notification.style.transform = 'translateY(0)';
notification.style.opacity = '1';
}, 10);
// Remove after a delay
setTimeout(() => {
notification.style.transform = 'translateY(-10px)';
notification.style.opacity = '0';
setTimeout(() => {
notification.remove();
}, 300);
}, 3000);
};
});
</script>
<?php
include 'includes/footer.php';
// Flush the output buffer at the end
ob_end_flush();
?>