forked from Wavyzz/dolibarr
Debug of feature for browser notifications
This commit is contained in:
@@ -24,31 +24,57 @@ if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC','1');
|
|||||||
if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN','1');
|
if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN','1');
|
||||||
|
|
||||||
require '../../main.inc.php';
|
require '../../main.inc.php';
|
||||||
require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
|
|
||||||
|
|
||||||
global $user, $db, $langs, $conf;
|
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();
|
//$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();
|
//dol_syslog('time='.$time.' $_SESSION[auto_ck_events_not_before]='.$_SESSION['auto_check_events_not_before']);
|
||||||
//$eventos[]=array('type'=>'agenda', 'id'=>1, 'tipo'=>'eee', 'location'=>'aaa');
|
|
||||||
|
|
||||||
// TODO Remove test on session. Timer should be managed by a javascript timer
|
// 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 ($_SESSION['auto_check_events'] <= (int) $time)
|
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 = 'SELECT id';
|
||||||
$sql .= ' FROM ' . MAIN_DB_PREFIX . 'actioncomm a, ' . MAIN_DB_PREFIX . 'actioncomm_resources ar';
|
$sql .= ' FROM ' . MAIN_DB_PREFIX . 'actioncomm a, ' . MAIN_DB_PREFIX . 'actioncomm_resources ar';
|
||||||
$sql .= ' WHERE a.id = ar.fk_actioncomm';
|
$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.
|
// 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 what we notified to avoid duplicate (save is not done yet).
|
// 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($time + 1) . "' AND '" . $db->idate($time + $time_update) . "'";
|
$sql .= " AND datep BETWEEN '" . $db->idate($starttime) . "' AND '" . $db->idate($time + $time_update - 1) . "'";
|
||||||
$sql .= ' AND a.code <> "AC_OTH_AUTO"';
|
$sql .= ' AND a.code <> "AC_OTH_AUTO"';
|
||||||
$sql .= ' AND ar.element_type = "user"';
|
$sql .= ' AND ar.element_type = "user"';
|
||||||
$sql .= ' AND ar.fk_element = ' . $user->id;
|
$sql .= ' AND ar.fk_element = ' . $user->id;
|
||||||
@@ -59,7 +85,9 @@ if ($_SESSION['auto_check_events'] <= (int) $time)
|
|||||||
|
|
||||||
$actionmod = new ActionComm($db);
|
$actionmod = new ActionComm($db);
|
||||||
|
|
||||||
while ($obj = $db->fetch_object($resql)) {
|
while ($obj = $db->fetch_object($resql))
|
||||||
|
{
|
||||||
|
$langs->load("agenda");
|
||||||
|
|
||||||
$actionmod->fetch($obj->id);
|
$actionmod->fetch($obj->id);
|
||||||
|
|
||||||
@@ -68,13 +96,13 @@ if ($_SESSION['auto_check_events'] <= (int) $time)
|
|||||||
$event['id'] = $actionmod->id;
|
$event['id'] = $actionmod->id;
|
||||||
$event['tipo'] = $langs->transnoentities('Action' . $actionmod->code);
|
$event['tipo'] = $langs->transnoentities('Action' . $actionmod->code);
|
||||||
$event['titulo'] = $actionmod->label;
|
$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);
|
||||||
|
|
||||||
|
|||||||
@@ -420,6 +420,8 @@ class Conf
|
|||||||
if (empty($this->global->MAIN_MONNAIE)) $this->global->MAIN_MONNAIE='EUR';
|
if (empty($this->global->MAIN_MONNAIE)) $this->global->MAIN_MONNAIE='EUR';
|
||||||
$this->currency=$this->global->MAIN_MONNAIE;
|
$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,...)
|
// 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'
|
if (empty($this->global->ACCOUNTING_MODE)) $this->global->ACCOUNTING_MODE='RECETTES-DEPENSES'; // By default. Can be 'RECETTES-DEPENSES' ou 'CREANCES-DETTES'
|
||||||
|
|
||||||
|
|||||||
@@ -35,20 +35,23 @@ if (!($_SERVER['HTTP_REFERER'] === $dolibarr_main_url_root . '/' || $_SERVER['HT
|
|||||||
// Define javascript type
|
// Define javascript type
|
||||||
header('Content-type: text/javascript; charset=UTF-8');
|
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.
|
// 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);
|
session_cache_limiter(FALSE);
|
||||||
header('Cache-Control: no-cache');
|
header('Cache-Control: no-cache');
|
||||||
session_start();
|
session_start();
|
||||||
if (!isset($_SESSION['auto_check_events'])) {
|
if (! isset($_SESSION['auto_check_events_not_before']))
|
||||||
// Round to eliminate the second part
|
{
|
||||||
$_SESSION['auto_check_events'] = floor(time() / 60) * 60;
|
print 'console.log("_SESSION[auto_check_events_not_before] is not set");'."\n";
|
||||||
print 'var time_session = ' . $_SESSION['auto_check_events'] . ';'."\n";
|
// Round to eliminate the seconds
|
||||||
print 'var now = ' . $_SESSION['auto_check_events'] . ';' . "\n";
|
$_SESSION['auto_check_events_not_before'] = $nowtime; // auto_check_events_not_before is rounded to previous minute
|
||||||
} else {
|
|
||||||
print 'var time_session = ' . $_SESSION['auto_check_events'] . ';' . "\n";
|
|
||||||
print 'var now = ' . time() . ';' . "\n";
|
|
||||||
}
|
}
|
||||||
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 */
|
/* Check if permission ok */
|
||||||
@@ -56,31 +59,27 @@ if (!($_SERVER['HTTP_REFERER'] === $dolibarr_main_url_root . '/' || $_SERVER['HT
|
|||||||
Notification.requestPermission()
|
Notification.requestPermission()
|
||||||
}
|
}
|
||||||
|
|
||||||
if (now > (time_session + time_auto_update) || now == time_session) {
|
/* Launch timer */
|
||||||
|
// We set a delay before launching first test so next check will arrive after the time_auto_update compared to previous one.
|
||||||
first_execution(); //firts run auto check
|
var time_first_execution = (time_auto_update - (nowtime - time_js_next_test)) * 1000; //need milliseconds
|
||||||
} else {
|
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
|
||||||
var time_first_execution = (time_auto_update - (now - time_session)) * 1000; //need milliseconds
|
|
||||||
|
|
||||||
setTimeout(first_execution, time_first_execution); //first run auto check
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
function first_execution() {
|
function first_execution() {
|
||||||
console.log("Call first_execution");
|
console.log("Call first_execution time_auto_update (MAIN_BROWSER_NOTIFICATION_FREQUENCY) = "+time_auto_update);
|
||||||
check_events();
|
check_events(); //one check before launching timer to launch other checks
|
||||||
setInterval(check_events, time_auto_update * 1000); //program time for run check events
|
setInterval(check_events, time_auto_update * 1000); //program time to run next check events
|
||||||
}
|
}
|
||||||
|
|
||||||
function check_events() {
|
function check_events() {
|
||||||
if (Notification.permission === "granted")
|
if (Notification.permission === "granted")
|
||||||
{
|
{
|
||||||
console.log("Call check_events");
|
console.log("Call check_events time_js_next_test="+time_js_next_test);
|
||||||
$.ajax("<?php print dol_buildpath('/core/ajax/check_notifications.php', 1); ?>", {
|
$.ajax("<?php print dol_buildpath('/core/ajax/check_notifications.php', 1); ?>", {
|
||||||
type: "post", // Usually post o get
|
type: "post", // Usually post o get
|
||||||
async: true,
|
async: true,
|
||||||
data: {time: time_session},
|
data: {time: time_js_next_test},
|
||||||
success: function (result) {
|
success: function (result) {
|
||||||
var arr = JSON.parse(result);
|
var arr = JSON.parse(result);
|
||||||
if (arr.length > 0) {
|
if (arr.length > 0) {
|
||||||
@@ -95,7 +94,7 @@ if (!($_SERVER['HTTP_REFERER'] === $dolibarr_main_url_root . '/' || $_SERVER['HT
|
|||||||
var title="Not defined";
|
var title="Not defined";
|
||||||
var body = value['tipo'] + ': ' + value['titulo'];
|
var body = value['tipo'] + ': ' + value['titulo'];
|
||||||
if (value['type'] == 'agenda' && value['location'] != null && value['location'] != '') {
|
if (value['type'] == 'agenda' && value['location'] != null && value['location'] != '') {
|
||||||
body += '\n <?php print $langs->transnoentities('Location')?>: ' + value['location'];
|
body += '\n' + value['location'];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (value['type'] == 'agenda')
|
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);
|
console.log("Cancel check_events. Useless because Notification.permission is "+Notification.permission);
|
||||||
}
|
}
|
||||||
|
|
||||||
time_session += time_auto_update;
|
time_js_next_test += time_auto_update;
|
||||||
}
|
}
|
||||||
<?php
|
<?php
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1307,7 +1307,7 @@ function top_htmlhead($head, $title='', $disablejs=0, $disablehead=0, $arrayofjs
|
|||||||
|
|
||||||
// Browser notifications
|
// Browser notifications
|
||||||
$enablebrowsernotif=false;
|
$enablebrowsernotif=false;
|
||||||
if (! empty($conf->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)
|
if ($enablebrowsernotif)
|
||||||
{
|
{
|
||||||
print '<!-- Includes JS of Dolibarr -->'."\n";
|
print '<!-- Includes JS of Dolibarr -->'."\n";
|
||||||
|
|||||||
@@ -235,7 +235,6 @@ class User extends CommonObject
|
|||||||
$sql.= " AND u.rowid = ".$id;
|
$sql.= " AND u.rowid = ".$id;
|
||||||
}
|
}
|
||||||
|
|
||||||
dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
|
|
||||||
$result = $this->db->query($sql);
|
$result = $this->db->query($sql);
|
||||||
if ($result)
|
if ($result)
|
||||||
{
|
{
|
||||||
@@ -618,7 +617,6 @@ class User extends CommonObject
|
|||||||
$sql.= " AND r.perms IS NOT NULL";
|
$sql.= " AND r.perms IS NOT NULL";
|
||||||
if ($moduletag) $sql.= " AND r.module = '".$this->db->escape($moduletag)."'";
|
if ($moduletag) $sql.= " AND r.module = '".$this->db->escape($moduletag)."'";
|
||||||
|
|
||||||
dol_syslog(get_class($this).'::getrights', LOG_DEBUG);
|
|
||||||
$resql = $this->db->query($sql);
|
$resql = $this->db->query($sql);
|
||||||
if ($resql)
|
if ($resql)
|
||||||
{
|
{
|
||||||
@@ -670,7 +668,6 @@ class User extends CommonObject
|
|||||||
$sql.= " AND r.perms IS NOT NULL";
|
$sql.= " AND r.perms IS NOT NULL";
|
||||||
if ($moduletag) $sql.= " AND r.module = '".$this->db->escape($moduletag)."'";
|
if ($moduletag) $sql.= " AND r.module = '".$this->db->escape($moduletag)."'";
|
||||||
|
|
||||||
dol_syslog(get_class($this).'::getrights', LOG_DEBUG);
|
|
||||||
$resql = $this->db->query($sql);
|
$resql = $this->db->query($sql);
|
||||||
if ($resql)
|
if ($resql)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user