PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/core/class/hookmanager.class.php

https://github.com/asterix14/dolibarr
PHP | 193 lines | 103 code | 24 blank | 66 comment | 47 complexity | 0963edd051c4599c2f7a73c8c4ea5fbc MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?php
  2. /* Copyright (C) 2006-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2005-2011 Regis Houssin <regis@dolibarr.fr>
  4. * Copyright (C) 2010-2011 Juanjo Menent <jmenent@2byte.es>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/core/class/hookmanager.class.php
  21. * \ingroup core
  22. * \brief File of class to manage hooks
  23. */
  24. /**
  25. * \class HookManager
  26. * \brief Class to manage hooks
  27. */
  28. class HookManager
  29. {
  30. var $db;
  31. // Context hookmanager was created for ('thirdpartycard', 'thirdpartydao', ...)
  32. var $contextarray=array();
  33. // Array with instantiated classes
  34. var $hooks=array();
  35. /**
  36. * Constructor
  37. *
  38. * @param DoliDB $DB Handler acces base de donnees
  39. */
  40. function HookManager($DB)
  41. {
  42. $this->db = $DB;
  43. }
  44. /**
  45. * Init array this->hooks with instantiated action controlers.
  46. *
  47. * First, a hook is declared by a module by adding a constant MAIN_MODULE_MYMODULENAME_HOOKS
  48. * with value 'nameofcontext1:nameofcontext2:...' into $this->const of module descriptor file.
  49. * This make conf->hooks_modules loaded with an entry ('modulename'=>array(nameofcontext1,nameofcontext2,...))
  50. * When callHooks function is called, with callHooks(list_of_contexts), an array this->hooks is defined with instance of controler
  51. * class found into file /mymodule/class/actions_mymodule.class.php (if module has declared the context as a managed context).
  52. * Then when a hook is executeHook('aMethod'...) is called, the method aMethod found into class will be executed.
  53. *
  54. * @param array $arraytype Array list of searched hooks tab/features. For example: 'thirdpartycard' (for hook methods into page card thirdparty), 'thirdpartydao' (for hook methods into Societe), ...
  55. * @return int Always 1
  56. */
  57. function callHooks($arraytype)
  58. {
  59. global $conf;
  60. // Test if there is hooks to manage
  61. if (! is_array($conf->hooks_modules) || empty($conf->hooks_modules)) return;
  62. // For backward compatibility
  63. if (! is_array($arraytype)) $arraytype=array($arraytype);
  64. $this->contextarray=array_merge($arraytype,$this->contextarray);
  65. $i=0;
  66. foreach($conf->hooks_modules as $module => $hooks)
  67. {
  68. if ($conf->$module->enabled)
  69. {
  70. foreach($arraytype as $type)
  71. {
  72. if (in_array($type,$hooks)) // We instantiate action class only if hook is required
  73. {
  74. $path = '/'.$module.'/class/';
  75. $actionfile = 'actions_'.$module.'.class.php';
  76. $pathroot = '';
  77. $this->hooks[$i]['type']=$type;
  78. // Include actions class overwriting hooks
  79. $resaction=dol_include_once($path.$actionfile);
  80. if ($resaction)
  81. {
  82. $controlclassname = 'Actions'.ucfirst($module);
  83. $actionInstance = new $controlclassname($this->db);
  84. $this->hooks[$i]['modules'][$module] = $actionInstance;
  85. }
  86. // Include dataservice class (model)
  87. // TODO storing dao is useless here. It's goal of controller to known which dao to manage
  88. $daofile = 'dao_'.$module.'.class.php';
  89. $resdao=dol_include_once($path.$daofile);
  90. if ($resdao)
  91. {
  92. // Instantiate dataservice class (model)
  93. $daoInstance = 'Dao'.ucfirst($module);
  94. $this->hooks[$i]['modules'][$module]->object = new $daoInstance($this->db);
  95. }
  96. $i++;
  97. }
  98. }
  99. }
  100. }
  101. return 1;
  102. }
  103. /**
  104. * Execute hooks (if the were initialized) for the given method
  105. *
  106. * @param string $method Name of method hooked ('doActions', 'printSearchForm', 'showInputField', ...)
  107. * @param array $parameters Array of parameters
  108. * @param Object &$object Object to use hooks on
  109. * @param string &$action Action code on calling page ('create', 'edit', 'view', 'add', 'update', 'delete'...)
  110. * @return mixed For doActions,showInputField,showOutputField: Return 0 if we want to keep standard actions, >0 if if want to stop standard actions, <0 means KO.
  111. * For printSearchForm,printLeftBlock: Return HTML string.
  112. * $this->error or this->errors are also defined by class called by this function if error.
  113. */
  114. function executeHooks($method, $parameters=false, &$object='', &$action='')
  115. {
  116. global $var;
  117. if (! is_array($this->hooks) || empty($this->hooks)) return '';
  118. $parameters['context']=join(':',$this->contextarray);
  119. dol_syslog(get_class($this).'::executeHooks method='.$method." action=".$action." context=".$parameters['context']);
  120. // Loop on each hook
  121. $resaction=0; $resprint='';
  122. foreach($this->hooks as $hook)
  123. {
  124. if (! empty($hook['modules']))
  125. {
  126. foreach($hook['modules'] as $module => $actioninstance)
  127. {
  128. $var=!$var;
  129. // Hooks that return int
  130. if ($method == 'doActions' && method_exists($actioninstance,$method))
  131. {
  132. $resaction+=$actioninstance->doActions($parameters, $object, $action); // action can be changed by method (to go back to other action for example), socid can be changed/set by method (during creation for example)
  133. if ($resaction < 0 || ! empty($actioninstance->error) || (! empty($actioninstance->errors) && count($actioninstance->errors) > 0))
  134. {
  135. $this->error=$actioninstance->error; $this->errors=$actioninstance->errors;
  136. if ($action=='add') $action='create'; // TODO this change must be inside the doActions
  137. if ($action=='update') $action='edit'; // TODO this change must be inside the doActions
  138. }
  139. }
  140. else if ($method == 'showInputFields' && method_exists($actioninstance,$method))
  141. {
  142. $resaction+=$actioninstance->showInputFields($parameters, $object, $action); // action can be changed by method (to go back to other action for example), socid can be changed/set by method (during creation for example)
  143. if ($resaction < 0 || ! empty($actioninstance->error) || (! empty($actioninstance->errors) && count($actioninstance->errors) > 0))
  144. {
  145. $this->error=$actioninstance->error; $this->errors=$actioninstance->errors;
  146. }
  147. }
  148. else if ($method == 'showOutputFields' && method_exists($actioninstance,$method))
  149. {
  150. $resaction+=$actioninstance->showOutputFields($parameters, $object, $action); // action can be changed by method (to go back to other action for example), socid can be changed/set by method (during creation for example)
  151. if ($resaction < 0 || ! empty($actioninstance->error) || (! empty($actioninstance->errors) && count($actioninstance->errors) > 0))
  152. {
  153. $this->error=$actioninstance->error; $this->errors=$actioninstance->errors;
  154. }
  155. }
  156. // Generic hooks that return a string (printSearchForm, printLeftBlock, formBuilddocOptions, ...)
  157. else if (method_exists($actioninstance,$method))
  158. {
  159. if (is_array($parameters) && $parameters['special_code'] > 3 && $parameters['special_code'] != $actioninstance->module_number) continue;
  160. $resprint.=$actioninstance->$method($parameters, $object, $action, $this);
  161. }
  162. }
  163. }
  164. }
  165. if ($method == 'doActions' || $method == 'showInputFields' || $method == 'showOutputFields') return $resaction;
  166. return $resprint;
  167. }
  168. }
  169. ?>