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 >
2010-04-02 11:59:18 +02:00
* Copyright ( C ) 2010 Regis Houssin < regis @ dolibarr . fr >
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 .
*/
/**
2010-11-22 10:18:53 +01:00
* \file htdocs / core / class / interfaces . class . php
* \ingroup core
2010-04-02 11:59:18 +02:00
* \brief Fichier de la classe de gestion des triggers
* \version $Id $
*/
2005-04-21 20:49:15 +02:00
/**
2010-04-02 11:59:18 +02:00
* \class Interfaces
* \brief Classe de la gestion des triggers
*/
2005-04-21 20:49:15 +02:00
2010-04-22 00:14:11 +02:00
class Interfaces
2005-04-21 20:49:15 +02:00
{
2010-04-02 13:32:34 +02:00
var $dir ; // Directory with all core and external triggers files
var $errors = array (); // Array for errors
2009-01-21 15:09:42 +01:00
2010-04-02 11:59:18 +02:00
/**
2010-04-27 13:08:07 +02:00
* \brief Constructor .
* \param DB Database handler
2006-12-24 16:11:56 +01:00
*/
function Interfaces ( $DB )
{
$this -> db = $DB ;
}
2009-01-21 15:09:42 +01:00
2010-04-02 11:59:18 +02: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 .
2010-04-27 13:08:07 +02:00
* \param action Trigger event code
2006-12-24 16:11:56 +01:00
* \param object Objet concern
* \param user Objet user
* \param lang Objet lang
* \param conf Objet conf
2010-04-27 13:08:07 +02:00
* \return int Nb of triggers ran if no error , - Nb of triggers with errors otherwise .
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
2010-04-27 13:08:07 +02:00
if ( ! is_object ( $object ) || ! is_object ( $conf )) // Error
{
2010-05-12 15:14:26 +02:00
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_ERROR );
2010-04-27 13:08:07 +02:00
return - 1 ;
}
if ( ! is_object ( $user ) || ! is_object ( $langs )) // Warning
2009-08-10 19:46:47 +02:00
{
2010-05-12 15:14:26 +02:00
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 );
2009-08-10 19:46:47 +02:00
}
2010-04-22 22:55:28 +02:00
2010-12-20 11:45:00 +01:00
foreach ( $conf -> triggers_modules as $reldir )
2006-12-24 16:11:56 +01:00
{
2010-12-20 11:45:00 +01:00
$dir = dol_buildpath ( $reldir , 0 );
//print "xx".$dir;exit;
2010-04-02 13:56:23 +02:00
// Check if directory exists
if ( ! is_dir ( $dir )) continue ;
2009-01-21 15:09:42 +01:00
2010-04-02 13:56:23 +02:00
$handle = opendir ( $dir );
$modules = array ();
$nbfile = $nbtotal = $nbok = $nbko = 0 ;
2010-12-15 19:15:08 +01:00
if ( is_resource ( $handle ))
{
while (( $file = readdir ( $handle )) !== false )
{
if ( is_readable ( $dir . " / " . $file ) && preg_match ( '/^interface_([^_]+)_(.+)\.class\.php$/i' , $file , $reg ))
{
$nbfile ++ ;
2010-04-22 22:55:28 +02:00
2010-12-15 19:15:08 +01:00
$modName = " Interface " . ucfirst ( $reg [ 2 ]);
//print "file=$file"; print "modName=$modName"; exit;
if ( in_array ( $modName , $modules ))
{
$langs -> load ( " errors " );
dol_syslog ( " Interface::run_triggers action= " . $action . " " . $langs -> trans ( " ErrorDuplicateTrigger " , $modName , " /htdocs/includes/triggers/ " ), LOG_ERR );
continue ;
}
2010-04-22 22:55:28 +02:00
2010-12-15 19:15:08 +01:00
// Check if trigger file is disabled by name
if ( preg_match ( '/NORUN$/i' , $file ))
{
continue ;
}
// Check if trigger file is for a particular module
$qualified = true ;
if ( strtolower ( $reg [ 1 ]) != 'all' )
{
$module = preg_replace ( '/^mod/i' , '' , $reg [ 1 ]);
$constparam = 'MAIN_MODULE_' . strtoupper ( $module );
if ( empty ( $conf -> global -> $constparam )) $qualified = false ;
}
2010-04-22 22:55:28 +02:00
2010-12-15 19:15:08 +01:00
if ( ! $qualified )
{
dol_syslog ( " Interfaces::run_triggers action= " . $action . " Triggers for file ' " . $file . " ' need module to be enabled " , LOG_INFO );
continue ;
}
2010-04-22 22:55:28 +02:00
2010-12-15 19:15:08 +01:00
include_once ( $dir . " / " . $file );
$objMod = new $modName ( $this -> db );
$i = 0 ;
if ( $objMod )
{
// Bypass if workflow module is enabled and if the trigger asked to be disable in such case
if ( $conf -> workflow -> enabled && ! empty ( $objMod -> disabled_if_workflow ))
{
dol_syslog ( " Interfaces::run_triggers action= " . $action . " Bypass triggers for file ' " . $file . " ' " , LOG_INFO );
continue ;
}
2010-04-22 22:55:28 +02:00
2010-12-15 19:15:08 +01:00
dol_syslog ( " Interfaces::run_triggers action= " . $action . " Launch triggers for file ' " . $file . " ' " , LOG_INFO );
2010-04-22 22:55:28 +02:00
2010-12-15 19:15:08 +01:00
$modules [ $i ] = $modName ;
//dol_syslog("Interfaces::run_triggers Launch triggers for file '".$file."'",LOG_INFO);
$result = $objMod -> run_trigger ( $action , $object , $user , $langs , $conf );
if ( $result > 0 )
{
// Action OK
$nbtotal ++ ;
$nbok ++ ;
}
if ( $result == 0 )
{
// Aucune action faite
$nbtotal ++ ;
}
if ( $result < 0 )
{
// Action KO
$nbtotal ++ ;
$nbko ++ ;
$this -> errors [] = $objMod -> error ;
}
$i ++ ;
}
else
{
dol_syslog ( " Interfaces::run_triggers action= " . $action . " Failed to instantiate trigger for file ' " . $file . " ' " , LOG_ERROR );
}
}
}
closedir ( $handle );
}
2006-12-24 16:11:56 +01:00
}
2010-04-22 22:55:28 +02:00
2007-02-11 17:32:29 +01:00
if ( $nbko )
{
2010-05-12 15:14:26 +02:00
dol_syslog ( " Interfaces::run_triggers action= " . $action . " 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
}
2010-04-22 22:55:28 +02:00
2010-04-22 20:37:42 +02:00
/**
2010-12-20 11:45:00 +01:00
* Return list of triggers . Function used by admin page htdoc / admin / triggers
* @ param workflow 0 = Return all triggers , 1 = Return only triggers not disabled if workflow module activated
* @ return array Array list of triggers
2010-04-22 20:37:42 +02:00
*/
function getTriggersList ( $workflow = 0 )
{
global $conf , $langs ;
2010-04-22 22:55:28 +02:00
2010-04-22 20:37:42 +02:00
$html = new Form ( $db );
2010-04-22 22:55:28 +02:00
2010-04-22 20:37:42 +02:00
$files = array ();
$modules = array ();
$orders = array ();
$i = 0 ;
2010-04-22 22:55:28 +02:00
2010-12-20 11:45:00 +01:00
foreach ( $conf -> triggers_modules as $reldir )
2010-04-22 20:37:42 +02:00
{
2010-12-20 11:45:00 +01:00
$dir = dol_buildpath ( $reldir , 0 );
//print "xx".$dir;exit;
2010-04-22 20:37:42 +02:00
// Check if directory exists
if ( ! is_dir ( $dir )) continue ;
2010-04-22 22:55:28 +02:00
2010-04-22 20:37:42 +02:00
$handle = opendir ( $dir );
2010-12-15 19:15:08 +01:00
if ( is_resource ( $handle ))
{
while (( $file = readdir ( $handle )) !== false )
{
if ( is_readable ( $dir . '/' . $file ) && preg_match ( '/^interface_([^_]+)_(.+)\.class\.php/' , $file , $reg ))
{
$modName = 'Interface' . ucfirst ( $reg [ 2 ]);
//print "file=$file"; print "modName=$modName"; exit;
if ( in_array ( $modName , $modules ))
{
$langs -> load ( " errors " );
print '<div class="error">' . $langs -> trans ( " Error " ) . ' : ' . $langs -> trans ( " ErrorDuplicateTrigger " , $modName , " /htdocs/includes/triggers/ " ) . '</div>' ;
$objMod = new $modName ( $db );
2010-04-22 22:55:28 +02:00
2010-12-15 19:15:08 +01:00
$modules [ $i ] = $modName ;
$files [ $i ] = $file ;
$orders [ $i ] = $objMod -> family ; // Tri par famille
$i ++ ;
}
else
{
include_once ( $dir . '/' . $file );
$objMod = new $modName ( $db );
2010-04-22 22:55:28 +02:00
2010-12-15 19:15:08 +01:00
$modules [ $i ] = $modName ;
$files [ $i ] = $file ;
$orders [ $i ] = $objMod -> family ; // Tri par famille
$i ++ ;
}
}
}
closedir ( $handle );
}
2010-04-22 20:37:42 +02:00
}
2010-04-22 22:55:28 +02:00
2010-04-22 20:37:42 +02:00
asort ( $orders );
2010-04-22 22:55:28 +02:00
2010-04-22 20:37:42 +02:00
$triggers = array ();
$j = 0 ;
2010-04-22 22:55:28 +02:00
2010-04-22 20:37:42 +02:00
// Loop on each trigger
foreach ( $orders as $key => $value )
{
$modName = $modules [ $key ];
if ( $modName )
{
$objMod = new $modName ( $db );
// Bypass if workflow module is enabled and if the trigger is compatible
2010-04-23 20:23:02 +02:00
if ( $workflow && ! empty ( $objMod -> disabled_if_workflow )) continue ;
2010-04-22 20:37:42 +02:00
}
2010-04-22 22:55:28 +02:00
2010-04-22 20:37:42 +02:00
// Define disabledbyname and disabledbymodule
$disabledbyname = 0 ;
$disabledbymodule = 1 ;
$module = '' ;
if ( preg_match ( '/NORUN$/i' , $files [ $key ])) $disabledbyname = 1 ;
if ( preg_match ( '/^interface_([^_]+)_(.+)\.class\.php/i' , $files [ $key ], $reg ))
{
// Check if trigger file is for a particular module
$module = preg_replace ( '/^mod/i' , '' , $reg [ 1 ]);
$constparam = 'MAIN_MODULE_' . strtoupper ( $module );
if ( strtolower ( $reg [ 1 ]) == 'all' ) $disabledbymodule = 0 ;
else if ( empty ( $conf -> global -> $constparam )) $disabledbymodule = 2 ;
}
2010-04-22 22:55:28 +02:00
2010-04-22 20:37:42 +02:00
$triggers [ $j ][ 'picto' ] = $objMod -> picto ? img_object ( '' , $objMod -> picto ) : img_object ( '' , 'generic' );
$triggers [ $j ][ 'file' ] = $files [ $key ];
$triggers [ $j ][ 'version' ] = $objMod -> getVersion ();
$triggers [ $j ][ 'status' ] = img_tick ();
if ( $disabledbyname > 0 || $disabledbymodule > 1 ) $triggers [ $j ][ 'status' ] = " " ;
2010-04-22 22:55:28 +02:00
2010-04-22 20:37:42 +02:00
$text = '<b>' . $langs -> trans ( " Description " ) . ':</b><br>' ;
$text .= $objMod -> getDesc () . '<br>' ;
$text .= '<br><b>' . $langs -> trans ( " Status " ) . ':</b><br>' ;
if ( $disabledbyname == 1 )
{
$text .= $langs -> trans ( " TriggerDisabledByName " ) . '<br>' ;
if ( $disabledbymodule == 2 ) $text .= $langs -> trans ( " TriggerDisabledAsModuleDisabled " , $module ) . '<br>' ;
}
else
{
if ( $disabledbymodule == 0 ) $text .= $langs -> trans ( " TriggerAlwaysActive " ) . '<br>' ;
if ( $disabledbymodule == 1 ) $text .= $langs -> trans ( " TriggerActiveAsModuleActive " , $module ) . '<br>' ;
if ( $disabledbymodule == 2 ) $text .= $langs -> trans ( " TriggerDisabledAsModuleDisabled " , $module ) . '<br>' ;
}
2010-04-22 22:55:28 +02:00
2010-04-22 20:37:42 +02:00
$triggers [ $j ][ 'info' ] = $html -> textwithpicto ( '' , $text );
$j ++ ;
}
return $triggers ;
}
2010-04-02 11:59:18 +02:00
2005-04-21 20:49:15 +02:00
}
?>