2
0
forked from Wavyzz/dolibarr

Work on browser notification

This commit is contained in:
Laurent Destailleur
2020-09-26 20:29:01 +02:00
parent 7865381434
commit 3a6b8ae202
2 changed files with 37 additions and 27 deletions

View File

@@ -46,8 +46,8 @@ if (!defined('NOREQUIRETRAN')) define('NOREQUIRETRAN', '1');
require '../../main.inc.php';
$time = (int) GETPOST('time', 'int'); // Use the time parameter that is always increased by time_update, even if call is late
//$time=dol_now();
//$time = (int) GETPOST('time', 'int'); // Use the time parameter that is always increased by time_update, even if call is late
$time=dol_now();
$action = GETPOST('action', 'aZ09');
$listofreminderids = GETPOST('listofreminderids', 'aZ09');
@@ -58,18 +58,20 @@ $listofreminderids = GETPOST('listofreminderids', 'aZ09');
if ($action == 'stopreminder') {
dol_syslog("Clear notification for listofreminderids=".$listofreminderids);
$listofreminderidsarray = explode('-', GETPOST('listofreminderids', 'aZ09'));
$listofreminderid = GETPOST('listofreminderids', 'intcomma');
// Set the reminder as done
foreach ($listofreminderidsarray as $listofreminderid) {
if (empty($listofreminderid)) continue;
//$sql = 'DELETE FROM '.MAIN_DB_PREFIX.'action_reminder WHERE rowid = '.$listofreminderid.' AND fk_user = '.$user->id;
$sql = 'UPDATE '.MAIN_DB_PREFIX.'actioncomm_reminder SET status = 1';
$sql .= ' WHERE status = 0 AND rowid = '.$listofreminderid.' AND fk_user = '.$user->id.' AND entity = '.$conf->entity;
$resql = $db->query($sql);
$sql .= ' WHERE status = 0 AND rowid IN ('.$db->sanitize($db->escape($listofreminderid)).')';
$sql .= ' AND fk_user = '.$user->id.' AND entity = '.$conf->entity;
/* $resql = $db->query($sql);
if (!$resql) {
dol_print_error($db);
}
*/
}
include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
@@ -90,7 +92,7 @@ if ($action == 'stopreminder') {
* View
*/
top_httphead('text/html'); // TODO Use a json mime type
top_httphead('application/json');
global $user, $db, $langs, $conf;
@@ -102,9 +104,9 @@ $eventfound = array();
// 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.
if ($time >= $_SESSION['auto_check_events_not_before'] || GETPOST('forcechecknow', 'int'))
if (empty($_SESSION['auto_check_events_not_before']) || $time >= $_SESSION['auto_check_events_not_before'] || GETPOST('forcechecknow', 'int'))
{
$time_update = (int) $conf->global->MAIN_BROWSER_NOTIFICATION_FREQUENCY; // Always defined
/*$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),
@@ -121,6 +123,7 @@ if ($time >= $_SESSION['auto_check_events_not_before'] || GETPOST('forcechecknow
}
$_SESSION['auto_check_events_not_before'] = $time + $time_update;
*/
// Force save of the session change we did.
// WARNING: Any change in sessions after that will not be saved !
@@ -131,19 +134,20 @@ if ($time >= $_SESSION['auto_check_events_not_before'] || GETPOST('forcechecknow
dol_syslog('NEW $_SESSION[auto_check_events_not_before]='.$_SESSION['auto_check_events_not_before']);
$sql = 'SELECT a.id, a.code, a.datep, a.label, a.location, ar.rowid as id_reminder, ar.dateremind, ar.fk_user as id_user_reminder';
$sql = 'SELECT a.id as id_agenda, a.code, a.datep, a.label, a.location, ar.rowid as id_reminder, ar.dateremind, ar.fk_user as id_user_reminder';
$sql .= ' FROM '.MAIN_DB_PREFIX.'actioncomm as a';
if (!empty($user->conf->MAIN_USER_WANT_ALL_EVENTS_NOTIFICATIONS)) {
$sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'actioncomm_reminder as ar ON a.id = ar.fk_actioncomm AND ar.fk_user = '.$user->id;
$sql .= ' WHERE a.code <> "AC_OTH_AUTO"';
$sql .= ' AND (';
$sql .= " (ar.typeremind = 'browser' AND ar.dateremind < '".$db->idate(dol_now())."' AND ar.status = 0 AND ar.entity = ".$conf->entity;
$sql .= " OR (a.datep BETWEEN '".$db->idate($starttime)."' AND '".$db->idate($time + $time_update - 1)."')";
// $sql .= " OR (a.datep BETWEEN '".$db->idate($starttime)."' AND '".$db->idate($time + $time_update - 1)."')";
$sql .= ' )';
} else {
$sql .= ' JOIN '.MAIN_DB_PREFIX.'actioncomm_reminder as ar ON a.id = ar.fk_actioncomm AND ar.fk_user = '.$user->id;
$sql .= " AND ar.typeremind = 'browser' AND ar.dateremind < '".$db->idate(dol_now())."' AND ar.status = 0 AND ar.entity = ".$conf->entity;
}
$sql .= $db->order('datep', 'ASC');
$sql .= ' LIMIT 10'; // Avoid too many notification at once
$resql = $db->query($sql);
@@ -162,11 +166,11 @@ if ($time >= $_SESSION['auto_check_events_not_before'] || GETPOST('forcechecknow
$event['reminder_date_formated'] = dol_print_date($db->jdate($obj->dateremind), 'standard');
$event['event_date_start_formated'] = dol_print_date($db->jdate($obj->datep), 'standard');
$eventfound[] = $event;
$eventfound[$obj->id] = $event;
}
} else {
dol_syslog("Error sql = ".$db->lasterror(), LOG_ERR);
}
}
print json_encode($eventfound);
print json_encode(array('pastreminders'=>$eventfound, 'nextreminder'=>''));

View File

@@ -74,10 +74,12 @@ if (!($_SERVER['HTTP_REFERER'] === $dolibarr_main_url_root.'/' || $_SERVER['HTTP
$.ajax("<?php print DOL_URL_ROOT.'/core/ajax/check_notifications.php'; ?>", {
type: "post", // Usually post or get
async: true,
data: { time_js_next_test: time_js_next_test },
data: { time_js_next_test: time_js_next_test, forcechecknow: 1, token: 'notrequired' },
dataType: "json",
success: function (result) {
var arr = JSON.parse(result);
if (arr.length > 0) {
console.log(result);
var arrayofpastreminders = Object.values(result.pastreminders);
if (arrayofpastreminders && arrayofpastreminders.length > 0) {
var audio = null;
<?php
if (!empty($conf->global->AGENDA_REMINDER_BROWSER_SOUND)) {
@@ -86,27 +88,29 @@ if (!($_SERVER['HTTP_REFERER'] === $dolibarr_main_url_root.'/' || $_SERVER['HTTP
?>
var listofreminderids = '';
$.each(arr, function (index, value) {
$.each(arrayofpastreminders, function (index, value) {
console.log(index);
console.log(value);
var url = "notdefined";
var title = "Not defined";
var body = value['type'] + ': ' + value['label'];
if (value['type'] == 'agenda' && value['location'] != null && value['location'] != '') {
body += '\n' + value['location'];
var body = value.label;
if (value.type == 'agenda' && value.location != null && value.location != '') {
body += '\n' + value.location;
}
if(value['type'] == 'agenda' && (value['event_date_start_formated'] != null || event_date_start_formated['event_date_start'] != '')) {
body += '\n' + value['event_date_start_formated'];
if (value.type == 'agenda' && (value.event_date_start_formated != null || value.event_date_start_formated['event_date_start'] != '')) {
body += '\n' + value.event_date_start_formated;
}
if (value['type'] == 'agenda')
if (value.type == 'agenda')
{
url = '<?php echo DOL_URL_ROOT.'/comm/action/card.php?id='; ?>' + value['id'];
title = '<?php print $langs->trans('Agenda') ?>';
url = '<?php echo DOL_URL_ROOT.'/comm/action/card.php?id='; ?>' + value.id;
title = '<?php print $langs->trans('AgendaReminder') ?>';
}
var extra = {
icon: '<?php print DOL_URL_ROOT.'/theme/common/bell.png'; ?>',
body: body,
tag: value['id']
tag: value.id
};
// We release the notify
@@ -126,7 +130,7 @@ if (!($_SERVER['HTTP_REFERER'] === $dolibarr_main_url_root.'/' || $_SERVER['HTTP
noti.close();
};
listofreminderids = listofreminderids + '-' + value['id_reminder']
listofreminderids = (listofreminderids == '' ? '' : listofreminderids + ',') + value.id_reminder
}
});
@@ -135,8 +139,10 @@ if (!($_SERVER['HTTP_REFERER'] === $dolibarr_main_url_root.'/' || $_SERVER['HTTP
$.ajax("<?php print DOL_URL_ROOT.'/core/ajax/check_notifications.php?action=stopreminder&listofreminderids='; ?>"+listofreminderids, {
type: "post", // Usually post or get
async: true,
data: { time_js_next_test: time_js_next_test }
data: { time_js_next_test: time_js_next_test, token: 'notrequired' }
});
} else {
console.log("No past reminder found, next try at "+time_js_next_test);
}
}
});