Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 71 additions & 8 deletions htdocs/core/class/notify.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@
'CONTRACT_MODIFY',
'STOCKTRANSFER_CREATE',
'STOCKTRANSFER_MODIFY',
'STOCKTRANSFER_DELETE',
'STOCKTRANSFER_VALIDATE',
'STOCKTRANSFER_UNVALIDATE',
'STOCKTRANSFER_CLOSE',
Expand All @@ -179,6 +180,51 @@
'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<string>
*/
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
*
Expand Down Expand Up @@ -482,7 +528,8 @@
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
}
}

Expand Down Expand Up @@ -574,7 +621,15 @@
// 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 {
Expand All @@ -583,7 +638,7 @@
}
}

$threshold = (float) $reg[1];

Check failure on line 641 in htdocs/core/class/notify.class.php

View workflow job for this annotation

GitHub Actions / phpstan / php-stan (8.2)

Variable $reg might not be defined.
if ($valueforthreshold < $threshold) {
continue;
}
Expand Down Expand Up @@ -652,6 +707,7 @@
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';

Expand Down Expand Up @@ -700,7 +756,7 @@
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);

Expand All @@ -717,8 +773,8 @@
$sql .= " AND c.statut = 1";
if (is_numeric($notifcode)) {
$sql .= " AND n.fk_action = ".((int) $notifcode); // Old usage
} else {

Check failure on line 776 in htdocs/core/class/notify.class.php

View workflow job for this annotation

GitHub Actions / windows-ci / win-test (windows-latest, 7.4)

NotSanitizedString in IN/NOT IN sql query `implode("','", array_map(array($this->db, 'escape'), $notifcodestosearch))."')"; ...`)
$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
Expand Down Expand Up @@ -1073,19 +1129,26 @@
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;
}

$sendto = $val;

$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');
}
Expand All @@ -1099,7 +1162,7 @@

$subject = '['.$appli.'] '.$langs->transnoentitiesnoconv("DolibarrNotification").($projtitle ? ' '.$projtitle : '');

switch ($notifcode) {
switch ($matchednotifcode) {
case 'BILL_VALIDATE':
$link = '<a href="'.$urlwithroot.'/compta/facture/card.php?facid='.$object->id.'&entity='.$object->entity.'">'.$newref.'</a>';
$dir_output = $conf->facture->dir_output."/".get_exdir(0, 0, 0, 1, $object, 'invoice');
Expand Down
1 change: 1 addition & 0 deletions htdocs/install/mysql/data/llx_c_action_trigger.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions htdocs/install/mysql/migration/23.0.0-24.0.0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions htdocs/langs/en_US/other.lang
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 8 additions & 4 deletions htdocs/product/stock/stocktransfer/class/stocktransfer.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}
Expand Down Expand Up @@ -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');
}

/**
Expand All @@ -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');
}

/**
Expand All @@ -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');
}

/**
Expand Down
17 changes: 8 additions & 9 deletions htdocs/product/stock/stocktransfer/stocktransfer_card.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down
Loading