3 Commits 9e34b1091e ... 81c3fd2722

Author SHA1 Message Date
  paul 81c3fd2722 mustard: notifs: Aggiunti controlli per cancellare le notifiche. 4 years ago
  paul 33e0f335a8 mustard: notifs.js: Aggiunta funzione per cancellare le notifiche. 4 years ago
  paul 888235e5bf mustard: notifsh: Aggiunto endpoint per cancellare le notifiche. 4 years ago
3 changed files with 54 additions and 30 deletions
  1. 27 0
      web/mustard/js/notifs.js.php
  2. 4 12
      web/mustard/notifs.php
  3. 23 18
      web/mustard/notifsh.php

+ 27 - 0
web/mustard/js/notifs.js.php

@@ -39,6 +39,33 @@ function markread(notif) {
 		alert('La richiesta è fallita.');
 	};
 }
+
+function markdeleted(notif) {
+    let xhr=new XMLHttpRequest();
+    xhr.open('GET','notifsh.php?act=delete&id='+notif.id.replace(/^notif-([0-9]+)$/,'$1'));
+    xhr.responseType='json';
+    xhr.send();
+    xhr.onload=function() {
+        if (xhr.response['deleted']) {
+            notif.remove();
+        } else {
+            alert("Il server non ha potuto cancellare la notifica " + notif.text);
+        }
+    };
+    xhr.onerror=function() {
+        alert('La richiesta è fallita.');
+    };
+}
+
+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);
+        }});
+}
+
 lmt=<?php echo($notifs['lastmicrotime']); ?>;
 chunk=0;
 end=false;

+ 4 - 12
web/mustard/notifs.php

@@ -66,19 +66,10 @@ if ($filtordon) {
                 box.checked = check.checked;
             })
         }
-
-        function readAllSelected() {
-            let notifs = document.querySelectorAll("div#notifs-list > div");
-            notifs.forEach(function (notif) {
-                let checkbox = notif.querySelector("input.seen-checkbox");
-                if (checkbox.checked) {
-                    markread(notif);
-                }
-            })
-        }
-
         // -->
     </script>
+
+    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/fork-awesome@1.1.7/css/fork-awesome.min.css" integrity="sha256-gsmEoJAws/Kd3CjuOQzLie5Q3yshhvmo7YNtBG7aaEY=" crossorigin="anonymous">
 </head>
 <body>
 
@@ -101,7 +92,8 @@ if ($filtordon) {
     <div id="middlerow">
         <div id="notifs-list-controls">
             <input id="global-selector" type="checkbox" onclick="toggleState(this)">
-            <button onclick="readAllSelected()">Segna tutti come letto</button>
+            <button onclick="applyAllSelected(markread)"><i class="fa fa-envelope-open-o" aria-hidden="true"></i>Segna come letto</button>
+            <button onclick="applyAllSelected(markdeleted)"><i class="fa fa-trash-o" aria-hidden="true"></i>Cancella</button>
         </div>
         <div id="notifs-list">
             <?php

+ 23 - 18
web/mustard/notifsh.php

@@ -7,25 +7,30 @@ require('include/muoribenejson.php');
 require('include/sessionstart.php');
 require('include/myconn.php');
 require('include/getadmacc.php');
-if ($account['Level']=='guest')
-	muoribene('Sorry, you are not authorized.',true);
+if ($account['Level'] == 'guest')
+	muoribene('Sorry, you are not authorized.', true);
 
-if (array_key_exists('act',$_GET) && $_GET['act']=='markread' && array_key_exists('id',$_GET) && preg_match('/^[0-9]+$/',$_GET['id'])===1) {
-	$_GET['id']+=0;
-	mysqli_query($link,'UPDATE Notifications SET Seen=1 WHERE ID='.$_GET['id'])
-		or muoribene(mysqli_error($link),true);
-	$res=mysqli_query($link,'SELECT ID FROM Notifications WHERE Seen=0')
-		or muoribene(mysqli_error($link),true);
-	echo('{"hmunseen":'.mysqli_num_rows($res).'}'.N);
-} elseif (array_key_exists('act',$_GET) && $_GET['act']=='loadchunk' && array_key_exists('chunk',$_GET) && preg_match('/^[0-9]+$/',$_GET['chunk'])===1) {
-	$_GET['chunk']+=0;
-	$chunksize=20; // questo dev'essere uguale in include/notifs.php
-	$res=mysqli_query($link,'SELECT * FROM Notifications ORDER BY Microtime DESC LIMIT '.($_GET['chunk']*$chunksize).','.$chunksize)
-		or muoribene(mysqli_error($link),true);
-	$buf=array();
-	while ($row=mysqli_fetch_assoc($res)) {
-		($row['Seen']==0) ? $notifclass='notifunseen' : $notifclass='notifseen';
-		$buf[]='<div id="notif-'.$row['ID'].'" class="'.$notifclass.'" onclick="markread(this)">'.strftime('%d/%m/%y %T',$row['Microtime']).': '.$row['Notification'].'</div>';
+if (array_key_exists('act', $_GET) && $_GET['act'] == 'markread' && array_key_exists('id', $_GET) && preg_match('/^[0-9]+$/', $_GET['id']) === 1) {
+	$_GET['id'] += 0;
+	mysqli_query($link, 'UPDATE Notifications SET Seen=1 WHERE ID=' . $_GET['id'])
+	or muoribene(mysqli_error($link), true);
+	$res = mysqli_query($link, 'SELECT ID FROM Notifications WHERE Seen=0')
+	or muoribene(mysqli_error($link), true);
+	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'])
+	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) {
+	$_GET['chunk'] += 0;
+	$chunksize = 20; // questo dev'essere uguale in include/notifs.php
+	$res = mysqli_query($link, 'SELECT * FROM Notifications ORDER BY Microtime DESC LIMIT ' . ($_GET['chunk'] * $chunksize) . ',' . $chunksize)
+	or muoribene(mysqli_error($link), true);
+	$buf = array();
+	while ($row = mysqli_fetch_assoc($res)) {
+		($row['Seen'] == 0) ? $notifclass = 'notifunseen' : $notifclass = 'notifseen';
+		$buf[] = '<div id="notif-' . $row['ID'] . '" class="' . $notifclass . '" onclick="markread(this)">' . strftime('%d/%m/%y %T', $row['Microtime']) . ': ' . $row['Notification'] . '</div>';
 	}
 	echo(json_encode($buf));
 } elseif (array_key_exists('act',$_GET) && $_GET['act']=='loadnew' && array_key_exists('lmt',$_GET) && preg_match('/^[0-9]+(\.[0-9]+)?$/',$_GET['lmt'])===1) {