Add delete button to message table

Signed-off-by: yubiuser <ckoenig@posteo.de>
This commit is contained in:
yubiuser
2021-08-08 22:19:06 +02:00
parent 1cefaf98ab
commit 9aaa45a034
3 changed files with 119 additions and 11 deletions
+1
View File
@@ -31,6 +31,7 @@
<th>Data3</th>
<th>Data4</th>
<th>Data5</th>
<th>Action</th>
</tr>
</thead>
</table>
+44 -11
View File
@@ -77,17 +77,6 @@ function renderMessage(data, type, row) {
case "DNSMASQ_CONFIG":
return "FTL failed to start due to " + row.message;
case "RATE_LIMIT":
return (
"Client " +
row.message +
" has been rate-limited (current config allows up to " +
parseInt(row.blob1, 10) +
" queries in " +
parseInt(row.blob2, 10) +
" seconds)"
);
default:
return "Unknown message type<pre>" + JSON.stringify(row) + "</pre>";
}
@@ -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 =
'<button type="button" class="btn btn-danger btn-xs" id="deleteMessage_' +
data.id +
'">' +
'<span class="far fa-trash-alt"></span>' +
"</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
}
});
};
+74
View File
@@ -0,0 +1,74 @@
<?php
/* Pi-hole: A black hole for Internet advertisements
* (c) 2021 Pi-hole, LLC (https://pi-hole.net)
* Network-wide ad blocking via your own hardware.
*
* This file is copyright under the latest version of the EUPL.
* Please see LICENSE file for your rights under this license. */
require_once('auth.php');
// Authentication checks
if (!isset($api)) {
if (isset($_POST['token'])) {
check_cors();
check_csrf($_POST['token']);
} else {
log_and_die('Not allowed (login session invalid or expired, please relogin on the Pi-hole dashboard)!');
}
}
$reload = false;
require_once('func.php');
require_once('database.php');
$QueriesDB = getQueriesDBFilename();
$db = SQLite3_connect($QueriesDB, SQLITE3_OPEN_READWRITE);
function JSON_success($message = null)
{
header('Content-type: application/json');
echo json_encode(array('success' => 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!');
}