Browse Source

Make notification by browser popup working.

Laurent Destailleur 4 years ago
parent
commit
da4362c492
2 changed files with 18 additions and 17 deletions
  1. 6 8
      htdocs/core/ajax/check_notifications.php
  2. 12 9
      htdocs/core/js/lib_notification.js.php

+ 6 - 8
htdocs/core/ajax/check_notifications.php

@@ -61,18 +61,17 @@ if ($action == 'stopreminder') {
 	$listofreminderid = GETPOST('listofreminderids', 'intcomma');
 
 	// Set the reminder as done
-	foreach ($listofreminderidsarray as $listofreminderid) {
-		if (empty($listofreminderid)) continue;
+	//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 IN ('.$db->sanitize($db->escape($listofreminderid)).')';
 		$sql .= ' AND fk_user = '.$user->id.' AND entity = '.$conf->entity;
-/*		$resql = $db->query($sql);
+		$resql = $db->query($sql);
 		if (!$resql) {
 			dol_print_error($db);
 		}
-*/
-	}
+	//}
 
 	include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
 
@@ -141,7 +140,6 @@ if (empty($_SESSION['auto_check_events_not_before']) || $time >= $_SESSION['auto
     	$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 .= ' )';
     } else {
     	$sql .= ' JOIN '.MAIN_DB_PREFIX.'actioncomm_reminder as ar ON a.id = ar.fk_actioncomm AND ar.fk_user = '.$user->id;
@@ -157,8 +155,8 @@ if (empty($_SESSION['auto_check_events_not_before']) || $time >= $_SESSION['auto
             // Message must be formated and translated to be used with javascript directly
             $event = array();
             $event['type'] = 'agenda';
-            $event['id'] = $obj->id;
             $event['id_reminder'] = $obj->id_reminder;
+            $event['id_agenda'] = $obj->id_agenda;
             $event['id_user'] = $obj->id_user_reminder;
             $event['code'] = $obj->code;
             $event['label'] = $obj->label;
@@ -166,7 +164,7 @@ if (empty($_SESSION['auto_check_events_not_before']) || $time >= $_SESSION['auto
             $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[$obj->id] = $event;
+            $eventfound[$obj->id_agenda] = $event;
         }
     } else {
         dol_syslog("Error sql = ".$db->lasterror(), LOG_ERR);

+ 12 - 9
htdocs/core/js/lib_notification.js.php

@@ -79,6 +79,7 @@ if (!($_SERVER['HTTP_REFERER'] === $dolibarr_main_url_root.'/' || $_SERVER['HTTP
                 success: function (result) {
                 	console.log(result);
                     var arrayofpastreminders = Object.values(result.pastreminders);
+                    console.log("arrayofpastreminders.length"+arrayofpastreminders.length);
                     if (arrayofpastreminders && arrayofpastreminders.length > 0) {
                     	var audio = null;
                         <?php
@@ -87,9 +88,9 @@ if (!($_SERVER['HTTP_REFERER'] === $dolibarr_main_url_root.'/' || $_SERVER['HTTP
 						}
 						?>
 						var listofreminderids = '';
+						var noti = []
 
                         $.each(arrayofpastreminders, function (index, value) {
-							console.log(index);
                         	console.log(value);
                             var url = "notdefined";
                             var title = "Not defined";
@@ -104,30 +105,32 @@ if (!($_SERVER['HTTP_REFERER'] === $dolibarr_main_url_root.'/' || $_SERVER['HTTP
 
                             if (value.type == 'agenda')
                             {
-                             	url = '<?php echo DOL_URL_ROOT.'/comm/action/card.php?id='; ?>' + value.id;
-                                title = '<?php print $langs->trans('AgendaReminder') ?>';
+                             	url = '<?php echo DOL_URL_ROOT.'/comm/action/card.php?id='; ?>' + value.id_agenda;
+                                title = '<?php print $langs->trans('EventReminder') ?>';
                             }
                             var extra = {
                                 icon: '<?php print DOL_URL_ROOT.'/theme/common/bell.png'; ?>',
+                                //image: '<?php print DOL_URL_ROOT.'/theme/common/bell.png'; ?>',
                                 body: body,
-                                tag: value.id
+                                tag: value.id_agenda,
+                                requireInteraction: true
                             };
 
                             // We release the notify
                             console.log("Send notification on browser");
-                            var noti = new Notification(title, extra);
+                            noti[index] = new Notification(title, extra);
                             if (index==0 && audio)
                             {
                             	audio.play();
                             }
 
-                            if (noti) {
-	                            noti.onclick = function (event) {
+                            if (noti[index]) {
+	                            noti[index].onclick = function (event) {
 	                                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();
+	                                noti[index].close();
 	                            };
 
 	                            listofreminderids = (listofreminderids == '' ? '' : listofreminderids + ',') + value.id_reminder
@@ -140,7 +143,7 @@ if (!($_SERVER['HTTP_REFERER'] === $dolibarr_main_url_root.'/' || $_SERVER['HTTP
 			                type: "post",   // Usually post or get
 			                async: true,
 			                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);
                     }