diff --git a/messages.php b/messages.php index a4dfb344..eab6b1c0 100644 --- a/messages.php +++ b/messages.php @@ -31,6 +31,7 @@
" + JSON.stringify(row) + ""; } @@ -112,7 +101,23 @@ $(function () { { data: "blob3", visible: false }, { data: "blob4", visible: false }, { data: "blob5", visible: false }, + { data: null, width: "80px", orderable: false }, ], + drawCallback: function () { + $('button[id^="deleteMessage_"]').on("click", deleteMessage); + // Remove visible dropdown to prevent orphaning + $("body > .bootstrap-select.dropdown").remove(); + }, + rowCallback: function (row, data) { + $(row).attr("data-id", data.id); + var button = + '"; + $("td:eq(3)", row).html(button); + }, dom: "<'row'<'col-sm-4'l><'col-sm-8'f>>" + "<'row'<'col-sm-12'<'table-responsive'tr>>>" + @@ -148,3 +153,31 @@ $(function () { }, }); }); + +function deleteMessage() { + var tr = $(this).closest("tr"); + var id = tr.attr("data-id"); + + utils.disableAll(); + utils.showAlert("info", "", "Deleting message # ",id); + $.ajax({ + url: "scripts/pi-hole/php/message.php", + method: "post", + dataType: "json", + data: { action: "delete_message", id: id, token: token }, + success: function (response) { + utils.enableAll(); + if (response.success) { + utils.showAlert("success", "far fa-trash-alt", "Successfully deleted message # ", id); + table.row(tr).remove().draw(false).ajax.reload(null, false); + } else { + utils.showAlert("error", "", "Error while deleting message with ID " + id, response.message); + } + }, + error: function (jqXHR, exception) { + utils.enableAll(); + utils.showAlert("error", "", "Error while deleting message with ID " + id, jqXHR.responseText); + console.log(exception); // eslint-disable-line no-console + } + }); +}; diff --git a/scripts/pi-hole/php/message.php b/scripts/pi-hole/php/message.php new file mode 100644 index 00000000..cfbb89a6 --- /dev/null +++ b/scripts/pi-hole/php/message.php @@ -0,0 +1,74 @@ + + true, 'message' => $message)); +} + +function JSON_error($message = null) +{ + header('Content-type: application/json'); + $response = array('success' => false, 'message' => $message); + if (isset($_POST['action'])) { + array_push($response, array('action' => $_POST['action'])); + } + echo json_encode($response); +} + +if ($_POST['action'] == 'delete_message') { +// Delete message identified by ID + try { + $db->query('BEGIN TRANSACTION;'); + + $stmt = $db->prepare('DELETE FROM message WHERE id=:id'); + if (!$stmt) { + throw new Exception('While preparing message statement: ' . $db->lastErrorMsg()); + } + + if (!$stmt->bindValue(':id', intval($_POST['id']), SQLITE3_INTEGER)) { + throw new Exception('While binding id to message statement: ' . $db->lastErrorMsg()); + } + + if (!$stmt->execute()) { + throw new Exception('While executing message statement: ' . $db->lastErrorMsg()); + } + + if(!$db->query('COMMIT;')) { + throw new Exception('While commiting changes to the database: ' . $db->lastErrorMsg()); + } + + $reload = true; + JSON_success(); + } catch (\Exception $ex) { + JSON_error($ex->getMessage()); + } +} else { + log_and_die('Requested action not supported!'); +}