check_notifications.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /* Copyright (C) 2016 Sergio Sanchis <sergiosanchis@hotmail.com>
  3. * Copyright (C) 2017 Juanjo Menent <jmenent@2byte.es>
  4. * Copyright (C) 2019 Frédéric France <frederic.france@netlogic.fr>
  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. if (!defined('NOTOKENRENEWAL')) {
  20. define('NOTOKENRENEWAL', '1'); // Do not roll the Anti CSRF token (used if MAIN_SECURITY_CSRF_WITH_TOKEN is on)
  21. }
  22. if (!defined('NOREQUIREMENU')) {
  23. define('NOREQUIREMENU', '1');
  24. }
  25. if (!defined('NOREQUIREHTML')) {
  26. define('NOREQUIREHTML', '1');
  27. }
  28. if (!defined('NOREQUIREAJAX')) {
  29. define('NOREQUIREAJAX', '1');
  30. }
  31. if (!defined('NOREQUIRESOC')) {
  32. define('NOREQUIRESOC', '1');
  33. }
  34. if (!defined('NOREQUIRETRAN')) {
  35. define('NOREQUIRETRAN', '1');
  36. }
  37. // Load Dolibarr environment
  38. require '../../main.inc.php';
  39. //$time = (int) GETPOST('time', 'int'); // Use the time parameter that is always increased by time_update, even if call is late
  40. $time = dol_now();
  41. $action = GETPOST('action', 'aZ09');
  42. $listofreminderids = GETPOST('listofreminderids', 'aZ09');
  43. /*
  44. * Actions
  45. */
  46. if ($action == 'stopreminder') {
  47. dol_syslog("Clear notification for listofreminderids=".$listofreminderids);
  48. $listofreminderid = GETPOST('listofreminderids', 'intcomma');
  49. // Set the reminder as done
  50. $sql = 'UPDATE '.MAIN_DB_PREFIX.'actioncomm_reminder SET status = 1';
  51. $sql .= ' WHERE status = 0 AND rowid IN ('.$db->sanitize($db->escape($listofreminderid)).')';
  52. $sql .= ' AND fk_user = '.((int) $user->id).' AND entity = '.((int) $conf->entity);
  53. $resql = $db->query($sql);
  54. if (!$resql) {
  55. dol_print_error($db);
  56. }
  57. //}
  58. include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  59. // Clean database
  60. $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'actioncomm_reminder';
  61. $sql .= " WHERE dateremind < '".$db->idate(dol_time_plus_duree(dol_now(), -1, 'm'))."'";
  62. $resql = $db->query($sql);
  63. if (!$resql) {
  64. dol_print_error($db);
  65. }
  66. exit;
  67. }
  68. /*
  69. * View
  70. */
  71. top_httphead('application/json');
  72. global $user, $db, $langs, $conf;
  73. $eventfound = array();
  74. //Uncomment this to force a test
  75. //$eventfound[]=array('type'=>'agenda', 'id'=>1, 'tipo'=>'eee', 'location'=>'aaa');
  76. //dol_syslog('time='.$time.' $_SESSION[auto_ck_events_not_before]='.$_SESSION['auto_check_events_not_before']);
  77. // TODO Try to make a solution with only a javascript timer that is easier. Difficulty is to avoid notification twice when several tabs are opened.
  78. // This need to extend period to be sure to not miss and save in session what we notified to avoid duplicate.
  79. if (empty($_SESSION['auto_check_events_not_before']) || $time >= $_SESSION['auto_check_events_not_before'] || GETPOST('forcechecknow', 'int')) {
  80. /*$time_update = (int) $conf->global->MAIN_BROWSER_NOTIFICATION_FREQUENCY; // Always defined
  81. if (!empty($_SESSION['auto_check_events_not_before']))
  82. {
  83. // We start scan from the not before so if two tabs were opend at differents seconds and we close one (so the js timer),
  84. // then we are not losing periods
  85. $starttime = $_SESSION['auto_check_events_not_before'];
  86. // Protection to avoid too long sessions
  87. if ($starttime < ($time - (int) $conf->global->MAIN_SESSION_TIMEOUT))
  88. {
  89. dol_syslog("We ask to check browser notification on a too large period. We fix this with current date.");
  90. $starttime = $time;
  91. }
  92. } else {
  93. $starttime = $time;
  94. }
  95. $_SESSION['auto_check_events_not_before'] = $time + $time_update;
  96. */
  97. // Force save of the session change we did.
  98. // WARNING: Any change in sessions after that will not be saved !
  99. session_write_close();
  100. require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
  101. dol_syslog('NEW $_SESSION[auto_check_events_not_before]='.(empty($_SESSION['auto_check_events_not_before']) ? '' : $_SESSION['auto_check_events_not_before']));
  102. $sql = 'SELECT a.id as id_agenda, a.code, a.datep, a.label, a.location, ar.rowid as id_reminder, ar.dateremind, ar.fk_user as id_user_reminder';
  103. $sql .= ' FROM '.MAIN_DB_PREFIX.'actioncomm as a';
  104. if (!empty($user->conf->MAIN_USER_WANT_ALL_EVENTS_NOTIFICATIONS)) {
  105. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'actioncomm_reminder as ar ON a.id = ar.fk_actioncomm AND ar.fk_user = '.((int) $user->id);
  106. $sql .= ' WHERE a.code <> "AC_OTH_AUTO"';
  107. $sql .= ' AND (';
  108. $sql .= " ar.typeremind = 'browser' AND ar.dateremind < '".$db->idate(dol_now())."' AND ar.status = 0 AND ar.entity = ".$conf->entity;
  109. $sql .= ' )';
  110. } else {
  111. $sql .= ' JOIN '.MAIN_DB_PREFIX.'actioncomm_reminder as ar ON a.id = ar.fk_actioncomm AND ar.fk_user = '.((int) $user->id);
  112. $sql .= " AND ar.typeremind = 'browser' AND ar.dateremind < '".$db->idate(dol_now())."' AND ar.status = 0 AND ar.entity = ".$conf->entity;
  113. }
  114. $sql .= $db->order('datep', 'ASC');
  115. $sql .= ' LIMIT 10'; // Avoid too many notification at once
  116. $resql = $db->query($sql);
  117. if ($resql) {
  118. while ($obj = $db->fetch_object($resql)) {
  119. // Message must be formated and translated to be used with javascript directly
  120. $event = array();
  121. $event['type'] = 'agenda';
  122. $event['id_reminder'] = $obj->id_reminder;
  123. $event['id_agenda'] = $obj->id_agenda;
  124. $event['id_user'] = $obj->id_user_reminder;
  125. $event['code'] = $obj->code;
  126. $event['label'] = $obj->label;
  127. $event['location'] = $obj->location;
  128. $event['reminder_date_formated_tzserver'] = dol_print_date($db->jdate($obj->dateremind), 'standard', 'tzserver');
  129. $event['event_date_start_formated_tzserver'] = dol_print_date($db->jdate($obj->datep), 'standard', 'tzserver');
  130. $event['reminder_date_formated'] = dol_print_date($db->jdate($obj->dateremind), 'standard', 'tzuser');
  131. $event['event_date_start_formated'] = dol_print_date($db->jdate($obj->datep), 'standard', 'tzuser');
  132. $eventfound[$obj->id_agenda] = $event;
  133. }
  134. } else {
  135. dol_syslog("Error sql = ".$db->lasterror(), LOG_ERR);
  136. }
  137. }
  138. print json_encode(array('pastreminders'=>$eventfound, 'nextreminder'=>''));