dolibarr/htdocs/core/js/lib_notification.js.php

160 lines
7.8 KiB
PHP
Raw Normal View History

2017-01-13 16:08:49 +01:00
<?php
/* Copyright (C) 2016 Sergio Sanchis <sergiosanchis@hotmail.com>
* Copyright (C) 2017 Juanjo Menent <jmenent@2byte.es>
2020-03-22 01:24:50 +01:00
* Copyright (C) 2020 Destailleur Laurent <eldy@users.sourceforge.net>
2017-01-13 16:08:49 +01:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2019-09-23 21:55:30 +02:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
2017-01-20 18:41:18 +01:00
* Library javascript to enable Browser notifications
*/
2017-01-13 16:08:49 +01:00
if (!defined('NOREQUIREUSER')) define('NOREQUIREUSER', '1');
if (!defined('NOREQUIRESOC')) define('NOREQUIRESOC', '1');
if (!defined('NOCSRFCHECK')) define('NOCSRFCHECK', 1);
if (!defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL', 1);
if (!defined('NOLOGIN')) define('NOLOGIN', 1);
if (!defined('NOREQUIREMENU')) define('NOREQUIREMENU', 1);
if (!defined('NOREQUIREHTML')) define('NOREQUIREHTML', 1);
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'])))
2017-01-20 18:41:18 +01:00
{
global $langs, $conf;
2017-01-13 16:08:49 +01:00
top_httphead('text/javascript; charset=UTF-8');
print 'var login = \''.$_SESSION['dol_login'].'\';'."\n";
2020-05-05 06:38:24 +02:00
print 'var nowtime = Date.now();';
print 'var time_auto_update = '.$conf->global->MAIN_BROWSER_NOTIFICATION_FREQUENCY.';'."\n"; // Always defined
print 'var time_js_next_test;'."\n";
?>
2017-01-20 18:41:18 +01:00
/* Check if permission ok */
if (Notification.permission !== "granted") {
2020-09-12 04:08:05 +02:00
console.log("Ask Notification.permission");
2017-01-20 18:41:18 +01:00
Notification.requestPermission()
}
2017-01-13 16:08:49 +01:00
/* Launch timer */
// We set a delay before launching first test so next check will arrive after the time_auto_update compared to previous one.
2020-09-19 13:04:23 +02:00
//var time_first_execution = (time_auto_update + (time_js_next_test - nowtime)) * 1000; //need milliseconds
2020-10-30 05:45:36 +01:00
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 != '') {
2020-09-19 16:05:55 +02:00
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
2017-01-13 16:08:49 +01:00
function first_execution() {
2020-09-19 16:05:55 +02:00
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
2017-01-13 16:08:49 +01:00
}
function check_events() {
2017-01-20 18:41:18 +01:00
if (Notification.permission === "granted")
{
2020-09-19 16:05:55 +02:00
time_js_next_test += time_auto_update;
console.log("Call ajax to check_events with time_js_next_test = "+time_js_next_test);
2017-04-30 04:32:03 +02:00
$.ajax("<?php print DOL_URL_ROOT.'/core/ajax/check_notifications.php'; ?>", {
type: "post", // Usually post or get
2017-01-20 18:41:18 +01:00
async: true,
2020-09-26 20:29:01 +02:00
data: { time_js_next_test: time_js_next_test, forcechecknow: 1, token: 'notrequired' },
dataType: "json",
2017-01-20 18:41:18 +01:00
success: function (result) {
2020-10-18 14:00:47 +02:00
//console.log(result);
2020-09-26 20:29:01 +02:00
var arrayofpastreminders = Object.values(result.pastreminders);
if (arrayofpastreminders && arrayofpastreminders.length > 0) {
2020-10-23 20:08:35 +02:00
console.log("Retrieved "+arrayofpastreminders.length+" reminders to do.");
var audio = null;
2017-01-13 16:08:49 +01:00
<?php
if (!empty($conf->global->AGENDA_REMINDER_BROWSER_SOUND)) {
2020-04-26 23:06:52 +02:00
print 'audio = new Audio(\''.DOL_URL_ROOT.'/theme/common/sound/notification_agenda.wav\');';
}
?>
var listofreminderids = '';
var noti = []
2020-09-26 20:29:01 +02:00
$.each(arrayofpastreminders, function (index, value) {
console.log(value);
2020-09-12 04:08:05 +02:00
var url = "notdefined";
var title = "Not defined";
2020-09-26 20:29:01 +02:00
var body = value.label;
if (value.type == 'agenda' && value.location != null && value.location != '') {
body += '\n' + value.location;
2017-01-13 16:08:49 +01:00
}
2020-09-26 20:29:01 +02:00
if (value.type == 'agenda' && (value.event_date_start_formated != null || value.event_date_start_formated['event_date_start'] != '')) {
body += '\n' + value.event_date_start_formated;
}
2020-09-26 20:29:01 +02:00
if (value.type == 'agenda')
2017-01-20 18:41:18 +01:00
{
2020-11-18 16:04:05 +01:00
url = '<?php print DOL_URL_ROOT.'/comm/action/card.php?id='; ?>' + value.id_agenda;
title = '<?php print dol_escape_js($langs->trans('EventReminder')) ?>';
2017-01-20 18:41:18 +01:00
}
2017-01-13 16:08:49 +01:00
var extra = {
2017-01-20 18:41:18 +01:00
icon: '<?php print DOL_URL_ROOT.'/theme/common/bell.png'; ?>',
//image: '<?php print DOL_URL_ROOT.'/theme/common/bell.png'; ?>',
2017-01-13 16:08:49 +01:00
body: body,
tag: value.id_agenda,
requireInteraction: true
2017-01-13 16:08:49 +01:00
};
2017-01-13 16:08:49 +01:00
// We release the notify
2020-09-12 04:08:05 +02:00
console.log("Send notification on browser");
noti[index] = new Notification(title, extra);
if (index==0 && audio)
2017-01-20 18:41:18 +01:00
{
audio.play();
2017-01-13 16:08:49 +01:00
}
if (noti[index]) {
noti[index].onclick = function (event) {
2020-09-12 04:08:05 +02:00
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();
};
2020-09-26 20:29:01 +02:00
listofreminderids = (listofreminderids == '' ? '' : listofreminderids + ',') + value.id_reminder
}
2017-01-13 16:08:49 +01:00
});
// Update status of all notifications we sent on browser (listofreminderids)
2020-09-12 04:08:05 +02:00
console.log("Flag notification as done for listofreminderids="+listofreminderids);
$.ajax("<?php print DOL_URL_ROOT.'/core/ajax/check_notifications.php?action=stopreminder&listofreminderids='; ?>"+listofreminderids, {
2020-09-19 16:05:55 +02:00
type: "post", // Usually post or get
async: true,
2020-09-26 20:29:01 +02:00
data: { time_js_next_test: time_js_next_test, token: 'notrequired' }
});
2020-09-26 20:29:01 +02:00
} else {
2020-10-18 14:00:47 +02:00
console.log("No reminder to do found, next search at "+time_js_next_test);
2017-01-13 16:08:49 +01:00
}
}
2017-01-20 18:41:18 +01:00
});
}
else
{
2020-09-19 13:04:23 +02:00
console.log("Cancel check_events. Useless because javascript Notification.permission is "+Notification.permission+" (blocked manualy or web site is not https).");
2017-01-20 18:41:18 +01:00
}
2017-01-13 16:08:49 +01:00
}
2019-10-20 11:17:54 +02:00
<?php
2017-01-20 18:41:18 +01:00
}