dolibarr 24.0.0-beta
commontrigger.class.php
1<?php
2/* Copyright (C) 2006-2026 Laurent Destailleur <eldy@users.sourceforge.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18
24trait CommonTrigger
25{
29 public $db;
30
35 public $error;
36
40 public $errors = array();
41
49 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.
50
51
52 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
63 public function call_trigger($triggerName, $user)
64 {
65 // phpcs:enable
66 global $langs, $conf;
67
68 if (!empty($this->TRIGGER_PREFIX) && strpos($triggerName, $this->TRIGGER_PREFIX . '_') !== 0) {
69 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)
70 dol_syslog('The trigger "' . $triggerName . '" does not start with "' . $this->TRIGGER_PREFIX . '_" as required.', LOG_ERR);
71 //dol_print_error(null, 'The trigger "' . $triggerName . '" does not start with "' . $this->TRIGGER_PREFIX . '_" as required.');
72 //exit;
73 }
74 }
75 if (!is_object($langs)) { // If lang was not defined, we set it. It is required by run_triggers().
76 dol_syslog("call_trigger was called with no langs variable defined so we init one. ".getCallerInfoString());
77 include_once DOL_DOCUMENT_ROOT.'/core/class/translate.class.php';
78 $langs = new Translate('', $conf);
79 $langs->setDefaultLang('');
80 $langs->load("main");
81 }
82
83 include_once DOL_DOCUMENT_ROOT.'/core/class/interfaces.class.php';
84 $interface = new Interfaces($this->db);
85 $result = $interface->run_triggers($triggerName, $this, $user, $langs, $conf);
86
87 if ($result < 0) {
88 if (!empty($this->errors)) {
89 $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.
90 } else {
91 $this->errors = $interface->errors;
92 }
93 }
94
95 return $result;
96 }
97}
Class to manage triggers.
Class to manage translations.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
getCallerInfoString()
Get caller info as a string that can be appended to a log message.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.