lib_notification.js.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 = '.max(1, getDolGlobalInt('MAIN_BROWSER_NOTIFICATION_FREQUENCY')).';'."\n"; // Always defined
  58. print ' var time_js_next_test;'."\n";
  59. ?>
  60. /* Check if Notification is supported */
  61. if ("Notification" in window) {
  62. /* Check if permission ok */
  63. if (Notification.permission !== "granted") {
  64. console.log("Ask Notification.permission");
  65. Notification.requestPermission()
  66. }
  67. /* Launch timer */
  68. // We set a delay before launching first test so next check will arrive after the time_auto_update compared to previous one.
  69. //var time_first_execution = (time_auto_update + (time_js_next_test - nowtime)) * 1000; //need milliseconds
  70. 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); ?>;
  71. setTimeout(first_execution, time_first_execution * 1000);
  72. time_js_next_test = nowtime + time_first_execution;
  73. 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);
  74. } else {
  75. console.log("This browser in this context does not support Notification.");
  76. }
  77. function first_execution() {
  78. console.log("Call first_execution then set repeat time to time_auto_update = MAIN_BROWSER_NOTIFICATION_FREQUENCY = "+time_auto_update);
  79. check_events(); //one check before setting the new time for other checks
  80. setInterval(check_events, time_auto_update * 1000); // Set new time to run next check events
  81. }
  82. function check_events() {
  83. if (Notification.permission === "granted")
  84. {
  85. time_js_next_test += time_auto_update;
  86. console.log("Call ajax to check_events with time_js_next_test = "+time_js_next_test);
  87. $.ajax("<?php print DOL_URL_ROOT.'/core/ajax/check_notifications.php'; ?>", {
  88. type: "post", // Usually post or get
  89. async: true,
  90. data: { time_js_next_test: time_js_next_test, forcechecknow: 1, token: 'notrequired' },
  91. dataType: "json",
  92. success: function (result) {
  93. //console.log(result);
  94. var arrayofpastreminders = Object.values(result.pastreminders);
  95. if (arrayofpastreminders && arrayofpastreminders.length > 0) {
  96. console.log("Retrieved "+arrayofpastreminders.length+" reminders to do.");
  97. var audio = null;
  98. <?php
  99. if (!empty($conf->global->AGENDA_REMINDER_BROWSER_SOUND)) {
  100. print 'audio = new Audio(\''.DOL_URL_ROOT.'/theme/common/sound/notification_agenda.wav\');';
  101. }
  102. ?>
  103. var listofreminderids = '';
  104. var noti = []
  105. $.each(arrayofpastreminders, function (index, value) {
  106. console.log(value);
  107. var url = "notdefined";
  108. var title = "Not defined";
  109. var body = value.label;
  110. if (value.type == 'agenda' && value.location != null && value.location != '') {
  111. body += '\n' + value.location;
  112. }
  113. if (value.type == 'agenda' && (value.event_date_start_formated != null || value.event_date_start_formated['event_date_start'] != '')) {
  114. body += '\n' + value.event_date_start_formated;
  115. }
  116. if (value.type == 'agenda')
  117. {
  118. url = '<?php print DOL_URL_ROOT.'/comm/action/card.php?id='; ?>' + value.id_agenda;
  119. title = '<?php print dol_escape_js($langs->transnoentities('EventReminder')) ?>';
  120. }
  121. var extra = {
  122. icon: '<?php print DOL_URL_ROOT.'/theme/common/bell.png'; ?>',
  123. //image: '<?php print DOL_URL_ROOT.'/theme/common/bell.png'; ?>',
  124. body: body,
  125. tag: value.id_agenda,
  126. requireInteraction: true
  127. };
  128. // We release the notify
  129. console.log("Send notification on browser");
  130. noti[index] = new Notification(title, extra);
  131. if (index==0 && audio)
  132. {
  133. audio.play();
  134. }
  135. if (noti[index]) {
  136. noti[index].onclick = function (event) {
  137. console.log("A click on notification on browser has been done");
  138. event.preventDefault(); // prevent the browser from focusing the Notification's tab
  139. window.focus();
  140. window.open(url, '_blank');
  141. noti[index].close();
  142. };
  143. listofreminderids = (listofreminderids == '' ? '' : listofreminderids + ',') + value.id_reminder
  144. }
  145. });
  146. // Update status of all notifications we sent on browser (listofreminderids)
  147. console.log("Flag notification as done for listofreminderids="+listofreminderids);
  148. $.ajax("<?php print DOL_URL_ROOT.'/core/ajax/check_notifications.php?action=stopreminder&listofreminderids='; ?>"+listofreminderids, {
  149. type: "POST", // Usually post or get
  150. async: true,
  151. data: { time_js_next_test: time_js_next_test, token: 'notrequired' }
  152. });
  153. } else {
  154. console.log("No reminder to do found, next search at "+time_js_next_test);
  155. }
  156. }
  157. });
  158. }
  159. else
  160. {
  161. console.log("Cancel check_events. Useless because javascript Notification.permission is "+Notification.permission+" (blocked manualy or web site is not https).");
  162. }
  163. }
  164. <?php
  165. print "\n";
  166. print '})'."\n";