2025-08-04 14:32:44 +00:00
< ? php
2026-02-21 11:28:45 +00:00
/* Copyright ( C ) 2006 - 2026 Laurent Destailleur < eldy @ users . sourceforge . net >
2025-08-04 14:32:44 +00:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < https :// www . gnu . org / licenses />.
*/
/**
2026-02-21 09:28:13 +00:00
* Parent class of all other business classes ( invoices , contracts , proposals , orders , ... )
2025-08-04 14:32:44 +00:00
*
* @ phan - forbid - undeclared - magic - properties
*/
trait CommonTrigger
{
2025-08-06 11:46:55 +00:00
/**
* @ var DoliDB Database handler ( result of a new DoliDB )
*/
public $db ;
/**
* @ var string Error string
* @ see $errors
*/
public $error ;
/**
* @ var string [] Array of error strings
*/
public $errors = array ();
2025-08-06 17:49:31 +00:00
/**
* @ var string Prefix to check for any trigger code of any business class to prevent bad value for trigger code .
* @ see CommonTrigger :: call_trigger ()
*
* We do not use a constant because PHP does not support constant in Trait and does not allow overriding a constant without using " override " key
* that is not available on all PHP versions
*/
2025-08-06 17:08:01 +00:00
public $TRIGGER_PREFIX = '' ; // to be overridden in child class implementations, i.e. 'BILL', 'TASK', 'PROPAL', etc. It is used to check that trigger code matches object name.
2025-08-06 11:46:55 +00:00
2025-08-04 14:32:44 +00:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Call trigger based on this instance .
* Some context information may also be provided into array property this -> context .
* NB : Error from trigger are stacked in interface -> errors
* NB2 : If return code of triggers are < 0 , action calling trigger should cancel all transaction .
*
2025-11-01 16:47:28 +00:00
* @ param string $triggerName Trigger ' s name to execute
* @ param ? User $user Object user
2026-06-16 21:20:00 +00:00
* @ return int Nb of triggers ran if no error , - Nb of triggers with errors otherwise .
2025-08-04 14:32:44 +00:00
*/
public function call_trigger ( $triggerName , $user )
{
// phpcs:enable
global $langs , $conf ;
2025-08-06 17:08:01 +00:00
if ( ! empty ( $this -> TRIGGER_PREFIX ) && strpos ( $triggerName , $this -> TRIGGER_PREFIX . '_' ) !== 0 ) {
2025-10-03 08:46:40 +00:00
if ( ! preg_match ( '/^OBJECT_LINK/' , $triggerName ) && ! preg_match ( '/^PAYMENTONLINE_/' , $triggerName )) { // We add this exception as we have some call_trigger with a triggeName that is not the business object (as in method inside the commonobjet.class.php to manage links)
2025-10-03 09:59:27 +00:00
dol_syslog ( 'The trigger "' . $triggerName . '" does not start with "' . $this -> TRIGGER_PREFIX . '_" as required.' , LOG_ERR );
2025-10-03 09:27:39 +00:00
//dol_print_error(null, 'The trigger "' . $triggerName . '" does not start with "' . $this->TRIGGER_PREFIX . '_" as required.');
//exit;
2025-08-07 13:26:57 +00:00
}
2025-08-04 14:32:44 +00:00
}
if ( ! is_object ( $langs )) { // If lang was not defined, we set it. It is required by run_triggers().
2026-01-21 18:19:36 +00:00
dol_syslog ( " call_trigger was called with no langs variable defined so we init one. " . getCallerInfoString ());
2025-08-04 14:32:44 +00:00
include_once DOL_DOCUMENT_ROOT . '/core/class/translate.class.php' ;
$langs = new Translate ( '' , $conf );
2026-01-21 18:19:36 +00:00
$langs -> setDefaultLang ( '' );
2025-08-04 14:32:44 +00:00
$langs -> load ( " main " );
}
include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php' ;
$interface = new Interfaces ( $this -> db );
$result = $interface -> run_triggers ( $triggerName , $this , $user , $langs , $conf );
if ( $result < 0 ) {
if ( ! empty ( $this -> errors )) {
$this -> errors = array_unique ( array_merge ( $this -> errors , $interface -> errors )); // We use array_unique because when a trigger call another trigger on same object, this->errors is added twice.
} else {
$this -> errors = $interface -> errors ;
}
}
2026-06-16 21:20:00 +00:00
2025-08-04 14:32:44 +00:00
return $result ;
}
}