Debug browser notification

This commit is contained in:
Laurent Destailleur 2017-01-20 18:41:18 +01:00
parent b602e04fc8
commit 809e3ce4d5
9 changed files with 176 additions and 149 deletions

View File

@ -408,31 +408,34 @@ print $form->selectarray('AGENDA_DEFAULT_VIEW', $tmplist, $conf->global->AGENDA_
print '</td></tr>'."\n";
// AGENDA NOTIFICATION
$var=!$var;
print '<tr '.$bc[$var].'>'."\n";
print '<td>'.$langs->trans('AGENDA_NOTIFICATION').'</td>'."\n";
print '<td align="center">&nbsp;</td>'."\n";
print '<td align="right">'."\n";
if (empty($conf->global->AGENDA_NOTIFICATION)) {
print '<a href="'.$_SERVER['PHP_SELF'].'?action=set_AGENDA_NOTIFICATION">'.img_picto($langs->trans('Disabled'),'switch_off').'</a>';
print '</td></tr>'."\n";
} else {
print '<a href="'.$_SERVER['PHP_SELF'].'?action=del_AGENDA_NOTIFICATION">'.img_picto($langs->trans('Enabled'),'switch_on').'</a>';
print '</td></tr>'."\n";
$var=!$var;
if ($conf->global->MAIN_FEATURES_LEVEL > 0)
{
$var=!$var;
print '<tr '.$bc[$var].'>'."\n";
print '<td>'.$langs->trans('AGENDA_NOTIFICATION_SOUND').'</td>'."\n";
print '<td>'.$langs->trans('AGENDA_NOTIFICATION').'</td>'."\n";
print '<td align="center">&nbsp;</td>'."\n";
print '<td align="right">'."\n";
if (empty($conf->global->AGENDA_NOTIFICATION_SOUND)) {
print '<a href="'.$_SERVER['PHP_SELF'].'?action=set_AGENDA_NOTIFICATION_SOUND">'.img_picto($langs->trans('Disabled'),'switch_off').'</a>';
if (empty($conf->global->AGENDA_NOTIFICATION)) {
print '<a href="'.$_SERVER['PHP_SELF'].'?action=set_AGENDA_NOTIFICATION">'.img_picto($langs->trans('Disabled'),'switch_off').'</a>';
print '</td></tr>'."\n";
} else {
print '<a href="'.$_SERVER['PHP_SELF'].'?action=del_AGENDA_NOTIFICATION_SOUND">'.img_picto($langs->trans('Enabled'),'switch_on').'</a>';
print '<a href="'.$_SERVER['PHP_SELF'].'?action=del_AGENDA_NOTIFICATION">'.img_picto($langs->trans('Enabled'),'switch_on').'</a>';
print '</td></tr>'."\n";
$var=!$var;
print '<tr '.$bc[$var].'>'."\n";
print '<td>'.$langs->trans('AGENDA_NOTIFICATION_SOUND').'</td>'."\n";
print '<td align="center">&nbsp;</td>'."\n";
print '<td align="right">'."\n";
if (empty($conf->global->AGENDA_NOTIFICATION_SOUND)) {
print '<a href="'.$_SERVER['PHP_SELF'].'?action=set_AGENDA_NOTIFICATION_SOUND">'.img_picto($langs->trans('Disabled'),'switch_off').'</a>';
} else {
print '<a href="'.$_SERVER['PHP_SELF'].'?action=del_AGENDA_NOTIFICATION_SOUND">'.img_picto($langs->trans('Enabled'),'switch_on').'</a>';
}
print '</td></tr>'."\n";
}
print '</td></tr>'."\n";
}
print '</table>';

View File

@ -1,70 +0,0 @@
<?php
/* Copyright (C) 2016 Sergio Sanchis <sergiosanchis@hotmail.com>
* Copyright (C) 2017 Juanjo Menent <jmenent@2byte.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/>.
*
*/
require '../../main.inc.php';
require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
global $user, $db, $langs, $conf;
$time = GETPOST('time');
session_start();
//TODO Configure how long the upgrade will take
$time_update = 60;
if (! empty($conf->global->AGENDA_NOTIFICATION)) {
if ($_SESSION['auto_check_events'] <= (int) $time) {
$_SESSION['auto_check_events'] = $time + $time_update;
$eventos = array();
$sql = 'SELECT id';
$sql .= ' FROM ' . MAIN_DB_PREFIX . 'actioncomm a, ' . MAIN_DB_PREFIX . 'actioncomm_resources ar';
$sql .= ' WHERE datep BETWEEN ' . $db->idate($time + 1) . ' AND ' . $db->idate($time + $time_update);
$sql .= ' AND a.id = ar.fk_actioncomm';
$sql .= ' AND a.code <> "AC_OTH_AUTO"';
$sql .= ' AND ar.element_type = "user"';
$sql .= ' AND ar.fk_element = ' . $user->id;
$resql = $db->query($sql);
if ($resql) {
$actionmod = new ActionComm($db);
while ($obj = $db->fetch_object($resql)) {
$event = array();
$actionmod->fetch($obj->id);
$event['id'] = $actionmod->id;
$event['tipo'] = $langs->transnoentities('Action' . $actionmod->code);
$event['titulo'] = $actionmod->label;
$event['location'] = $actionmod->location;
$eventos[] = $event;
$actionmod->initAsSpecimen();
}
}
print json_encode($eventos);
}
}

View File

@ -0,0 +1,80 @@
<?php
/* Copyright (C) 2016 Sergio Sanchis <sergiosanchis@hotmail.com>
* Copyright (C) 2017 Juanjo Menent <jmenent@2byte.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/>.
*/
if (! defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL','1'); // Disables token renewal
if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU','1');
if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML','1');
if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX','1');
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=dol_now();
session_start();
$time_update = (empty($conf->global->MAIN_BROWSER_NOTIFICATION_FREQUENCY)?'3':(int) $conf->global->MAIN_BROWSER_NOTIFICATION_FREQUENCY);
$eventos = array();
//$eventos[]=array('type'=>'agenda', 'id'=>1, 'tipo'=>'eee', 'location'=>'aaa');
// TODO Remove test on session. Timer should be managed by a javascript timer
if ($_SESSION['auto_check_events'] <= (int) $time)
{
$_SESSION['auto_check_events'] = $time + $time_update;
$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) . "'";
$sql .= ' AND a.code <> "AC_OTH_AUTO"';
$sql .= ' AND ar.element_type = "user"';
$sql .= ' AND ar.fk_element = ' . $user->id;
$sql .= ' LIMIT 10'; // Avoid too many notification at once
$resql = $db->query($sql);
if ($resql) {
$actionmod = new ActionComm($db);
while ($obj = $db->fetch_object($resql)) {
$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;
$eventos[] = $event;
}
}
}
print json_encode($eventos);

View File

@ -14,8 +14,10 @@
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
*
* Library javascript to enable Browser notifications
*/
if (!defined('NOREQUIREUSER')) define('NOREQUIREUSER', '1');
if (!defined('NOREQUIRESOC')) define('NOREQUIRESOC', '1');
if (!defined('NOCSRFCHECK')) define('NOCSRFCHECK', 1);
@ -24,110 +26,113 @@ if (!defined('NOLOGIN')) define('NOLOGIN', 1);
if (!defined('NOREQUIREMENU')) define('NOREQUIREMENU', 1);
if (!defined('NOREQUIREHTML')) define('NOREQUIREHTML', 1);
session_cache_limiter(FALSE);
require_once '../../main.inc.php';
if(!($_SERVER['HTTP_REFERER'] === $dolibarr_main_url_root . '/' || $_SERVER['HTTP_REFERER'] === $dolibarr_main_url_root . '/index.php')){
if (!($_SERVER['HTTP_REFERER'] === $dolibarr_main_url_root . '/' || $_SERVER['HTTP_REFERER'] === $dolibarr_main_url_root . '/index.php'))
{
global $langs, $conf;
$langs->load('agenda');
// Define javascript type
header('Content-type: text/javascript; charset=UTF-8');
// 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');
// Check notification permissions API HTML5
print 'if (Notification.permission !== "granted") {
Notification.requestPermission()
}' . PHP_EOL;
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'] . ';' . PHP_EOL;
print 'var now = ' . $_SESSION['auto_check_events'] . ';' . PHP_EOL;
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'] . ';' . PHP_EOL;
print 'var now = ' . time() . ';' . PHP_EOL;
print 'var time_session = ' . $_SESSION['auto_check_events'] . ';' . "\n";
print 'var now = ' . time() . ';' . "\n";
}
//TODO provisionally set to be checked every 60 seconds, the 1000 is because it needs to be in milliseconds
print 'var time_auto_update = 60;' . "\n";
print 'var time_auto_update = '.(empty($conf->global->MAIN_BROWSER_NOTIFICATION_FREQUENCY)?'3':(int) $conf->global->MAIN_BROWSER_NOTIFICATION_FREQUENCY).';' . "\n";
?>
/* 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;
var time_first_execution = (time_auto_update - (now - time_session)) * 1000; //need milliseconds
setTimeout(first_execution, time_first_execution); //firts run auto check
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
}
function check_events() {
$.ajax("<?php print dol_buildpath('/core/ajax/check_events.php', 1); ?>", {
type: "post", // Usually post o get
async: true,
data: {time: time_session},
success: function (result) {
var arr = JSON.parse(result);
if (arr.length > 0) {
if (Notification.permission === "granted") {
if (Notification.permission === "granted")
{
console.log("Call check_events");
$.ajax("<?php print dol_buildpath('/core/ajax/check_notifications.php', 1); ?>", {
type: "post", // Usually post o get
async: true,
data: {time: time_session},
success: function (result) {
var arr = JSON.parse(result);
if (arr.length > 0) {
<?php
if($conf->global->AGENDA_NOTIFICATION_SOUND){
print 'var audio = new Audio(\''.dol_buildpath('/comm/action/sound/notification.mp3', 1).'\');';
if (! empty($conf->global->AGENDA_NOTIFICATION_SOUND)) {
print 'var audio = new Audio(\''.DOL_URL_ROOT.'/theme/common/sound/notification_agenda.wav'.'\');';
}
?>
$.each(arr, function (index, value) {
var url="notdefined";
var title="Not defined";
var body = value['tipo'] + ': ' + value['titulo'];
if (value['location'] != null) {
if (value['type'] == 'agenda' && value['location'] != null && value['location'] != '') {
body += '\n <?php print $langs->transnoentities('Location')?>: ' + value['location'];
}
var title = "<?php print $langs->trans('Agenda') ?>";
if (value['type'] == 'agenda')
{
url = '<?php echo DOL_URL_ROOT.'/comm/action/card.php?id='; ?>' + value['id'];
title = '<?php print $langs->trans('Agenda') ?>';
}
var extra = {
icon: "<?php print dol_buildpath('/theme/common/bell.png', 1); ?>",
icon: '<?php print DOL_URL_ROOT.'/theme/common/bell.png'; ?>',
body: body,
tag: value['id']
};
// We release the notify
var noti = new Notification(title, extra);
<?php
if($conf->global->AGENDA_NOTIFICATION_SOUND){
print 'if(index==0)audio.play();'."\n";
if (index==0 && audio)
{
audio.play();
}
?>
noti.onclick = function (event) {
console.log("An event to notify on browser was received");
event.preventDefault(); // prevent the browser from focusing the Notification's tab
window.focus();
window.open("<?php print dol_buildpath('/comm/action/card.php?id=', 1); ?>" + value['id'], '_blank');
window.open(url, '_blank');
noti.close();
};
});
}
}
}
});
});
}
else
{
console.log("Cancel check_events. Useless because Notification.permission is "+Notification.permission);
}
time_session += time_auto_update;
}
<?php }
<?php
}

View File

@ -75,7 +75,7 @@ class modAgenda extends DolibarrModules
$this->langfiles = array("companies");
// Module parts
$this->module_parts = array('js' => array('/core/js/agenda_notification.js.php'));
$this->module_parts = array();
// Constants
//-----------

View File

@ -1475,8 +1475,8 @@ AGENDA_USE_EVENT_TYPE_DEFAULT=Set automatically this default value for type of e
AGENDA_DEFAULT_FILTER_TYPE=Set automatically this type of event into search filter of agenda view
AGENDA_DEFAULT_FILTER_STATUS=Set automatically this status for events into search filter of agenda view
AGENDA_DEFAULT_VIEW=Which tab do you want to open by default when selecting menu Agenda
AGENDA_NOTIFICATION=Enable events notification
AGENDA_NOTIFICATION_SOUND=Enable sound on notifications
AGENDA_NOTIFICATION=Enable event notification on user browsers when event date is reached (each user is able to refuse this from the browser confirmation question)
AGENDA_NOTIFICATION_SOUND=Enable sound notification
##### ClickToDial #####
ClickToDialDesc=This module allows to make phone numbers clickable. A click on this icon will call make your phone to call the phone number. This can be used to call a call center system from Dolibarr that can call the phone number on a SIP system for example.
ClickToDialUseTelLink=Use just a link "tel:" on phone numbers

View File

@ -1303,7 +1303,16 @@ function top_htmlhead($head, $title='', $disablejs=0, $disablehead=0, $arrayofjs
print '</script>'."\n";
print '<script type="text/javascript" src="'.$pathckeditor.$jsckeditor.($ext?'?'.$ext:'').'"></script>'."\n";
}
// Browser notifications
$enablebrowsernotif=false;
if (! empty($conf->agenda->enabled) && ! empty($conf->global->AGENDA_NOTIFICATION) && ! empty($conf->global->AGENDA_NOTIFICATION_SOUND)) $enablebrowsernotif=true;
if ($enablebrowsernotif)
{
print '<!-- Includes JS of Dolibarr -->'."\n";
print '<script type="text/javascript" src="'.DOL_URL_ROOT.'/core/js/lib_notification.js.php?version='.urlencode(DOL_VERSION).($ext?'&amp;'.$ext:'').'"></script>'."\n";
}
// Global js function
print '<!-- Includes JS of Dolibarr -->'."\n";
print '<script type="text/javascript" src="'.DOL_URL_ROOT.'/core/js/lib_head.js.php?version='.urlencode(DOL_VERSION).($ext?'&amp;'.$ext:'').'"></script>'."\n";

Binary file not shown.