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']))
return;
90 if (!is_array($arraycontext)) $arraycontext = array($arraycontext);
92 $this->contextarray = array_unique(array_merge($arraycontext, $this->contextarray));
94 $arraytolog = array();
95 foreach ($conf->modules_parts[
'hooks'] as $module => $hooks)
97 if (empty($conf->$module->enabled))
continue;
100 foreach ($arraycontext as $context)
102 if (is_array($hooks)) $arrayhooks = $hooks;
103 else $arrayhooks = explode(
':', $hooks);
105 if (in_array($context, $arrayhooks) || in_array(
'all', $arrayhooks))
108 if (empty($this->hooks[$context][$module]) || !is_object($this->hooks[$context][$module]))
110 $path =
'/'.$module.
'/class/';
111 $actionfile =
'actions_'.$module.
'.class.php';
113 $arraytolog[] =
'context='.$context.
'-path='.$path.$actionfile;
117 $controlclassname =
'Actions'.ucfirst($module);
118 $actionInstance =
new $controlclassname($this->
db);
119 $this->hooks[$context][$module] = $actionInstance;
125 if (count($arraytolog) > 0)
127 dol_syslog(get_class($this).
"::initHooks Loading hooks: ".join(
', ', $arraytolog), LOG_DEBUG);
145 public function executeHooks($method, $parameters = array(), &$object =
'', &$action =
'')
147 if (!is_array($this->hooks) || empty($this->hooks))
return 0;
149 $parameters[
'context'] = join(
':', $this->contextarray);
154 $hooktype =
'output';
160 'addMoreActionsButtons',
161 'addMoreMassActions',
164 'createDictionaryFieldlist',
165 'editDictionaryFieldlist',
172 'formCreateThirdpartyOptions',
175 'formBuilddocLineOptions',
176 'formatNotificationMessage',
178 'getAccessForbiddenMessage',
181 'getFormatedCustomerRef',
182 'getFormatedSupplierRef',
191 'pdf_getlineref_supplier',
192 'pdf_getlinevatrate',
193 'pdf_getlineupexcltax',
194 'pdf_getlineupwithtax',
196 'pdf_getlineqty_asked',
197 'pdf_getlineqty_shipped',
198 'pdf_getlineqty_keeptoship',
200 'pdf_getlineremisepercent',
201 'pdf_getlineprogress',
202 'pdf_getlinetotalexcltax',
203 'pdf_getlinetotalwithtax',
204 'paymentsupplierinvoices',
210 'printObjectSubLine',
215 'showLinkToObjectBlock',
216 'setContentSecurityPolicy',
220 )) $hooktype =
'addreplace';
223 $this->resPrint =
''; $this->resArray = array(); $this->resNbOfHooks = 0;
226 $modulealreadyexecuted = array();
227 $resaction = 0; $error = 0;
228 foreach ($this->hooks as $context => $modules)
230 if (!empty($modules))
232 foreach ($modules as $module => $actionclassinstance)
237 if (in_array($module, $modulealreadyexecuted))
continue;
240 if (!method_exists($actionclassinstance, $method))
continue;
242 $this->resNbOfHooks++;
244 $modulealreadyexecuted[$module] = $module;
247 $actionclassinstance->error = 0;
248 $actionclassinstance->errors = array();
250 dol_syslog(get_class($this).
"::executeHooks Qualified hook found (hooktype=".$hooktype.
"). We call method ".$method.
" of class ".get_class($actionclassinstance).
", module=".$module.
", action=".$action.
" context=".$context, LOG_DEBUG);
253 $parameters[
'currentcontext'] = $context;
255 if ($hooktype ==
'addreplace')
257 $resaction += $actionclassinstance->$method($parameters, $object, $action, $this);
258 if ($resaction < 0 || !empty($actionclassinstance->error) || (!empty($actionclassinstance->errors) && count($actionclassinstance->errors) > 0))
261 $this->error = $actionclassinstance->error; $this->errors = array_merge($this->errors, (array) $actionclassinstance->errors);
262 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);
265 if (isset($actionclassinstance->results) && is_array($actionclassinstance->results)) $this->resArray = array_merge($this->resArray, $actionclassinstance->results);
266 if (!empty($actionclassinstance->resprints)) $this->resPrint .= $actionclassinstance->resprints;
271 if (is_array($parameters) && !empty($parameters[
'special_code']) && $parameters[
'special_code'] > 3 && $parameters[
'special_code'] != $actionclassinstance->module_number)
continue;
274 $resaction = $actionclassinstance->$method($parameters, $object, $action, $this);
276 if (!empty($actionclassinstance->results) && is_array($actionclassinstance->results)) $this->resArray = array_merge($this->resArray, $actionclassinstance->results);
277 if (!empty($actionclassinstance->resprints)) $this->resPrint .= $actionclassinstance->resprints;
278 if (is_numeric($resaction) && $resaction < 0)
281 $this->error = $actionclassinstance->error; $this->errors = array_merge($this->errors, (array) $actionclassinstance->errors);
282 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);
285 if (!is_array($resaction) && !is_numeric($resaction))
287 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);
288 if (empty($actionclassinstance->resprints)) { $this->resPrint .= $resaction; $resaction = 0; }
294 unset($actionclassinstance->results);
295 unset($actionclassinstance->resprints);
300 return ($error ? -1 : $resaction);
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname= '')
Make an include_once using default root and alternate root if it fails.
executeHooks($method, $parameters=array(), &$object= '', &$action= '')
Execute hooks (if they were initialized) for the given method.
initHooks($arraycontext)
Init array $this->hooks with instantiated action controlers.
$conf db
API class for accounts.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
__construct($db)
Constructor.