Merge pull request #1160 from pi-hole/new/clients_comment_fields

Add support for client comment and timestamp fields
This commit is contained in:
DL6ER
2020-02-18 17:41:29 +01:00
committed by GitHub
3 changed files with 70 additions and 11 deletions

View File

@@ -30,11 +30,12 @@
<label for="ex1">Known clients:</label>
<select id="select" class="form-control" placeholder="">
<option disabled selected>Loading...</option>
</select>
</select><br>
<input id="ip-custom" type="text" class="form-control" disabled placeholder="Client IP address (IPv4 or IPv6, optional)">
</div>
<div class="col-md-6">
<label for="ex2">Custom client:</label>
<input id="ip-custom" type="text" class="form-control" disabled placeholder="Client IP address (IPv4 or IPv6, optional)">
<label for="ex3">Comment:</label>
<input id="new_comment" type="text" class="form-control" placeholder="Client description (optional)">
</div>
</div>
</div>
@@ -59,6 +60,7 @@
<tr>
<th>ID</th>
<th>IP address</th>
<th>Comment</th>
<th>Group assignment</th>
<th>Action</th>
</tr>

View File

@@ -38,7 +38,7 @@ function reload_client_suggestions() {
sel.append(
$("<option />")
.val("custom")
.text("Custom, specified on the right")
.text("Custom, specified below...")
);
},
"json"
@@ -80,6 +80,7 @@ function initTable() {
columns: [
{ data: "id", visible: false },
{ data: "ip" },
{ data: "comment" },
{ data: "groups", searchable: false },
{ data: "name", width: "80px", orderable: false }
],
@@ -87,7 +88,13 @@ function initTable() {
$(".deleteClient").on("click", deleteClient);
},
rowCallback: function(row, data) {
var tooltip = "Database ID: " + data.id;
var tooltip =
"Added: " +
utils.datetime(data.date_added) +
"\nLast modified: " +
utils.datetime(data.date_modified) +
"\nDatabase ID: " +
data.id;
var ip_name =
'<code id="ip" title="' +
tooltip +
@@ -100,8 +107,16 @@ function initTable() {
ip_name += '<br><code id="name" title="' + tooltip + '">' + data.name + "</code>";
$("td:eq(0)", row).html(ip_name);
$("td:eq(1)", row).empty();
$("td:eq(1)", row).append(
$("td:eq(1)", row).html(
'<input id="comment" class="form-control"><input id="id" type="hidden" value="' +
data.id +
'">'
);
$("#comment", row).val(data.comment);
$("#comment", row).on("change", editClient);
$("td:eq(2)", row).empty();
$("td:eq(2)", row).append(
'<div id="selectHome' +
data.id +
'"><select id="multiselect" multiple="multiple"></select></div>'
@@ -161,7 +176,7 @@ function initTable() {
'">' +
'<span class="glyphicon glyphicon-trash"></span>' +
"</button>";
$("td:eq(2)", row).html(button);
$("td:eq(3)", row).html(button);
},
dom:
"<'row'<'col-sm-4'l><'col-sm-8'f>>" +
@@ -212,6 +227,7 @@ function initTable() {
function addClient() {
var ip = $("#select").val();
var comment = $("#new_comment").val();
if (ip === "custom") {
ip = $("#ip-custom").val();
}
@@ -229,7 +245,7 @@ function addClient() {
url: "scripts/pi-hole/php/groups.php",
method: "post",
dataType: "json",
data: { action: "add_client", ip: ip, token: token },
data: { action: "add_client", ip: ip, comment: comment, token: token },
success: function(response) {
utils.enableAll();
if (response.success) {
@@ -255,12 +271,16 @@ function editClient() {
var groups = tr.find("#multiselect").val();
var ip = tr.find("#ip").text();
var name = tr.find("#name").text();
var comment = tr.find("#comment").val();
var done = "edited";
var not_done = "editing";
if (elem === "multiselect") {
done = "edited groups of";
not_done = "editing groups of";
} else if (elem === "comment") {
done = "edited comment of";
not_done = "editing comment of";
}
var ip_name = ip;
@@ -274,7 +294,13 @@ function editClient() {
url: "scripts/pi-hole/php/groups.php",
method: "post",
dataType: "json",
data: { action: "edit_client", id: id, groups: groups, token: token },
data: {
action: "edit_client",
id: id,
groups: groups,
token: token,
comment: comment
},
success: function(response) {
utils.enableAll();
if (response.success) {

View File

@@ -232,7 +232,7 @@ if ($_POST['action'] == 'get_groups') {
} elseif ($_POST['action'] == 'add_client') {
// Add new client
try {
$stmt = $db->prepare('INSERT INTO client (ip) VALUES (:ip)');
$stmt = $db->prepare('INSERT INTO client (ip,comment) VALUES (:ip,:comment)');
if (!$stmt) {
throw new Exception('While preparing statement: ' . $db->lastErrorMsg());
}
@@ -241,6 +241,15 @@ if ($_POST['action'] == 'get_groups') {
throw new Exception('While binding ip: ' . $db->lastErrorMsg());
}
$comment = $_POST['comment'];
if (strlen($comment) == 0) {
// Store NULL in database for empty comments
$comment = null;
}
if (!$stmt->bindValue(':comment', $comment, SQLITE3_TEXT)) {
throw new Exception('While binding comment: ' . $db->lastErrorMsg());
}
if (!$stmt->execute()) {
throw new Exception('While executing: ' . $db->lastErrorMsg());
}
@@ -253,6 +262,28 @@ if ($_POST['action'] == 'get_groups') {
} elseif ($_POST['action'] == 'edit_client') {
// Edit client identified by ID
try {
$stmt = $db->prepare('UPDATE client SET comment=:comment WHERE id = :id');
if (!$stmt) {
throw new Exception('While preparing statement: ' . $db->lastErrorMsg());
}
$comment = $_POST['comment'];
if (strlen($comment) == 0) {
// Store NULL in database for empty comments
$comment = null;
}
if (!$stmt->bindValue(':comment', $comment, SQLITE3_TEXT)) {
throw new Exception('While binding comment: ' . $db->lastErrorMsg());
}
if (!$stmt->bindValue(':id', intval($_POST['id']), SQLITE3_INTEGER)) {
throw new Exception('While binding id: ' . $db->lastErrorMsg());
}
if (!$stmt->execute()) {
throw new Exception('While executing: ' . $db->lastErrorMsg());
}
$stmt = $db->prepare('DELETE FROM client_by_group WHERE client_id = :id');
if (!$stmt) {
throw new Exception('While preparing DELETE statement: ' . $db->lastErrorMsg());