dolibarr  7.0.0-beta
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  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * Library javascript to enable Browser notifications
19  */
20 
21 if (!defined('NOREQUIREUSER')) define('NOREQUIREUSER', '1');
22 if (!defined('NOREQUIRESOC')) define('NOREQUIRESOC', '1');
23 if (!defined('NOCSRFCHECK')) define('NOCSRFCHECK', 1);
24 if (!defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL', 1);
25 if (!defined('NOLOGIN')) define('NOLOGIN', 1);
26 if (!defined('NOREQUIREMENU')) define('NOREQUIREMENU', 1);
27 if (!defined('NOREQUIREHTML')) define('NOREQUIREHTML', 1);
28 
29 require_once '../../main.inc.php';
30 
31 if (! ($_SERVER['HTTP_REFERER'] === $dolibarr_main_url_root . '/' || $_SERVER['HTTP_REFERER'] === $dolibarr_main_url_root . '/index.php'
32  || preg_match('/getmenu_div\.php/', $_SERVER['HTTP_REFERER'])))
33 {
34  global $langs, $conf;
35 
36  top_httphead('text/javascript; charset=UTF-8');
37 
38  $nowtime = time();
39  //$nowtimeprevious = floor($nowtime / 60) * 60; // auto_check_events_not_before is rounded to previous minute
40 
41  // TODO Try to make a solution with only a javascript timer that is easier. Difficulty is to avoid notification twice when.
42  /* session already started into main
43  session_cache_limiter(FALSE);
44  header('Cache-Control: no-cache');
45  session_set_cookie_params(0, '/', null, false, true); // Add tag httponly on session cookie
46  session_start();*/
47  if (! isset($_SESSION['auto_check_events_not_before']))
48  {
49  print 'console.log("_SESSION[auto_check_events_not_before] is not set");'."\n";
50  // Round to eliminate the seconds
51  $_SESSION['auto_check_events_not_before'] = $nowtime;
52  }
53  print 'var nowtime = ' . $nowtime . ';' . "\n";
54  print 'var login = \'' . $_SESSION['dol_login'] . '\';' . "\n";
55  print 'var auto_check_events_not_before = '.$_SESSION['auto_check_events_not_before']. ';'."\n";
56  print 'var time_js_next_test = Math.max(nowtime, auto_check_events_not_before);'."\n";
57  print 'var time_auto_update = '.$conf->global->MAIN_BROWSER_NOTIFICATION_FREQUENCY.';'."\n"; // Always defined
58  ?>
59 
60  /* Check if permission ok */
61  if (Notification.permission !== "granted") {
62  Notification.requestPermission()
63  }
64 
65  /* Launch timer */
66  // We set a delay before launching first test so next check will arrive after the time_auto_update compared to previous one.
67  var time_first_execution = (time_auto_update - (nowtime - time_js_next_test)) * 1000; //need milliseconds
68  if (login != '') {
69  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+" auto_check_events_not_before (val in session)= "+auto_check_events_not_before+" time_js_next_test (max now,auto_check_events_not_before) = "+time_js_next_test+" time_auto_update="+time_auto_update);
70  setTimeout(first_execution, time_first_execution);
71  } //first run auto check
72 
73 
74  function first_execution() {
75  console.log("Call first_execution time_auto_update (MAIN_BROWSER_NOTIFICATION_FREQUENCY) = "+time_auto_update);
76  check_events(); //one check before launching timer to launch other checks
77  setInterval(check_events, time_auto_update * 1000); //program time to run next check events
78  }
79 
80  function check_events() {
81  if (Notification.permission === "granted")
82  {
83  console.log("Call check_events time_js_next_test = date we are looking for event after ="+time_js_next_test);
84  $.ajax("<?php print DOL_URL_ROOT.'/core/ajax/check_notifications.php'; ?>", {
85  type: "post", // Usually post or get
86  async: true,
87  data: {time: time_js_next_test},
88  success: function (result) {
89  var arr = JSON.parse(result);
90  if (arr.length > 0) {
91  var audio = null;
92  <?php
93  if (! empty($conf->global->AGENDA_REMINDER_BROWSER_SOUND)) {
94  print 'audio = new Audio(\''.DOL_URL_ROOT.'/theme/common/sound/notification_agenda.wav'.'\');';
95  }
96  ?>
97 
98  $.each(arr, function (index, value) {
99  var url="notdefined";
100  var title="Not defined";
101  var body = value['tipo'] + ': ' + value['titulo'];
102  if (value['type'] == 'agenda' && value['location'] != null && value['location'] != '') {
103  body += '\n' + value['location'];
104  }
105 
106  if (value['type'] == 'agenda')
107  {
108  url = '<?php echo DOL_URL_ROOT.'/comm/action/card.php?id='; ?>' + value['id'];
109  title = '<?php print $langs->trans('Agenda') ?>';
110  }
111  var extra = {
112  icon: '<?php print DOL_URL_ROOT.'/theme/common/bell.png'; ?>',
113  body: body,
114  tag: value['id']
115  };
116 
117  // We release the notify
118  var noti = new Notification(title, extra);
119  if (index==0 && audio)
120  {
121  audio.play();
122  }
123  noti.onclick = function (event) {
124  console.log("An event to notify on browser was received");
125  event.preventDefault(); // prevent the browser from focusing the Notification's tab
126  window.focus();
127  window.open(url, '_blank');
128  noti.close();
129  };
130  });
131  }
132  }
133  });
134  }
135  else
136  {
137  console.log("Cancel check_events. Useless because Notification.permission is "+Notification.permission);
138  }
139 
140  time_js_next_test += time_auto_update;
141  console.log('Updated time_js_next_test. New value is '+time_js_next_test);
142  }
143 <?php
144 }
print
Draft customers invoices.
Definition: index.php:91
if(!defined('NOREQUIREMENU')) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html')
Show HTTP header.
Definition: main.inc.php:1052
type
Definition: viewcat.php:283