diff --git a/htdocs/core/class/notify.class.php b/htdocs/core/class/notify.class.php index 12628d059b661..de5319a97734e 100644 --- a/htdocs/core/class/notify.class.php +++ b/htdocs/core/class/notify.class.php @@ -170,6 +170,7 @@ class Notify 'CONTRACT_MODIFY', 'STOCKTRANSFER_CREATE', 'STOCKTRANSFER_MODIFY', + 'STOCKTRANSFER_DELETE', 'STOCKTRANSFER_VALIDATE', 'STOCKTRANSFER_UNVALIDATE', 'STOCKTRANSFER_CLOSE', @@ -179,6 +180,51 @@ class Notify 'STOCKTRANSFER_ADDSTOCK_CANCEL' ); + /** + * Return list of notification codes to search. + * This allows backward compatibility of notification subscriptions while keeping CRUD trigger events. + * + * @param string $notifcode Code of action in llx_c_action_trigger + * @param CommonObject $object Object the notification is about + * @return array + */ + private function getNotificationCodesToSearch($notifcode, $object = null) + { + $notifcodes = array($notifcode); + + if ($notifcode !== 'STOCKTRANSFER_MODIFY' || !is_object($object)) { + return $notifcodes; + } + + $operation = empty($object->context['stocktransfer_operation']) ? '' : $object->context['stocktransfer_operation']; + switch ($operation) { + case 'validate': + $notifcodes[] = 'STOCKTRANSFER_VALIDATE'; + break; + case 'setdraft': + $notifcodes[] = 'STOCKTRANSFER_UNVALIDATE'; + break; + case 'cancel': + $notifcodes[] = 'STOCKTRANSFER_CLOSE'; + break; + case 'destock': + $notifcodes[] = 'STOCKTRANSFER_DESTOCK'; + break; + case 'destock_cancel': + $notifcodes[] = 'STOCKTRANSFER_DESTOCK_CANCEL'; + break; + case 'addstock': + $notifcodes[] = 'STOCKTRANSFER_CLOSE'; + $notifcodes[] = 'STOCKTRANSFER_ADDSTOCK'; + break; + case 'addstock_cancel': + $notifcodes[] = 'STOCKTRANSFER_ADDSTOCK_CANCEL'; + break; + } + + return array_unique($notifcodes); + } + /** * Constructor * @@ -482,7 +528,8 @@ public function getNotificationsArray($notifcode, $socid = 0, $object = null, $u if (is_numeric($notifcode)) { $sqlnotifcode = " AND n.fk_action = ".((int) $notifcode); // Old usage } else { - $sqlnotifcode = " AND a.code = '".$this->db->escape($notifcode)."'"; // New usage + $notifcodestosearch = $this->getNotificationCodesToSearch($notifcode, $object); + $sqlnotifcode = " AND a.code IN ('".implode("','", array_map(array($this->db, 'escape'), $notifcodestosearch))."')"; // New usage } } @@ -574,7 +621,15 @@ public function getNotificationsArray($notifcode, $socid = 0, $object = null, $u // List of notifications enabled for fixed email foreach ($conf->global as $key => $val) { if ($notifcode) { - if ($val == '' || !preg_match('/^NOTIFICATION_FIXEDEMAIL_'.$notifcode.'_THRESHOLD_HIGHER_(.*)$/', $key, $reg)) { + $notifcodestosearch = is_numeric($notifcode) ? array($notifcode) : $this->getNotificationCodesToSearch($notifcode, $object); + $foundmatch = false; + foreach ($notifcodestosearch as $tmpnotifcode) { + if (preg_match('/^NOTIFICATION_FIXEDEMAIL_'.$tmpnotifcode.'_THRESHOLD_HIGHER_(.*)$/', $key, $reg)) { + $foundmatch = true; + break; + } + } + if ($val == '' || !$foundmatch) { continue; } } else { @@ -652,6 +707,7 @@ public function send($notifcode, $object, $filename_list = array(), $mimetype_li if (!in_array($notifcode, Notify::$arrayofnotifsupported)) { return 0; } + $notifcodestosearch = is_numeric($notifcode) ? array($notifcode) : $this->getNotificationCodesToSearch($notifcode, $object); include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php'; @@ -700,7 +756,7 @@ public function send($notifcode, $object, $filename_list = array(), $mimetype_li if (is_numeric($notifcode)) { $sql .= " AND n.fk_action = ".((int) $notifcode); // Old usage } else { - $sql .= " AND a.code = '".$this->db->escape($notifcode)."'"; // New usage + $sql .= " AND a.code IN ('".implode("','", array_map(array($this->db, 'escape'), $notifcodestosearch))."')"; // New usage } $sql .= " AND s.rowid = ".((int) $object->socid); @@ -718,7 +774,7 @@ public function send($notifcode, $object, $filename_list = array(), $mimetype_li if (is_numeric($notifcode)) { $sql .= " AND n.fk_action = ".((int) $notifcode); // Old usage } else { - $sql .= " AND a.code = '".$this->db->escape($notifcode)."'"; // New usage + $sql .= " AND a.code IN ('".implode("','", array_map(array($this->db, 'escape'), $notifcodestosearch))."')"; // New usage } // Check notification fixed @@ -1073,7 +1129,14 @@ public function send($notifcode, $object, $filename_list = array(), $mimetype_li if (!$error) { foreach ($conf->global as $key => $val) { $reg = array(); - if ($val == '' || !preg_match('/^NOTIFICATION_FIXEDEMAIL_'.$notifcode.'_THRESHOLD_HIGHER_(.*)$/', $key, $reg)) { + $matchednotifcode = ''; + foreach ($notifcodestosearch as $tmpnotifcode) { + if (preg_match('/^NOTIFICATION_FIXEDEMAIL_'.$tmpnotifcode.'_THRESHOLD_HIGHER_(.*)$/', $key, $reg)) { + $matchednotifcode = $tmpnotifcode; + break; + } + } + if ($val == '' || empty($matchednotifcode)) { continue; } @@ -1081,11 +1144,11 @@ public function send($notifcode, $object, $filename_list = array(), $mimetype_li $threshold = (float) $reg[1]; if (!empty($object->total_ht) && $object->total_ht <= $threshold) { - dol_syslog("A notification is requested for notifcode = ".$notifcode." but amount = ".$object->total_ht." so lower than threshold = ".$threshold.". We discard this notification"); + dol_syslog("A notification is requested for notifcode = ".$matchednotifcode." but amount = ".$object->total_ht." so lower than threshold = ".$threshold.". We discard this notification"); continue; } - $notifcodedefid = dol_getIdFromCode($this->db, $notifcode, 'c_action_trigger', 'code', 'rowid'); + $notifcodedefid = dol_getIdFromCode($this->db, $matchednotifcode, 'c_action_trigger', 'code', 'rowid'); if ($notifcodedefid <= 0) { dol_print_error($this->db, 'Failed to get id from code'); } @@ -1099,7 +1162,7 @@ public function send($notifcode, $object, $filename_list = array(), $mimetype_li $subject = '['.$appli.'] '.$langs->transnoentitiesnoconv("DolibarrNotification").($projtitle ? ' '.$projtitle : ''); - switch ($notifcode) { + switch ($matchednotifcode) { case 'BILL_VALIDATE': $link = ''.$newref.''; $dir_output = $conf->facture->dir_output."/".get_exdir(0, 0, 0, 1, $object, 'invoice'); diff --git a/htdocs/install/mysql/data/llx_c_action_trigger.sql b/htdocs/install/mysql/data/llx_c_action_trigger.sql index aab9027f223f6..ab0e8ec49c0e8 100644 --- a/htdocs/install/mysql/data/llx_c_action_trigger.sql +++ b/htdocs/install/mysql/data/llx_c_action_trigger.sql @@ -161,6 +161,7 @@ insert into llx_c_action_trigger (code,label,description,elementtype,rang) value insert into llx_c_action_trigger (code,label,description,elementtype,rang) values ('STOCKTRANSFER_CLOSE','Stock transfer closed','Executed when a stock transfer is closed after destination stock increment','stocktransfer',676); insert into llx_c_action_trigger (code,label,description,elementtype,rang) values ('STOCKTRANSFER_ADDSTOCK','Stock transfer destination stock incremented','Executed when destination warehouses stock is incremented for a stock transfer','stocktransfer',677); insert into llx_c_action_trigger (code,label,description,elementtype,rang) values ('STOCKTRANSFER_ADDSTOCK_CANCEL','Stock transfer destination stock increment canceled','Executed when destination warehouses stock increment is canceled for a stock transfer','stocktransfer',678); +insert into llx_c_action_trigger (code,label,description,elementtype,rang) values ('STOCKTRANSFER_DELETE','Stock transfer deleted','Executed when a stock transfer is deleted','stocktransfer',679); insert into llx_c_action_trigger (code,label,description,elementtype,rang) values ('CONTACT_CREATE','Contact address created','Executed when a contact is created','contact',50); insert into llx_c_action_trigger (code,label,description,elementtype,rang) values ('CONTACT_MODIFY','Contact address update','Executed when a contact is updated','contact',51); insert into llx_c_action_trigger (code,label,description,elementtype,rang) values ('CONTACT_SENTBYMAIL','Mails sent from third party card','Executed when you send email from contact address record','contact',52); diff --git a/htdocs/install/mysql/migration/23.0.0-24.0.0.sql b/htdocs/install/mysql/migration/23.0.0-24.0.0.sql index f30a42f81e255..d9d71a1cb08ea 100644 --- a/htdocs/install/mysql/migration/23.0.0-24.0.0.sql +++ b/htdocs/install/mysql/migration/23.0.0-24.0.0.sql @@ -200,6 +200,7 @@ INSERT IGNORE INTO llx_c_action_trigger (code,label,description,elementtype,rang INSERT IGNORE INTO llx_c_action_trigger (code,label,description,elementtype,rang) VALUES ('STOCKTRANSFER_CLOSE','Stock transfer closed','Executed when a stock transfer is closed after destination stock increment','stocktransfer',676); INSERT IGNORE INTO llx_c_action_trigger (code,label,description,elementtype,rang) VALUES ('STOCKTRANSFER_ADDSTOCK','Stock transfer destination stock incremented','Executed when destination warehouses stock is incremented for a stock transfer','stocktransfer',677); INSERT IGNORE INTO llx_c_action_trigger (code,label,description,elementtype,rang) VALUES ('STOCKTRANSFER_ADDSTOCK_CANCEL','Stock transfer destination stock increment canceled','Executed when destination warehouses stock increment is canceled for a stock transfer','stocktransfer',678); +INSERT IGNORE INTO llx_c_action_trigger (code,label,description,elementtype,rang) VALUES ('STOCKTRANSFER_DELETE','Stock transfer deleted','Executed when a stock transfer is deleted','stocktransfer',679); ALTER TABLE llx_c_ticket_category ADD COLUMN fk_ticket_type integer NULL; -- end of migration diff --git a/htdocs/langs/en_US/other.lang b/htdocs/langs/en_US/other.lang index 90e8abe17e749..456e2b215fcf7 100644 --- a/htdocs/langs/en_US/other.lang +++ b/htdocs/langs/en_US/other.lang @@ -427,6 +427,8 @@ SendPrompt=Send ConfirmAiAction=Are you sure you want to %1$s using the %2$s tool? Notify_STOCKTRANSFER_CREATE=Stock transfer created +Notify_STOCKTRANSFER_MODIFY=Stock transfer modified +Notify_STOCKTRANSFER_DELETE=Stock transfer deleted Notify_STOCKTRANSFER_VALIDATE=Stock transfer validated Notify_STOCKTRANSFER_UNVALIDATE=Stock transfer set back to draft Notify_STOCKTRANSFER_DESTOCK=Source stock decreased for stock transfer diff --git a/htdocs/product/stock/stocktransfer/class/stocktransfer.class.php b/htdocs/product/stock/stocktransfer/class/stocktransfer.class.php index 1d36b0e0af6ac..c72efa18c135d 100644 --- a/htdocs/product/stock/stocktransfer/class/stocktransfer.class.php +++ b/htdocs/product/stock/stocktransfer/class/stocktransfer.class.php @@ -635,7 +635,8 @@ public function validate($user, $notrigger = 0) if (!$error && !$notrigger) { // Call trigger - $result = $this->call_trigger('STOCKTRANSFER_VALIDATE', $user); + $this->context = array_merge($this->context, array('stocktransfer_operation' => 'validate')); + $result = $this->call_trigger('STOCKTRANSFER_MODIFY', $user); if ($result < 0) { $error++; } @@ -718,7 +719,8 @@ public function setDraft($user, $notrigger = 0) return 0; } - return $this->setStatusCommon($user, self::STATUS_DRAFT, $notrigger, 'STOCKTRANSFER_UNVALIDATE'); + $this->context = array_merge($this->context, array('stocktransfer_operation' => 'setdraft')); + return $this->setStatusCommon($user, self::STATUS_DRAFT, $notrigger, 'STOCKTRANSFER_MODIFY'); } /** @@ -735,7 +737,8 @@ public function cancel($user, $notrigger = 0) return 0; } - return $this->setStatusCommon($user, self::STATUS_CLOSED, $notrigger, 'STOCKTRANSFER_CLOSE'); + $this->context = array_merge($this->context, array('stocktransfer_operation' => 'cancel')); + return $this->setStatusCommon($user, self::STATUS_CLOSED, $notrigger, 'STOCKTRANSFER_MODIFY'); } /** @@ -752,7 +755,8 @@ public function reopen($user, $notrigger = 0) return 0; } - return $this->setStatusCommon($user, self::STATUS_VALIDATED, $notrigger, 'STOCKTRANSFER_REOPEN'); + $this->context = array_merge($this->context, array('stocktransfer_operation' => 'reopen')); + return $this->setStatusCommon($user, self::STATUS_VALIDATED, $notrigger, 'STOCKTRANSFER_MODIFY'); } /** diff --git a/htdocs/product/stock/stocktransfer/stocktransfer_card.php b/htdocs/product/stock/stocktransfer/stocktransfer_card.php index 9272e68103962..cb45e996c059a 100644 --- a/htdocs/product/stock/stocktransfer/stocktransfer_card.php +++ b/htdocs/product/stock/stocktransfer/stocktransfer_card.php @@ -310,7 +310,8 @@ $object->status = $object::STATUS_TRANSFERED; $object->date_reelle_depart = dol_now(); $object->update($user); - $result = $object->call_trigger('STOCKTRANSFER_DESTOCK', $user); + $object->context = array_merge($object->context, array('stocktransfer_operation' => 'destock')); + $result = $object->call_trigger('STOCKTRANSFER_MODIFY', $user); if ($result < 0) { $error++; setEventMessages($object->error, $object->errors, 'errors'); @@ -342,7 +343,8 @@ $object->status = $object::STATUS_VALIDATED; $object->date_reelle_depart = null; $object->update($user); - $result = $object->call_trigger('STOCKTRANSFER_DESTOCK_CANCEL', $user); + $object->context = array_merge($object->context, array('stocktransfer_operation' => 'destock_cancel')); + $result = $object->call_trigger('STOCKTRANSFER_MODIFY', $user); if ($result < 0) { $error++; setEventMessages($object->error, $object->errors, 'errors'); @@ -374,12 +376,8 @@ $object->status = $object::STATUS_CLOSED; $object->date_reelle_arrivee = dol_now(); $object->update($user); - $result = $object->call_trigger('STOCKTRANSFER_CLOSE', $user); - if ($result < 0) { - $error++; - setEventMessages($object->error, $object->errors, 'errors'); - } - $result = $object->call_trigger('STOCKTRANSFER_ADDSTOCK', $user); + $object->context = array_merge($object->context, array('stocktransfer_operation' => 'addstock')); + $result = $object->call_trigger('STOCKTRANSFER_MODIFY', $user); if ($result < 0) { $error++; setEventMessages($object->error, $object->errors, 'errors'); @@ -411,7 +409,8 @@ $object->status = $object::STATUS_TRANSFERED; $object->date_reelle_arrivee = null; $object->update($user); - $result = $object->call_trigger('STOCKTRANSFER_ADDSTOCK_CANCEL', $user); + $object->context = array_merge($object->context, array('stocktransfer_operation' => 'addstock_cancel')); + $result = $object->call_trigger('STOCKTRANSFER_MODIFY', $user); if ($result < 0) { $error++; setEventMessages($object->error, $object->errors, 'errors');