111 if (!is_array(
$conf->modules_parts[
'hooks']) || empty(
$conf->modules_parts[
'hooks'])) {
116 if (!is_array($arraycontext)) {
117 $arraycontext = array($arraycontext);
120 $this->contextarray = array_unique(array_merge($arraycontext, $this->contextarray));
122 $foundcontextmodule =
false;
125 foreach (
$conf->modules_parts[
'hooks'] as $module =>
$hooks) {
126 if (!isModEnabled($module)) {
131 foreach ($arraycontext as
$context) {
135 $arrayhooks = explode(
':',
$hooks);
138 if (in_array(
$context, $arrayhooks) || in_array(
'all', $arrayhooks)) {
140 if (empty($this->hooks[
$context][$module]) || !is_object($this->hooks[
$context][$module])) {
141 $path =
'/'.$module.
'/class/';
142 $actionfile =
'actions_'.$module.
'.class.php';
146 $controlclassname =
'Actions'.ucfirst($module);
148 $actionInstance =
new $controlclassname($this->db);
149 '@phan-var-force CommonHookActions $actionInstance';
152 $priority = empty($actionInstance->priority) ? 50 : $actionInstance->priority;
154 $this->hooks[
$context][$module] = $actionInstance;
155 $this->hooksSorted[
$context][$priority.
':'.$module] = $actionInstance;
157 $foundcontextmodule =
true;
160 $stringtolog =
'context='.$context.
'-path='.$path.$actionfile.
'-priority='.$priority;
161 dol_syslog(get_class($this).
"::initHooks Loading hooks: ".$stringtolog, LOG_DEBUG);
163 dol_syslog(get_class($this).
"::initHooks Failed to load hook in ".$path.$actionfile, LOG_WARNING);
175 if ($foundcontextmodule) {
176 foreach ($arraycontext as
$context) {
177 if (!empty($this->hooksSorted[
$context])) {
178 ksort($this->hooksSorted[
$context], SORT_NATURAL);
202 if (isModEnabled(
'debugbar') && function_exists(
'debug_backtrace')) {
203 $trace = debug_backtrace();
204 if (isset($trace[0])) {
205 $hookInformations = [
207 'contexts' => $this->contextarray,
208 'file' => $trace[0][
'file'],
209 'line' => $trace[0][
'line'],
212 $hash = md5(json_encode($hookInformations));
213 if (!empty($this->hooksHistory[$hash])) {
214 $this->hooksHistory[$hash][
'count']++;
216 $hookInformations[
'count'] = 1;
217 $this->hooksHistory[$hash] = $hookInformations;
222 if (!is_array($this->hooks) || empty($this->hooks)) {
225 if (!is_array($parameters)) {
226 dol_syslog(
'executeHooks was called with a non array $parameters. Surely a bug.', LOG_WARNING);
227 $parameters = array();
230 $parameters[
'context'] = implode(
':', $this->contextarray);
234 $hooktype =
'addreplace';
236 if (in_array($method, array(
238 'dashboardAccountancy',
239 'dashboardActivities',
240 'dashboardCommercials',
241 'dashboardContracts',
243 'dashboardEmailings',
244 'dashboardExpenseReport',
246 'dashboardInterventions',
249 'dashboardOpensurvey',
251 'dashboardOrdersSuppliers',
252 'dashboardProductServices',
255 'dashboardSpecialBills',
256 'dashboardSupplierProposal',
257 'dashboardThirdparties',
259 'dashboardUsersGroups',
260 'dashboardWarehouse',
261 'dashboardWarehouseReceptions',
262 'dashboardWarehouseSendings',
267 'formBuilddocOptions',
270 $hooktype =
'output';
275 $localResArray = array();
277 $this->resNbOfHooks = 0;
281 $modulealreadyexecuted = array();
284 foreach ($this->hooksSorted as
$context => $modules) {
285 if (!empty($modules)) {
286 '@phan-var-force array<string,CommonHookActions> $modules';
288 foreach ($modules as $module => $actionclassinstance) {
289 $module = preg_replace(
'/^\d+:/',
'', $module);
293 if (in_array($module, $modulealreadyexecuted)) {
298 if (!method_exists($actionclassinstance, $method)) {
302 $this->resNbOfHooks++;
304 $modulealreadyexecuted[$module] = $module;
307 $actionclassinstance->error =
'';
308 $actionclassinstance->errors = array();
312 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);
317 $parameters[
'currentcontext'] =
$context;
319 if ($hooktype ==
'addreplace') {
321 $resactiontmp = $actionclassinstance->$method($parameters,
$object, $action, $this);
322 $resaction += $resactiontmp;
324 if ($resactiontmp < 0 || !empty($actionclassinstance->error) || (!empty($actionclassinstance->errors) && count($actionclassinstance->errors) > 0)) {
326 $this->error = $actionclassinstance->error;
327 $this->errors = array_merge($this->errors, (array) $actionclassinstance->errors);
328 dol_syslog(
"Error on hook module=".$module.
", method ".$method.
", class ".get_class($actionclassinstance).
", hooktype=".$hooktype.(empty($this->error) ?
'' :
" ".$this->error).(empty($this->errors) ?
'' :
" ".implode(
",", $this->errors)), LOG_ERR);
331 if (isset($actionclassinstance->results) && is_array($actionclassinstance->results)) {
332 if ($resactiontmp > 0) {
333 $localResArray = $actionclassinstance->results;
335 $localResArray = array_merge_recursive($localResArray, $actionclassinstance->results);
339 if (!empty($actionclassinstance->resprints)) {
340 if ($resactiontmp > 0) {
341 $localResPrint = (string) $actionclassinstance->resprints;
343 $localResPrint .= (string) $actionclassinstance->resprints;
350 if (is_array($parameters) && !empty($parameters[
'special_code']) && $parameters[
'special_code'] > 3 && $parameters[
'special_code'] != $actionclassinstance->module_number) {
355 dol_syslog(
"Call method ".$method.
" of class ".get_class($actionclassinstance).
", module=".$module.
", hooktype=".$hooktype, LOG_DEBUG);
359 $resactiontmp = $actionclassinstance->$method($parameters,
$object, $action, $this);
360 $resaction += $resactiontmp;
362 if (!empty($actionclassinstance->results) && is_array($actionclassinstance->results)) {
363 $localResArray = array_merge_recursive($localResArray, $actionclassinstance->results);
365 if (!empty($actionclassinstance->resprints)) {
366 $localResPrint .= (string) $actionclassinstance->resprints;
368 if (is_numeric($resactiontmp) && $resactiontmp < 0) {
370 $this->error = $actionclassinstance->error;
371 $this->errors = array_merge($this->errors, (array) $actionclassinstance->errors);
372 dol_syslog(
"Error on hook module=".$module.
", method ".$method.
", class ".get_class($actionclassinstance).
", hooktype=".$hooktype.(empty($this->error) ?
'' :
" ".$this->error).(empty($this->errors) ?
'' :
" ".implode(
",", $this->errors)), LOG_ERR);
376 if (!is_array($resactiontmp) && !is_numeric($resactiontmp)) {
377 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);
378 if (empty($actionclassinstance->resprints)) {
379 $localResPrint .= $resactiontmp;
386 $actionclassinstance->results = array();
387 $actionclassinstance->resprints =
null;
392 $this->resPrint = $localResPrint;
393 $this->resArray = $localResArray;
395 return ($error ? -1 : $resaction);