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;
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 =
'', &$action =
'')
158 if (!is_array($this->hooks) || empty($this->hooks)) {
162 $parameters[
'context'] = join(
':', $this->contextarray);
166 $hooktype =
'addreplace';
168 if (in_array($method, array(
170 'dashboardAccountancy',
171 'dashboardActivities',
172 'dashboardCommercials',
173 'dashboardContracts',
175 'dashboardEmailings',
176 'dashboardExpenseReport',
178 'dashboardInterventions',
181 'dashboardOpensurvey',
183 'dashboardOrdersSuppliers',
184 'dashboardProductServices',
187 'dashboardSpecialBills',
188 'dashboardSupplierProposal',
189 'dashboardThirdparties',
191 'dashboardUsersGroups',
192 'dashboardWarehouse',
193 'dashboardWarehouseReceptions',
194 'dashboardWarehouseSendings',
199 'formBuilddocOption',
202 $hooktype =
'output';
206 $this->resPrint =
'';
207 $this->resArray = array();
208 $this->resNbOfHooks = 0;
212 $modulealreadyexecuted = array();
215 foreach ($this->hooks as $context => $modules) {
216 if (!empty($modules)) {
218 foreach ($modules as $module => $actionclassinstance) {
219 $module = preg_replace(
'/^\d+:/',
'', $module);
223 if (in_array($module, $modulealreadyexecuted)) {
228 if (!method_exists($actionclassinstance, $method)) {
232 $this->resNbOfHooks++;
234 $modulealreadyexecuted[$module] = $module;
237 $actionclassinstance->error = 0;
238 $actionclassinstance->errors = array();
240 if (
getDolGlobalInt(
'MAIN_DEBUG_SHOW_EACH_QUALIFIED_HOOK_CALL') >= 2) {
242 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);
247 $parameters[
'currentcontext'] = $context;
249 if ($hooktype ==
'addreplace') {
250 $resactiontmp = $actionclassinstance->$method($parameters, $object, $action, $this);
251 $resaction += $resactiontmp;
253 if ($resactiontmp < 0 || !empty($actionclassinstance->error) || (!empty($actionclassinstance->errors) && count($actionclassinstance->errors) > 0)) {
255 $this->error = $actionclassinstance->error;
256 $this->errors = array_merge($this->errors, (array) $actionclassinstance->errors);
257 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);
260 if (isset($actionclassinstance->results) && is_array($actionclassinstance->results)) {
261 if ($resactiontmp > 0) {
262 $this->resArray = $actionclassinstance->results;
264 $this->resArray = array_merge($this->resArray, $actionclassinstance->results);
267 if (!empty($actionclassinstance->resprints)) {
268 if ($resactiontmp > 0) {
269 $this->resPrint = $actionclassinstance->resprints;
271 $this->resPrint .= $actionclassinstance->resprints;
278 if (is_array($parameters) && !empty($parameters[
'special_code']) && $parameters[
'special_code'] > 3 && $parameters[
'special_code'] != $actionclassinstance->module_number) {
283 $resactiontmp = $actionclassinstance->$method($parameters, $object, $action, $this);
284 $resaction += $resactiontmp;
286 if (!empty($actionclassinstance->results) && is_array($actionclassinstance->results)) {
287 $this->resArray = array_merge($this->resArray, $actionclassinstance->results);
289 if (!empty($actionclassinstance->resprints)) {
290 $this->resPrint .= $actionclassinstance->resprints;
292 if (is_numeric($resactiontmp) && $resactiontmp < 0) {
294 $this->error = $actionclassinstance->error;
295 $this->errors = array_merge($this->errors, (array) $actionclassinstance->errors);
296 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);
300 if (!is_array($resactiontmp) && !is_numeric($resactiontmp)) {
301 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);
302 if (empty($actionclassinstance->resprints)) {
303 $this->resPrint .= $resactiontmp;
310 unset($actionclassinstance->results);
311 unset($actionclassinstance->resprints);
316 return ($error ? -1 : $resaction);