interfaces.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. /* Copyright (C) 2005-2009 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  4. * Copyright (C) 2010 Regis Houssin <regis@dolibarr.fr>
  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/interfaces.class.php
  21. * \ingroup core
  22. * \brief Fichier de la classe de gestion des triggers
  23. */
  24. /**
  25. * Class to manage triggers
  26. */
  27. class Interfaces
  28. {
  29. var $dir; // Directory with all core and external triggers files
  30. var $errors = array(); // Array for errors
  31. /**
  32. * Constructor
  33. *
  34. * @param DoliDB $db Database handler
  35. */
  36. function Interfaces($db)
  37. {
  38. $this->db = $db;
  39. }
  40. /**
  41. * Function called when a Dolibarr business event occurs
  42. * This function call all qualified triggers.
  43. *
  44. * @param string $action Trigger event code
  45. * @param Object $object Objet concern
  46. * @param User $user Objet user
  47. * @param Lang $lang Objet lang
  48. * @param Conf $conf Objet conf
  49. * @return int Nb of triggers ran if no error, -Nb of triggers with errors otherwise.
  50. */
  51. function run_triggers($action,$object,$user,$langs,$conf)
  52. {
  53. // Check parameters
  54. if (! is_object($object) || ! is_object($conf)) // Error
  55. {
  56. dol_syslog(get_class($this).'::run_triggers was called with wrong parameters action='.$action.' object='.is_object($object).' user='.is_object($user).' langs='.is_object($langs).' conf='.is_object($conf), LOG_ERR);
  57. return -1;
  58. }
  59. if (! is_object($user) || ! is_object($langs)) // Warning
  60. {
  61. dol_syslog(get_class($this).'::run_triggers was called with wrong parameters action='.$action.' object='.is_object($object).' user='.is_object($user).' langs='.is_object($langs).' conf='.is_object($conf), LOG_WARNING);
  62. }
  63. $nbfile = $nbtotal = $nbok = $nbko = 0;
  64. $files = array();
  65. $modules = array();
  66. $orders = array();
  67. $i=0;
  68. foreach($conf->triggers_modules as $reldir)
  69. {
  70. $dir=dol_buildpath($reldir,0);
  71. //print "xx".$dir;exit;
  72. // Check if directory exists
  73. if (!is_dir($dir)) continue;
  74. $handle=opendir($dir);
  75. if (is_resource($handle))
  76. {
  77. while (($file = readdir($handle))!==false)
  78. {
  79. if (is_readable($dir."/".$file) && preg_match('/^interface_([0-9]+)_([^_]+)_(.+)\.class\.php$/i',$file,$reg))
  80. {
  81. $part1=$reg[1];
  82. $part2=$reg[2];
  83. $part3=$reg[3];
  84. $nbfile++;
  85. $modName = "Interface".ucfirst($reg[3]);
  86. //print "file=$file"; print "modName=$modName"; exit;
  87. if (in_array($modName,$modules))
  88. {
  89. $langs->load("errors");
  90. dol_syslog(get_class($this)."::run_triggers action=".$action." ".$langs->trans("ErrorDuplicateTrigger",$modName,"/htdocs/core/triggers/"),LOG_ERR);
  91. continue;
  92. }
  93. else
  94. {
  95. include_once($dir.'/'.$file);
  96. }
  97. // Check if trigger file is disabled by name
  98. if (preg_match('/NORUN$/i',$file)) continue;
  99. // Check if trigger file is for a particular module
  100. $qualified=true;
  101. if (strtolower($reg[2]) != 'all')
  102. {
  103. $module=preg_replace('/^mod/i','',$reg[2]);
  104. $constparam='MAIN_MODULE_'.strtoupper($module);
  105. if (empty($conf->global->$constparam)) $qualified=false;
  106. }
  107. if (! $qualified)
  108. {
  109. dol_syslog(get_class($this)."::run_triggers action=".$action." Triggers for file '".$file."' need module to be enabled",LOG_INFO);
  110. continue;
  111. }
  112. $modules[$i] = $modName;
  113. $files[$i] = $file;
  114. $orders[$i] = $part1.'_'.$part2.'_'.$part3; // Set sort criteria value
  115. $i++;
  116. }
  117. }
  118. }
  119. }
  120. asort($orders);
  121. // Loop on each trigger
  122. foreach ($orders as $key => $value)
  123. {
  124. $modName = $modules[$key];
  125. if (empty($modName)) continue;
  126. $objMod = new $modName($this->db);
  127. if ($objMod)
  128. {
  129. dol_syslog(get_class($this)."::run_triggers action=".$action." Launch triggers for file '".$files[$key]."'",LOG_INFO);
  130. $result=$objMod->run_trigger($action,$object,$user,$langs,$conf);
  131. if ($result > 0)
  132. {
  133. // Action OK
  134. $nbtotal++;
  135. $nbok++;
  136. }
  137. if ($result == 0)
  138. {
  139. // Aucune action faite
  140. $nbtotal++;
  141. }
  142. if ($result < 0)
  143. {
  144. // Action KO
  145. $nbtotal++;
  146. $nbko++;
  147. $this->errors[]=$objMod->error;
  148. }
  149. }
  150. else
  151. {
  152. dol_syslog(get_class($this)."::run_triggers action=".$action." Failed to instantiate trigger for file '".$files[$key]."'",LOG_ERR);
  153. }
  154. }
  155. if ($nbko)
  156. {
  157. dol_syslog(get_class($this)."::run_triggers action=".$action." Files found: ".$nbfile.", Files launched: ".$nbtotal.", Done: ".$nbok.", Failed: ".$nbko, LOG_ERR);
  158. return -$nbko;
  159. }
  160. else
  161. {
  162. //dol_syslog(get_class($this)."::run_triggers Files found: ".$nbfile.", Files launched: ".$nbtotal.", Done: ".$nbok.", Failed: ".$nbko, LOG_DEBUG);
  163. return $nbok;
  164. }
  165. }
  166. /**
  167. * Return list of triggers. Function used by admin page htdoc/admin/triggers.
  168. * List is sorted by trigger filename so by priority to run.
  169. *
  170. * @return array Array list of triggers
  171. */
  172. function getTriggersList()
  173. {
  174. global $conf, $langs;
  175. $form = new Form($this->db);
  176. $files = array();
  177. $modules = array();
  178. $orders = array();
  179. $i = 0;
  180. foreach($conf->triggers_modules as $reldir)
  181. {
  182. $dir=dol_buildpath($reldir,0);
  183. //print "xx".$dir;exit;
  184. // Check if directory exists
  185. if (!is_dir($dir)) continue;
  186. $handle=opendir($dir);
  187. if (is_resource($handle))
  188. {
  189. while (($file = readdir($handle))!==false)
  190. {
  191. if (is_readable($dir.'/'.$file) && preg_match('/^interface_([0-9]+)_([^_]+)_(.+)\.class\.php/',$file,$reg))
  192. {
  193. $part1=$reg[1];
  194. $part2=$reg[2];
  195. $part3=$reg[3];
  196. $modName = 'Interface'.ucfirst($reg[3]);
  197. //print "file=$file"; print "modName=$modName"; exit;
  198. if (in_array($modName,$modules))
  199. {
  200. $langs->load("errors");
  201. print '<div class="error">'.$langs->trans("Error").' : '.$langs->trans("ErrorDuplicateTrigger",$modName,"/htdocs/core/triggers/").'</div>';
  202. }
  203. else
  204. {
  205. include_once($dir.'/'.$file);
  206. }
  207. $files[$i] = $file;
  208. $modules[$i] = $modName;
  209. $orders[$i] = $part1.'_'.$part2.'_'.$part3; // Set sort criteria value
  210. $i++;
  211. }
  212. }
  213. closedir($handle);
  214. }
  215. }
  216. asort($orders);
  217. $triggers = array();
  218. $j = 0;
  219. // Loop on each trigger
  220. foreach ($orders as $key => $value)
  221. {
  222. $modName = $modules[$key];
  223. if (empty($modName)) continue;
  224. $objMod = new $modName($this->db);
  225. // Define disabledbyname and disabledbymodule
  226. $disabledbyname=0;
  227. $disabledbymodule=1;
  228. $module='';
  229. // Check if trigger file is disabled by name
  230. if (preg_match('/NORUN$/i',$files[$key])) $disabledbyname=1;
  231. // Check if trigger file is for a particular module
  232. if (preg_match('/^interface_([0-9]+)_([^_]+)_(.+)\.class\.php/i',$files[$key],$reg))
  233. {
  234. $module=preg_replace('/^mod/i','',$reg[2]);
  235. $constparam='MAIN_MODULE_'.strtoupper($module);
  236. if (strtolower($reg[2]) == 'all') $disabledbymodule=0;
  237. else if (empty($conf->global->$constparam)) $disabledbymodule=2;
  238. }
  239. // We set info of modules
  240. $triggers[$j]['picto'] = $objMod->picto?img_object('',$objMod->picto):img_object('','generic');
  241. $triggers[$j]['file'] = $files[$key];
  242. $triggers[$j]['version'] = $objMod->getVersion();
  243. $triggers[$j]['status'] = img_picto($langs->trans("Active"),'tick');
  244. if ($disabledbyname > 0 || $disabledbymodule > 1) $triggers[$j]['status'] = "&nbsp;";
  245. $text ='<b>'.$langs->trans("Description").':</b><br>';
  246. $text.=$objMod->getDesc().'<br>';
  247. $text.='<br><b>'.$langs->trans("Status").':</b><br>';
  248. if ($disabledbyname == 1)
  249. {
  250. $text.=$langs->trans("TriggerDisabledByName").'<br>';
  251. if ($disabledbymodule == 2) $text.=$langs->trans("TriggerDisabledAsModuleDisabled",$module).'<br>';
  252. }
  253. else
  254. {
  255. if ($disabledbymodule == 0) $text.=$langs->trans("TriggerAlwaysActive").'<br>';
  256. if ($disabledbymodule == 1) $text.=$langs->trans("TriggerActiveAsModuleActive",$module).'<br>';
  257. if ($disabledbymodule == 2) $text.=$langs->trans("TriggerDisabledAsModuleDisabled",$module).'<br>';
  258. }
  259. $triggers[$j]['info'] = $form->textwithpicto('',$text);
  260. $j++;
  261. }
  262. return $triggers;
  263. }
  264. }
  265. ?>