dolibarr  9.0.0
interfaces.class.php
Go to the documentation of this file.
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.houssin@inodbox.com>
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 3 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 
26 require_once DOL_DOCUMENT_ROOT.'/core/triggers/dolibarrtriggers.class.php';
27 
28 
33 {
37  public $db;
38 
39  var $dir; // Directory with all core and external triggers files
40 
44  public $errors = array();
45 
51  function __construct($db)
52  {
53  $this->db = $db;
54  }
55 
56  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
68  function run_triggers($action,$object,$user,$langs,$conf)
69  {
70  // phpcs:enable
71  // Check parameters
72  if (! is_object($object) || ! is_object($conf)) // Error
73  {
74  $this->error='function run_triggers called with wrong parameters action='.$action.' object='.is_object($object).' user='.is_object($user).' langs='.is_object($langs).' conf='.is_object($conf);
75  dol_syslog(get_class($this).'::run_triggers '.$this->error, LOG_ERR);
76  $this->errors[]=$this->error;
77  return -1;
78  }
79  if (! is_object($langs)) // Warning
80  {
81  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);
82  }
83  if (! is_object($user)) // Warning
84  {
85  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);
86  global $db;
87  $user = new User($db);
88  }
89  //dol_syslog(get_class($this)."::run_triggers action=".$action." Launch run_triggers", LOG_DEBUG);
90 
91  $nbfile = $nbtotal = $nbok = $nbko = 0;
92 
93  $files = array();
94  $modules = array();
95  $orders = array();
96  $i=0;
97 
98 
99  $dirtriggers=array_merge(array('/core/triggers'),$conf->modules_parts['triggers']);
100  foreach($dirtriggers as $reldir)
101  {
102  $dir=dol_buildpath($reldir,0);
103  $newdir=dol_osencode($dir);
104  //print "xx".$dir;exit;
105 
106  // Check if directory exists (we do not use dol_is_dir to avoir loading files.lib.php at each call)
107  if (! is_dir($newdir)) continue;
108 
109  $handle=opendir($newdir);
110  if (is_resource($handle))
111  {
112  while (($file = readdir($handle))!==false)
113  {
114  if (is_readable($newdir."/".$file) && preg_match('/^interface_([0-9]+)_([^_]+)_(.+)\.class\.php$/i',$file,$reg))
115  {
116  $part1=$reg[1];
117  $part2=$reg[2];
118  $part3=$reg[3];
119 
120  $nbfile++;
121 
122  // Check if trigger file is disabled by name
123  if (preg_match('/NORUN$/i',$file)) continue;
124  // Check if trigger file is for a particular module
125  $qualified=true;
126  if (strtolower($reg[2]) != 'all')
127  {
128  $module=preg_replace('/^mod/i','',$reg[2]);
129  $constparam='MAIN_MODULE_'.strtoupper($module);
130  if (empty($conf->global->$constparam)) $qualified=false;
131  }
132 
133  if (! $qualified)
134  {
135  //dol_syslog(get_class($this)."::run_triggers action=".$action." Triggers for file '".$file."' need module to be enabled", LOG_DEBUG);
136  continue;
137  }
138 
139  $modName = "Interface".ucfirst($reg[3]);
140  //print "file=$file - modName=$modName\n";
141  if (in_array($modName,$modules)) // $modules = list of modName already loaded
142  {
143  $langs->load("errors");
144  dol_syslog(get_class($this)."::run_triggers action=".$action." ".$langs->trans("ErrorDuplicateTrigger", $newdir."/".$file, $fullpathfiles[$modName]), LOG_WARNING);
145  continue;
146  }
147 
148  try {
149  //print 'Todo for '.$modName." : ".$newdir.'/'.$file."\n";
150  include_once $newdir.'/'.$file;
151  //print 'Done for '.$modName."\n";
152  }
153  catch(Exception $e)
154  {
155  dol_syslog('ko for '.$modName." ".$e->getMessage()."\n", LOG_ERR);
156  }
157 
158  $modules[$i] = $modName;
159  $files[$i] = $file;
160  $fullpathfiles[$modName] = $newdir.'/'.$file;
161  $orders[$i] = $part1.'_'.$part2.'_'.$part3; // Set sort criteria value
162 
163  $i++;
164  }
165  }
166  }
167  }
168 
169  asort($orders);
170 
171  // Loop on each trigger
172  foreach ($orders as $key => $value)
173  {
174  $modName = $modules[$key];
175  if (empty($modName)) continue;
176 
177  $objMod = new $modName($this->db);
178  if ($objMod)
179  {
180  $result=0;
181 
182  if (method_exists($objMod, 'runTrigger')) // New method to implement
183  {
184  //dol_syslog(get_class($this)."::run_triggers action=".$action." Launch runTrigger for file '".$files[$key]."'", LOG_DEBUG);
185  $result=$objMod->runTrigger($action,$object,$user,$langs,$conf);
186  }
187  elseif (method_exists($objMod, 'run_trigger')) // Deprecated method
188  {
189  dol_syslog(get_class($this)."::run_triggers action=".$action." Launch old method run_trigger (rename your trigger into runTrigger) for file '".$files[$key]."'", LOG_WARNING);
190  $result=$objMod->run_trigger($action,$object,$user,$langs,$conf);
191  }
192  else
193  {
194  dol_syslog(get_class($this)."::run_triggers action=".$action." A trigger was declared for class ".get_class($objMod)." but method runTrigger was not found", LOG_ERR);
195  }
196 
197  if ($result > 0)
198  {
199  // Action OK
200  $nbtotal++;
201  $nbok++;
202  }
203  if ($result == 0)
204  {
205  // Aucune action faite
206  $nbtotal++;
207  }
208  if ($result < 0)
209  {
210  // Action KO
211  //dol_syslog("Error in trigger ".$action." - Nb of error string returned = ".count($objMod->errors), LOG_ERR);
212  $nbtotal++;
213  $nbko++;
214  if (! empty($objMod->errors)) $this->errors=array_merge($this->errors,$objMod->errors);
215  else if (! empty($objMod->error)) $this->errors[]=$objMod->error;
216  //dol_syslog("Error in trigger ".$action." - Nb of error string returned = ".count($this->errors), LOG_ERR);
217  }
218  }
219  else
220  {
221  dol_syslog(get_class($this)."::run_triggers action=".$action." Failed to instantiate trigger for file '".$files[$key]."'", LOG_ERR);
222  }
223  }
224 
225  if ($nbko)
226  {
227  dol_syslog(get_class($this)."::run_triggers action=".$action." Files found: ".$nbfile.", Files launched: ".$nbtotal.", Done: ".$nbok.", Failed: ".$nbko." - Nb of error string returned in this->errors = ".count($this->errors), LOG_ERR);
228  return -$nbko;
229  }
230  else
231  {
232  //dol_syslog(get_class($this)."::run_triggers Files found: ".$nbfile.", Files launched: ".$nbtotal.", Done: ".$nbok.", Failed: ".$nbko, LOG_DEBUG);
233  return $nbok;
234  }
235  }
236 
244  function getTriggersList($forcedirtriggers=null)
245  {
246  global $conf, $langs, $db;
247 
248  $files = array();
249  $fullpath = array();
250  $relpath = array();
251  $iscoreorexternal = array();
252  $modules = array();
253  $orders = array();
254  $i = 0;
255 
256  $dirtriggers=array_merge(array('/core/triggers/'),$conf->modules_parts['triggers']);
257  if (is_array($forcedirtriggers))
258  {
259  $dirtriggers=$forcedirtriggers;
260  }
261 
262  foreach($dirtriggers as $reldir)
263  {
264  $dir=dol_buildpath($reldir,0);
265  $newdir=dol_osencode($dir);
266 
267  // Check if directory exists (we do not use dol_is_dir to avoid loading files.lib.php at each call)
268  if (! is_dir($newdir)) continue;
269 
270  $handle=opendir($newdir);
271  if (is_resource($handle))
272  {
273  while (($file = readdir($handle))!==false)
274  {
275  if (is_readable($newdir.'/'.$file) && preg_match('/^interface_([0-9]+)_([^_]+)_(.+)\.class\.php/',$file,$reg))
276  {
277  if (preg_match('/\.back$/',$file)) continue;
278 
279  $part1=$reg[1];
280  $part2=$reg[2];
281  $part3=$reg[3];
282 
283  $modName = 'Interface'.ucfirst($reg[3]);
284  //print "file=$file"; print "modName=$modName"; exit;
285  if (in_array($modName,$modules))
286  {
287  $langs->load("errors");
288  print '<div class="error">'.$langs->trans("Error").' : '.$langs->trans("ErrorDuplicateTrigger",$modName,"/htdocs/core/triggers/").'</div>';
289  }
290  else
291  {
292  include_once $newdir.'/'.$file;
293  }
294 
295  $files[$i] = $file;
296  $fullpath[$i] = $dir.'/'.$file;
297  $relpath[$i] = preg_replace('/^\//','',$reldir).'/'.$file;
298  $iscoreorexternal[$i] = ($reldir == '/core/triggers/'?'internal':'external');
299  $modules[$i] = $modName;
300  $orders[$i] = $part1.'_'.$part2.'_'.$part3; // Set sort criteria value
301 
302  $i++;
303  }
304  }
305  closedir($handle);
306  }
307  }
308 
309  asort($orders);
310 
311  $triggers = array();
312  $j = 0;
313 
314  // Loop on each trigger
315  foreach ($orders as $key => $value)
316  {
317  $modName = $modules[$key];
318  if (empty($modName)) continue;
319 
320  if (! class_exists($modName))
321  {
322  print 'Error: A trigger file was found but its class "'.$modName.'" was not found.'."<br>\n";
323  continue;
324  }
325 
326  $objMod = new $modName($db);
327 
328  // Define disabledbyname and disabledbymodule
329  $disabledbyname=0;
330  $disabledbymodule=1;
331  $module='';
332 
333  // Check if trigger file is disabled by name
334  if (preg_match('/NORUN$/i',$files[$key])) $disabledbyname=1;
335  // Check if trigger file is for a particular module
336  if (preg_match('/^interface_([0-9]+)_([^_]+)_(.+)\.class\.php/i',$files[$key],$reg))
337  {
338  $module=preg_replace('/^mod/i','',$reg[2]);
339  $constparam='MAIN_MODULE_'.strtoupper($module);
340  if (strtolower($module) == 'all') $disabledbymodule=0;
341  else if (empty($conf->global->$constparam)) $disabledbymodule=2;
342  $triggers[$j]['module']=strtolower($module);
343  }
344 
345  // We set info of modules
346  $triggers[$j]['picto'] = $objMod->picto?img_object('',$objMod->picto):img_object('','generic');
347  $triggers[$j]['file'] = $files[$key];
348  $triggers[$j]['fullpath'] = $fullpath[$key];
349  $triggers[$j]['relpath'] = $relpath[$key];
350  $triggers[$j]['iscoreorexternal'] = $iscoreorexternal[$key];
351  $triggers[$j]['version'] = $objMod->getVersion();
352  $triggers[$j]['status'] = img_picto($langs->trans("Active"),'tick');
353  if ($disabledbyname > 0 || $disabledbymodule > 1) $triggers[$j]['status'] = '';
354 
355  $text ='<b>'.$langs->trans("Description").':</b><br>';
356  $text.=$objMod->getDesc().'<br>';
357  $text.='<br><b>'.$langs->trans("Status").':</b><br>';
358  if ($disabledbyname == 1)
359  {
360  $text.=$langs->trans("TriggerDisabledByName").'<br>';
361  if ($disabledbymodule == 2) $text.=$langs->trans("TriggerDisabledAsModuleDisabled",$module).'<br>';
362  }
363  else
364  {
365  if ($disabledbymodule == 0) $text.=$langs->trans("TriggerAlwaysActive").'<br>';
366  if ($disabledbymodule == 1) $text.=$langs->trans("TriggerActiveAsModuleActive",$module).'<br>';
367  if ($disabledbymodule == 2) $text.=$langs->trans("TriggerDisabledAsModuleDisabled",$module).'<br>';
368  }
369 
370  $triggers[$j]['info'] = $text;
371  $j++;
372  }
373  return $triggers;
374  }
375 }
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
print
Draft customers invoices.
Definition: index.php:91
Class to manage Dolibarr users.
Definition: user.class.php:41
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
__construct($db)
Constructor.
run_triggers($action, $object, $user, $langs, $conf)
Function called when a Dolibarr business event occurs This function call all qualified triggers...
getTriggersList($forcedirtriggers=null)
Return list of triggers.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='')
Show picto whatever it&#39;s its name (generic function)
Class to manage triggers.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)