dolibarr 20.0.0
lib_notification.js.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2016 Sergio Sanchis <sergiosanchis@hotmail.com>
3 * Copyright (C) 2017 Juanjo Menent <jmenent@2byte.es>
4 * Copyright (C) 2020-2023 Destailleur Laurent <eldy@users.sourceforge.net>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 *
19 * Library javascript to enable Browser notifications
20 */
21
27if (!defined('NOREQUIREUSER')) {
28 define('NOREQUIREUSER', '1');
29}
30if (!defined('NOREQUIRESOC')) {
31 define('NOREQUIRESOC', '1');
32}
33if (!defined('NOCSRFCHECK')) {
34 define('NOCSRFCHECK', 1);
35}
36if (!defined('NOTOKENRENEWAL')) {
37 define('NOTOKENRENEWAL', 1);
38}
39if (!defined('NOLOGIN')) {
40 define('NOLOGIN', 1);
41}
42if (!defined('NOREQUIREMENU')) {
43 define('NOREQUIREMENU', 1);
44}
45if (!defined('NOREQUIREHTML')) {
46 define('NOREQUIREHTML', 1);
47}
48
49session_cache_limiter('public');
50
51require_once '../../main.inc.php';
52
53
54/*
55 * View
56 */
57
58top_httphead('text/javascript; charset=UTF-8');
59// Important: Following code is to avoid page request by browser and PHP CPU at each Dolibarr page access.
60if (empty($dolibarr_nocache)) {
61 header('Cache-Control: max-age=10800, public, must-revalidate');
62} else {
63 header('Cache-Control: no-cache');
64}
65
66
67print "jQuery(document).ready(function () {\n";
68
69//print " console.log('referrer=".dol_escape_js($_SERVER['HTTP_REFERER'])."');\n";
70
71print ' var nowtime = Date.now();';
72print ' var time_auto_update = '.max(1, getDolGlobalInt('MAIN_BROWSER_NOTIFICATION_FREQUENCY')).';'."\n"; // Always defined
73print ' var time_js_next_test;'."\n";
74print ' var dolnotif_nb_test_for_page = 0;'."\n";
75print ' var dolnotif_idinterval = null;'."\n";
76?>
77
78/* Check if Notification is supported */
79if ("Notification" in window) {
80 /* Check if permission ok */
81 if (Notification.permission !== "granted") {
82 console.log("Ask Notification.permission");
83 Notification.requestPermission(function(result) {
84 console.log("result for Notification.requestPermission is "+result);
85 });
86 }
87
88 /* Launch timer */
89
90 // We set a delay before launching first test so next check will arrive after the time_auto_update compared to previous one.
91 //var time_first_execution = (time_auto_update + (time_js_next_test - nowtime)) * 1000; //need milliseconds
92 var time_first_execution = <?php echo max(3, getDolGlobalInt('MAIN_BROWSER_NOTIFICATION_CHECK_FIRST_EXECUTION', 0)); ?>;
93
94 setTimeout(first_execution, time_first_execution * 1000); // Launch a first execution after a time_first_execution delay
95 time_js_next_test = nowtime + time_first_execution;
96 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);
97} else {
98 console.log("This browser in this context does not support Notification.");
99}
100
101/* The method called after time_first_execution on each page */
102function first_execution() {
103 console.log("Call first_execution of check_events()");
104 result = check_events(); //one check before setting the new time for other checks
105 if (result > 0) {
106 console.log("check_events() is scheduled as a repeated task with a time_auto_update = MAIN_BROWSER_NOTIFICATION_FREQUENCY = "+time_auto_update+"s");
107 dolnotif_idinterval = setInterval(check_events, time_auto_update * 1000); // Set new time to run next check events. time_auto_update=nb of seconds
108 }
109}
110
111/* the method call frequently every time_auto_update */
112function check_events() {
113 var result = 0;
114 dolnotif_nb_test_for_page += 1;
115 var methodfornotification = '<?php print getDolUserString('AGENDA_NOTIFICATION_METHOD', getDolGlobalString('AGENDA_NOTIFICATION_METHOD', 'jsnotification')); ?>';
116
117 permissionok = 0;
118 if (methodfornotification == "jsnotification" && Notification.permission == "granted") {
119 permissionok = 1;
120 }
121 if (methodfornotification == "jnotify") {
122 permissionok = 1;
123 }
124
125 if (permissionok == 1) {
126 var currentToken = 'notrequired';
127 const allMeta = document.getElementsByTagName("meta");
128 for (let i = 0; i < allMeta.length; i++) {
129 if (allMeta[i].getAttribute("name") == 'anti-csrf-currenttoken') {
130 currentToken = allMeta[i].getAttribute('content');
131 console.log("currentToken in page = "+currentToken);
132 }
133 }
134 time_js_next_test += time_auto_update;
135
136 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);
137
138 $.ajax("<?php print DOL_URL_ROOT.'/core/ajax/check_notifications.php'; ?>", {
139 type: "POST", // Usually post or get
140 async: true,
141 data: { time_js_next_test: time_js_next_test, forcechecknow: 1, token: currentToken, dolnotif_nb_test_for_page: dolnotif_nb_test_for_page },
142 dataType: "json",
143 success: function (result) {
144 //console.log(result);
145 var arrayofpastreminders = Object.values(result.pastreminders);
146 if (arrayofpastreminders && arrayofpastreminders.length > 0) {
147 console.log("Retrieved "+arrayofpastreminders.length+" reminders to do.");
148 var audio = null;
149 <?php
150 if (getDolUserString('AGENDA_REMINDER_BROWSER_SOUND', getDolGlobalString('AGENDA_REMINDER_BROWSER_SOUND'))) {
151 print 'audio = new Audio(\''.DOL_URL_ROOT.'/theme/common/sound/notification_agenda.wav\');';
152 }
153 ?>
154 var icon = '<?php print DOL_URL_ROOT.'/theme/common/octicons/build/svg/bell.svg'; ?>';
155 var listofreminderids = '';
156 var noti = []
157
158 $.each(arrayofpastreminders, function (index, value) {
159 console.log(value);
160 var url = "notdefined";
161 var title = "Not defined";
162 var body = "";
163
164 if (value.type == 'agenda')
165 {
166 url = '<?php print DOL_URL_ROOT.'/comm/action/card.php?id='; ?>' + value.id_agenda;
167 title = '<?php print dol_escape_js($langs->transnoentities('EventReminder')) ?>';
168 }
169
170 if (methodfornotification == "jsnotification") {
171 body = value.label;
172 if (value.type == 'agenda' && value.location != null && value.location != '') {
173 body += '\n' + value.location;
174 }
175
176 if (value.type == 'agenda' && (value.event_date_start_formated != null || value.event_date_start_formated['event_date_start'] != '')) {
177 body += '\n' + value.event_date_start_formated;
178 }
179 } else {
180 if (title != "Not defined") {
181 body = title+'<br><br>';
182 }
183 body += '<img src="'+icon+'">';
184 if (value.type == 'agenda' && (value.event_date_start_formated != null || value.event_date_start_formated['event_date_start'] != '')) {
185 body += ' '+value.event_date_start_formated;
186 }
187 body += ' - <a href="'+url+'"><?php echo img_picto("", "url", 'class="pictofixedwidth"').dol_escape_js($langs->trans("ShowDetails")); ?></a>';
188 body += '<br>'+value.label;
189 if (value.type == 'agenda' && value.location != null && value.location != '') {
190 body += '<br>' + value.location;
191 }
192 }
193
194 // We release the notify
195 console.log("Send notification on browser url="+url+" using method="+methodfornotification);
196
197 // Using the js browser Notification() popup
198 if (methodfornotification == 'jsnotification') {
199 var extra = {
200 icon: icon,
201 body: body,
202 lang: '<?php print dol_escape_js($langs->getDefaultLang(1)); ?>',
203 tag: value.id_agenda,
204 requireInteraction: true /* wait that the user click or close the notification */
205 /* "actions:" parameter is only supported for persistent notification shown using ServiceWorkerRegistration.showNotification() so disabled */
206 /* actions: [{ action: 'action1', title: 'New Button Label' }, { action: 'action2', title: 'Another Button' }] */
207 };
208
209 noti[index] = new Notification(title, extra);
210 if (index==0 && audio)
211 {
212 audio.play();
213 }
214
215 if (noti[index]) {
216 noti[index].onclick = function (event) {
217 /* If the user has clicked on button Activate */
218 console.log("A click on notification on browser has been done for url="+url);
219 event.preventDefault(); // prevent the browser from focusing the Notification's tab
220 window.focus();
221 window.open(url, '_blank');
222 noti[index].close();
223 };
224
225 listofreminderids = (listofreminderids == '' ? '' : listofreminderids + ',') + value.id_reminder
226 }
227 }
228
229 // Using jNotify popup
230 if (methodfornotification == 'jnotify') {
231 // Output a message with level "warning"
232 $.jnotify(body, 'warning', true, { remove: function (){} } );
233
234 listofreminderids = (listofreminderids == '' ? '' : listofreminderids + ',') + value.id_reminder
235 }
236 });
237
238 // Update status of all notifications we sent on browser (listofreminderids)
239 if (listofreminderids != '') {
240 console.log("Flag notification as done for listofreminderids="+listofreminderids);
241 $.ajax("<?php print DOL_URL_ROOT.'/core/ajax/check_notifications.php?action=stopreminder&listofreminderids='; ?>"+listofreminderids, {
242 type: "POST",
243 async: true,
244 data: { time_js_next_test: time_js_next_test, token: currentToken }
245 });
246 }
247 } else {
248 console.log("No remind to do found, next search at "+time_js_next_test);
249 }
250 }
251 });
252
253 result = 1;
254 } else {
255 console.log("Cancel check_events() with dolnotif_nb_test_for_page="+dolnotif_nb_test_for_page+". Check is useless because permission is off. Javascript Notification.permission is "+Notification.permission+" (blocked manually or web site is not https or browser is in Private mode).");
256
257 result = 2; // We return a positive so the repeated check will be done even if authorization is not yet allowed may be after this check)
258 }
259
260 if (dolnotif_nb_test_for_page >= 5) {
261 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);
262 clearInterval(dolnotif_idinterval);
263 }
264
265 return result;
266}
267<?php
268
269print "\n";
270print '})'."\n";
currentToken()
Return the value of token currently saved into session with name 'token'.
getDolUserString($key, $default='', $tmpuser=null)
Return Dolibarr user constant string value.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
table table fiche title col title div col center btnTitle icon
Automatically calls the icon named with the corresponding "object_" prefix.
if(!defined( 'CSRFCHECK_WITH_TOKEN'))
if(!defined( 'NOREQUIREMENU')) if(!empty(GETPOST('seteventmessages', 'alpha'))) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition repair.php:137