mirror of
https://github.com/pi-hole/web.git
synced 2025-12-24 12:48:29 +00:00
Wrap addition and removal of domains in a transaction + deliver a more detailed success message to the user specifying how many domains are actually added (in case some already existed).
Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
2
list.php
2
list.php
@@ -49,7 +49,7 @@ function getFullName() {
|
||||
</div>
|
||||
<div id="alSuccess" class="alert alert-success alert-dismissible fade in" role="alert" hidden="true">
|
||||
<button type="button" class="close" data-hide="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
|
||||
Success!
|
||||
<span id="success-message"></span>
|
||||
</div>
|
||||
<div id="alFailure" class="alert alert-danger alert-dismissible fade in" role="alert" hidden="true">
|
||||
<button type="button" class="close" data-hide="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
|
||||
|
||||
@@ -123,6 +123,10 @@ function sub(index, entry, arg) {
|
||||
var alSuccess = $("#alSuccess");
|
||||
var alFailure = $("#alFailure");
|
||||
var alWarning = $("#alWarning");
|
||||
var err = $("#err");
|
||||
var warn = $("#warn");
|
||||
var msg = $("#success-message");
|
||||
|
||||
|
||||
var domain = $(list+" #"+index);
|
||||
domain.hide("highlight");
|
||||
@@ -142,6 +146,7 @@ function sub(index, entry, arg) {
|
||||
});
|
||||
} else {
|
||||
alSuccess.show();
|
||||
msg.html(response);
|
||||
alSuccess.delay(1000).fadeOut(2000, function() {
|
||||
alSuccess.hide();
|
||||
});
|
||||
@@ -174,6 +179,7 @@ function add(type) {
|
||||
var alWarning = $("#alWarning");
|
||||
var err = $("#err");
|
||||
var warn = $("#warn");
|
||||
var msg = $("#success-message");
|
||||
alInfo.show();
|
||||
alSuccess.hide();
|
||||
alFailure.hide();
|
||||
@@ -194,6 +200,7 @@ function add(type) {
|
||||
});
|
||||
} else {
|
||||
alSuccess.show();
|
||||
msg.html(response);
|
||||
alSuccess.delay(1000).fadeOut(2000, function() {
|
||||
alSuccess.hide();
|
||||
});
|
||||
|
||||
@@ -72,6 +72,16 @@ function SQLite3_connect($filename, $mode=SQLITE3_OPEN_READONLY)
|
||||
*/
|
||||
function add_to_table($db, $table, $domains, $wildcardstyle=false, $returnnum=false)
|
||||
{
|
||||
// Begin transaction
|
||||
if(!$db->exec("BEGIN TRANSACTION;"))
|
||||
{
|
||||
if($returnnum)
|
||||
return 0;
|
||||
else
|
||||
return "Error: Unable to begin transaction for ".$table." table.";
|
||||
}
|
||||
$initialcount = intval($db->querySingle("SELECT COUNT(*) FROM ".$table.";"));
|
||||
|
||||
// Prepare SQLite statememt
|
||||
$stmt = $db->prepare("INSERT OR IGNORE INTO ".$table." (domain) VALUES (:domain);");
|
||||
|
||||
@@ -105,16 +115,32 @@ function add_to_table($db, $table, $domains, $wildcardstyle=false, $returnnum=fa
|
||||
if($returnnum)
|
||||
return $num;
|
||||
else
|
||||
return "Error: ".$db->lastErrorMsg().", added: ".$num;
|
||||
{
|
||||
if($num === 1)
|
||||
$plural = "";
|
||||
else
|
||||
$plural = "s";
|
||||
return "Error: ".$db->lastErrorMsg().", added ".$num." domain".$plural;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Close prepared statement and return number of processed rows
|
||||
$stmt->close();
|
||||
$db->exec("COMMIT;");
|
||||
|
||||
if($returnnum)
|
||||
return $num;
|
||||
else
|
||||
return "Success, added: ".$num;
|
||||
{
|
||||
$finalcount = intval($db->querySingle("SELECT COUNT(*) FROM ".$table.";"));
|
||||
$modified = $finalcount - $initialcount;
|
||||
if($num === 1)
|
||||
$plural = "";
|
||||
else
|
||||
$plural = "s";
|
||||
return "Success, added ".$modified." of ".$num." domain".$plural;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -128,6 +154,16 @@ function add_to_table($db, $table, $domains, $wildcardstyle=false, $returnnum=fa
|
||||
*/
|
||||
function remove_from_table($db, $table, $domains, $returnnum=false)
|
||||
{
|
||||
// Begin transaction
|
||||
if(!$db->exec("BEGIN TRANSACTION;"))
|
||||
{
|
||||
if($returnnum)
|
||||
return 0;
|
||||
else
|
||||
return "Error: Unable to begin transaction for ".$table." table.";
|
||||
}
|
||||
$initialcount = intval($db->querySingle("SELECT COUNT(*) FROM ".$table.";"));
|
||||
|
||||
// Prepare SQLite statememt
|
||||
$stmt = $db->prepare("DELETE FROM ".$table." WHERE domain = :domain;");
|
||||
|
||||
@@ -154,16 +190,30 @@ function remove_from_table($db, $table, $domains, $returnnum=false)
|
||||
if($returnnum)
|
||||
return $num;
|
||||
else
|
||||
return "Error: ".$db->lastErrorMsg().", removed: ".$num;
|
||||
{
|
||||
if($num === 1)
|
||||
$plural = "";
|
||||
else
|
||||
$plural = "s";
|
||||
return "Error: ".$db->lastErrorMsg().", removed ".$num." domain".$plural;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Close prepared statement and return number or processed rows
|
||||
$stmt->close();
|
||||
$db->exec("COMMIT;");
|
||||
|
||||
if($returnnum)
|
||||
return $num;
|
||||
else
|
||||
return "Success, removed: ".$num;
|
||||
{
|
||||
if($num === 1)
|
||||
$plural = "";
|
||||
else
|
||||
$plural = "s";
|
||||
return "Success, removed ".$num." domain".$plural;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user