lib_notification.js.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. session_cache_limiter('public');
  43. require_once '../../main.inc.php';
  44. /*
  45. * View
  46. */
  47. top_httphead('text/javascript; charset=UTF-8');
  48. // Important: Following code is to avoid page request by browser and PHP CPU at each Dolibarr page access.
  49. if (empty($dolibarr_nocache)) {
  50. header('Cache-Control: max-age=10800, public, must-revalidate');
  51. } else {
  52. header('Cache-Control: no-cache');
  53. }
  54. print "jQuery(document).ready(function () {\n";
  55. //print " console.log('referrer=".dol_escape_js($_SERVER['HTTP_REFERER'])."');\n";
  56. print ' var nowtime = Date.now();';
  57. print ' var time_auto_update = '.$conf->global->MAIN_BROWSER_NOTIFICATION_FREQUENCY.';'."\n"; // Always defined
  58. print ' var time_js_next_test;'."\n";
  59. ?>
  60. /* Check if permission ok */
  61. if (Notification.permission !== "granted") {
  62. console.log("Ask Notification.permission");
  63. Notification.requestPermission()
  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 + (time_js_next_test - nowtime)) * 1000; //need milliseconds
  68. 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); ?>;
  69. setTimeout(first_execution, time_first_execution * 1000);
  70. time_js_next_test = nowtime + time_first_execution;
  71. 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);
  72. function first_execution() {
  73. console.log("Call first_execution then set repeat time to time_auto_update = MAIN_BROWSER_NOTIFICATION_FREQUENCY = "+time_auto_update);
  74. check_events(); //one check before setting the new time for other checks
  75. setInterval(check_events, time_auto_update * 1000); // Set new time to run next check events
  76. }
  77. function check_events() {
  78. if (Notification.permission === "granted")
  79. {
  80. time_js_next_test += time_auto_update;
  81. console.log("Call ajax to check_events with time_js_next_test = "+time_js_next_test);
  82. $.ajax("<?php print DOL_URL_ROOT.'/core/ajax/check_notifications.php'; ?>", {
  83. type: "post", // Usually post or get
  84. async: true,
  85. data: { time_js_next_test: time_js_next_test, forcechecknow: 1, token: 'notrequired' },
  86. dataType: "json",
  87. success: function (result) {
  88. //console.log(result);
  89. var arrayofpastreminders = Object.values(result.pastreminders);
  90. if (arrayofpastreminders && arrayofpastreminders.length > 0) {
  91. console.log("Retrieved "+arrayofpastreminders.length+" reminders to do.");
  92. var audio = null;
  93. <?php
  94. if (!empty($conf->global->AGENDA_REMINDER_BROWSER_SOUND)) {
  95. print 'audio = new Audio(\''.DOL_URL_ROOT.'/theme/common/sound/notification_agenda.wav\');';
  96. }
  97. ?>
  98. var listofreminderids = '';
  99. var noti = []
  100. $.each(arrayofpastreminders, function (index, value) {
  101. console.log(value);
  102. var url = "notdefined";
  103. var title = "Not defined";
  104. var body = value.label;
  105. if (value.type == 'agenda' && value.location != null && value.location != '') {
  106. body += '\n' + value.location;
  107. }
  108. if (value.type == 'agenda' && (value.event_date_start_formated != null || value.event_date_start_formated['event_date_start'] != '')) {
  109. body += '\n' + value.event_date_start_formated;
  110. }
  111. if (value.type == 'agenda')
  112. {
  113. url = '<?php print DOL_URL_ROOT.'/comm/action/card.php?id='; ?>' + value.id_agenda;
  114. title = '<?php print dol_escape_js($langs->transnoentities('EventReminder')) ?>';
  115. }
  116. var extra = {
  117. icon: '<?php print DOL_URL_ROOT.'/theme/common/bell.png'; ?>',
  118. //image: '<?php print DOL_URL_ROOT.'/theme/common/bell.png'; ?>',
  119. body: body,
  120. tag: value.id_agenda,
  121. requireInteraction: true
  122. };
  123. // We release the notify
  124. console.log("Send notification on browser");
  125. noti[index] = new Notification(title, extra);
  126. if (index==0 && audio)
  127. {
  128. audio.play();
  129. }
  130. if (noti[index]) {
  131. noti[index].onclick = function (event) {
  132. console.log("A click on notification on browser has been done");
  133. event.preventDefault(); // prevent the browser from focusing the Notification's tab
  134. window.focus();
  135. window.open(url, '_blank');
  136. noti[index].close();
  137. };
  138. listofreminderids = (listofreminderids == '' ? '' : listofreminderids + ',') + value.id_reminder
  139. }
  140. });
  141. // Update status of all notifications we sent on browser (listofreminderids)
  142. console.log("Flag notification as done for listofreminderids="+listofreminderids);
  143. $.ajax("<?php print DOL_URL_ROOT.'/core/ajax/check_notifications.php?action=stopreminder&listofreminderids='; ?>"+listofreminderids, {
  144. type: "POST", // Usually post or get
  145. async: true,
  146. data: { time_js_next_test: time_js_next_test, token: 'notrequired' }
  147. });
  148. } else {
  149. console.log("No reminder to do found, next search at "+time_js_next_test);
  150. }
  151. }
  152. });
  153. }
  154. else
  155. {
  156. console.log("Cancel check_events. Useless because javascript Notification.permission is "+Notification.permission+" (blocked manualy or web site is not https).");
  157. }
  158. }
  159. <?php
  160. print "\n";
  161. print '})'."\n";