mirror of
https://github.com/Dolibarr/dolibarr.git
synced 2025-12-06 01:28:19 +01:00
Clean code of lib_notification. Now use cache like other js.
This commit is contained in:
@@ -47,6 +47,11 @@ session_cache_limiter('public');
|
|||||||
|
|
||||||
require_once '../../main.inc.php';
|
require_once '../../main.inc.php';
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* View
|
||||||
|
*/
|
||||||
|
|
||||||
// Define javascript type
|
// Define javascript type
|
||||||
top_httphead('text/javascript; charset=UTF-8');
|
top_httphead('text/javascript; charset=UTF-8');
|
||||||
// Important: Following code is to avoid page request by browser and PHP CPU at each Dolibarr page access.
|
// Important: Following code is to avoid page request by browser and PHP CPU at each Dolibarr page access.
|
||||||
|
|||||||
@@ -50,6 +50,11 @@ session_cache_limiter('public');
|
|||||||
|
|
||||||
require_once '../../main.inc.php';
|
require_once '../../main.inc.php';
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* View
|
||||||
|
*/
|
||||||
|
|
||||||
// Define javascript type
|
// Define javascript type
|
||||||
top_httphead('text/javascript; charset=UTF-8');
|
top_httphead('text/javascript; charset=UTF-8');
|
||||||
// Important: Following code is to avoid page request by browser and PHP CPU at each Dolibarr page access.
|
// Important: Following code is to avoid page request by browser and PHP CPU at each Dolibarr page access.
|
||||||
|
|||||||
@@ -41,132 +41,147 @@ if (!defined('NOREQUIREHTML')) {
|
|||||||
define('NOREQUIREHTML', 1);
|
define('NOREQUIREHTML', 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
session_cache_limiter('public');
|
||||||
|
|
||||||
require_once '../../main.inc.php';
|
require_once '../../main.inc.php';
|
||||||
|
|
||||||
if (!($_SERVER['HTTP_REFERER'] === $dolibarr_main_url_root.'/' || $_SERVER['HTTP_REFERER'] === $dolibarr_main_url_root.'/index.php'
|
|
||||||
|| preg_match('/getmenu_div\.php/', $_SERVER['HTTP_REFERER']))) {
|
|
||||||
global $langs, $conf;
|
|
||||||
|
|
||||||
top_httphead('text/javascript; charset=UTF-8');
|
/*
|
||||||
|
* View
|
||||||
|
*/
|
||||||
|
|
||||||
print 'var login = \''.$_SESSION['dol_login'].'\';'."\n";
|
top_httphead('text/javascript; charset=UTF-8');
|
||||||
print 'var nowtime = Date.now();';
|
// Important: Following code is to avoid page request by browser and PHP CPU at each Dolibarr page access.
|
||||||
print 'var time_auto_update = '.$conf->global->MAIN_BROWSER_NOTIFICATION_FREQUENCY.';'."\n"; // Always defined
|
if (empty($dolibarr_nocache)) {
|
||||||
print 'var time_js_next_test;'."\n";
|
header('Cache-Control: max-age=10800, public, must-revalidate');
|
||||||
?>
|
} else {
|
||||||
|
header('Cache-Control: no-cache');
|
||||||
/* Check if permission ok */
|
}
|
||||||
if (Notification.permission !== "granted") {
|
|
||||||
console.log("Ask Notification.permission");
|
|
||||||
Notification.requestPermission()
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Launch timer */
|
|
||||||
// We set a delay before launching first test so next check will arrive after the time_auto_update compared to previous one.
|
|
||||||
//var time_first_execution = (time_auto_update + (time_js_next_test - nowtime)) * 1000; //need milliseconds
|
|
||||||
var time_first_execution = <?php echo max(3, empty($conf->global->MAIN_BROWSER_NOTIFICATION_CHECK_FIRST_EXECUTION) ? 0 : $conf->global->MAIN_BROWSER_NOTIFICATION_CHECK_FIRST_EXECUTION); ?>;
|
|
||||||
if (login != '') {
|
|
||||||
setTimeout(first_execution, time_first_execution * 1000);
|
|
||||||
time_js_next_test = nowtime + time_first_execution;
|
|
||||||
console.log("Launch browser notif check: setTimeout is set to launch 'first_execution' function after a wait of time_first_execution="+time_first_execution+". nowtime (time php page generation) = "+nowtime+" time_js_next_check = "+time_js_next_test);
|
|
||||||
} //first run auto check
|
|
||||||
|
|
||||||
|
|
||||||
function first_execution() {
|
print "jQuery(document).ready(function () {\n";
|
||||||
console.log("Call first_execution then set repeat time to time_auto_update = MAIN_BROWSER_NOTIFICATION_FREQUENCY = "+time_auto_update);
|
|
||||||
check_events(); //one check before setting the new time for other checks
|
|
||||||
setInterval(check_events, time_auto_update * 1000); // Set new time to run next check events
|
|
||||||
}
|
|
||||||
|
|
||||||
function check_events() {
|
//print " console.log('referrer=".dol_escape_js($_SERVER['HTTP_REFERER'])."');\n";
|
||||||
if (Notification.permission === "granted")
|
|
||||||
{
|
|
||||||
time_js_next_test += time_auto_update;
|
|
||||||
console.log("Call ajax to check_events with time_js_next_test = "+time_js_next_test);
|
|
||||||
|
|
||||||
$.ajax("<?php print DOL_URL_ROOT.'/core/ajax/check_notifications.php'; ?>", {
|
print ' var nowtime = Date.now();';
|
||||||
type: "post", // Usually post or get
|
print ' var time_auto_update = '.$conf->global->MAIN_BROWSER_NOTIFICATION_FREQUENCY.';'."\n"; // Always defined
|
||||||
async: true,
|
print ' var time_js_next_test;'."\n";
|
||||||
data: { time_js_next_test: time_js_next_test, forcechecknow: 1, token: 'notrequired' },
|
?>
|
||||||
dataType: "json",
|
|
||||||
success: function (result) {
|
/* Check if permission ok */
|
||||||
//console.log(result);
|
if (Notification.permission !== "granted") {
|
||||||
var arrayofpastreminders = Object.values(result.pastreminders);
|
console.log("Ask Notification.permission");
|
||||||
if (arrayofpastreminders && arrayofpastreminders.length > 0) {
|
Notification.requestPermission()
|
||||||
console.log("Retrieved "+arrayofpastreminders.length+" reminders to do.");
|
}
|
||||||
var audio = null;
|
|
||||||
<?php
|
/* Launch timer */
|
||||||
if (!empty($conf->global->AGENDA_REMINDER_BROWSER_SOUND)) {
|
|
||||||
print 'audio = new Audio(\''.DOL_URL_ROOT.'/theme/common/sound/notification_agenda.wav\');';
|
// We set a delay before launching first test so next check will arrive after the time_auto_update compared to previous one.
|
||||||
|
//var time_first_execution = (time_auto_update + (time_js_next_test - nowtime)) * 1000; //need milliseconds
|
||||||
|
var time_first_execution = <?php echo max(3, empty($conf->global->MAIN_BROWSER_NOTIFICATION_CHECK_FIRST_EXECUTION) ? 0 : $conf->global->MAIN_BROWSER_NOTIFICATION_CHECK_FIRST_EXECUTION); ?>;
|
||||||
|
|
||||||
|
setTimeout(first_execution, time_first_execution * 1000);
|
||||||
|
time_js_next_test = nowtime + time_first_execution;
|
||||||
|
console.log("Launch browser notif check: setTimeout is set to launch 'first_execution' function after a wait of time_first_execution="+time_first_execution+". nowtime (time php page generation) = "+nowtime+" time_js_next_check = "+time_js_next_test);
|
||||||
|
|
||||||
|
|
||||||
|
function first_execution() {
|
||||||
|
console.log("Call first_execution then set repeat time to time_auto_update = MAIN_BROWSER_NOTIFICATION_FREQUENCY = "+time_auto_update);
|
||||||
|
check_events(); //one check before setting the new time for other checks
|
||||||
|
setInterval(check_events, time_auto_update * 1000); // Set new time to run next check events
|
||||||
|
}
|
||||||
|
|
||||||
|
function check_events() {
|
||||||
|
if (Notification.permission === "granted")
|
||||||
|
{
|
||||||
|
time_js_next_test += time_auto_update;
|
||||||
|
console.log("Call ajax to check_events with time_js_next_test = "+time_js_next_test);
|
||||||
|
|
||||||
|
$.ajax("<?php print DOL_URL_ROOT.'/core/ajax/check_notifications.php'; ?>", {
|
||||||
|
type: "post", // Usually post or get
|
||||||
|
async: true,
|
||||||
|
data: { time_js_next_test: time_js_next_test, forcechecknow: 1, token: 'notrequired' },
|
||||||
|
dataType: "json",
|
||||||
|
success: function (result) {
|
||||||
|
//console.log(result);
|
||||||
|
var arrayofpastreminders = Object.values(result.pastreminders);
|
||||||
|
if (arrayofpastreminders && arrayofpastreminders.length > 0) {
|
||||||
|
console.log("Retrieved "+arrayofpastreminders.length+" reminders to do.");
|
||||||
|
var audio = null;
|
||||||
|
<?php
|
||||||
|
if (!empty($conf->global->AGENDA_REMINDER_BROWSER_SOUND)) {
|
||||||
|
print 'audio = new Audio(\''.DOL_URL_ROOT.'/theme/common/sound/notification_agenda.wav\');';
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
var listofreminderids = '';
|
||||||
|
var noti = []
|
||||||
|
|
||||||
|
$.each(arrayofpastreminders, function (index, value) {
|
||||||
|
console.log(value);
|
||||||
|
var url = "notdefined";
|
||||||
|
var title = "Not defined";
|
||||||
|
var body = value.label;
|
||||||
|
if (value.type == 'agenda' && value.location != null && value.location != '') {
|
||||||
|
body += '\n' + value.location;
|
||||||
}
|
}
|
||||||
?>
|
|
||||||
var listofreminderids = '';
|
|
||||||
var noti = []
|
|
||||||
|
|
||||||
$.each(arrayofpastreminders, function (index, value) {
|
if (value.type == 'agenda' && (value.event_date_start_formated != null || value.event_date_start_formated['event_date_start'] != '')) {
|
||||||
console.log(value);
|
body += '\n' + value.event_date_start_formated;
|
||||||
var url = "notdefined";
|
}
|
||||||
var title = "Not defined";
|
|
||||||
var body = value.label;
|
|
||||||
if (value.type == 'agenda' && value.location != null && value.location != '') {
|
|
||||||
body += '\n' + value.location;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (value.type == 'agenda' && (value.event_date_start_formated != null || value.event_date_start_formated['event_date_start'] != '')) {
|
if (value.type == 'agenda')
|
||||||
body += '\n' + value.event_date_start_formated;
|
{
|
||||||
}
|
url = '<?php print DOL_URL_ROOT.'/comm/action/card.php?id='; ?>' + value.id_agenda;
|
||||||
|
title = '<?php print dol_escape_js($langs->trans('EventReminder')) ?>';
|
||||||
|
}
|
||||||
|
var extra = {
|
||||||
|
icon: '<?php print DOL_URL_ROOT.'/theme/common/bell.png'; ?>',
|
||||||
|
//image: '<?php print DOL_URL_ROOT.'/theme/common/bell.png'; ?>',
|
||||||
|
body: body,
|
||||||
|
tag: value.id_agenda,
|
||||||
|
requireInteraction: true
|
||||||
|
};
|
||||||
|
|
||||||
if (value.type == 'agenda')
|
// We release the notify
|
||||||
{
|
console.log("Send notification on browser");
|
||||||
url = '<?php print DOL_URL_ROOT.'/comm/action/card.php?id='; ?>' + value.id_agenda;
|
noti[index] = new Notification(title, extra);
|
||||||
title = '<?php print dol_escape_js($langs->trans('EventReminder')) ?>';
|
if (index==0 && audio)
|
||||||
}
|
{
|
||||||
var extra = {
|
audio.play();
|
||||||
icon: '<?php print DOL_URL_ROOT.'/theme/common/bell.png'; ?>',
|
}
|
||||||
//image: '<?php print DOL_URL_ROOT.'/theme/common/bell.png'; ?>',
|
|
||||||
body: body,
|
if (noti[index]) {
|
||||||
tag: value.id_agenda,
|
noti[index].onclick = function (event) {
|
||||||
requireInteraction: true
|
console.log("A click on notification on browser has been done");
|
||||||
|
event.preventDefault(); // prevent the browser from focusing the Notification's tab
|
||||||
|
window.focus();
|
||||||
|
window.open(url, '_blank');
|
||||||
|
noti[index].close();
|
||||||
};
|
};
|
||||||
|
|
||||||
// We release the notify
|
listofreminderids = (listofreminderids == '' ? '' : listofreminderids + ',') + value.id_reminder
|
||||||
console.log("Send notification on browser");
|
}
|
||||||
noti[index] = new Notification(title, extra);
|
});
|
||||||
if (index==0 && audio)
|
|
||||||
{
|
|
||||||
audio.play();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (noti[index]) {
|
// Update status of all notifications we sent on browser (listofreminderids)
|
||||||
noti[index].onclick = function (event) {
|
console.log("Flag notification as done for listofreminderids="+listofreminderids);
|
||||||
console.log("A click on notification on browser has been done");
|
$.ajax("<?php print DOL_URL_ROOT.'/core/ajax/check_notifications.php?action=stopreminder&listofreminderids='; ?>"+listofreminderids, {
|
||||||
event.preventDefault(); // prevent the browser from focusing the Notification's tab
|
type: "POST", // Usually post or get
|
||||||
window.focus();
|
async: true,
|
||||||
window.open(url, '_blank');
|
data: { time_js_next_test: time_js_next_test, token: 'notrequired' }
|
||||||
noti[index].close();
|
});
|
||||||
};
|
} else {
|
||||||
|
console.log("No reminder to do found, next search at "+time_js_next_test);
|
||||||
listofreminderids = (listofreminderids == '' ? '' : listofreminderids + ',') + value.id_reminder
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Update status of all notifications we sent on browser (listofreminderids)
|
|
||||||
console.log("Flag notification as done for listofreminderids="+listofreminderids);
|
|
||||||
$.ajax("<?php print DOL_URL_ROOT.'/core/ajax/check_notifications.php?action=stopreminder&listofreminderids='; ?>"+listofreminderids, {
|
|
||||||
type: "POST", // Usually post or get
|
|
||||||
async: true,
|
|
||||||
data: { time_js_next_test: time_js_next_test, token: 'notrequired' }
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
console.log("No reminder to do found, next search at "+time_js_next_test);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
}
|
});
|
||||||
else
|
}
|
||||||
{
|
else
|
||||||
console.log("Cancel check_events. Useless because javascript Notification.permission is "+Notification.permission+" (blocked manualy or web site is not https).");
|
{
|
||||||
}
|
console.log("Cancel check_events. Useless because javascript Notification.permission is "+Notification.permission+" (blocked manualy or web site is not https).");
|
||||||
}
|
}
|
||||||
<?php
|
|
||||||
}
|
}
|
||||||
|
<?php
|
||||||
|
|
||||||
|
print "\n";
|
||||||
|
print '})'."\n";
|
||||||
|
|||||||
@@ -47,6 +47,10 @@ session_cache_limiter('public');
|
|||||||
|
|
||||||
require_once '../../main.inc.php';
|
require_once '../../main.inc.php';
|
||||||
|
|
||||||
|
/*
|
||||||
|
* View
|
||||||
|
*/
|
||||||
|
|
||||||
// Define javascript type
|
// Define javascript type
|
||||||
top_httphead('text/javascript; charset=UTF-8');
|
top_httphead('text/javascript; charset=UTF-8');
|
||||||
// Important: Following code is to avoid page request by browser and PHP CPU at each Dolibarr page access.
|
// Important: Following code is to avoid page request by browser and PHP CPU at each Dolibarr page access.
|
||||||
|
|||||||
Reference in New Issue
Block a user