dolibarr  20.0.0-beta
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 
27 if (!defined('NOREQUIREUSER')) {
28  define('NOREQUIREUSER', '1');
29 }
30 if (!defined('NOREQUIRESOC')) {
31  define('NOREQUIRESOC', '1');
32 }
33 if (!defined('NOCSRFCHECK')) {
34  define('NOCSRFCHECK', 1);
35 }
36 if (!defined('NOTOKENRENEWAL')) {
37  define('NOTOKENRENEWAL', 1);
38 }
39 if (!defined('NOLOGIN')) {
40  define('NOLOGIN', 1);
41 }
42 if (!defined('NOREQUIREMENU')) {
43  define('NOREQUIREMENU', 1);
44 }
45 if (!defined('NOREQUIREHTML')) {
46  define('NOREQUIREHTML', 1);
47 }
48 
49 session_cache_limiter('public');
50 
51 require_once '../../main.inc.php';
52 
53 
54 /*
55  * View
56  */
57 
58 top_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.
60 if (empty($dolibarr_nocache)) {
61  header('Cache-Control: max-age=10800, public, must-revalidate');
62 } else {
63  header('Cache-Control: no-cache');
64 }
65 
66 
67 print "jQuery(document).ready(function () {\n";
68 
69 //print " console.log('referrer=".dol_escape_js($_SERVER['HTTP_REFERER'])."');\n";
70 
71 print ' var nowtime = Date.now();';
72 print ' var time_auto_update = '.max(1, getDolGlobalInt('MAIN_BROWSER_NOTIFICATION_FREQUENCY')).';'."\n"; // Always defined
73 print ' var time_js_next_test;'."\n";
74 print ' var dolnotif_nb_test_for_page = 0;'."\n";
75 print ' var dolnotif_idinterval = null;'."\n";
76 ?>
77 
78 /* Check if Notification is supported */
79 if ("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, !getDolGlobalString('MAIN_BROWSER_NOTIFICATION_CHECK_FIRST_EXECUTION') ? 0 : $conf->global->MAIN_BROWSER_NOTIFICATION_CHECK_FIRST_EXECUTION); ?>;
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 */
102 function 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 */
112 function check_events() {
113  var result = 0;
114  dolnotif_nb_test_for_page += 1;
115 
116  if (Notification.permission === "granted") {
117  var currentToken = 'notrequired';
118  const allMeta = document.getElementsByTagName("meta");
119  for (let i = 0; i < allMeta.length; i++) {
120  if (allMeta[i].getAttribute("name") == 'anti-csrf-currenttoken') {
121  currentToken = allMeta[i].getAttribute('content');
122  console.log("currentToken in page = "+currentToken);
123  }
124  }
125  time_js_next_test += time_auto_update;
126 
127  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);
128 
129  $.ajax("<?php print DOL_URL_ROOT.'/core/ajax/check_notifications.php'; ?>", {
130  type: "POST", // Usually post or get
131  async: true,
132  data: { time_js_next_test: time_js_next_test, forcechecknow: 1, token: currentToken, dolnotif_nb_test_for_page: dolnotif_nb_test_for_page },
133  dataType: "json",
134  success: function (result) {
135  //console.log(result);
136  var arrayofpastreminders = Object.values(result.pastreminders);
137  if (arrayofpastreminders && arrayofpastreminders.length > 0) {
138  console.log("Retrieved "+arrayofpastreminders.length+" reminders to do.");
139  var audio = null;
140  <?php
141  if (getDolGlobalString('AGENDA_REMINDER_BROWSER_SOUND')) {
142  print 'audio = new Audio(\''.DOL_URL_ROOT.'/theme/common/sound/notification_agenda.wav\');';
143  }
144  ?>
145  var listofreminderids = '';
146  var noti = []
147 
148  $.each(arrayofpastreminders, function (index, value) {
149  console.log(value);
150  var url = "notdefined";
151  var title = "Not defined";
152  var body = value.label;
153  var icon = '<?php print DOL_URL_ROOT.'/theme/common/octicons/build/svg/bell.svg'; ?>';
154  var image = '<?php print DOL_URL_ROOT.'/theme/common/octicons/build/svg/bell.svg'; ?>';
155  if (value.type == 'agenda' && value.location != null && value.location != '') {
156  body += '\n' + value.location;
157  }
158 
159  if (value.type == 'agenda' && (value.event_date_start_formated != null || value.event_date_start_formated['event_date_start'] != '')) {
160  body += '\n' + value.event_date_start_formated;
161  }
162 
163  if (value.type == 'agenda')
164  {
165  url = '<?php print DOL_URL_ROOT.'/comm/action/card.php?id='; ?>' + value.id_agenda;
166  title = '<?php print dol_escape_js($langs->transnoentities('EventReminder')) ?>';
167  }
168  var extra = {
169  icon: icon,
170  body: body,
171  lang: '<?php print dol_escape_js($langs->getDefaultLang(1)); ?>',
172  tag: value.id_agenda,
173  requireInteraction: true /* wait that the user click or close the notification */
174  /* only supported for persistent notification shown using ServiceWorkerRegistration.showNotification() so disabled */
175  /* actions: [{ action: 'action1', title: 'New Button Label' }, { action: 'action2', title: 'Another Button' }] */
176  };
177 
178  // We release the notify
179  console.log("Send notification on browser url="+url);
180  noti[index] = new Notification(title, extra);
181  if (index==0 && audio)
182  {
183  audio.play();
184  }
185 
186  if (noti[index]) {
187  noti[index].onclick = function (event) {
188  /* If the user has clicked on button Activate */
189  console.log("A click on notification on browser has been done for url="+url);
190  event.preventDefault(); // prevent the browser from focusing the Notification's tab
191  window.focus();
192  window.open(url, '_blank');
193  noti[index].close();
194  };
195 
196  listofreminderids = (listofreminderids == '' ? '' : listofreminderids + ',') + value.id_reminder
197  }
198  });
199 
200  // Update status of all notifications we sent on browser (listofreminderids)
201  console.log("Flag notification as done for listofreminderids="+listofreminderids);
202  $.ajax("<?php print DOL_URL_ROOT.'/core/ajax/check_notifications.php?action=stopreminder&listofreminderids='; ?>"+listofreminderids, {
203  type: "POST", // Usually post or get
204  async: true,
205  data: { time_js_next_test: time_js_next_test, token: currentToken }
206  });
207  } else {
208  console.log("No remind to do found, next search at "+time_js_next_test);
209  }
210  }
211  });
212 
213  result = 1;
214  } else {
215  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).");
216 
217  result = 2; // We return a positive so the repeated check will done even if authorization is not yet allowed may be after this check)
218  }
219 
220  if (dolnotif_nb_test_for_page >= 5) {
221  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);
222  clearInterval(dolnotif_idinterval);
223  }
224 
225  return result;
226 }
227 <?php
228 
229 print "\n";
230 print '})'."\n";
currentToken()
Return the value of token currently saved into session with name 'token'.
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 right 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.
Definition: main.inc.php:1648
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:123