lib_notification.js.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. if (!defined('NOREQUIREUSER')) define('NOREQUIREUSER', '1');
  21. if (!defined('NOREQUIRESOC')) define('NOREQUIRESOC', '1');
  22. if (!defined('NOCSRFCHECK')) define('NOCSRFCHECK', 1);
  23. if (!defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL', 1);
  24. if (!defined('NOLOGIN')) define('NOLOGIN', 1);
  25. if (!defined('NOREQUIREMENU')) define('NOREQUIREMENU', 1);
  26. if (!defined('NOREQUIREHTML')) define('NOREQUIREHTML', 1);
  27. require_once '../../main.inc.php';
  28. if (! ($_SERVER['HTTP_REFERER'] === $dolibarr_main_url_root . '/' || $_SERVER['HTTP_REFERER'] === $dolibarr_main_url_root . '/index.php'
  29. || preg_match('/getmenu_div\.php/', $_SERVER['HTTP_REFERER'])))
  30. {
  31. global $langs, $conf;
  32. // Define javascript type
  33. header('Content-type: text/javascript; charset=UTF-8');
  34. $nowtime = time();
  35. //$nowtimeprevious = floor($nowtime / 60) * 60; // auto_check_events_not_before is rounded to previous minute
  36. // TODO Try to make a solution with only a javascript timer that is easier. Difficulty is to avoid notification twice when.
  37. session_cache_limiter(FALSE);
  38. header('Cache-Control: no-cache');
  39. session_start();
  40. if (! isset($_SESSION['auto_check_events_not_before']))
  41. {
  42. print 'console.log("_SESSION[auto_check_events_not_before] is not set");'."\n";
  43. // Round to eliminate the seconds
  44. $_SESSION['auto_check_events_not_before'] = $nowtime; // auto_check_events_not_before is rounded to previous minute
  45. }
  46. print 'var nowtime = ' . $nowtime . ';' . "\n";
  47. print 'var login = \'' . $_SESSION['dol_login'] . '\';' . "\n";
  48. print 'var auto_check_events_not_before = '.$_SESSION['auto_check_events_not_before']. ';'."\n";
  49. print 'var time_js_next_test = Math.max(nowtime, auto_check_events_not_before);'."\n";
  50. print 'var time_auto_update = '.$conf->global->MAIN_BROWSER_NOTIFICATION_FREQUENCY.';'."\n"; // Always defined
  51. ?>
  52. /* Check if permission ok */
  53. if (Notification.permission !== "granted") {
  54. Notification.requestPermission()
  55. }
  56. /* Launch timer */
  57. // We set a delay before launching first test so next check will arrive after the time_auto_update compared to previous one.
  58. var time_first_execution = (time_auto_update - (nowtime - time_js_next_test)) * 1000; //need milliseconds
  59. if (login != '') {
  60. 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);
  61. setTimeout(first_execution, time_first_execution);
  62. } //first run auto check
  63. function first_execution() {
  64. console.log("Call first_execution time_auto_update (MAIN_BROWSER_NOTIFICATION_FREQUENCY) = "+time_auto_update);
  65. check_events(); //one check before launching timer to launch other checks
  66. setInterval(check_events, time_auto_update * 1000); //program time to run next check events
  67. }
  68. function check_events() {
  69. if (Notification.permission === "granted")
  70. {
  71. console.log("Call check_events time_js_next_test = date we are looking for event after ="+time_js_next_test);
  72. $.ajax("<?php print DOL_URL_ROOT.'/core/ajax/check_notifications.php'; ?>", {
  73. type: "post", // Usually post or get
  74. async: true,
  75. data: {time: time_js_next_test},
  76. success: function (result) {
  77. var arr = JSON.parse(result);
  78. if (arr.length > 0) {
  79. var audio = null;
  80. <?php
  81. if (! empty($conf->global->AGENDA_NOTIFICATION_SOUND)) {
  82. print 'audio = new Audio(\''.DOL_URL_ROOT.'/theme/common/sound/notification_agenda.wav'.'\');';
  83. }
  84. ?>
  85. $.each(arr, function (index, value) {
  86. var url="notdefined";
  87. var title="Not defined";
  88. var body = value['tipo'] + ': ' + value['titulo'];
  89. if (value['type'] == 'agenda' && value['location'] != null && value['location'] != '') {
  90. body += '\n' + value['location'];
  91. }
  92. if (value['type'] == 'agenda')
  93. {
  94. url = '<?php echo DOL_URL_ROOT.'/comm/action/card.php?id='; ?>' + value['id'];
  95. title = '<?php print $langs->trans('Agenda') ?>';
  96. }
  97. var extra = {
  98. icon: '<?php print DOL_URL_ROOT.'/theme/common/bell.png'; ?>',
  99. body: body,
  100. tag: value['id']
  101. };
  102. // We release the notify
  103. var noti = new Notification(title, extra);
  104. if (index==0 && audio)
  105. {
  106. audio.play();
  107. }
  108. noti.onclick = function (event) {
  109. console.log("An event to notify on browser was received");
  110. event.preventDefault(); // prevent the browser from focusing the Notification's tab
  111. window.focus();
  112. window.open(url, '_blank');
  113. noti.close();
  114. };
  115. });
  116. }
  117. }
  118. });
  119. }
  120. else
  121. {
  122. console.log("Cancel check_events. Useless because Notification.permission is "+Notification.permission);
  123. }
  124. time_js_next_test += time_auto_update;
  125. console.log('Updated time_js_next_test. New value is '+time_js_next_test);
  126. }
  127. <?php
  128. }