diff --git a/htdocs/core/ajax/check_notifications.php b/htdocs/core/ajax/check_notifications.php index 10172628805..b70afe936f7 100644 --- a/htdocs/core/ajax/check_notifications.php +++ b/htdocs/core/ajax/check_notifications.php @@ -46,6 +46,45 @@ 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(); +$action = GETPOST('action', 'aZ09'); +$listofreminderids = GETPOST('listofreminderids', 'aZ09'); + + +/* + * Actions + */ + +if ($action == 'stopreminder') { + dol_syslog("Clear notification for listofreminderids=".$listofreminderids); + $listofreminderidsarray = explode('-', GETPOST('listofreminderids', 'aZ09')); + + // 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); + if (!$resql) { + dol_print_error($db); + } + } + + include_once(DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php'); + + // Clean database + $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'actioncomm_reminder'; + $sql .= " WHERE dateremind < '".$db->idate(dol_time_plus_duree(dol_now(), -1, 'm'))."'"; + $resql = $db->query($sql); + if (!$resql) { + dol_print_error($db); + } + + exit; +} + /* * View @@ -55,10 +94,6 @@ top_httphead('text/html'); // TODO Use a json mime type global $user, $db, $langs, $conf; -$time = (int) GETPOST('time', 'int'); // Use the time parameter that is always increased by time_update, even if call is late -//$time=dol_now(); - - $eventfound = array(); //Uncomment this to force a test //$eventfound[]=array('type'=>'agenda', 'id'=>1, 'tipo'=>'eee', 'location'=>'aaa'); @@ -96,7 +131,7 @@ 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.label, a.location, ar.rowid as id_reminder, ar.dateremind, ar.fk_user'; + $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 .= ' 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; @@ -120,10 +155,12 @@ if ($time >= $_SESSION['auto_check_events_not_before'] || GETPOST('forcechecknow $event['type'] = 'agenda'; $event['id'] = $obj->id; $event['id_reminder'] = $obj->id_reminder; + $event['id_user'] = $obj->id_user_reminder; $event['code'] = $obj->code; $event['label'] = $obj->label; $event['location'] = $obj->location; - $event['date'] = $db->jdate($obj->dateremind); + $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; } diff --git a/htdocs/core/js/lib_notification.js.php b/htdocs/core/js/lib_notification.js.php index fc3fd3512ad..50d3f7f3268 100644 --- a/htdocs/core/js/lib_notification.js.php +++ b/htdocs/core/js/lib_notification.js.php @@ -44,6 +44,7 @@ if (!($_SERVER['HTTP_REFERER'] === $dolibarr_main_url_root.'/' || $_SERVER['HTTP /* Check if permission ok */ if (Notification.permission !== "granted") { + console.log("Ask Notification.permission"); Notification.requestPermission() } @@ -65,7 +66,7 @@ if (!($_SERVER['HTTP_REFERER'] === $dolibarr_main_url_root.'/' || $_SERVER['HTTP function check_events() { if (Notification.permission === "granted") { - console.log("Call check_events time_js_next_test = date we are looking for event after ="+time_js_next_test); + console.log("Call check_events time_js_next_test = date we are looking for event after this date = "+time_js_next_test); $.ajax("", { type: "post", // Usually post or get async: true, @@ -82,15 +83,15 @@ if (!($_SERVER['HTTP_REFERER'] === $dolibarr_main_url_root.'/' || $_SERVER['HTTP var listofreminderids = ''; $.each(arr, function (index, value) { - var url="notdefined"; - var title="Not defined"; - var body = value['tipo'] + ': ' + value['titulo']; + 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']; } - if(value['type'] == 'agenda' && (value['date'] != null || value['date'] != '')) { - body += '\n' + value['date']; + 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') @@ -105,6 +106,7 @@ if (!($_SERVER['HTTP_REFERER'] === $dolibarr_main_url_root.'/' || $_SERVER['HTTP }; // We release the notify + console.log("Send notification on browser"); var noti = new Notification(title, extra); if (index==0 && audio) { @@ -113,19 +115,20 @@ if (!($_SERVER['HTTP_REFERER'] === $dolibarr_main_url_root.'/' || $_SERVER['HTTP if (noti) { noti.onclick = function (event) { - console.log("An event to notify on browser was received"); + console.log("A click on notification on browser has been done"); event.preventDefault(); // prevent the browser from focusing the Notification's tab window.focus(); window.open(url, '_blank'); noti.close(); }; - listofreminderids = listofreminderids + ',' + value['id'] + listofreminderids = listofreminderids + '-' + value['id_reminder'] } }); // Update status of all notifications we sent on browser (listofreminderids) - $.ajax(""+listofreminderids, { + console.log("Flag notification as done for listofreminderids="+listofreminderids); + $.ajax(""+listofreminderids, { type: "get", // Usually post or get async: true, data: {time: time_js_next_test} @@ -136,7 +139,7 @@ if (!($_SERVER['HTTP_REFERER'] === $dolibarr_main_url_root.'/' || $_SERVER['HTTP } else { - console.log("Cancel check_events. Useless because Notification.permission is "+Notification.permission); + console.log("Cancel check_events. Useless because javascript Notification.permission is "+Notification.permission+"."); } time_js_next_test += time_auto_update;