dolibarr  13.0.2
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')) define('NOREQUIREUSER', '1');
23 if (!defined('NOREQUIRESOC')) define('NOREQUIRESOC', '1');
24 if (!defined('NOCSRFCHECK')) define('NOCSRFCHECK', 1);
25 if (!defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL', 1);
26 if (!defined('NOLOGIN')) define('NOLOGIN', 1);
27 if (!defined('NOREQUIREMENU')) define('NOREQUIREMENU', 1);
28 if (!defined('NOREQUIREHTML')) define('NOREQUIREHTML', 1);
29 
30 require_once '../../main.inc.php';
31 
32 if (!($_SERVER['HTTP_REFERER'] === $dolibarr_main_url_root.'/' || $_SERVER['HTTP_REFERER'] === $dolibarr_main_url_root.'/index.php'
33  || preg_match('/getmenu_div\.php/', $_SERVER['HTTP_REFERER'])))
34 {
35  global $langs, $conf;
36 
37  top_httphead('text/javascript; charset=UTF-8');
38 
39  print 'var login = \''.$_SESSION['dol_login'].'\';'."\n";
40  print 'var nowtime = Date.now();';
41  print 'var time_auto_update = '.$conf->global->MAIN_BROWSER_NOTIFICATION_FREQUENCY.';'."\n"; // Always defined
42  print 'var time_js_next_test;'."\n";
43  ?>
44 
45  /* Check if permission ok */
46  if (Notification.permission !== "granted") {
47  console.log("Ask Notification.permission");
48  Notification.requestPermission()
49  }
50 
51  /* Launch timer */
52  // We set a delay before launching first test so next check will arrive after the time_auto_update compared to previous one.
53  //var time_first_execution = (time_auto_update + (time_js_next_test - nowtime)) * 1000; //need milliseconds
54  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); ?>;
55  if (login != '') {
56  setTimeout(first_execution, time_first_execution * 1000);
57  time_js_next_test = nowtime + time_first_execution;
58  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);
59  } //first run auto check
60 
61 
62  function first_execution() {
63  console.log("Call first_execution then set repeat time to time_auto_update = MAIN_BROWSER_NOTIFICATION_FREQUENCY = "+time_auto_update);
64  check_events(); //one check before setting the new time for other checks
65  setInterval(check_events, time_auto_update * 1000); // Set new time to run next check events
66  }
67 
68  function check_events() {
69  if (Notification.permission === "granted")
70  {
71  time_js_next_test += time_auto_update;
72  console.log("Call ajax to check_events with time_js_next_test = "+time_js_next_test);
73 
74  $.ajax("<?php print DOL_URL_ROOT.'/core/ajax/check_notifications.php'; ?>", {
75  type: "post", // Usually post or get
76  async: true,
77  data: { time_js_next_test: time_js_next_test, forcechecknow: 1, token: 'notrequired' },
78  dataType: "json",
79  success: function (result) {
80  //console.log(result);
81  var arrayofpastreminders = Object.values(result.pastreminders);
82  if (arrayofpastreminders && arrayofpastreminders.length > 0) {
83  console.log("Retrieved "+arrayofpastreminders.length+" reminders to do.");
84  var audio = null;
85  <?php
86  if (!empty($conf->global->AGENDA_REMINDER_BROWSER_SOUND)) {
87  print 'audio = new Audio(\''.DOL_URL_ROOT.'/theme/common/sound/notification_agenda.wav\');';
88  }
89  ?>
90  var listofreminderids = '';
91  var noti = []
92 
93  $.each(arrayofpastreminders, function (index, value) {
94  console.log(value);
95  var url = "notdefined";
96  var title = "Not defined";
97  var body = value.label;
98  if (value.type == 'agenda' && value.location != null && value.location != '') {
99  body += '\n' + value.location;
100  }
101 
102  if (value.type == 'agenda' && (value.event_date_start_formated != null || value.event_date_start_formated['event_date_start'] != '')) {
103  body += '\n' + value.event_date_start_formated;
104  }
105 
106  if (value.type == 'agenda')
107  {
108  url = '<?php print DOL_URL_ROOT.'/comm/action/card.php?id='; ?>' + value.id_agenda;
109  title = '<?php print dol_escape_js($langs->trans('EventReminder')) ?>';
110  }
111  var extra = {
112  icon: '<?php print DOL_URL_ROOT.'/theme/common/bell.png'; ?>',
113  //image: '<?php print DOL_URL_ROOT.'/theme/common/bell.png'; ?>',
114  body: body,
115  tag: value.id_agenda,
116  requireInteraction: true
117  };
118 
119  // We release the notify
120  console.log("Send notification on browser");
121  noti[index] = new Notification(title, extra);
122  if (index==0 && audio)
123  {
124  audio.play();
125  }
126 
127  if (noti[index]) {
128  noti[index].onclick = function (event) {
129  console.log("A click on notification on browser has been done");
130  event.preventDefault(); // prevent the browser from focusing the Notification's tab
131  window.focus();
132  window.open(url, '_blank');
133  noti[index].close();
134  };
135 
136  listofreminderids = (listofreminderids == '' ? '' : listofreminderids + ',') + value.id_reminder
137  }
138  });
139 
140  // Update status of all notifications we sent on browser (listofreminderids)
141  console.log("Flag notification as done for listofreminderids="+listofreminderids);
142  $.ajax("<?php print DOL_URL_ROOT.'/core/ajax/check_notifications.php?action=stopreminder&listofreminderids='; ?>"+listofreminderids, {
143  type: "post", // Usually post or get
144  async: true,
145  data: { time_js_next_test: time_js_next_test, token: 'notrequired' }
146  });
147  } else {
148  console.log("No reminder to do found, next search at "+time_js_next_test);
149  }
150  }
151  });
152  }
153  else
154  {
155  console.log("Cancel check_events. Useless because javascript Notification.permission is "+Notification.permission+" (blocked manualy or web site is not https).");
156  }
157  }
158  <?php
159 }
if(!defined('NOREQUIREMENU')) if(!function_exists("llxHeader")) top_httphead($contenttype= 'text/html', $forcenocache=0)
Show HTTP header.
Definition: main.inc.php:1214
print $_SERVER["PHP_SELF"]
Edit parameters.
print
Draft customers invoices.
Definition: index.php:89
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:105