Skip to content

Commit

Permalink
Debug of feature for browser notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
eldy committed Feb 14, 2017
1 parent 5de0d81 commit eb70d6e
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 47 deletions.
64 changes: 46 additions & 18 deletions htdocs/core/ajax/check_notifications.php
Expand Up @@ -24,31 +24,57 @@
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;
Expand All @@ -59,22 +85,24 @@

$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();
$event['type'] = 'agenda';
$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);

2 changes: 2 additions & 0 deletions htdocs/core/class/conf.class.php
Expand Up @@ -420,6 +420,8 @@ function setValues($db)
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'

Expand Down
49 changes: 24 additions & 25 deletions htdocs/core/js/lib_notification.js.php
Expand Up @@ -35,52 +35,51 @@
// 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 */
if (Notification.permission !== "granted") {
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("<?php print dol_buildpath('/core/ajax/check_notifications.php', 1); ?>", {
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) {
Expand All @@ -95,7 +94,7 @@ function check_events() {
var title="Not defined";
var body = value['tipo'] + ': ' + value['titulo'];
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')
Expand Down Expand Up @@ -132,7 +131,7 @@ function check_events() {
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
}
2 changes: 1 addition & 1 deletion htdocs/main.inc.php
Expand Up @@ -1307,7 +1307,7 @@ function top_htmlhead($head, $title='', $disablejs=0, $disablehead=0, $arrayofjs

// Browser notifications
$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)
{
print '<!-- Includes JS of Dolibarr -->'."\n";
Expand Down
3 changes: 0 additions & 3 deletions htdocs/user/class/user.class.php
Expand Up @@ -235,7 +235,6 @@ function fetch($id='', $login='',$sid='',$loadpersonalconf=1, $entity=-1)
$sql.= " AND u.rowid = ".$id;
}

dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
$result = $this->db->query($sql);
if ($result)
{
Expand Down Expand Up @@ -618,7 +617,6 @@ function getrights($moduletag='')
$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)
{
Expand Down Expand Up @@ -670,7 +668,6 @@ function getrights($moduletag='')
$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)
{
Expand Down

0 comments on commit eb70d6e

Please sign in to comment.