diff --git a/app/pages/includes/post-card.php b/app/pages/includes/post-card.php index cdd91dd2cfadd99cbe32096ab23f5529047495ae..15e1ee82be7e37b72ffc0030a5c2a2c902d1e063 100644 --- a/app/pages/includes/post-card.php +++ b/app/pages/includes/post-card.php @@ -67,7 +67,7 @@ <button class="btn btn-link p-0 like-btn" data-post-id="<?= $id ?>"> <i class="bi <?= $isLiked ? 'bi-heart-fill text-danger' : 'bi-heart' ?> like-icon fs-5"></i> </button> - <a href="<?=ROOT?>/post/<?=$post['slug']?>" class="text-muted text-decoration-none"><span class="ms-1 like-count"><?= $likes ?></span></a> + <a href="<?=ROOT?>/post/<?=$post['slug']?>" class="text-muted text-decoration-none"><span class="ms-1 like-count"><?= $likes ?> Likes</span></a> </div> <!-- Comments --> diff --git a/app/pages/post.php b/app/pages/post.php index 9db1ef3f57851fdb199c188a221199fd5105dc2b..d7e7a4dede70d5c7b5be74cf4ba6c9d450620744 100644 --- a/app/pages/post.php +++ b/app/pages/post.php @@ -21,18 +21,11 @@ if(!empty($_POST['comment']) && ($_POST['postid']) && ($_POST['userid'])){ $data['userid'] = $_POST['userid']; $data['postid'] = $_POST['postid']; $data['comment'] = $_POST['comment']; - $slug = md5($_POST['comment']); - - $query = "SELECT id FROM comments WHERE slug=:slug AND active=1 LIMIT 1"; - $existing_slug = query($query, ['slug' => $slug]); - - // Ensure $slug is a string - if (!empty($existing_slug)) { - $slug .= rand(1000, 9999); - } + + $data['date'] = date('Y-m-d H:i:s'); - $query = "INSERT INTO comments(user_id,post_id,comment,slug,date) values(:userid,:postid,:comment,:slug,:date)"; - // query($query,$data); + $query = "INSERT INTO comments(user_id,post_id,comment,date) values(:userid,:postid,:comment,:date)"; + query($query,$data); } if(!empty($_POST['edited_comment']) && ($_POST['comment_id'])){ $data['comment_id'] = $_POST['comment_id']; @@ -120,35 +113,36 @@ if($result){ </div> </div> - <!-- Liked By Section --> - <div class="mt-4"> - <h5 class="fw-bold">Liked by</h5> - <div class="liked-by border rounded p-3 bg-light d-flex align-items-center"> - <?php - $like_query = "SELECT users.id, users.username, users.image FROM likes + <!-- Liked By Section --> + <div class="mt-4"> + <h5 class="fw-bold">Liked by</h5> + <div class="liked-by border rounded p-3 bg-light d-flex flex-wrap align-items-center justify-content-start gap-2"> + <?php + $like_query = "SELECT users.id, users.username, users.image FROM likes JOIN users ON likes.user_id = users.id WHERE likes.post_id = $id LIMIT 3"; // Limit to 3 users for a clean UI - $liked_users = query($like_query); - - if (!empty($liked_users)): - foreach ($liked_users as $user): ?> - <a href="<?= ROOT ?>/profile/<?= $user['id'] ?>" class="d-flex align-items-center text-decoration-none me-3"> - <img src="<?= get_image($user['image']) ?>" alt="User Image" class="rounded-circle border shadow-sm me-1" - style="width: 30px; height: 30px; object-fit: cover;"> - <span class="fw-bold text-primary"><?= htmlspecialchars($user['username']) ?> ,</span> - </a> - <?php endforeach; - - if (count($liked_users) >= 3): ?> - <span class="text-muted">and others</span> - <?php endif; - else: - echo '<p class="mb-0 text-muted">Be the first to like this!</p>'; - endif; - ?> - </div> + $liked_users = query($like_query); + + if (!empty($liked_users)): + foreach ($liked_users as $user): ?> + <a href="<?= ROOT ?>/profile/<?= $user['id'] ?>" class="d-flex align-items-center text-decoration-none"> + <img src="<?= get_image($user['image']) ?>" alt="User Image" class="rounded-circle border shadow-sm me-1" + style="width: 30px; height: 30px; object-fit: cover;"> + <span class="fw-bold text-primary"><?= htmlspecialchars($user['username']) ?></span> + </a> + <?php endforeach; + + if (count($liked_users) >= 3): ?> + <span class="text-muted">and others</span> + <?php endif; + else: + echo '<p class="mb-0 text-muted">Be the first to like this!</p>'; + endif; + ?> </div> + </div> + @@ -156,43 +150,81 @@ if($result){ <div class="mt-4"> <h5 class="fw-bold">Comments</h5> <div class="comments-section border rounded p-3 text-body back bg-secondary" data-bs-theme="dark"> - <?php - $query1 = "SELECT comments.*, a.username, a.image AS user_image, a.id AS comment_user_id - FROM comments - JOIN users AS a ON comments.user_id = a.id - WHERE comments.active = 1 AND comments.post_id = $id"; - $comments = query($query1); - ?> - <?php foreach ($comments as $comment): ?> - <div class="d-flex align-items-start mb-3"> - <a class="text-decoration-none" href="<?=ROOT?>/profile/<?=$comment['user_id']?>"> - <img src="<?= get_image($comment['user_image']) ?>" class="rounded-circle border me-2 shadow-sm" - style="width: 40px; height: 40px; object-fit: cover;"> + <?php + $query1 = "SELECT comments.*, a.username, a.image AS user_image, a.id AS comment_user_id + FROM comments + JOIN users AS a ON comments.user_id = a.id + WHERE comments.active = 1 AND comments.post_id = $id"; + $comments = query($query1); + ?> + + <?php foreach ($comments as $comment): ?> + <div class="d-flex align-items-start mb-3"> + <a class="text-decoration-none" href="<?=ROOT?>/profile/<?=$comment['user_id']?>"> + <img src="<?= get_image($comment['user_image']) ?>" class="rounded-circle border me-2 shadow-sm" + style="width: 40px; height: 40px; object-fit: cover;"> + </a> + <div class="flex-grow-1"> + <a class="text-decoration-none text-dark" href="<?=ROOT?>/profile/<?=$comment['user_id']?>"> + <strong><?= $comment['username'] ?></strong> </a> - <div class="flex-grow-1"> - <a class="text-decoration-none text-dark" href="<?=ROOT?>/profile/<?=$comment['user_id']?>"> - <strong><?= $comment['username'] ?></strong> - </a> - <p class="mb-1 text-dark comment-text" id="comment-text-<?= $comment['id'] ?>"> - <?= $comment['comment'] ?> - </p> - <small class="text-dark"><?= timepicker($comment['date']) ?></small> - </div> + + <p class="mb-1 text-dark comment-text" id="comment-text-<?= $comment['id'] ?>"> + <?= $comment['comment'] ?> + </p> + + <!-- Edit Form (Initially Hidden) --> + <div id="edit-form-<?= $comment['id'] ?>" class="d-none edit-comment-form" data-id="<?= $comment['id'] ?>"> + <form action="" method="POST"> + <input type="hidden" name="comment_id" value="<?= $comment['id'] ?>"> + <input type="text" name="edited_comment" class="form-control" value="<?= $comment['comment'] ?>"> + <button type="submit" class="btn btn-success btn-sm mt-2">Save</button> + <button type="button" class="btn btn-success btn-sm cancel-edit mt-2">Cancel</button> - <?php if ($comment['user_id'] == $_SESSION['user_id'] || $userid == $_SESSION['user_id']): ?> - <div class="ms-2"> - <button class="btn btn-sm btn-outline-primary edit-btn" data-id="<?= $comment['id'] ?>"> - <i class="bi bi-pencil"></i> - </button> - <button class="btn btn-sm btn-outline-danger delete-btn" data-bs-toggle="modal" - data-bs-target="#deleteCommentModal-<?= $comment['id'] ?>"> - <i class="bi bi-trash"></i> - </button> + </form> + </div> + + <small class="text-dark"> <?= timepicker($comment['date']) ?> </small> + </div> + + <?php if ($comment['user_id'] == $_SESSION['user_id'] || $userid == $_SESSION['user_id']): ?> + <div class="ms-2"> + <?php if ($comment['user_id'] == $_SESSION['user_id']):?> + <button class="btn btn-sm btn-outline-primary edit-btn" data-id="<?= $comment['id'] ?>"> + <i class="bi bi-pencil"></i> + </button> + <?php endif;?> + <button class="btn btn-sm btn-outline-danger delete-btn" data-bs-toggle="modal" + data-bs-target="#deleteCommentModal-<?= $comment['id'] ?>"> + <i class="bi bi-trash"></i> + </button> + </div> + <?php endif; ?> + </div> + + <!-- Delete Modal --> + <div class="modal fade" id="deleteCommentModal-<?= $comment['id'] ?>" tabindex="-1" aria-hidden="true"> + <div class="modal-dialog"> + <div class="modal-content"> + <div class="modal-header"> + <h5 class="modal-title">Delete Comment</h5> + <button type="button" class="btn-close" data-bs-dismiss="modal"></button> + </div> + <div class="modal-body"> Are you sure you want to delete this comment? </div> + <div class="modal-footer"> + <form action="" method="POST"> + <input type="hidden" name="del_comment_id" value="<?= $comment['id'] ?>"> + <button type="submit" class="btn btn-danger">Delete</button> + </form> + <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button> </div> - <?php endif; ?> + </div> </div> - <?php endforeach; ?> - </div> + </div> + <?php endforeach; ?> + </div> + + <?php $userimage = find('users', $_SESSION['user_id'], 'image'); ?> @@ -226,12 +258,6 @@ if($result){ </div> </div> - - - - - - <?php endforeach;?> <? @@ -243,4 +269,48 @@ else{ } ?> +<!-- Delete Confirmation Modal --> +<div class="modal fade" id="deleteModal" tabindex="-1" aria-labelledby="deleteModalLabel" aria-hidden="true"> + <div class="modal-dialog"> + <div class="modal-content"> + <div class="modal-header"> + <h5 class="modal-title" id="deleteModalLabel">Confirm Deletion</h5> + <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button> + </div> + <div class="modal-body"> + Are you sure you want to delete this post? + </div> + <div class="modal-footer"> + <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button> + <a href="<?= ROOT ?>/user/post/delete/<?= $id ?>" class="btn btn-danger">Delete</a> + </div> + </div> + </div> +</div> + +<!-- JavaScript for Edit and Delete --> +<script> + document.addEventListener("DOMContentLoaded", function () { + // Show edit form when clicking the edit button + document.querySelectorAll(".edit-btn").forEach(btn => { + btn.addEventListener("click", function () { + let commentId = this.getAttribute("data-id"); + document.getElementById("comment-text-" + commentId).classList.add("d-none"); + document.getElementById("edit-form-" + commentId).classList.remove("d-none"); + }); + }); + + // Cancel edit and revert back to original comment + document.querySelectorAll(".cancel-edit").forEach(button => { + button.addEventListener("click", function () { + let form = this.closest(".edit-comment-form"); + let commentId = form.getAttribute("data-id"); + + document.getElementById("comment-text-" + commentId).classList.remove("d-none"); + form.classList.add("d-none"); + }); + }); +}); + +</script> <!--blog--> diff --git a/app/pages/profile.php b/app/pages/profile.php index a755eea42564e9c8b619121d1faa53c47bce3d4b..f51aedfa41154feb0239aa2047d382aea98079af 100644 --- a/app/pages/profile.php +++ b/app/pages/profile.php @@ -431,28 +431,35 @@ $postn = $post_no[0]['count']; <a href="<?=ROOT?>/post/<?=$post['slug']?>"> <img src="<?= get_image($image) ?>" class="card-img-top gallery-image" alt="Post Image"> </a> + <?php + $query = "SELECT COUNT(*) as num FROM likes WHERE post_id = $post[id]"; + $likes = query_row($query); + $likes = $likes['num']; + $query = "Select count(id) as num from comments where post_id = $id and active = 1"; + $num_comment = query_row($query); + ?> <div class="card-body text-center"> - <ul class="list-inline mb-0"> + <a href="<?=ROOT?>/post/<?=$post['slug']?>" class="text-dark text-decoration-none"> + <ul class="list-inline mb-0"> <li class="list-inline-item"> - <i class="bi bi-heart-fill"></i> 56 + <i class="bi bi-heart-fill"></i> <?=$likes?> </li> <li class="list-inline-item"> - <i class="bi bi-chat-fill"></i> 2 + <i class="bi bi-chat-fill"></i> <?=$num_comment['num']?> </li> </ul> + </a> </div> </div> </div> <?php endforeach; ?> + + <?php else:?> + <h3><center>No Posts</center></h3> <?php endif; ?> </div> </div> - - <!-- End of gallery --> - - <div class="loader"></div> - </div> <!-- End of container --> diff --git a/app/pages/user/user-controller.php b/app/pages/user/user-controller.php index eb84eebe1f24204aa3e09225eca589a5235032af..9f5441df7028ad3e5f27ba389a03178eb7768aa9 100644 --- a/app/pages/user/user-controller.php +++ b/app/pages/user/user-controller.php @@ -117,7 +117,8 @@ if($action=='edit'){ if(empty($_POST['password'])) { // echo "empty"; - $query="UPDATE users SET username=:username,email=:email,image=:image WHERE id=:id"; + $data['bio']=$_POST['bio']; + $query="UPDATE users SET username=:username,email=:email,bio=:bio,image=:image WHERE id=:id"; } else { @@ -129,7 +130,7 @@ if($action=='edit'){ $query="UPDATE users SET username=:username,email=:email,password=:password,bio=:bio,image=:image,role=:role WHERE id=:id"; } - query($query,$data); + query($query,$data); redirect(ROOT.'/profile/'.$_SESSION['user_id']); diff --git a/app/pages/user/user.php b/app/pages/user/user.php index 8ed4e58f02383eec5e416325a4f01221c75f8752..95c05710b4b486f9b428ba52aba6be9f959f670d 100644 --- a/app/pages/user/user.php +++ b/app/pages/user/user.php @@ -73,6 +73,8 @@ </select> <label for="floatingRole">Role</label> </div> + <?php else: ?> + <input type="hidden" value="<?=$user['role']?>" name="role"> <?php endif; ?> <!-- Buttons -->