45 public $errors = array();
48 public $contextarray = array();
51 public $hooks = array();
54 public $resArray = array();
56 public $resPrint =
'';
58 public $resNbOfHooks = 0;
87 if (!is_array($conf->modules_parts[
'hooks']) || empty($conf->modules_parts[
'hooks'])) {
92 if (!is_array($arraycontext)) {
93 $arraycontext = array($arraycontext);
96 $this->contextarray = array_unique(array_merge($arraycontext, $this->contextarray));
98 $arraytolog = array();
99 foreach ($conf->modules_parts[
'hooks'] as $module => $hooks) {
100 if (empty($conf->$module->enabled)) {
105 foreach ($arraycontext as $context) {
106 if (is_array($hooks)) {
107 $arrayhooks = $hooks;
109 $arrayhooks = explode(
':', $hooks);
112 if (in_array($context, $arrayhooks) || in_array(
'all', $arrayhooks)) {
114 if (empty($this->hooks[$context][$module]) || !is_object($this->hooks[$context][$module])) {
115 $path =
'/'.$module.
'/class/';
116 $actionfile =
'actions_'.$module.
'.class.php';
118 $arraytolog[] =
'context='.$context.
'-path='.$path.$actionfile;
121 $controlclassname =
'Actions'.ucfirst($module);
122 $actionInstance =
new $controlclassname($this->
db);
123 $priority = empty($actionInstance->priority) ? 50 : $actionInstance->priority;
124 $this->hooks[$context][$priority.
':'.$module] = $actionInstance;
130 if (count($arraytolog) > 0) {
131 dol_syslog(get_class($this).
"::initHooks Loading hooks: ".join(
', ', $arraytolog), LOG_DEBUG);
134 foreach ($arraycontext as $context) {
135 if (!empty($this->hooks[$context])) {
136 ksort($this->hooks[$context], SORT_NATURAL);
155 public function executeHooks($method, $parameters = array(), &$object =
'', &$action =
'')
157 if (!is_array($this->hooks) || empty($this->hooks)) {
161 $parameters[
'context'] = join(
':', $this->contextarray);
166 $hooktype =
'output';
172 'addMoreActionsButtons',
173 'addMoreMassActions',
178 'createDictionaryFieldlist',
179 'editDictionaryFieldlist',
186 'formCreateThirdpartyOptions',
189 'formBuilddocLineOptions',
190 'formatNotificationMessage',
192 'getAccessForbiddenMessage',
196 'getFormatedCustomerRef',
197 'getFormatedSupplierRef',
202 'menuDropdownQuickaddItems',
210 'pdf_getlineref_supplier',
211 'pdf_getlinevatrate',
212 'pdf_getlineupexcltax',
213 'pdf_getlineupwithtax',
215 'pdf_getlineqty_asked',
216 'pdf_getlineqty_shipped',
217 'pdf_getlineqty_keeptoship',
219 'pdf_getlineremisepercent',
220 'pdf_getlineprogress',
221 'pdf_getlinetotalexcltax',
222 'pdf_getlinetotalwithtax',
223 'paymentsupplierinvoices',
229 'printObjectSubLine',
234 'showLinkToObjectBlock',
235 'setContentSecurityPolicy',
239 'displayMarginInfos',
242 $hooktype =
'addreplace';
246 $this->resPrint =
'';
247 $this->resArray = array();
248 $this->resNbOfHooks = 0;
252 $modulealreadyexecuted = array();
255 foreach ($this->hooks as $context => $modules) {
256 if (!empty($modules)) {
258 foreach ($modules as $module => $actionclassinstance) {
259 $module = preg_replace(
'/^\d+:/',
'', $module);
263 if (in_array($module, $modulealreadyexecuted)) {
268 if (!method_exists($actionclassinstance, $method)) {
272 $this->resNbOfHooks++;
274 $modulealreadyexecuted[$module] = $module;
277 $actionclassinstance->error = 0;
278 $actionclassinstance->errors = array();
280 dol_syslog(get_class($this).
"::executeHooks Qualified hook found (hooktype=".$hooktype.
"). We call method ".get_class($actionclassinstance).
'->'.$method.
", context=".$context.
", module=".$module.
", action=".$action.((is_object($object) && property_exists($object,
'id')) ?
', object id='.$object->id :
'').((is_object($object) && property_exists($object,
'element')) ?
', object element='.$object->element :
''), LOG_DEBUG);
284 $parameters[
'currentcontext'] = $context;
286 if ($hooktype ==
'addreplace') {
287 $resactiontmp = $actionclassinstance->$method($parameters, $object, $action, $this);
288 $resaction += $resactiontmp;
290 if ($resactiontmp < 0 || !empty($actionclassinstance->error) || (!empty($actionclassinstance->errors) && count($actionclassinstance->errors) > 0)) {
292 $this->error = $actionclassinstance->error;
293 $this->errors = array_merge($this->errors, (array) $actionclassinstance->errors);
294 dol_syslog(
"Error on hook module=".$module.
", method ".$method.
", class ".get_class($actionclassinstance).
", hooktype=".$hooktype.(empty($this->error) ?
'' :
" ".$this->error).(empty($this->errors) ?
'' :
" ".join(
",", $this->errors)), LOG_ERR);
297 if (isset($actionclassinstance->results) && is_array($actionclassinstance->results)) {
298 if ($resactiontmp > 0) {
299 $this->resArray = $actionclassinstance->results;
301 $this->resArray = array_merge($this->resArray, $actionclassinstance->results);
304 if (!empty($actionclassinstance->resprints)) {
305 if ($resactiontmp > 0) {
306 $this->resPrint = $actionclassinstance->resprints;
308 $this->resPrint .= $actionclassinstance->resprints;
315 if (is_array($parameters) && !empty($parameters[
'special_code']) && $parameters[
'special_code'] > 3 && $parameters[
'special_code'] != $actionclassinstance->module_number) {
320 $resactiontmp = $actionclassinstance->$method($parameters, $object, $action, $this);
321 $resaction += $resactiontmp;
323 if (!empty($actionclassinstance->results) && is_array($actionclassinstance->results)) {
324 $this->resArray = array_merge($this->resArray, $actionclassinstance->results);
326 if (!empty($actionclassinstance->resprints)) {
327 $this->resPrint .= $actionclassinstance->resprints;
329 if (is_numeric($resactiontmp) && $resactiontmp < 0) {
331 $this->error = $actionclassinstance->error;
332 $this->errors = array_merge($this->errors, (array) $actionclassinstance->errors);
333 dol_syslog(
"Error on hook module=".$module.
", method ".$method.
", class ".get_class($actionclassinstance).
", hooktype=".$hooktype.(empty($this->error) ?
'' :
" ".$this->error).(empty($this->errors) ?
'' :
" ".join(
",", $this->errors)), LOG_ERR);
337 if (!is_array($resactiontmp) && !is_numeric($resactiontmp)) {
338 dol_syslog(
'Error: Bug into hook '.$method.
' of module class '.get_class($actionclassinstance).
'. Method must not return a string but an int (0=OK, 1=Replace, -1=KO) and set string into ->resprints', LOG_ERR);
339 if (empty($actionclassinstance->resprints)) {
340 $this->resPrint .= $resactiontmp;
347 unset($actionclassinstance->results);
348 unset($actionclassinstance->resprints);
353 return ($error ? -1 : $resaction);
initHooks($arraycontext)
Init array $this->hooks with instantiated action controlers.
executeHooks($method, $parameters=array(), &$object='', &$action='')
Execute hooks (if they were initialized) for the given method.
__construct($db)
Constructor.
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
$conf db
API class for accounts.