From df9782ab1e6b61914936f2ef16ca107159ab7f27 Mon Sep 17 00:00:00 2001 From: paul Date: Mon, 18 May 2020 15:13:23 +0200 Subject: [PATCH] mustard: Aggiunti controlli per gestione in massa delle notifiche. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * web/mustard/js/notifs.js.php: Aggiunte funzioni per cancellare e marcare come lette che operano su liste di notifiche. * web/mustard/notifs.php: Quando riceve una POST con un body JSON esegue un'azione su liste di notifiche. * web/mustard/notifsh.php: Le notifiche non vengono più cancellate dal DB ma vengono marcate "deleted". * web/mustard/theme.css: Aggiunta classe "deleted". --- web/mustard/js/notifs.js.php | 63 ++++++++++++++++++++++++++++++------ web/mustard/notifs.php | 59 +++++++++++++++++++++++++++------ web/mustard/notifsh.php | 2 +- web/mustard/theme.css | 4 +++ 4 files changed, 109 insertions(+), 19 deletions(-) diff --git a/web/mustard/js/notifs.js.php b/web/mustard/js/notifs.js.php index b508fff..a08bed6 100644 --- a/web/mustard/js/notifs.js.php +++ b/web/mustard/js/notifs.js.php @@ -47,7 +47,7 @@ function markdeleted(notif) { xhr.send(); xhr.onload = function () { if (xhr.response['deleted']) { - notif.remove(); + notif.classList.add("deleted"); } else { alert("Il server non ha potuto cancellare la notifica '" + notif.text + "'"); } @@ -57,16 +57,61 @@ function markdeleted(notif) { }; } -function applyAllSelected(fun) { - let notifs = document.querySelectorAll("div#notifs-list > div"); - notifs.forEach(function (notif) { - let checkbox = notif.querySelector("input.seen-checkbox"); - if (checkbox.checked) { - fun(notif); - }}); +function readSelected() { + let selected = getSelectedNotifs() + let request = { + "act": "massread", + "ids": selected.map(div => div.id.replace(/^notif-([0-9]+)$/, '$1')) + } + let xhr = new XMLHttpRequest(); + xhr.open('POST', 'notifs.php'); + xhr.setRequestHeader('Content-type','application/json'); + xhr.responseType = 'json'; + xhr.send(JSON.stringify(request)); + xhr.onload = function () { + if (xhr.response['done']) { + selected.forEach(notif => { + notif.classList.remove("unseen"); + notif.classList.add("seen"); + }) + } else { + alert("Il server non ha potuto completare la richiesta:\n" + xhr.response["error"]); + } + }; + xhr.onerror = function () { + alert('La richiesta è fallita.'); + }; } -lmt=; +function deleteSelected() { + let selected = getSelectedNotifs() + let request = { + "act": "massdelete", + "ids": selected.map(div => div.id.replace(/^notif-([0-9]+)$/, '$1')) + } + let xhr = new XMLHttpRequest(); + xhr.open('POST', 'notifs.php'); + xhr.setRequestHeader('Content-type','application/json'); + xhr.responseType = 'json'; + xhr.send(JSON.stringify(request)); + xhr.onload = function () { + if (xhr.response['done']) { + selected.forEach(notif => notif.classList.add("deleted")); + } else { + alert("Il server non ha potuto completare la richiesta:\n" + xhr.response["error"]); + } + }; + xhr.onerror = function () { + alert('La richiesta è fallita.'); + }; +} + +function getSelectedNotifs() { + let notifs = Array.from(document.querySelectorAll("div.notif")); + return notifs.filter(notif => notif.querySelector("input.seen-checkbox").checked) +} + +lmt =; chunk=0; end=false; loading=false; diff --git a/web/mustard/notifs.php b/web/mustard/notifs.php index bae2c6e..50a59c0 100644 --- a/web/mustard/notifs.php +++ b/web/mustard/notifs.php @@ -20,6 +20,51 @@ function hspech($str) return (htmlspecialchars($str, ENT_QUOTES | ENT_HTML5, 'UTF-8')); } +function are_valid_ids($ids) +{ + if (count($ids) <= 0) return false; + foreach ($ids as $id) { + if (!(preg_match('/^[0-9]+$/', $id) === 1)) { + return false; + } + } + return true; +} + +if ($_SERVER['REQUEST_METHOD'] === 'POST' && $_SERVER["CONTENT_TYPE"] === "application/json") { + $response = array( + "done" => true, + "error" => "" + ); + $body = json_decode(file_get_contents('php://input'), true); + if (array_key_exists('act', $body) && + array_key_exists('ids', $body) && + are_valid_ids($body["ids"])) { + switch ($body['act']) { + case "massread": + mysqli_query($link, 'UPDATE Notifications SET Seen=1 WHERE ID in (' . implode(", ", $body["ids"]) . ')') + or muoribene(mysqli_error($link), true); + break; + case "massdelete": + mysqli_query($link, 'UPDATE Notifications SET Deleted=1 WHERE ID in (' . implode(", ", $body["ids"]) . ')') + or muoribene(mysqli_error($link), true); + break; + default: + http_response_code(400); + $response["done"] = false; + $response["error"] = "Unknown act."; + } + } else { + http_response_code(400); + $response["done"] = false; + $response["error"] = "Bad request."; + } + echo(json_encode($response)); + mysqli_close($link); + exit(0); +} + + $dbg .= $dlang . '
' . N; $dbg .= '
' . print_r($_GET, 1) . '
'; @@ -115,20 +160,17 @@ if ($filtordon) {
- -

CIAONEE

"sev-normal", 2 => "sev-warning", @@ -139,8 +181,7 @@ if ($filtordon) { 1 => "seen" ); foreach ($notifs['notifs'] as $n) { - echo("
" . + echo("
" . "" . "" . "" . diff --git a/web/mustard/notifsh.php b/web/mustard/notifsh.php index f96cffe..3b28176 100644 --- a/web/mustard/notifsh.php +++ b/web/mustard/notifsh.php @@ -19,7 +19,7 @@ if (array_key_exists('act', $_GET) && $_GET['act'] == 'markread' && array_key_ex echo('{"hmunseen":' . mysqli_num_rows($res) . '}' . N); } elseif (array_key_exists('act', $_GET) && $_GET['act'] == 'delete' && array_key_exists('id', $_GET) && preg_match('/^[0-9]+$/', $_GET['id']) === 1) { $_GET['id'] += 0; - mysqli_query($link, 'DELETE FROM Notifications WHERE ID=' . $_GET['id']) + mysqli_query($link, 'UPDATE Notifications SET Deleted=1 WHERE ID=' . $_GET['id']) or muoribene(mysqli_error($link), true); echo('{"deleted":' . true . '}' . N); } elseif (array_key_exists('act', $_GET) && $_GET['act'] == 'loadchunk' && array_key_exists('chunk', $_GET) && preg_match('/^[0-9]+$/', $_GET['chunk']) === 1) { diff --git a/web/mustard/theme.css b/web/mustard/theme.css index 0060599..c586701 100644 --- a/web/mustard/theme.css +++ b/web/mustard/theme.css @@ -872,6 +872,10 @@ div.notif button { float: right; } +div.deleted { + display: none; +} + div.sev-warning { background-color: #FFEEAA; }