dolibarr  16.0.5
lib_notification.js.php
1 <?php
2 /* Copyright (C) 2016 Sergio Sanchis <sergiosanchis@hotmail.com>
3  * Copyright (C) 2017 Juanjo Menent <jmenent@2byte.es>
4  * Copyright (C) 2020 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 
22 if (!defined('NOREQUIREUSER')) {
23  define('NOREQUIREUSER', '1');
24 }
25 if (!defined('NOREQUIRESOC')) {
26  define('NOREQUIRESOC', '1');
27 }
28 if (!defined('NOCSRFCHECK')) {
29  define('NOCSRFCHECK', 1);
30 }
31 if (!defined('NOTOKENRENEWAL')) {
32  define('NOTOKENRENEWAL', 1);
33 }
34 if (!defined('NOLOGIN')) {
35  define('NOLOGIN', 1);
36 }
37 if (!defined('NOREQUIREMENU')) {
38  define('NOREQUIREMENU', 1);
39 }
40 if (!defined('NOREQUIREHTML')) {
41  define('NOREQUIREHTML', 1);
42 }
43 
44 session_cache_limiter('public');
45 
46 require_once '../../main.inc.php';
47 
48 
49 /*
50  * View
51  */
52 
53 top_httphead('text/javascript; charset=UTF-8');
54 // Important: Following code is to avoid page request by browser and PHP CPU at each Dolibarr page access.
55 if (empty($dolibarr_nocache)) {
56  header('Cache-Control: max-age=10800, public, must-revalidate');
57 } else {
58  header('Cache-Control: no-cache');
59 }
60 
61 
62 print "jQuery(document).ready(function () {\n";
63 
64 //print " console.log('referrer=".dol_escape_js($_SERVER['HTTP_REFERER'])."');\n";
65 
66 print ' var nowtime = Date.now();';
67 print ' var time_auto_update = '.max(1, getDolGlobalInt('MAIN_BROWSER_NOTIFICATION_FREQUENCY')).';'."\n"; // Always defined
68 print ' var time_js_next_test;'."\n";
69 ?>
70 
71 /* Check if Notification is supported */
72 if ("Notification" in window) {
73  /* Check if permission ok */
74  if (Notification.permission !== "granted") {
75  console.log("Ask Notification.permission");
76  Notification.requestPermission()
77  }
78 
79  /* Launch timer */
80 
81  // We set a delay before launching first test so next check will arrive after the time_auto_update compared to previous one.
82  //var time_first_execution = (time_auto_update + (time_js_next_test - nowtime)) * 1000; //need milliseconds
83  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); ?>;
84 
85  setTimeout(first_execution, time_first_execution * 1000);
86  time_js_next_test = nowtime + time_first_execution;
87  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);
88 } else {
89  console.log("This browser in this context does not support Notification.");
90 }
91 
92 
93 function first_execution() {
94  console.log("Call first_execution then set repeat time to time_auto_update = MAIN_BROWSER_NOTIFICATION_FREQUENCY = "+time_auto_update);
95  check_events(); //one check before setting the new time for other checks
96  setInterval(check_events, time_auto_update * 1000); // Set new time to run next check events
97 }
98 
99 function check_events() {
100  if (Notification.permission === "granted")
101  {
102  time_js_next_test += time_auto_update;
103  console.log("Call ajax to check_events with time_js_next_test = "+time_js_next_test);
104 
105  $.ajax("<?php print DOL_URL_ROOT.'/core/ajax/check_notifications.php'; ?>", {
106  type: "post", // Usually post or get
107  async: true,
108  data: { time_js_next_test: time_js_next_test, forcechecknow: 1, token: 'notrequired' },
109  dataType: "json",
110  success: function (result) {
111  //console.log(result);
112  var arrayofpastreminders = Object.values(result.pastreminders);
113  if (arrayofpastreminders && arrayofpastreminders.length > 0) {
114  console.log("Retrieved "+arrayofpastreminders.length+" reminders to do.");
115  var audio = null;
116  <?php
117  if (!empty($conf->global->AGENDA_REMINDER_BROWSER_SOUND)) {
118  print 'audio = new Audio(\''.DOL_URL_ROOT.'/theme/common/sound/notification_agenda.wav\');';
119  }
120  ?>
121  var listofreminderids = '';
122  var noti = []
123 
124  $.each(arrayofpastreminders, function (index, value) {
125  console.log(value);
126  var url = "notdefined";
127  var title = "Not defined";
128  var body = value.label;
129  if (value.type == 'agenda' && value.location != null && value.location != '') {
130  body += '\n' + value.location;
131  }
132 
133  if (value.type == 'agenda' && (value.event_date_start_formated != null || value.event_date_start_formated['event_date_start'] != '')) {
134  body += '\n' + value.event_date_start_formated;
135  }
136 
137  if (value.type == 'agenda')
138  {
139  url = '<?php print DOL_URL_ROOT.'/comm/action/card.php?id='; ?>' + value.id_agenda;
140  title = '<?php print dol_escape_js($langs->transnoentities('EventReminder')) ?>';
141  }
142  var extra = {
143  icon: '<?php print DOL_URL_ROOT.'/theme/common/bell.png'; ?>',
144  //image: '<?php print DOL_URL_ROOT.'/theme/common/bell.png'; ?>',
145  body: body,
146  tag: value.id_agenda,
147  requireInteraction: true
148  };
149 
150  // We release the notify
151  console.log("Send notification on browser");
152  noti[index] = new Notification(title, extra);
153  if (index==0 && audio)
154  {
155  audio.play();
156  }
157 
158  if (noti[index]) {
159  noti[index].onclick = function (event) {
160  console.log("A click on notification on browser has been done");
161  event.preventDefault(); // prevent the browser from focusing the Notification's tab
162  window.focus();
163  window.open(url, '_blank');
164  noti[index].close();
165  };
166 
167  listofreminderids = (listofreminderids == '' ? '' : listofreminderids + ',') + value.id_reminder
168  }
169  });
170 
171  // Update status of all notifications we sent on browser (listofreminderids)
172  console.log("Flag notification as done for listofreminderids="+listofreminderids);
173  $.ajax("<?php print DOL_URL_ROOT.'/core/ajax/check_notifications.php?action=stopreminder&listofreminderids='; ?>"+listofreminderids, {
174  type: "POST", // Usually post or get
175  async: true,
176  data: { time_js_next_test: time_js_next_test, token: 'notrequired' }
177  });
178  } else {
179  console.log("No reminder to do found, next search at "+time_js_next_test);
180  }
181  }
182  });
183  }
184  else
185  {
186  console.log("Cancel check_events. Useless because javascript Notification.permission is "+Notification.permission+" (blocked manualy or web site is not https).");
187  }
188 }
189 <?php
190 
191 print "\n";
192 print '})'."\n";
top_httphead
if(!defined('NOREQUIREMENU')) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
Definition: main.inc.php:1407
getDolGlobalInt
getDolGlobalInt($key, $default=0)
Return dolibarr global constant int value.
Definition: functions.lib.php:93
type
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:119
if
if(!defined( 'CSRFCHECK_WITH_TOKEN'))
Definition: journals_list.php:25