2017-01-13 16:08:49 +01:00
< ? php
/* Copyright ( C ) 2016 Sergio Sanchis < sergiosanchis @ hotmail . com >
* Copyright ( C ) 2017 Juanjo Menent < jmenent @ 2 byte . es >
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < http :// www . gnu . org / licenses />.
2018-04-16 13:51:16 +02:00
*
2017-01-20 18:41:18 +01:00
* Library javascript to enable Browser notifications
*/
2017-01-13 16:08:49 +01:00
if ( ! defined ( 'NOREQUIREUSER' )) define ( 'NOREQUIREUSER' , '1' );
if ( ! defined ( 'NOREQUIRESOC' )) define ( 'NOREQUIRESOC' , '1' );
if ( ! defined ( 'NOCSRFCHECK' )) define ( 'NOCSRFCHECK' , 1 );
if ( ! defined ( 'NOTOKENRENEWAL' )) define ( 'NOTOKENRENEWAL' , 1 );
if ( ! defined ( 'NOLOGIN' )) define ( 'NOLOGIN' , 1 );
if ( ! defined ( 'NOREQUIREMENU' )) define ( 'NOREQUIREMENU' , 1 );
if ( ! defined ( 'NOREQUIREHTML' )) define ( 'NOREQUIREHTML' , 1 );
require_once '../../main.inc.php' ;
2017-02-23 00:45:27 +01:00
if ( ! ( $_SERVER [ 'HTTP_REFERER' ] === $dolibarr_main_url_root . '/' || $_SERVER [ 'HTTP_REFERER' ] === $dolibarr_main_url_root . '/index.php'
|| preg_match ( '/getmenu_div\.php/' , $_SERVER [ 'HTTP_REFERER' ])))
2017-01-20 18:41:18 +01:00
{
2017-01-13 16:08:49 +01:00
global $langs , $conf ;
2017-05-09 21:01:37 +02:00
top_httphead ( 'text/javascript; charset=UTF-8' );
2018-04-16 13:51:16 +02:00
2017-02-14 18:04:12 +01:00
$nowtime = time ();
//$nowtimeprevious = floor($nowtime / 60) * 60; // auto_check_events_not_before is rounded to previous minute
2017-01-20 18:41:18 +01:00
// TODO Try to make a solution with only a javascript timer that is easier. Difficulty is to avoid notification twice when.
2017-05-09 21:01:37 +02:00
/* session already started into main
2018-04-16 13:51:16 +02:00
session_cache_limiter ( false );
2017-01-20 18:41:18 +01:00
header ( 'Cache-Control: no-cache' );
2017-05-09 21:01:37 +02:00
session_set_cookie_params ( 0 , '/' , null , false , true ); // Add tag httponly on session cookie
session_start (); */
2018-04-16 13:51:16 +02:00
if ( ! isset ( $_SESSION [ 'auto_check_events_not_before' ]))
2017-02-14 18:04:12 +01:00
{
print 'console.log("_SESSION[auto_check_events_not_before] is not set");' . " \n " ;
// Round to eliminate the seconds
2017-05-09 21:01:37 +02:00
$_SESSION [ 'auto_check_events_not_before' ] = $nowtime ;
2017-01-13 16:08:49 +01:00
}
2017-02-14 18:04:12 +01:00
print 'var nowtime = ' . $nowtime . ';' . " \n " ;
2017-02-22 21:18:34 +01:00
print 'var login = \'' . $_SESSION [ 'dol_login' ] . '\';' . " \n " ;
2017-02-14 18:04:12 +01:00
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 " ;
2017-02-22 21:18:34 +01:00
print 'var time_auto_update = ' . $conf -> global -> MAIN_BROWSER_NOTIFICATION_FREQUENCY . ';' . " \n " ; // Always defined
2017-01-13 16:08:49 +01:00
?>
2017-02-22 21:18:34 +01:00
2017-01-20 18:41:18 +01:00
/* Check if permission ok */
if ( Notification . permission !== " granted " ) {
Notification . requestPermission ()
}
2017-01-13 16:08:49 +01:00
2017-02-14 18:04:12 +01:00
/* 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
2018-04-16 13:51:16 +02:00
if ( login != '' ) {
2017-04-30 04:32:03 +02:00
console . log ( " Launch browser notif check: setTimeout is set to launch 'first_execution' function after a wait of time_first_execution= " + time_first_execution + " . nowtime (time php page generation) = " + nowtime + " auto_check_events_not_before (val in session)= " + auto_check_events_not_before + " time_js_next_test (max now,auto_check_events_not_before) = " + time_js_next_test + " time_auto_update= " + time_auto_update );
2018-04-16 13:51:16 +02:00
setTimeout ( first_execution , time_first_execution );
2017-02-22 21:18:34 +01:00
} //first run auto check
2017-01-13 16:08:49 +01:00
function first_execution () {
2017-02-14 18:04:12 +01:00
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
2017-01-13 16:08:49 +01:00
}
function check_events () {
2017-01-20 18:41:18 +01:00
if ( Notification . permission === " granted " )
{
2017-04-30 04:32:03 +02:00
console . log ( " Call check_events time_js_next_test = date we are looking for event after = " + time_js_next_test );
$ . ajax ( " <?php print DOL_URL_ROOT.'/core/ajax/check_notifications.php'; ?> " , {
type : " post " , // Usually post or get
2017-01-20 18:41:18 +01:00
async : true ,
2017-02-14 18:04:12 +01:00
data : { time : time_js_next_test },
2017-01-20 18:41:18 +01:00
success : function ( result ) {
var arr = JSON . parse ( result );
if ( arr . length > 0 ) {
2018-04-16 13:51:16 +02:00
var audio = null ;
2017-01-13 16:08:49 +01:00
< ? php
2017-08-20 16:07:22 +02:00
if ( ! empty ( $conf -> global -> AGENDA_REMINDER_BROWSER_SOUND )) {
2017-04-30 04:32:03 +02:00
print 'audio = new Audio(\'' . DOL_URL_ROOT . '/theme/common/sound/notification_agenda.wav' . '\');' ;
2017-01-13 16:08:49 +01:00
}
?>
2018-04-16 13:51:16 +02:00
2017-01-13 16:08:49 +01:00
$ . each ( arr , function ( index , value ) {
2017-01-20 18:41:18 +01:00
var url = " notdefined " ;
var title = " Not defined " ;
2017-01-13 16:08:49 +01:00
var body = value [ 'tipo' ] + ': ' + value [ 'titulo' ];
2017-01-20 18:41:18 +01:00
if ( value [ 'type' ] == 'agenda' && value [ 'location' ] != null && value [ 'location' ] != '' ) {
2017-02-14 18:04:12 +01:00
body += '\n' + value [ 'location' ];
2017-01-13 16:08:49 +01:00
}
2018-04-16 13:51:16 +02:00
2017-01-20 18:41:18 +01:00
if ( value [ 'type' ] == 'agenda' )
{
url = '<?php echo DOL_URL_ROOT.' / comm / action / card . php ? id = '; ?>' + value [ 'id' ];
title = '<?php print $langs->trans(' Agenda ') ?>' ;
}
2017-01-13 16:08:49 +01:00
var extra = {
2017-01-20 18:41:18 +01:00
icon : '<?php print DOL_URL_ROOT.' / theme / common / bell . png '; ?>' ,
2017-01-13 16:08:49 +01:00
body : body ,
tag : value [ 'id' ]
};
2018-04-16 13:51:16 +02:00
2017-01-13 16:08:49 +01:00
// We release the notify
var noti = new Notification ( title , extra );
2018-04-16 13:51:16 +02:00
if ( index == 0 && audio )
2017-01-20 18:41:18 +01:00
{
audio . play ();
2017-01-13 16:08:49 +01:00
}
noti . onclick = function ( event ) {
2017-01-20 18:41:18 +01:00
console . log ( " An event to notify on browser was received " );
2017-01-13 16:08:49 +01:00
event . preventDefault (); // prevent the browser from focusing the Notification's tab
window . focus ();
2017-01-20 18:41:18 +01:00
window . open ( url , '_blank' );
2017-01-13 16:08:49 +01:00
noti . close ();
};
});
}
}
2017-01-20 18:41:18 +01:00
});
}
else
{
console . log ( " Cancel check_events. Useless because Notification.permission is " + Notification . permission );
}
2017-02-14 18:04:12 +01:00
time_js_next_test += time_auto_update ;
2017-04-30 04:32:03 +02:00
console . log ( 'Updated time_js_next_test. New value is ' + time_js_next_test );
2017-01-13 16:08:49 +01:00
}
2018-04-16 13:51:16 +02:00
< ? php
2017-01-20 18:41:18 +01:00
}