lib_notification.js.php 6.9 KB

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