diff --git a/htdocs/core/ajax/check_notifications.php b/htdocs/core/ajax/check_notifications.php index af56cb23700..4db67aef939 100644 --- a/htdocs/core/ajax/check_notifications.php +++ b/htdocs/core/ajax/check_notifications.php @@ -24,31 +24,57 @@ if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC','1'); if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN','1'); require '../../main.inc.php'; -require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php'; global $user, $db, $langs, $conf; -$time = GETPOST('time'); +$time = (int) GETPOST('time'); // Use the time parameter that is always increased by time_update, even if call is late //$time=dol_now(); -session_start(); -$time_update = (empty($conf->global->MAIN_BROWSER_NOTIFICATION_FREQUENCY)?'3':(int) $conf->global->MAIN_BROWSER_NOTIFICATION_FREQUENCY); +$eventfound = array(); +//Uncomment this to force a test +//$eventfound[]=array('type'=>'agenda', 'id'=>1, 'tipo'=>'eee', 'location'=>'aaa'); -$eventos = array(); -//$eventos[]=array('type'=>'agenda', 'id'=>1, 'tipo'=>'eee', 'location'=>'aaa'); +//dol_syslog('time='.$time.' $_SESSION[auto_ck_events_not_before]='.$_SESSION['auto_check_events_not_before']); -// TODO Remove test on session. Timer should be managed by a javascript timer -if ($_SESSION['auto_check_events'] <= (int) $time) +// 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. +if ($time >= $_SESSION['auto_check_events_not_before']) { - $_SESSION['auto_check_events'] = $time + $time_update; - + $time_update = (int) $conf->global->MAIN_BROWSER_NOTIFICATION_FREQUENCY; // Always defined + if (! empty($_SESSION['auto_check_events_not_before'])) + { + // We start scan from the not before so if two tabs were opend at differents seconds and we close one (so the js timer), + // then we are not losing periods + $starttime = $_SESSION['auto_check_events_not_before']; + // Protection to avoid too long sessions + if ($starttime < ($time - (int) $conf->global->MAIN_SESSION_TIMEOUT)) + { + dol_syslog("We ask to check browser notification on a too large period. We fix this with current date."); + $starttime = $time; + } + } + else + { + $starttime = $time; + } + + $_SESSION['auto_check_events_not_before'] = $time + $time_update; + + // Force save of session change we did. + // WARNING: Any change in sessions after that will not be saved ! + session_write_close(); + + require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php'; + + + dol_syslog('NEW $_SESSION[auto_check_events_not_before]='.$_SESSION['auto_check_events_not_before']); + $sql = 'SELECT id'; $sql .= ' FROM ' . MAIN_DB_PREFIX . 'actioncomm a, ' . MAIN_DB_PREFIX . 'actioncomm_resources ar'; $sql .= ' WHERE a.id = ar.fk_actioncomm'; - // TODO Try to make a solution with only a javascript timer that is easier. Difficulty is to avoid notification twice when. - // This need to extend period to be sure to not miss and save what we notified to avoid duplicate (save is not done yet). - $sql .= " AND datep BETWEEN '" . $db->idate($time + 1) . "' AND '" . $db->idate($time + $time_update) . "'"; + // 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. + // 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). + $sql .= " AND datep BETWEEN '" . $db->idate($starttime) . "' AND '" . $db->idate($time + $time_update - 1) . "'"; $sql .= ' AND a.code <> "AC_OTH_AUTO"'; $sql .= ' AND ar.element_type = "user"'; $sql .= ' AND ar.fk_element = ' . $user->id; @@ -59,8 +85,10 @@ if ($_SESSION['auto_check_events'] <= (int) $time) $actionmod = new ActionComm($db); - while ($obj = $db->fetch_object($resql)) { - + while ($obj = $db->fetch_object($resql)) + { + $langs->load("agenda"); + $actionmod->fetch($obj->id); $event = array(); @@ -68,13 +96,13 @@ if ($_SESSION['auto_check_events'] <= (int) $time) $event['id'] = $actionmod->id; $event['tipo'] = $langs->transnoentities('Action' . $actionmod->code); $event['titulo'] = $actionmod->label; - $event['location'] = $actionmod->location; + $event['location'] = $langs->transnoentities('Location').': '.$actionmod->location; - $eventos[] = $event; + $eventfound[] = $event; } } } -print json_encode($eventos); +print json_encode($eventfound); diff --git a/htdocs/core/class/conf.class.php b/htdocs/core/class/conf.class.php index 064beaa8226..bb63e4c3560 100644 --- a/htdocs/core/class/conf.class.php +++ b/htdocs/core/class/conf.class.php @@ -420,6 +420,8 @@ class Conf if (empty($this->global->MAIN_MONNAIE)) $this->global->MAIN_MONNAIE='EUR'; $this->currency=$this->global->MAIN_MONNAIE; + if (empty($conf->global->MAIN_BROWSER_NOTIFICATION_FREQUENCY)) $conf->global->MAIN_BROWSER_NOTIFICATION_FREQUENCY = 30; // Less than 1 minutes to be sure + // conf->global->ACCOUNTING_MODE = Option des modules Comptabilites (simple ou expert). Defini le mode de calcul des etats comptables (CA,...) if (empty($this->global->ACCOUNTING_MODE)) $this->global->ACCOUNTING_MODE='RECETTES-DEPENSES'; // By default. Can be 'RECETTES-DEPENSES' ou 'CREANCES-DETTES' diff --git a/htdocs/core/js/lib_notification.js.php b/htdocs/core/js/lib_notification.js.php index a870066c775..af8e0492cdc 100644 --- a/htdocs/core/js/lib_notification.js.php +++ b/htdocs/core/js/lib_notification.js.php @@ -35,20 +35,23 @@ if (!($_SERVER['HTTP_REFERER'] === $dolibarr_main_url_root . '/' || $_SERVER['HT // Define javascript type header('Content-type: text/javascript; charset=UTF-8'); + $nowtime = time(); + //$nowtimeprevious = floor($nowtime / 60) * 60; // auto_check_events_not_before is rounded to previous minute + // TODO Try to make a solution with only a javascript timer that is easier. Difficulty is to avoid notification twice when. session_cache_limiter(FALSE); header('Cache-Control: no-cache'); session_start(); - if (!isset($_SESSION['auto_check_events'])) { - // Round to eliminate the second part - $_SESSION['auto_check_events'] = floor(time() / 60) * 60; - print 'var time_session = ' . $_SESSION['auto_check_events'] . ';'."\n"; - print 'var now = ' . $_SESSION['auto_check_events'] . ';' . "\n"; - } else { - print 'var time_session = ' . $_SESSION['auto_check_events'] . ';' . "\n"; - print 'var now = ' . time() . ';' . "\n"; + if (! isset($_SESSION['auto_check_events_not_before'])) + { + print 'console.log("_SESSION[auto_check_events_not_before] is not set");'."\n"; + // Round to eliminate the seconds + $_SESSION['auto_check_events_not_before'] = $nowtime; // auto_check_events_not_before is rounded to previous minute } - print 'var time_auto_update = '.(empty($conf->global->MAIN_BROWSER_NOTIFICATION_FREQUENCY)?'3':(int) $conf->global->MAIN_BROWSER_NOTIFICATION_FREQUENCY).';' . "\n"; + print 'var nowtime = ' . $nowtime . ';' . "\n"; + print 'var auto_check_events_not_before = '.$_SESSION['auto_check_events_not_before']. ';'."\n"; + print 'var time_js_next_test = Math.max(nowtime, auto_check_events_not_before);'."\n"; + print 'var time_auto_update = '.$conf->global->MAIN_BROWSER_NOTIFICATION_FREQUENCY; // Always defined ?> /* Check if permission ok */ @@ -56,31 +59,27 @@ if (!($_SERVER['HTTP_REFERER'] === $dolibarr_main_url_root . '/' || $_SERVER['HT Notification.requestPermission() } - if (now > (time_session + time_auto_update) || now == time_session) { - - first_execution(); //firts run auto check - } else { - - var time_first_execution = (time_auto_update - (now - time_session)) * 1000; //need milliseconds - - setTimeout(first_execution, time_first_execution); //first run auto check - } + /* Launch timer */ + // We set a delay before launching first test so next check will arrive after the time_auto_update compared to previous one. + var time_first_execution = (time_auto_update - (nowtime - time_js_next_test)) * 1000; //need milliseconds + console.log("Launch browser notif check: setTimeout to wait time_first_execution="+time_first_execution+" before first check - nowtime = "+nowtime+" auto_check_events_not_before = "+auto_check_events_not_before+" time_js_next_test = "+time_js_next_test+" time_auto_update="+time_auto_update); + setTimeout(first_execution, time_first_execution); //first run auto check function first_execution() { - console.log("Call first_execution"); - check_events(); - setInterval(check_events, time_auto_update * 1000); //program time for run check events + console.log("Call first_execution time_auto_update (MAIN_BROWSER_NOTIFICATION_FREQUENCY) = "+time_auto_update); + check_events(); //one check before launching timer to launch other checks + setInterval(check_events, time_auto_update * 1000); //program time to run next check events } function check_events() { if (Notification.permission === "granted") { - console.log("Call check_events"); + console.log("Call check_events time_js_next_test="+time_js_next_test); $.ajax("", { type: "post", // Usually post o get async: true, - data: {time: time_session}, + data: {time: time_js_next_test}, success: function (result) { var arr = JSON.parse(result); if (arr.length > 0) { @@ -95,7 +94,7 @@ if (!($_SERVER['HTTP_REFERER'] === $dolibarr_main_url_root . '/' || $_SERVER['HT var title="Not defined"; var body = value['tipo'] + ': ' + value['titulo']; if (value['type'] == 'agenda' && value['location'] != null && value['location'] != '') { - body += '\n transnoentities('Location')?>: ' + value['location']; + body += '\n' + value['location']; } if (value['type'] == 'agenda') @@ -132,7 +131,7 @@ if (!($_SERVER['HTTP_REFERER'] === $dolibarr_main_url_root . '/' || $_SERVER['HT console.log("Cancel check_events. Useless because Notification.permission is "+Notification.permission); } - time_session += time_auto_update; + time_js_next_test += time_auto_update; } agenda->enabled) && ! empty($conf->global->AGENDA_NOTIFICATION) && ! empty($conf->global->AGENDA_NOTIFICATION_SOUND)) $enablebrowsernotif=true; + if (! empty($conf->agenda->enabled) && ! empty($conf->global->AGENDA_NOTIFICATION)) $enablebrowsernotif=true; if ($enablebrowsernotif) { print ''."\n"; diff --git a/htdocs/user/class/user.class.php b/htdocs/user/class/user.class.php index 8281ef244d1..f087fc3db9b 100644 --- a/htdocs/user/class/user.class.php +++ b/htdocs/user/class/user.class.php @@ -235,7 +235,6 @@ class User extends CommonObject $sql.= " AND u.rowid = ".$id; } - dol_syslog(get_class($this)."::fetch", LOG_DEBUG); $result = $this->db->query($sql); if ($result) { @@ -618,7 +617,6 @@ class User extends CommonObject $sql.= " AND r.perms IS NOT NULL"; if ($moduletag) $sql.= " AND r.module = '".$this->db->escape($moduletag)."'"; - dol_syslog(get_class($this).'::getrights', LOG_DEBUG); $resql = $this->db->query($sql); if ($resql) { @@ -670,7 +668,6 @@ class User extends CommonObject $sql.= " AND r.perms IS NOT NULL"; if ($moduletag) $sql.= " AND r.module = '".$this->db->escape($moduletag)."'"; - dol_syslog(get_class($this).'::getrights', LOG_DEBUG); $resql = $this->db->query($sql); if ($resql) {