lib_notification.js.php 6.6 KB

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