Skip to content

Commit

Permalink
Manage comment functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Edward144 committed Sep 29, 2021
1 parent d532864 commit 249ca4d
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 1 deletion.
49 changes: 49 additions & 0 deletions admin/manage-comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,55 @@
checkaccess(basename(__FILE__));
$title = 'Manage Comments';

//Modify Comment
if(isset($_POST['method']) && $_POST['method'] == 'modifyComment') {
$currentComment = $mysqli->prepare("SELECT original_content, modified FROM `comments` WHERE id = ?");
$currentComment->bind_param('i', $_POST['id']);
$currentComment->execute();
$commentResult = $currentComment->get_result();

if($commentResult->num_rows > 0) {
$currComment = $commentResult->fetch_assoc();
$_POST['comment'] = (empty($_POST['comment']) ? $currComment['original_content'] : $_POST['comment']);

if($currComment['modified'] == 1) {
$modify = $mysqli->prepare("UPDATE `comments` SET content = ?, modified = 1, approved = ? WHERE id = ?");
$modify->bind_param('sii', $_POST['comment'], $_POST['approved'], $_POST['id']);
}
else {
$modify = $mysqli->prepare("UPDATE `comments` SET content = ?, original_content = content, modified = 1, approved = ? WHERE id = ?");
$modify->bind_param('sii', $_POST['comment'], $_POST['approved'], $_POST['id']);
}

$modify->execute();

if($modify->error) {
echo json_encode(['status' => 'danger', 'message' => 'Failed to modify comment']);
}
else {
echo json_encode(['status' => 'success', 'message' => 'Comment modified successfully']);
}
}

exit();
}

//Delete Comment
if(isset($_POST['method']) && $_POST['method'] == 'deleteComment') {
$delete = $mysqli->prepare("DELETE FROM `comments` WHERE id = ?");
$delete->bind_param('i', $_POST['id']);
$delete->execute();

if($delete->error) {
echo json_encode(['status' => 'danger', 'message' => 'Failed to delete comment']);
}
else {
echo json_encode(['status' => 'success', 'message' => 'Successfully deleted comment']);
}

exit();
}

require_once(dirname(__FILE__) . '/includes/header.php')
?>

Expand Down
75 changes: 75 additions & 0 deletions js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,81 @@ $(".deleteRole").submit(function() {
}
});

//Delete Comment
$("input[name='deleteComment']").click(function() {
var btn = $(this);

if(confirm("Are you sure you want to delete this comment? Any replies will also be hidden.")) {
if(btn.attr("data-id").length) {
$.ajax({
url: window.location.pathname,
method: "post",
dataType: "json",
data: ({id: $(this).attr("data-id"), method: "deleteComment"}),
success: function(data) {
if(data["status"] == "success") {
location.reload();
}
else {
var message = "<div class='alert alert-" + data["status"] + "'>" + data["message"] + "</div>";

if(btn.parents("table").first().length > 0) {
btn.parents("table").first().find(".alert").remove();
$(message).insertBefore(btn.parents("table").first());
}
else {
btn.parents("form").first().find(".alert").remove();
$(message).appendTo(btn.parents("form").first());
}
}
}
});
}
}
});

//Modify Comment
$("input[name='modifyComment']").click(function() {
$(this).parents("table").first().find(".alert").remove();

var btn = $(this);
var id = btn.attr("data-id");
var comment = btn.parents("tr").first().find("textarea[name='comment']").val();

if(btn.siblings(".form-check").children("input[name='approved']").is(":checked")) {
var approved = 1;
}
else {
var approved = 0;
}

if(btn.attr("data-id").length) {
$.ajax({
url: window.location.pathname,
method: "post",
dataType: "json",
data: ({id: id, approved: approved, comment: comment, method: "modifyComment"}),
success: function(data) {
if(data["status"] == "success") {
location.reload();
}
else {
var message = "<div class='alert alert-" + data["status"] + "'>" + data["message"] + "</div>";

if(btn.parents("table").first().length > 0) {
btn.parents("table").first().find(".alert").remove();
$(message).insertBefore(btn.parents("table").first());
}
else {
btn.parents("form").first().find(".alert").remove();
$(message).appendTo(btn.parents("form").first());
}
}
}
});
}
});

////Carousel

//Show Controls
Expand Down
Loading

0 comments on commit 249ca4d

Please sign in to comment.