PageRenderTime 61ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/core/class/interfaces.class.php

https://github.com/asterix14/dolibarr
PHP | 295 lines | 198 code | 32 blank | 65 comment | 54 complexity | 235b7a726bd4282f3db5b1806d6d2d0f MD5 | raw file
Possible License(s): LGPL-2.0
  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 Interfaces
  26. * \brief Classe de la gestion des triggers
  27. */
  28. class Interfaces
  29. {
  30. var $dir; // Directory with all core and external triggers files
  31. var $errors = array(); // Array for errors
  32. /**
  33. * Constructor
  34. *
  35. * @param DoliDB $DB Database handler
  36. */
  37. function Interfaces($DB)
  38. {
  39. $this->db = $DB ;
  40. }
  41. /**
  42. * \brief Fonction appelee lors du declenchement d'un evenement Dolibarr.
  43. * Cette fonction declenche tous les triggers trouves actifs.
  44. * \param action Trigger event code
  45. * \param object Objet concern
  46. * \param user Objet user
  47. * \param lang Objet lang
  48. * \param 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('interface::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('interface::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. foreach($conf->triggers_modules as $reldir)
  64. {
  65. $dir=dol_buildpath($reldir,0);
  66. //print "xx".$dir;exit;
  67. // Check if directory exists
  68. if (!is_dir($dir)) continue;
  69. $handle=opendir($dir);
  70. $modules = array();
  71. $nbfile = $nbtotal = $nbok = $nbko = 0;
  72. if (is_resource($handle))
  73. {
  74. while (($file = readdir($handle))!==false)
  75. {
  76. if (is_readable($dir."/".$file) && preg_match('/^interface_([^_]+)_(.+)\.class\.php$/i',$file,$reg))
  77. {
  78. $nbfile++;
  79. $modName = "Interface".ucfirst($reg[2]);
  80. //print "file=$file"; print "modName=$modName"; exit;
  81. if (in_array($modName,$modules))
  82. {
  83. $langs->load("errors");
  84. dol_syslog("Interface::run_triggers action=".$action." ".$langs->trans("ErrorDuplicateTrigger",$modName,"/htdocs/core/triggers/"),LOG_ERR);
  85. continue;
  86. }
  87. // Check if trigger file is disabled by name
  88. if (preg_match('/NORUN$/i',$file))
  89. {
  90. continue;
  91. }
  92. // Check if trigger file is for a particular module
  93. $qualified=true;
  94. if (strtolower($reg[1]) != 'all')
  95. {
  96. $module=preg_replace('/^mod/i','',$reg[1]);
  97. $constparam='MAIN_MODULE_'.strtoupper($module);
  98. if (empty($conf->global->$constparam)) $qualified=false;
  99. }
  100. if (! $qualified)
  101. {
  102. dol_syslog("Interfaces::run_triggers action=".$action." Triggers for file '".$file."' need module to be enabled",LOG_INFO);
  103. continue;
  104. }
  105. include_once($dir."/".$file);
  106. $objMod = new $modName($this->db);
  107. $i=0;
  108. if ($objMod)
  109. {
  110. // Bypass if workflow module is enabled and if the trigger asked to be disable in such case
  111. if (! empty($conf->workflow->enabled) && ! empty($objMod->disabled_if_workflow))
  112. {
  113. dol_syslog("Interfaces::run_triggers action=".$action." Bypass triggers for file '".$file."'",LOG_INFO);
  114. continue;
  115. }
  116. dol_syslog("Interfaces::run_triggers action=".$action." Launch triggers for file '".$file."'",LOG_INFO);
  117. $modules[$i] = $modName;
  118. //dol_syslog("Interfaces::run_triggers Launch triggers for file '".$file."'",LOG_INFO);
  119. $result=$objMod->run_trigger($action,$object,$user,$langs,$conf);
  120. if ($result > 0)
  121. {
  122. // Action OK
  123. $nbtotal++;
  124. $nbok++;
  125. }
  126. if ($result == 0)
  127. {
  128. // Aucune action faite
  129. $nbtotal++;
  130. }
  131. if ($result < 0)
  132. {
  133. // Action KO
  134. $nbtotal++;
  135. $nbko++;
  136. $this->errors[]=$objMod->error;
  137. }
  138. $i++;
  139. }
  140. else
  141. {
  142. dol_syslog("Interfaces::run_triggers action=".$action." Failed to instantiate trigger for file '".$file."'",LOG_ERR);
  143. }
  144. }
  145. }
  146. closedir($handle);
  147. }
  148. }
  149. if ($nbko)
  150. {
  151. dol_syslog("Interfaces::run_triggers action=".$action." Files found: ".$nbfile.", Files launched: ".$nbtotal.", Done: ".$nbok.", Failed: ".$nbko, LOG_ERR);
  152. return -$nbko;
  153. }
  154. else
  155. {
  156. //dol_syslog("Interfaces::run_triggers Files found: ".$nbfile.", Files launched: ".$nbtotal.", Done: ".$nbok.", Failed: ".$nbko, LOG_DEBUG);
  157. return $nbok;
  158. }
  159. }
  160. /**
  161. * Return list of triggers. Function used by admin page htdoc/admin/triggers
  162. * @param workflow 0=Return all triggers, 1=Return only triggers not disabled if workflow module activated
  163. * @return array Array list of triggers
  164. */
  165. function getTriggersList($workflow=0)
  166. {
  167. global $conf, $langs;
  168. $form = new Form($this->db);
  169. $files = array();
  170. $modules = array();
  171. $orders = array();
  172. $i = 0;
  173. foreach($conf->triggers_modules as $reldir)
  174. {
  175. $dir=dol_buildpath($reldir,0);
  176. //print "xx".$dir;exit;
  177. // Check if directory exists
  178. if (!is_dir($dir)) continue;
  179. $handle=opendir($dir);
  180. if (is_resource($handle))
  181. {
  182. while (($file = readdir($handle))!==false)
  183. {
  184. if (is_readable($dir.'/'.$file) && preg_match('/^interface_([^_]+)_(.+)\.class\.php/',$file,$reg))
  185. {
  186. $modName = 'Interface'.ucfirst($reg[2]);
  187. //print "file=$file"; print "modName=$modName"; exit;
  188. if (in_array($modName,$modules))
  189. {
  190. $langs->load("errors");
  191. print '<div class="error">'.$langs->trans("Error").' : '.$langs->trans("ErrorDuplicateTrigger",$modName,"/htdocs/core/triggers/").'</div>';
  192. $objMod = new $modName($this->db);
  193. $modules[$i] = $modName;
  194. $files[$i] = $file;
  195. $orders[$i] = $objMod->family; // Tri par famille
  196. $i++;
  197. }
  198. else
  199. {
  200. include_once($dir.'/'.$file);
  201. $objMod = new $modName($this->db);
  202. $modules[$i] = $modName;
  203. $files[$i] = $file;
  204. $orders[$i] = $objMod->family; // Tri par famille
  205. $i++;
  206. }
  207. }
  208. }
  209. closedir($handle);
  210. }
  211. }
  212. asort($orders);
  213. $triggers = array();
  214. $j = 0;
  215. // Loop on each trigger
  216. foreach ($orders as $key => $value)
  217. {
  218. $modName = $modules[$key];
  219. if ($modName)
  220. {
  221. $objMod = new $modName($this->db);
  222. // Bypass if workflow module is enabled and if the trigger is compatible
  223. if ($workflow && ! empty($objMod->disabled_if_workflow)) continue;
  224. }
  225. // Define disabledbyname and disabledbymodule
  226. $disabledbyname=0;
  227. $disabledbymodule=1;
  228. $module='';
  229. if (preg_match('/NORUN$/i',$files[$key])) $disabledbyname=1;
  230. if (preg_match('/^interface_([^_]+)_(.+)\.class\.php/i',$files[$key],$reg))
  231. {
  232. // Check if trigger file is for a particular module
  233. $module=preg_replace('/^mod/i','',$reg[1]);
  234. $constparam='MAIN_MODULE_'.strtoupper($module);
  235. if (strtolower($reg[1]) == 'all') $disabledbymodule=0;
  236. else if (empty($conf->global->$constparam)) $disabledbymodule=2;
  237. }
  238. $triggers[$j]['picto'] = $objMod->picto?img_object('',$objMod->picto):img_object('','generic');
  239. $triggers[$j]['file'] = $files[$key];
  240. $triggers[$j]['version'] = $objMod->getVersion();
  241. $triggers[$j]['status'] = img_picto($langs->trans("Active"),'tick');
  242. if ($disabledbyname > 0 || $disabledbymodule > 1) $triggers[$j]['status'] = "&nbsp;";
  243. $text ='<b>'.$langs->trans("Description").':</b><br>';
  244. $text.=$objMod->getDesc().'<br>';
  245. $text.='<br><b>'.$langs->trans("Status").':</b><br>';
  246. if ($disabledbyname == 1)
  247. {
  248. $text.=$langs->trans("TriggerDisabledByName").'<br>';
  249. if ($disabledbymodule == 2) $text.=$langs->trans("TriggerDisabledAsModuleDisabled",$module).'<br>';
  250. }
  251. else
  252. {
  253. if ($disabledbymodule == 0) $text.=$langs->trans("TriggerAlwaysActive").'<br>';
  254. if ($disabledbymodule == 1) $text.=$langs->trans("TriggerActiveAsModuleActive",$module).'<br>';
  255. if ($disabledbymodule == 2) $text.=$langs->trans("TriggerDisabledAsModuleDisabled",$module).'<br>';
  256. }
  257. $triggers[$j]['info'] = $form->textwithpicto('',$text);
  258. $j++;
  259. }
  260. return $triggers;
  261. }
  262. }
  263. ?>