2005-04-21 20:49:15 +02:00
< ? php
2009-01-21 15:09:42 +01:00
/* Copyright ( C ) 2005 - 2009 Laurent Destailleur < eldy @ users . sourceforge . net >
2007-10-13 01:48:20 +02:00
* Copyright ( C ) 2006 Rodolphe Quiedeville < rodolphe @ quiedeville . org >
2005-04-21 20:49:15 +02:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 2 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program ; if not , write to the Free Software
* Foundation , Inc . , 59 Temple Place - Suite 330 , Boston , MA 02111 - 1307 , USA .
*/
/**
2008-01-29 21:34:58 +01:00
\file htdocs / interfaces . class . php
\ingroup core
\brief Fichier de la classe de gestion des triggers
\version $Id $
2005-04-21 20:49:15 +02:00
*/
/**
2006-12-04 12:35:55 +01:00
\class Interfaces
\brief Classe de la gestion des triggers
2005-04-21 20:49:15 +02:00
*/
class Interfaces
{
2007-02-11 17:32:29 +01:00
var $dir ; // Directory with all trigger files
var $errors = array (); // Array for errors
2009-01-21 15:09:42 +01:00
2006-12-24 16:11:56 +01:00
/**
* \brief Constructeur .
2009-06-07 11:36:18 +02:00
* \param DB handler d ' acces base
2006-12-24 16:11:56 +01:00
*/
function Interfaces ( $DB )
{
$this -> db = $DB ;
$this -> dir = DOL_DOCUMENT_ROOT . " /includes/triggers " ;
}
2009-01-21 15:09:42 +01:00
2006-12-24 16:11:56 +01:00
/**
2009-06-07 11:36:18 +02:00
* \brief Fonction appelee lors du declenchement d ' un evenement Dolibarr .
* Cette fonction declenche tous les triggers trouves actifs .
2006-12-24 16:11:56 +01:00
* \param action Code de l ' evenement
* \param object Objet concern
* \param user Objet user
* \param lang Objet lang
* \param conf Objet conf
2007-12-27 22:18:24 +01:00
* \return int Nb triggers ayant agit si pas d ' erreurs , - Nb en erreur sinon .
2006-12-24 16:11:56 +01:00
*/
2008-02-28 21:37:04 +01:00
function run_triggers ( $action , $object , $user , $langs , $conf )
2006-12-24 16:11:56 +01:00
{
2009-08-10 19:46:47 +02:00
// Check parameters
if ( ! is_object ( $object ) || ! is_object ( $user ) || ! is_object ( $langs ) || ! is_object ( $conf ))
{
dol_syslog ( 'interface::run_triggers was called with wrong parameters object=' . is_object ( $object ) . ' user=' . is_object ( $user ) . ' langs=' . is_object ( $langs ) . ' conf=' . is_object ( $conf ), LOG_WARNING );
}
2006-12-24 16:11:56 +01:00
$handle = opendir ( $this -> dir );
$modules = array ();
2007-12-27 22:18:24 +01:00
$nbfile = $nbtotal = $nbok = $nbko = 0 ;
2009-01-21 15:09:42 +01:00
2006-12-24 16:11:56 +01:00
while (( $file = readdir ( $handle )) !== false )
{
2009-10-24 08:10:00 +02:00
if ( is_readable ( $this -> dir . " / " . $file ) && preg_match ( '/^interface_([^_]+)_(.+)\.class\.php$/i' , $file , $reg ))
2006-12-24 16:11:56 +01:00
{
2007-12-27 22:18:24 +01:00
$nbfile ++ ;
2009-01-21 15:09:42 +01:00
2007-12-27 22:18:24 +01:00
$modName = " Interface " . ucfirst ( $reg [ 2 ]);
2006-12-24 16:11:56 +01:00
//print "file=$file"; print "modName=$modName"; exit;
2007-12-27 22:18:24 +01:00
if ( in_array ( $modName , $modules ))
{
$langs -> load ( " errors " );
2009-02-20 23:53:15 +01:00
dol_syslog ( " Interface::run_triggers " . $langs -> trans ( " ErrorDuplicateTrigger " , $modName , " /htdocs/includes/triggers/ " ), LOG_ERR );
2007-12-27 22:18:24 +01:00
continue ;
}
// Check if trigger file is disabled by name
2009-10-21 16:02:14 +02:00
if ( preg_match ( '/NORUN$/i' , $file ))
2006-12-24 16:11:56 +01:00
{
2007-12-27 22:18:24 +01:00
continue ;
}
// Check if trigger file is for a particular module
$qualified = true ;
if ( strtolower ( $reg [ 1 ]) != 'all' )
{
2009-10-21 16:02:14 +02:00
$module = preg_replace ( '/^mod/i' , '' , $reg [ 1 ]);
2007-12-27 22:18:24 +01:00
$constparam = 'MAIN_MODULE_' . strtoupper ( $module );
if ( empty ( $conf -> global -> $constparam )) $qualified = false ;
}
2009-01-21 15:09:42 +01:00
2007-12-27 22:18:24 +01:00
if ( ! $qualified )
{
2009-02-20 23:53:15 +01:00
dol_syslog ( " Interfaces::run_triggers Triggers for file ' " . $file . " ' need module to be enabled " , LOG_INFO );
2007-12-27 22:18:24 +01:00
continue ;
}
2008-11-11 20:32:48 +01:00
2009-02-20 23:53:15 +01:00
dol_syslog ( " Interfaces::run_triggers Launch triggers for file ' " . $file . " ' " , LOG_INFO );
2007-12-27 22:18:24 +01:00
include_once ( $this -> dir . " / " . $file );
$objMod = new $modName ( $this -> db );
2009-01-21 15:09:42 +01:00
$i = 0 ;
2007-12-27 22:18:24 +01:00
if ( $objMod )
{
$modules [ $i ] = $modName ;
2009-02-20 23:53:15 +01:00
//dol_syslog("Interfaces::run_triggers Launch triggers for file '".$file."'",LOG_INFO);
2008-02-28 21:37:04 +01:00
$result = $objMod -> run_trigger ( $action , $object , $user , $langs , $conf );
2007-12-27 22:18:24 +01:00
if ( $result > 0 )
{
// Action OK
$nbtotal ++ ;
$nbok ++ ;
}
if ( $result == 0 )
2006-12-24 16:11:56 +01:00
{
2007-12-27 22:18:24 +01:00
// Aucune action faite
$nbtotal ++ ;
2006-12-24 16:11:56 +01:00
}
2007-12-27 22:18:24 +01:00
if ( $result < 0 )
2006-12-24 16:11:56 +01:00
{
2007-12-27 22:18:24 +01:00
// Action KO
$nbtotal ++ ;
$nbko ++ ;
$this -> errors [] = $objMod -> error ;
2006-12-24 16:11:56 +01:00
}
2007-12-27 22:18:24 +01:00
$i ++ ;
2006-12-24 16:11:56 +01:00
}
}
}
2007-02-11 17:32:29 +01:00
if ( $nbko )
{
2009-02-20 23:53:15 +01:00
dol_syslog ( " Interfaces::run_triggers Files found: " . $nbfile . " , Files launched: " . $nbtotal . " , Done: " . $nbok . " , Failed: " . $nbko , LOG_ERR );
2007-02-11 17:32:29 +01:00
return - $nbko ;
}
else
{
2009-02-20 23:53:15 +01:00
//dol_syslog("Interfaces::run_triggers Files found: ".$nbfile.", Files launched: ".$nbtotal.", Done: ".$nbok.", Failed: ".$nbko, LOG_DEBUG);
2007-02-11 17:32:29 +01:00
return $nbok ;
}
2009-01-21 15:09:42 +01:00
}
2005-04-21 20:49:15 +02:00
}
?>