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 $this->resArray = array();
212 $this->resNbOfHooks = 0;
216 $modulealreadyexecuted = array();
219 foreach ($this->hooks as $context => $modules) {
220 if (!empty($modules)) {
222 foreach ($modules as $module => $actionclassinstance) {
223 $module = preg_replace(
'/^\d+:/',
'', $module);
227 if (in_array($module, $modulealreadyexecuted)) {
232 if (!method_exists($actionclassinstance, $method)) {
236 $this->resNbOfHooks++;
238 $modulealreadyexecuted[$module] = $module;
241 $actionclassinstance->error = 0;
242 $actionclassinstance->errors = array();
246 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);
251 $parameters[
'currentcontext'] = $context;
253 if ($hooktype ==
'addreplace') {
254 $resactiontmp = $actionclassinstance->$method($parameters, $object, $action, $this);
255 $resaction += $resactiontmp;
257 if ($resactiontmp < 0 || !empty($actionclassinstance->error) || (!empty($actionclassinstance->errors) && count($actionclassinstance->errors) > 0)) {
259 $this->error = $actionclassinstance->error;
260 $this->errors = array_merge($this->errors, (array) $actionclassinstance->errors);
261 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);
264 if (isset($actionclassinstance->results) && is_array($actionclassinstance->results)) {
265 if ($resactiontmp > 0) {
266 $this->resArray = $actionclassinstance->results;
268 $this->resArray = array_merge($this->resArray, $actionclassinstance->results);
271 if (!empty($actionclassinstance->resprints)) {
272 if ($resactiontmp > 0) {
273 $localResPrint = $actionclassinstance->resprints;
275 $localResPrint .= $actionclassinstance->resprints;
282 if (is_array($parameters) && !empty($parameters[
'special_code']) && $parameters[
'special_code'] > 3 && $parameters[
'special_code'] != $actionclassinstance->module_number) {
287 dol_syslog(
"Call method ".$method.
" of class ".get_class($actionclassinstance).
", module=".$module.
", hooktype=".$hooktype, LOG_DEBUG);
290 $resactiontmp = $actionclassinstance->$method($parameters, $object, $action, $this);
291 $resaction += $resactiontmp;
293 if (!empty($actionclassinstance->results) && is_array($actionclassinstance->results)) {
294 $this->resArray = array_merge($this->resArray, $actionclassinstance->results);
296 if (!empty($actionclassinstance->resprints)) {
297 $localResPrint .= $actionclassinstance->resprints;
299 if (is_numeric($resactiontmp) && $resactiontmp < 0) {
301 $this->error = $actionclassinstance->error;
302 $this->errors = array_merge($this->errors, (array) $actionclassinstance->errors);
303 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);
307 if (!is_array($resactiontmp) && !is_numeric($resactiontmp)) {
308 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);
309 if (empty($actionclassinstance->resprints)) {
310 $localResPrint .= $resactiontmp;
317 unset($actionclassinstance->results);
318 unset($actionclassinstance->resprints);
323 $this->resPrint = $localResPrint;
325 return ($error ? -1 : $resaction);