check_notifications.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. if (! defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL','1'); // Disables token renewal
  19. if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU','1');
  20. if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML','1');
  21. if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX','1');
  22. if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC','1');
  23. require '../../main.inc.php';
  24. /*
  25. * View
  26. */
  27. top_httphead('text/html'); // TODO Use a json mime type
  28. global $user, $db, $langs, $conf;
  29. $time = (int) GETPOST('time','int'); // Use the time parameter that is always increased by time_update, even if call is late
  30. //$time=dol_now();
  31. $eventfound = array();
  32. //Uncomment this to force a test
  33. //$eventfound[]=array('type'=>'agenda', 'id'=>1, 'tipo'=>'eee', 'location'=>'aaa');
  34. //dol_syslog('time='.$time.' $_SESSION[auto_ck_events_not_before]='.$_SESSION['auto_check_events_not_before']);
  35. // 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.
  36. if ($time >= $_SESSION['auto_check_events_not_before'])
  37. {
  38. $time_update = (int) $conf->global->MAIN_BROWSER_NOTIFICATION_FREQUENCY; // Always defined
  39. if (! empty($_SESSION['auto_check_events_not_before']))
  40. {
  41. // We start scan from the not before so if two tabs were opend at differents seconds and we close one (so the js timer),
  42. // then we are not losing periods
  43. $starttime = $_SESSION['auto_check_events_not_before'];
  44. // Protection to avoid too long sessions
  45. if ($starttime < ($time - (int) $conf->global->MAIN_SESSION_TIMEOUT))
  46. {
  47. dol_syslog("We ask to check browser notification on a too large period. We fix this with current date.");
  48. $starttime = $time;
  49. }
  50. }
  51. else
  52. {
  53. $starttime = $time;
  54. }
  55. $_SESSION['auto_check_events_not_before'] = $time + $time_update;
  56. // Force save of session change we did.
  57. // WARNING: Any change in sessions after that will not be saved !
  58. session_write_close();
  59. require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
  60. dol_syslog('NEW $_SESSION[auto_check_events_not_before]='.$_SESSION['auto_check_events_not_before']);
  61. $sql = 'SELECT id';
  62. $sql .= ' FROM ' . MAIN_DB_PREFIX . 'actioncomm a, ' . MAIN_DB_PREFIX . 'actioncomm_resources ar';
  63. $sql .= ' WHERE a.id = ar.fk_actioncomm';
  64. // 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.
  65. // This need to extend period to be sure to not miss and save in session what we notified to avoid duplicate (save is not done yet).
  66. $sql .= " AND datep BETWEEN '" . $db->idate($starttime) . "' AND '" . $db->idate($time + $time_update - 1) . "'";
  67. $sql .= ' AND a.code <> "AC_OTH_AUTO"';
  68. $sql .= ' AND ar.element_type = "user"';
  69. $sql .= ' AND ar.fk_element = ' . $user->id;
  70. $sql .= ' LIMIT 10'; // Avoid too many notification at once
  71. $resql = $db->query($sql);
  72. if ($resql) {
  73. $actionmod = new ActionComm($db);
  74. while ($obj = $db->fetch_object($resql))
  75. {
  76. $langs->load("agenda");
  77. $langs->load("commercial");
  78. $actionmod->fetch($obj->id);
  79. // Message must be formated and translated to be used with javascript directly
  80. $event = array();
  81. $event['type'] = 'agenda';
  82. $event['id'] = $actionmod->id;
  83. $event['tipo'] = $langs->transnoentities('Action' . $actionmod->code);
  84. $event['titulo'] = $actionmod->label;
  85. $event['location'] = $langs->transnoentities('Location').': '.$actionmod->location;
  86. $eventfound[] = $event;
  87. }
  88. }
  89. else
  90. {
  91. dol_syslog("Error sql = ".$db->lasterror(), LOG_ERR);
  92. }
  93. }
  94. print json_encode($eventfound);