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

231 lines
9.0 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>
2023-08-22 17:05:01 +02:00
* Copyright (C) 2020-2023 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
*/
2023-08-22 17:05:01 +02:00
/**
* \file htdocs/core/js/lib_notification.js.php
* \brief Javascript code to manage browser reminers
*/
2021-02-23 22:03:23 +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);
}
2017-01-13 16:08:49 +01:00
session_cache_limiter('public');
2017-01-13 16:08:49 +01:00
require_once '../../main.inc.php';
/*
* View
*/
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.
if (empty($dolibarr_nocache)) {
header('Cache-Control: max-age=10800, public, must-revalidate');
} else {
header('Cache-Control: no-cache');
}
2017-01-13 16:08:49 +01:00
print "jQuery(document).ready(function () {\n";
//print " console.log('referrer=".dol_escape_js($_SERVER['HTTP_REFERER'])."');\n";
print ' var nowtime = Date.now();';
2022-01-17 19:07:02 +01:00
print ' var time_auto_update = '.max(1, getDolGlobalInt('MAIN_BROWSER_NOTIFICATION_FREQUENCY')).';'."\n"; // Always defined
print ' var time_js_next_test;'."\n";
2023-02-10 19:00:44 +01:00
print ' var dolnotif_nb_test_for_page = 0;'."\n";
print ' var dolnotif_idinterval = null;'."\n";
?>
2021-02-23 22:03:23 +01:00
2021-12-01 12:00:19 +01:00
/* Check if Notification is supported */
if ("Notification" in window) {
/* Check if permission ok */
if (Notification.permission !== "granted") {
console.log("Ask Notification.permission");
2023-09-19 12:16:27 +02:00
Notification.requestPermission(function(result) {
2023-12-08 23:45:18 +01:00
console.log("result for Notification.requestPermission is "+result);
2023-09-19 12:16:27 +02:00
});
2021-12-01 12:00:19 +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.
//var time_first_execution = (time_auto_update + (time_js_next_test - nowtime)) * 1000; //need milliseconds
2023-11-27 11:39:32 +01:00
var time_first_execution = <?php echo max(3, !getDolGlobalString('MAIN_BROWSER_NOTIFICATION_CHECK_FIRST_EXECUTION') ? 0 : $conf->global->MAIN_BROWSER_NOTIFICATION_CHECK_FIRST_EXECUTION); ?>;
2021-12-01 12:00:19 +01:00
2023-02-10 19:00:44 +01:00
setTimeout(first_execution, time_first_execution * 1000); // Launch a first execution after a time_first_execution delay
2021-12-01 12:00:19 +01:00
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);
} else {
console.log("This browser in this context does not support Notification.");
}
2023-09-19 12:16:27 +02:00
/* The method called after time_first_execution on each page */
function first_execution() {
2023-02-10 19:08:03 +01:00
console.log("Call first_execution of check_events()");
2023-02-10 19:00:44 +01:00
result = check_events(); //one check before setting the new time for other checks
if (result > 0) {
console.log("check_events() is scheduled as a repeated task with a time_auto_update = MAIN_BROWSER_NOTIFICATION_FREQUENCY = "+time_auto_update+"s");
2023-02-10 19:00:44 +01:00
dolnotif_idinterval = setInterval(check_events, time_auto_update * 1000); // Set new time to run next check events. time_auto_update=nb of seconds
}
}
2023-09-19 12:16:27 +02:00
/* the method call frequently every time_auto_update */
function check_events() {
var result = 0;
dolnotif_nb_test_for_page += 1;
2023-02-10 19:00:44 +01:00
if (Notification.permission === "granted") {
2023-02-06 14:28:11 +01:00
var currentToken = 'notrequired';
2022-10-06 15:24:23 +02:00
const allMeta = document.getElementsByTagName("meta");
for (let i = 0; i < allMeta.length; i++) {
if (allMeta[i].getAttribute("name") == 'anti-csrf-currenttoken') {
2023-02-06 14:28:11 +01:00
currentToken = allMeta[i].getAttribute('content');
console.log("currentToken in page = "+currentToken);
2022-10-06 15:24:23 +02:00
}
}
time_js_next_test += time_auto_update;
2023-02-10 19:00:44 +01:00
console.log("Call ajax to check events with time_js_next_test = "+time_js_next_test+" dolnotif_nb_test_for_page="+dolnotif_nb_test_for_page);
$.ajax("<?php print DOL_URL_ROOT.'/core/ajax/check_notifications.php'; ?>", {
2023-09-19 12:16:27 +02:00
type: "POST", // Usually post or get
async: true,
2023-08-22 17:05:01 +02:00
data: { time_js_next_test: time_js_next_test, forcechecknow: 1, token: currentToken, dolnotif_nb_test_for_page: dolnotif_nb_test_for_page },
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
2023-11-27 11:39:32 +01:00
if (getDolGlobalString('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;
2023-09-19 12:16:27 +02:00
var icon = '<?php print DOL_URL_ROOT.'/theme/common/octicons/build/svg/bell.svg'; ?>';
var image = '<?php print DOL_URL_ROOT.'/theme/common/octicons/build/svg/bell.svg'; ?>';
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'] != '')) {
body += '\n' + value.event_date_start_formated;
}
if (value.type == 'agenda')
{
2021-07-21 01:40:45 +02:00
url = '<?php print DOL_URL_ROOT.'/comm/action/card.php?id='; ?>' + value.id_agenda;
title = '<?php print dol_escape_js($langs->transnoentities('EventReminder')) ?>';
}
var extra = {
2023-09-19 12:16:27 +02:00
icon: icon,
body: body,
2023-09-19 12:16:27 +02:00
lang: '<?php print dol_escape_js($langs->getDefaultLang(1)); ?>',
tag: value.id_agenda,
2024-02-09 12:12:54 +01:00
requireInteraction: true /* wait that the user click or close the notification */
2023-09-19 12:16:27 +02:00
/* only supported for persistent notification shown using ServiceWorkerRegistration.showNotification() so disabled */
/* actions: [{ action: 'action1', title: 'New Button Label' }, { action: 'action2', title: 'Another Button' }] */
};
// We release the notify
2023-09-19 12:16:27 +02:00
console.log("Send notification on browser url="+url);
noti[index] = new Notification(title, extra);
if (index==0 && audio)
{
audio.play();
}
if (noti[index]) {
noti[index].onclick = function (event) {
2024-02-09 12:12:54 +01:00
/* If the user has clicked on button Activate */
2023-09-19 12:16:27 +02:00
console.log("A click on notification on browser has been done for url="+url);
event.preventDefault(); // prevent the browser from focusing the Notification's tab
window.focus();
window.open(url, '_blank');
noti[index].close();
2021-02-23 22:03:23 +01:00
};
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,
2023-02-06 14:28:11 +01:00
data: { time_js_next_test: time_js_next_test, token: currentToken }
});
} else {
2023-02-10 19:08:03 +01:00
console.log("No remind to do found, next search at "+time_js_next_test);
2021-02-23 22:03:23 +01:00
}
}
});
2023-02-10 19:00:44 +01:00
result = 1;
2023-02-10 19:00:44 +01:00
} else {
console.log("Cancel check_events() with dolnotif_nb_test_for_page="+dolnotif_nb_test_for_page+". Check is useless because javascript Notification.permission is "+Notification.permission+" (blocked manually or web site is not https or browser is in Private mode).");
2023-02-10 19:00:44 +01:00
Fix #28071 - New branch to fix bad merge (#28083) * Merge branch 'develop' of git@github.com:Dolibarr/dolibarr.git into develop * Merge branch 'develop' of git@github.com:Dolibarr/dolibarr.git into develop * Qual: Introduce getDataToShowPhoto to prepare generic code * Merge branch 'develop' of git@github.com:Dolibarr/dolibarr.git into develop * Fix missing trans * Fix langs * Merge branch 'develop' of git@github.com:Dolibarr/dolibarr.git into develop * Remove useless files in web templates * Clean code * Fix duplicate translation key * Fix duplicate translation key * Merge branch 'develop' of git@github.com:Dolibarr/dolibarr.git into develop * Fix duplicate key * Fix $object * Debug v19 * WIP SMSing * Merge branch 'develop' of git@github.com:Dolibarr/dolibarr.git into develop * WIP EMAILINGS_SUPPORT_ALSO_SMS * Merge branch 'develop' of git@github.com:Dolibarr/dolibarr.git into develop * Merge branch 'develop' of git@github.com:Dolibarr/dolibarr.git into develop * WIP SMSing * Debug the "validate" feature * Merge branch 'develop' of git@github.com:Dolibarr/dolibarr.git into develop * Clean code * Move rights->x->y into hasRight('x', 'y') * Move rights->x->y into hasRight('x', 'y') * Move rights->x->y into hasRight('x', 'y') * Move rights->x->y into hasRight('x', 'y') * Move rights->x->y into hasRight('x', 'y') * Move rights->x->y into hasRight('x', 'y') * Merge branch 'develop' of git@github.com:Dolibarr/dolibarr.git into develop * Merge branch 'develop' of git@github.com:Dolibarr/dolibarr.git into develop * Enhance rector to fix empty($user->rights->modulex->perm1) * Merge branch 'develop' of git@github.com:Dolibarr/dolibarr.git into develop * Fix template to use v19 dev rules * Fix use v19 dev rules * Fix phpunit * Debug v19 * Clean code * Use rector to convert user->rights into user->hasRight * Clean code * Use rector to convert user->rights into user->hasRight * Use rector to convert user->rights into user->hasRight * Clean code * Fix phpcs * add editorconfig for sql files (#27999) Co-authored-by: Laurent Destailleur <eldy@destailleur.fr> * add model_pdf field in llx_ticket-ticket.sql (#27996) * add model_pdf field in llx_ticket-ticket.sql * Update 19.0.0-20.0.0.sql * Update 19.0.0-20.0.0.sql * Improve wording in README (#27994) * fix phpstan (#27989) * fix phpstan * Update UserRightsToFunction.php --------- Co-authored-by: Laurent Destailleur <eldy@destailleur.fr> * Qual: Fix spelling/working in datapolicy translations (#28006) # Qual: Fix spelling/wording in datapolicy translations Fixed some spelling and wording in datapolicy translations. * qual: phpstan for htdocs/ticket/class/ticketstats.class.php (#27986) htdocs/ticket/class/ticketstats.class.php 98 Parameter #1 $year (string) of method TicketStats::getNbByMonth() should be compatible with parameter $year (int) of method Stats::getNbByMonth() * Merge branch '19.0' of git@github.com:Dolibarr/dolibarr.git into develop * Fix user with readonly perm on email template must be able to read. * Fix doc * Merge branch 'develop' of git@github.com:Dolibarr/dolibarr.git into develop * Better message * Add missing fields in merge of thirdparty * Debug v19 selection of ticket printer per terminal * Merge branch 'develop' of git@github.com:Dolibarr/dolibarr.git into develop * Use constant * NEW: Adding a recipient on emails sent, change status to sent partialy. * fix travis (#28052) * fix travis * Update partnership.class.php * fix php doc (#28047) * fix undefined array key (#28048) * Add region and departament for Cuba (#28046) * Update llx_10_c_regions.sql Add Cuba Regions (id_country=77) * Update llx_20_c_departements.sql Add Provinces Cuba (id country=77) * Find the typo (#28050) * Find the typo * clean code * add last_main_doc field to product (#28045) * add las_main_doc field to product * add field fetch * NEW Add Categorie filter for ActionComm (#28041) * New Add Categorie filter for ActionComm New Add Categorie filter for ActionComm * Fix space errors Fix space errors * Fix space errors 2 Fix space errors 2 * Update cunits.class.php (#28056) FIX: error SQL when creating a Cunit * Update codespell-lines-ignore.txt to avoid PR merge conflict --------- Co-authored-by: Laurent Destailleur <eldy@destailleur.fr> Co-authored-by: Frédéric FRANCE <frederic34@users.noreply.github.com> Co-authored-by: thibdrev <thibault.drevet@gmail.com> Co-authored-by: sonikf <93765174+sonikf@users.noreply.github.com> Co-authored-by: Ikarus <44511582+LeKarSol@users.noreply.github.com> Co-authored-by: Anthony Damhet <73399671+EchoLoGeek@users.noreply.github.com> Co-authored-by: Quentin-Seekness <72733832+Quentin-Seekness@users.noreply.github.com>
2024-02-09 15:58:49 +01:00
result = 2; // We return a positive so the repeated check will done even if authorization is not yet allowed may be after this check)
2021-02-23 22:03:23 +01:00
}
2023-02-10 19:24:50 +01:00
if (dolnotif_nb_test_for_page >= 5) {
console.log("We did "+dolnotif_nb_test_for_page+" consecutive test on this page. We stop checking now from here by clearing dolnotif_idinterval="+dolnotif_idinterval);
clearInterval(dolnotif_idinterval);
}
return result;
2017-01-20 18:41:18 +01:00
}
<?php
print "\n";
print '})'."\n";