🔔 User Notifications v0.5
This commit is contained in:
@@ -273,7 +273,7 @@ if ($ENABLED_DARKMODE === True) {
|
|||||||
<i class="fa fa-angle-left pull-right"></i>
|
<i class="fa fa-angle-left pull-right"></i>
|
||||||
</span>
|
</span>
|
||||||
</a>
|
</a>
|
||||||
<ul class="treeview-menu " style="display: <?php if (in_array (basename($_SERVER['SCRIPT_NAME']), array('presence.php', 'report.php', 'events.php' ) ) ){ echo 'block'; } else {echo 'none';} ?>;">
|
<ul class="treeview-menu " style="display: <?php if (in_array (basename($_SERVER['SCRIPT_NAME']), array('presence.php', 'report.php', 'events.php', 'userNotifications.php' ) ) ){ echo 'block'; } else {echo 'none';} ?>;">
|
||||||
<li>
|
<li>
|
||||||
<a href="presence.php"> <?= lang("Navigation_Presence");?> </a>
|
<a href="presence.php"> <?= lang("Navigation_Presence");?> </a>
|
||||||
</li>
|
</li>
|
||||||
|
|||||||
@@ -456,7 +456,7 @@
|
|||||||
"Navigation_Notifications": "Notifications",
|
"Navigation_Notifications": "Notifications",
|
||||||
"Navigation_Plugins": "Plugins",
|
"Navigation_Plugins": "Plugins",
|
||||||
"Navigation_Presence": "Presence",
|
"Navigation_Presence": "Presence",
|
||||||
"Navigation_Report": "Report",
|
"Navigation_Report": "Sent Reports",
|
||||||
"Navigation_Settings": "Settings",
|
"Navigation_Settings": "Settings",
|
||||||
"Navigation_SystemInfo": "System info",
|
"Navigation_SystemInfo": "System info",
|
||||||
"Navigation_Workflows": "Workflows",
|
"Navigation_Workflows": "Workflows",
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ require 'php/templates/header.php';
|
|||||||
<script src="lib/AdminLTE/bower_components/datatables.net/js/dataTables.select.min.js"></script>
|
<script src="lib/AdminLTE/bower_components/datatables.net/js/dataTables.select.min.js"></script>
|
||||||
|
|
||||||
<div id="notifications" class="content-wrapper">
|
<div id="notifications" class="content-wrapper">
|
||||||
|
<div class="box-body table-responsive">
|
||||||
<table id="notificationsTable" class="table table-bordered table-hover table-striped display">
|
<table id="notificationsTable" class="table table-bordered table-hover table-striped display">
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
@@ -25,10 +26,12 @@ require 'php/templates/header.php';
|
|||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
|
<button id="clearNotificationsBtn" class="btn btn-danger">Clear All Notifications</button>
|
||||||
|
</div>
|
||||||
<script>
|
<script>
|
||||||
function fetchData(callback) {
|
function fetchData(callback) {
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: '/api/user_notifications.json',
|
url: '/api/user_notifications.json?nocache=' + Date.now(),
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
dataType: 'json',
|
dataType: 'json',
|
||||||
success: function(response) {
|
success: function(response) {
|
||||||
@@ -62,7 +65,12 @@ require 'php/templates/header.php';
|
|||||||
{
|
{
|
||||||
"data": "content",
|
"data": "content",
|
||||||
"render": function(data, type, row) {
|
"render": function(data, type, row) {
|
||||||
return `<span class="btn-show-message">${data}</span>`;
|
if (data.includes("Report:")) {
|
||||||
|
var guid = data.split(":")[1].trim();
|
||||||
|
return `<a href="/report.php?guid=${guid}">Click to see Sent Report</a>`;
|
||||||
|
} else {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
@@ -77,6 +85,28 @@ require 'php/templates/header.php';
|
|||||||
fetchData(function(data) {
|
fetchData(function(data) {
|
||||||
table.clear().rows.add(data).draw();
|
table.clear().rows.add(data).draw();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
const phpEndpoint = 'php/server/utilNotification.php';
|
||||||
|
|
||||||
|
// Function to clear all notifications
|
||||||
|
$('#clearNotificationsBtn').click(function() {
|
||||||
|
$.ajax({
|
||||||
|
url: phpEndpoint,
|
||||||
|
type: 'GET',
|
||||||
|
data: {
|
||||||
|
action: 'notifications_clear'
|
||||||
|
},
|
||||||
|
success: function(response) {
|
||||||
|
// Clear the table and reload data
|
||||||
|
table.clear().draw();
|
||||||
|
},
|
||||||
|
error: function(xhr, status, error) {
|
||||||
|
console.log("An error occurred while clearing notifications: " + error);
|
||||||
|
// You can display an error message here if needed
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
import datetime
|
import datetime
|
||||||
|
import os
|
||||||
|
import _io
|
||||||
import json
|
import json
|
||||||
import uuid
|
import uuid
|
||||||
import socket
|
import socket
|
||||||
@@ -183,7 +185,7 @@ class Notification_obj:
|
|||||||
self.HTML = final_html
|
self.HTML = final_html
|
||||||
|
|
||||||
# Notify frontend
|
# Notify frontend
|
||||||
write_notification("Report:" + self.GUID, "alert")
|
write_notification(f'Report:{self.GUID}', "alert", self.DateTimeCreated )
|
||||||
|
|
||||||
self.upsert()
|
self.upsert()
|
||||||
|
|
||||||
@@ -238,37 +240,7 @@ class Notification_obj:
|
|||||||
|
|
||||||
self.save()
|
self.save()
|
||||||
|
|
||||||
def write_notification(content, level="interrupt"):
|
|
||||||
NOTIFICATION_API_FILE = apiPath + 'user_notifications.json'
|
|
||||||
|
|
||||||
# Generate GUID
|
|
||||||
guid = str(uuid.uuid4())
|
|
||||||
|
|
||||||
# Generate timestamp
|
|
||||||
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
||||||
|
|
||||||
# Prepare notification dictionary
|
|
||||||
notification = {
|
|
||||||
'timestamp': timestamp,
|
|
||||||
'guid': guid,
|
|
||||||
'read': 0,
|
|
||||||
'level': level,
|
|
||||||
'content': content
|
|
||||||
}
|
|
||||||
|
|
||||||
# If file exists, load existing data, otherwise initialize as empty list
|
|
||||||
if os.path.exists(NOTIFICATION_API_FILE):
|
|
||||||
with open(NOTIFICATION_API_FILE, 'r') as file:
|
|
||||||
notifications = json.load(file)
|
|
||||||
else:
|
|
||||||
notifications = []
|
|
||||||
|
|
||||||
# Append new notification
|
|
||||||
notifications.append(notification)
|
|
||||||
|
|
||||||
# Write updated data back to file
|
|
||||||
with open(NOTIFICATION_API_FILE, 'w') as file:
|
|
||||||
json.dump(notifications, file, indent=4)
|
|
||||||
|
|
||||||
|
|
||||||
def clearPendingEmailFlag(self):
|
def clearPendingEmailFlag(self):
|
||||||
@@ -309,6 +281,46 @@ class Notification_obj:
|
|||||||
# Reporting
|
# Reporting
|
||||||
#-------------------------------------------------------------------------------
|
#-------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
# Handle Frontend User Notifications
|
||||||
|
def write_notification(content, level, timestamp):
|
||||||
|
NOTIFICATION_API_FILE = apiPath + 'user_notifications.json'
|
||||||
|
|
||||||
|
# Generate GUID
|
||||||
|
guid = str(uuid.uuid4())
|
||||||
|
|
||||||
|
# Prepare notification dictionary
|
||||||
|
notification = {
|
||||||
|
'timestamp': str(timestamp),
|
||||||
|
'guid': guid,
|
||||||
|
'read': 0,
|
||||||
|
'level': level,
|
||||||
|
'content': content
|
||||||
|
}
|
||||||
|
|
||||||
|
# If file exists, load existing data, otherwise initialize as empty list
|
||||||
|
if os.path.exists(NOTIFICATION_API_FILE):
|
||||||
|
with open(NOTIFICATION_API_FILE, 'r') as file:
|
||||||
|
# Check if the file object is of type _io.TextIOWrapper
|
||||||
|
if isinstance(file, _io.TextIOWrapper):
|
||||||
|
file_contents = file.read() # Read file contents
|
||||||
|
if file_contents == '':
|
||||||
|
file_contents = '[]' # If file is empty, initialize as empty list
|
||||||
|
|
||||||
|
mylog('debug', ['[Notification] User Notifications file: ', file_contents])
|
||||||
|
notifications = json.loads(file_contents) # Parse JSON data
|
||||||
|
else:
|
||||||
|
mylog('error', 'File is not of type _io.TextIOWrapper')
|
||||||
|
notifications = []
|
||||||
|
else:
|
||||||
|
notifications = []
|
||||||
|
|
||||||
|
# Append new notification
|
||||||
|
notifications.append(notification)
|
||||||
|
|
||||||
|
# Write updated data back to file
|
||||||
|
with open(NOTIFICATION_API_FILE, 'w') as file:
|
||||||
|
json.dump(notifications, file, indent=4)
|
||||||
|
|
||||||
#-------------------------------------------------------------------------------
|
#-------------------------------------------------------------------------------
|
||||||
def construct_notifications(JSON, section):
|
def construct_notifications(JSON, section):
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user