2011-08-10 02:50:16 +02:00
< ? php
/* Copyright ( C ) 2006 - 2011 Laurent Destailleur < eldy @ users . sourceforge . net >
* Copyright ( C ) 2005 - 2011 Regis Houssin < regis @ dolibarr . fr >
* Copyright ( C ) 2010 - 2011 Juanjo Menent < jmenent @ 2 byte . es >
*
* 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 , see < http :// www . gnu . org / licenses />.
*/
/**
* \file htdocs / core / class / hookmanager . class . php
* \ingroup core
* \brief File of class to manage hooks
*/
/**
* \class HookManager
* \brief Class to manage hooks
*/
class HookManager
{
var $db ;
var $linkedObjectBlock ;
var $objectid ;
// Array with instantiated classes
var $hooks = array ();
/**
* Constructeur de la classe
* @ param DB Handler acces base de donnees
*/
function HookManager ( $DB )
{
$this -> db = $DB ;
}
/**
2011-08-10 21:34:44 +02:00
* Init array this -> hooks with instantiated controler
* A hook is declared by a module by adding a constant MAIN_MODULE_MYMODULENAME_HOOKS
2011-08-11 00:47:33 +02:00
* with value nameofhookkey1 : nameofhookkey2 :...: nameofhookkeyn .
* This add into conf -> hooks_module an entries ( 'modulename' => nameofhookkey )
* After this , this -> hooks is defined
2011-08-10 02:50:16 +02:00
* @ param arraytype Array list of hooked tab / features . For example : thirdpartytab , ...
* @ return int Always 1
*/
function callHooks ( $arraytype )
{
global $conf ;
// Test if ther is hooks to manage
if ( ! is_array ( $conf -> hooks_modules ) || empty ( $conf -> hooks_modules )) return ;
// For backward compatibility
if ( ! is_array ( $arraytype )) $arraytype = array ( $arraytype );
$i = 0 ;
foreach ( $conf -> hooks_modules as $module => $hooks )
{
if ( $conf -> $module -> enabled )
{
foreach ( $arraytype as $type )
{
if ( in_array ( $type , $hooks ))
{
2011-08-21 20:34:49 +02:00
$path = '/' . $module . '/class/' ;
2011-08-10 02:50:16 +02:00
$actionfile = 'actions_' . $module . '.class.php' ;
$pathroot = '' ;
$this -> hooks [ $i ][ 'type' ] = $type ;
// Include actions class overwriting hooks
$resaction = dol_include_once ( $path . $actionfile );
if ( $resaction )
{
$controlclassname = 'Actions' . ucfirst ( $module );
2011-08-11 00:47:33 +02:00
$actionInstance = new $controlclassname ( $this -> db );
$this -> hooks [ $i ][ 'modules' ][ $module ] = $actionInstance ;
2011-08-10 02:50:16 +02:00
}
2011-08-11 00:47:33 +02:00
2011-08-10 12:55:34 +02:00
// Include dataservice class (model)
// TODO storing dao is useless here. It's goal of controller to known which dao to manage
$daofile = 'dao_' . $module . '.class.php' ;
$resdao = dol_include_once ( $path . $daofile );
if ( $resdao )
{
// Instantiate dataservice class (model)
2011-08-11 00:47:33 +02:00
$daoInstance = 'Dao' . ucfirst ( $module );
$this -> hooks [ $i ][ 'modules' ][ $module ] -> object = new $daoInstance ( $this -> db );
2011-08-10 12:55:34 +02:00
}
2011-08-10 02:50:16 +02:00
$i ++ ;
}
}
}
}
return 1 ;
}
/**
* Execute hooks ( if the were initialized ) for the given method
* @ param method Method name to hook ( 'doActions' , 'printSearchForm' , ... )
2011-08-11 00:47:33 +02:00
* @ param parameters Array of parameters
* @ param action Action code on calling page ( 'create' , 'edit' , 'view' , 'add' , 'update' , 'delete' ... )
2011-08-10 02:50:16 +02:00
* @ param object Object to use hooks on
* @ param string For doActions , showInputField , showOutputField : Return 0 if we want to keep doing standard actions , > 0 if if want to stop standard actions , > 0 means KO .
* For printSearchForm , printLeftBlock : Return HTML string .
* $this -> error or this -> errors are also defined with hooks errors .
*/
2011-08-11 09:51:50 +02:00
function executeHooks ( $method , $parameters = false , & $object = '' , & $action = '' )
2011-08-10 02:50:16 +02:00
{
2011-08-10 12:55:34 +02:00
global $var ;
2011-08-11 00:47:33 +02:00
2011-08-10 02:50:16 +02:00
if ( ! is_array ( $this -> hooks ) || empty ( $this -> hooks )) return '' ;
2011-08-20 19:00:34 +02:00
dol_syslog ( get_class ( $this ) . '::executeHooks method=' . $method . " action= " . $action );
2011-08-10 02:50:16 +02:00
// Loop on each hook
$resaction = 0 ; $resprint = '' ;
foreach ( $this -> hooks as $hook )
{
2011-08-10 12:55:34 +02:00
if ( ! empty ( $hook [ 'modules' ]))
2011-08-10 02:50:16 +02:00
{
2011-08-11 00:47:33 +02:00
foreach ( $hook [ 'modules' ] as $module => $actioninstance )
2011-08-10 02:50:16 +02:00
{
2011-08-10 12:55:34 +02:00
$var =! $var ;
2011-08-11 00:47:33 +02:00
2011-08-10 02:50:16 +02:00
// Hooks that return int
2011-08-11 00:47:33 +02:00
if ( $method == 'doActions' && method_exists ( $actioninstance , $method ))
2011-08-10 02:50:16 +02:00
{
2011-08-18 09:05:13 +02:00
$resaction += $actioninstance -> doActions ( $parameters , $object , $action ); // action can be changed by method (to go back to other action for example), socid can be changed/set by method (during creation for example)
2011-09-17 21:49:50 +02:00
if ( $resaction < 0 || ! empty ( $actioninstance -> error ) || ( ! empty ( $actioninstance -> errors ) && count ( $actioninstance -> errors ) > 0 ))
2011-08-10 02:50:16 +02:00
{
2011-08-11 00:47:33 +02:00
$this -> error = $actioninstance -> error ; $this -> errors = $actioninstance -> errors ;
2011-08-10 02:50:16 +02:00
if ( $action == 'add' ) $action = 'create' ; // TODO this change must be inside the doActions
if ( $action == 'update' ) $action = 'edit' ; // TODO this change must be inside the doActions
}
}
2011-08-11 00:47:33 +02:00
else if ( $method == 'showInputFields' && method_exists ( $actioninstance , $method ))
2011-08-10 02:50:16 +02:00
{
2011-08-18 09:05:13 +02:00
$resaction += $actioninstance -> showInputFields ( $parameters , $object , $action ); // action can be changed by method (to go back to other action for example), socid can be changed/set by method (during creation for example)
2011-09-17 21:49:50 +02:00
if ( $resaction < 0 || ! empty ( $actioninstance -> error ) || ( ! empty ( $actioninstance -> errors ) && count ( $actioninstance -> errors ) > 0 ))
2011-08-10 02:50:16 +02:00
{
2011-08-11 00:47:33 +02:00
$this -> error = $actioninstance -> error ; $this -> errors = $actioninstance -> errors ;
2011-08-10 02:50:16 +02:00
}
}
2011-08-11 00:47:33 +02:00
else if ( $method == 'showOutputFields' && method_exists ( $actioninstance , $method ))
2011-08-10 02:50:16 +02:00
{
2011-08-18 09:05:13 +02:00
$resaction += $actioninstance -> showOutputFields ( $parameters , $object , $action ); // action can be changed by method (to go back to other action for example), socid can be changed/set by method (during creation for example)
2011-09-17 21:49:50 +02:00
if ( $resaction < 0 || ! empty ( $actioninstance -> error ) || ( ! empty ( $actioninstance -> errors ) && count ( $actioninstance -> errors ) > 0 ))
2011-08-10 02:50:16 +02:00
{
2011-08-11 00:47:33 +02:00
$this -> error = $actioninstance -> error ; $this -> errors = $actioninstance -> errors ;
2011-08-10 02:50:16 +02:00
}
}
2011-08-11 00:47:33 +02:00
// Generic hooks that return a string (printSearchForm, printLeftBlock, formBuilddocOptions, ...)
else if ( method_exists ( $actioninstance , $method ))
2011-08-10 12:55:34 +02:00
{
2011-08-11 11:26:34 +02:00
if ( is_array ( $parameters ) && $parameters [ 'special_code' ] > 3 && $parameters [ 'special_code' ] != $actioninstance -> module_number ) continue ;
2011-08-11 00:47:33 +02:00
$resprint .= $actioninstance -> $method ( $parameters , $object , $action , $this );
2011-08-10 12:55:34 +02:00
}
2011-08-10 02:50:16 +02:00
}
}
}
2011-08-10 19:40:42 +02:00
if ( $method == 'doActions' || $method == 'showInputFields' || $method == 'showOutputFields' ) return $resaction ;
2011-08-10 02:50:16 +02:00
return $resprint ;
}
}
?>