From bce8b9f680af4eb5481441e52e1d82b11da722be Mon Sep 17 00:00:00 2001 From: rvelices Date: Wed, 1 Nov 2006 05:54:35 +0000 Subject: plugins last modifications + events are triggered now from picture.php git-svn-id: http://piwigo.org/svn/trunk@1590 68402e56-0260-453c-a942-63ccdbb3a9ee --- include/functions_plugins.inc.php | 123 ++++++++++++++++++++++++++++++-------- 1 file changed, 98 insertions(+), 25 deletions(-) (limited to 'include/functions_plugins.inc.php') diff --git a/include/functions_plugins.inc.php b/include/functions_plugins.inc.php index f381b0a89..e83f76df7 100644 --- a/include/functions_plugins.inc.php +++ b/include/functions_plugins.inc.php @@ -35,17 +35,22 @@ string. define('PHPWG_PLUGINS_PATH',PHPWG_ROOT_PATH.'plugins/'); +define('EVENT_HANDLER_PRIORITY_NEUTRAL', 50); + /* Register a event handler. * @param string $event the name of the event to listen to * @param mixed $func the function that will handle the event + * @param int $priority optional priority (greater priority will + * be executed at last) */ -function add_event_handler($event, $func, $priority=50, $accepted_args=1) +function add_event_handler($event, $func, + $priority=EVENT_HANDLER_PRIORITY_NEUTRAL, $accepted_args=1) { global $pwg_event_handlers; - if ( isset($pwg_event_handlers[$event]["$priority"]) ) + if ( isset($pwg_event_handlers[$event][$priority]) ) { - foreach($pwg_event_handlers[$event]["$priority"] as $handler) + foreach($pwg_event_handlers[$event][$priority] as $handler) { if ( $handler['function'] == $func ) { @@ -54,18 +59,50 @@ function add_event_handler($event, $func, $priority=50, $accepted_args=1) } } - trigger_event('add_event_handler', - array('event'=>$event, 'function'=>$func) - ); - - $pwg_event_handlers[$event]["$priority"][] = + $pwg_event_handlers[$event][$priority][] = array( 'function'=>$func, 'accepted_args'=>$accepted_args); - + ksort( $pwg_event_handlers[$event] ); return true; } +/* Register a event handler. + * @param string $event the name of the event to listen to + * @param mixed $func the function that needs removal + * @param int $priority optional priority (greater priority will + * be executed at last) +*/ +function remove_event_handler($event, $func, + $priority=EVENT_HANDLER_PRIORITY_NEUTRAL) +{ + global $pwg_event_handlers; + + if (!isset( $pwg_event_handlers[$event][$priority] ) ) + { + return false; + } + for ($i=0; $i$event, 'data'=>$data) ); - if ( !isset($pwg_event_handlers[$event]) ) - { - trigger_event('post_trigger_event', - array('event'=>$event, 'data'=>$data) ); - } - } if ( !isset($pwg_event_handlers[$event]) ) { + trigger_action('post_trigger_event', + array('event'=>$event, 'data'=>$data) ); return $data; } $args = array_slice(func_get_args(), 2); @@ -114,17 +147,53 @@ function trigger_event($event, $data=null) } } } + trigger_action('post_trigger_event', + array('event'=>$event, 'data'=>$data) ); + return $data; +} - if ($event!='pre_trigger_event' and $event!='post_trigger_event') - { - trigger_event('post_trigger_event', + +function trigger_action($event, $data=null) +{ + global $pwg_event_handlers; + if ($event!='pre_trigger_event' + and $event!='post_trigger_event' + and $event!='trigger_action') + {// special case for debugging - avoid recursive calls + trigger_action('trigger_action', array('event'=>$event, 'data'=>$data) ); } - return $data; -} + if ( !isset($pwg_event_handlers[$event]) ) + { + return; + } + $args = array_slice(func_get_args(), 2); + foreach ($pwg_event_handlers[$event] as $priority => $handlers) + { + if ( !is_null($handlers) ) + { + foreach($handlers as $handler) + { + $all_args = array_merge( array($data), $args); + $function_name = $handler['function']; + $accepted_args = $handler['accepted_args']; + if ( $accepted_args == 1 ) + $the_args = array($data); + elseif ( $accepted_args > 1 ) + $the_args = array_slice($all_args, 0, $accepted_args); + elseif ( $accepted_args == 0 ) + $the_args = NULL; + else + $the_args = $all_args; + + call_user_func_array($function_name, $the_args); + } + } + } +} /* Returns an array of plugins defined in the database @@ -173,8 +242,12 @@ function load_plugins() $plugins = get_db_plugins('active'); foreach( $plugins as $plugin) { - @include_once( PHPWG_PLUGINS_PATH.$plugin['id'].'/index.php' ); + $file_name = PHPWG_PLUGINS_PATH.$plugin['id'].'/main.inc.php'; + if ( file_exists($file_name) ) + { + include_once( $file_name ); + } } - trigger_event('plugins_loaded'); + trigger_action('plugins_loaded'); } ?> \ No newline at end of file -- cgit v1.2.3