Clean code for browser notifications

This commit is contained in:
Laurent Destailleur
2021-12-01 12:00:19 +01:00
parent 8b15741840
commit 4575bbae82
3 changed files with 29 additions and 43 deletions

View File

@@ -62,13 +62,11 @@ class Notify
*/ */
public $fk_project; public $fk_project;
// Les codes actions sont definis dans la table llx_notify_def // This codes actions are defined into table llx_notify_def
static public $arrayofnotifsupported = array(
// codes actions supported are
// @todo defined also into interface_50_modNotification_Notification.class.php
public $arrayofnotifsupported = array(
'BILL_VALIDATE', 'BILL_VALIDATE',
'BILL_PAYED', 'BILL_PAYED',
'ORDER_CREATE',
'ORDER_VALIDATE', 'ORDER_VALIDATE',
'PROPAL_VALIDATE', 'PROPAL_VALIDATE',
'PROPAL_CLOSE_SIGNED', 'PROPAL_CLOSE_SIGNED',

View File

@@ -68,6 +68,8 @@ print ' var time_auto_update = '.$conf->global->MAIN_BROWSER_NOTIFICATION_FREQUE
print ' var time_js_next_test;'."\n"; print ' var time_js_next_test;'."\n";
?> ?>
/* Check if Notification is supported */
if ("Notification" in window) {
/* Check if permission ok */ /* Check if permission ok */
if (Notification.permission !== "granted") { if (Notification.permission !== "granted") {
console.log("Ask Notification.permission"); console.log("Ask Notification.permission");
@@ -83,6 +85,9 @@ var time_first_execution = <?php echo max(3, empty($conf->global->MAIN_BROWSER_N
setTimeout(first_execution, time_first_execution * 1000); setTimeout(first_execution, time_first_execution * 1000);
time_js_next_test = nowtime + time_first_execution; time_js_next_test = nowtime + time_first_execution;
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+" time_js_next_check = "+time_js_next_test); 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+" time_js_next_check = "+time_js_next_test);
} else {
console.log("This browser in this context does not support Notification.");
}
function first_execution() { function first_execution() {

View File

@@ -23,6 +23,7 @@
* \brief File of class of triggers for notification module * \brief File of class of triggers for notification module
*/ */
require_once DOL_DOCUMENT_ROOT.'/core/triggers/dolibarrtriggers.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/triggers/dolibarrtriggers.class.php';
include_once DOL_DOCUMENT_ROOT.'/core/class/notify.class.php';
/** /**
@@ -30,26 +31,7 @@ require_once DOL_DOCUMENT_ROOT.'/core/triggers/dolibarrtriggers.class.php';
*/ */
class InterfaceNotification extends DolibarrTriggers class InterfaceNotification extends DolibarrTriggers
{ {
// @todo Defined also into notify.class.php public $listofmanagedevents = array();
public $listofmanagedevents = array(
'BILL_VALIDATE',
'BILL_PAYED',
'ORDER_CREATE',
'ORDER_VALIDATE',
'PROPAL_VALIDATE',
'PROPAL_CLOSE_SIGNED',
'FICHINTER_VALIDATE',
'FICHINTER_ADD_CONTACT',
'ORDER_SUPPLIER_VALIDATE',
'ORDER_SUPPLIER_APPROVE',
'ORDER_SUPPLIER_REFUSE',
'SHIPPING_VALIDATE',
'EXPENSE_REPORT_VALIDATE',
'EXPENSE_REPORT_APPROVE',
'HOLIDAY_VALIDATE',
'HOLIDAY_APPROVE',
'ACTION_CREATE'
);
/** /**
* Constructor * Constructor
@@ -66,6 +48,8 @@ class InterfaceNotification extends DolibarrTriggers
// 'development', 'experimental', 'dolibarr' or version // 'development', 'experimental', 'dolibarr' or version
$this->version = self::VERSION_DOLIBARR; $this->version = self::VERSION_DOLIBARR;
$this->picto = 'email'; $this->picto = 'email';
$this->listofmanagedevents = Notify::$arrayofnotifsupported;
} }
/** /**
@@ -85,15 +69,13 @@ class InterfaceNotification extends DolibarrTriggers
return 0; // Module not active, we do nothing return 0; // Module not active, we do nothing
} }
require_once DOL_DOCUMENT_ROOT.'/core/class/notify.class.php'; if (!in_array($action, $this->listofmanagedevents)) {
$notify = new Notify($this->db);
if (!in_array($action, $notify->arrayofnotifsupported)) {
return 0; return 0;
} }
dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id); dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
$notify = new Notify($this->db);
$notify->send($action, $object); $notify->send($action, $object);
return 1; return 1;
@@ -114,6 +96,7 @@ class InterfaceNotification extends DolibarrTriggers
$sql = "SELECT rowid, code, label, description, elementtype"; $sql = "SELECT rowid, code, label, description, elementtype";
$sql .= " FROM ".MAIN_DB_PREFIX."c_action_trigger"; $sql .= " FROM ".MAIN_DB_PREFIX."c_action_trigger";
$sql .= $this->db->order("rang, elementtype, code"); $sql .= $this->db->order("rang, elementtype, code");
dol_syslog("getListOfManagedEvents Get list of notifications", LOG_DEBUG); dol_syslog("getListOfManagedEvents Get list of notifications", LOG_DEBUG);
$resql = $this->db->query($sql); $resql = $this->db->query($sql);
if ($resql) { if ($resql) {