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;
131 if (count($arraytolog) > 0) {
132 dol_syslog(get_class($this).
"::initHooks Loading hooks: ".join(
', ', $arraytolog), LOG_DEBUG);
135 foreach ($arraycontext as $context) {
136 if (!empty($this->hooks[$context])) {
137 ksort($this->hooks[$context], SORT_NATURAL);
156 public function executeHooks($method, $parameters = array(), &$object =
null, &$action =
'')
158 if (!is_array($this->hooks) || empty($this->hooks)) {
161 if (!is_array($parameters)) {
162 dol_syslog(
'executeHooks was called with a non array $parameters. Surely a bug.', LOG_WARNING);
163 $parameters = array();
166 $parameters[
'context'] = join(
':', $this->contextarray);
170 $hooktype =
'addreplace';
172 if (in_array($method, array(
174 'dashboardAccountancy',
175 'dashboardActivities',
176 'dashboardCommercials',
177 'dashboardContracts',
179 'dashboardEmailings',
180 'dashboardExpenseReport',
182 'dashboardInterventions',
185 'dashboardOpensurvey',
187 'dashboardOrdersSuppliers',
188 'dashboardProductServices',
191 'dashboardSpecialBills',
192 'dashboardSupplierProposal',
193 'dashboardThirdparties',
195 'dashboardUsersGroups',
196 'dashboardWarehouse',
197 'dashboardWarehouseReceptions',
198 'dashboardWarehouseSendings',
203 'formBuilddocOptions',
206 $hooktype =
'output';
211 $localResArray = array();
212 $this->resArray = array();
213 $this->resNbOfHooks = 0;
217 $modulealreadyexecuted = array();
220 foreach ($this->hooks as $context => $modules) {
221 if (!empty($modules)) {
223 foreach ($modules as $module => $actionclassinstance) {
224 $module = preg_replace(
'/^\d+:/',
'', $module);
228 if (in_array($module, $modulealreadyexecuted)) {
233 if (!method_exists($actionclassinstance, $method)) {
237 $this->resNbOfHooks++;
239 $modulealreadyexecuted[$module] = $module;
242 $actionclassinstance->error = 0;
243 $actionclassinstance->errors = array();
247 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);
252 $parameters[
'currentcontext'] = $context;
254 if ($hooktype ==
'addreplace') {
255 $resactiontmp = $actionclassinstance->$method($parameters, $object, $action, $this);
256 $resaction += $resactiontmp;
258 if ($resactiontmp < 0 || !empty($actionclassinstance->error) || (!empty($actionclassinstance->errors) && count($actionclassinstance->errors) > 0)) {
260 $this->error = $actionclassinstance->error;
261 $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)) {
266 if ($resactiontmp > 0) {
267 $localResArray = $actionclassinstance->results;
269 $localResArray = array_merge($localResArray, $actionclassinstance->results);
272 if (!empty($actionclassinstance->resprints)) {
273 if ($resactiontmp > 0) {
274 $localResPrint = $actionclassinstance->resprints;
276 $localResPrint .= $actionclassinstance->resprints;
283 if (is_array($parameters) && !empty($parameters[
'special_code']) && $parameters[
'special_code'] > 3 && $parameters[
'special_code'] != $actionclassinstance->module_number) {
288 dol_syslog(
"Call method ".$method.
" of class ".get_class($actionclassinstance).
", module=".$module.
", hooktype=".$hooktype, LOG_DEBUG);
291 $resactiontmp = $actionclassinstance->$method($parameters, $object, $action, $this);
292 $resaction += $resactiontmp;
294 if (!empty($actionclassinstance->results) && is_array($actionclassinstance->results)) {
295 $localResArray = array_merge($localResArray, $actionclassinstance->results);
297 if (!empty($actionclassinstance->resprints)) {
298 $localResPrint .= $actionclassinstance->resprints;
300 if (is_numeric($resactiontmp) && $resactiontmp < 0) {
302 $this->error = $actionclassinstance->error;
303 $this->errors = array_merge($this->errors, (array) $actionclassinstance->errors);
304 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);
308 if (!is_array($resactiontmp) && !is_numeric($resactiontmp)) {
309 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);
310 if (empty($actionclassinstance->resprints)) {
311 $localResPrint .= $resactiontmp;
318 unset($actionclassinstance->results);
319 unset($actionclassinstance->resprints);
324 $this->resPrint = $localResPrint;
325 $this->resArray = $localResArray;
327 return ($error ? -1 : $resaction);