Ignore:
Timestamp:
Nov 1, 2006, 6:54:35 AM (18 years ago)
Author:
rvelices
Message:

plugins last modifications + events are triggered now from picture.php

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/functions_plugins.inc.php

    r1584 r1590  
    3636define('PHPWG_PLUGINS_PATH',PHPWG_ROOT_PATH.'plugins/');
    3737
     38define('EVENT_HANDLER_PRIORITY_NEUTRAL', 50);
     39
    3840/* Register a event handler.
    3941 * @param string $event the name of the event to listen to
    4042 * @param mixed $func the function that will handle the event
    41 */
    42 function add_event_handler($event, $func, $priority=50, $accepted_args=1)
    43 {
    44   global $pwg_event_handlers;
    45 
    46   if ( isset($pwg_event_handlers[$event]["$priority"]) )
    47   {
    48     foreach($pwg_event_handlers[$event]["$priority"] as $handler)
     43 * @param int $priority optional priority (greater priority will
     44 * be executed at last)
     45*/
     46function add_event_handler($event, $func,
     47    $priority=EVENT_HANDLER_PRIORITY_NEUTRAL, $accepted_args=1)
     48{
     49  global $pwg_event_handlers;
     50
     51  if ( isset($pwg_event_handlers[$event][$priority]) )
     52  {
     53    foreach($pwg_event_handlers[$event][$priority] as $handler)
    4954    {
    5055      if ( $handler['function'] == $func )
     
    5560  }
    5661
    57   trigger_event('add_event_handler',
    58       array('event'=>$event, 'function'=>$func)
    59     );
    60 
    61   $pwg_event_handlers[$event]["$priority"][] =
     62  $pwg_event_handlers[$event][$priority][] =
    6263    array(
    6364      'function'=>$func,
    6465      'accepted_args'=>$accepted_args);
    65 
     66  ksort( $pwg_event_handlers[$event] );
    6667  return true;
    6768}
    6869
     70/* Register a event handler.
     71 * @param string $event the name of the event to listen to
     72 * @param mixed $func the function that needs removal
     73 * @param int $priority optional priority (greater priority will
     74 * be executed at last)
     75*/
     76function remove_event_handler($event, $func,
     77   $priority=EVENT_HANDLER_PRIORITY_NEUTRAL)
     78{
     79  global $pwg_event_handlers;
     80
     81  if (!isset( $pwg_event_handlers[$event][$priority] ) )
     82  {
     83    return false;
     84  }
     85  for ($i=0; $i<count($pwg_event_handlers[$event][$priority]); $i++)
     86  {
     87    if ($pwg_event_handlers[$event][$priority][$i]['function']==$func)
     88    {
     89      unset($pwg_event_handlers[$event][$priority][$i]);
     90      $pwg_event_handlers[$event][$priority] =
     91        array_values($pwg_event_handlers[$event][$priority]);
     92
     93      if ( empty($pwg_event_handlers[$event][$priority]) )
     94      {
     95        unset( $pwg_event_handlers[$event][$priority] );
     96        if (empty( $pwg_event_handlers[$event] ) )
     97        {
     98          unset( $pwg_event_handlers[$event] );
     99        }
     100      }
     101      return true;
     102    }
     103  }
     104  return false;
     105}
    69106
    70107/* Triggers an event and calls all registered event handlers
     
    75112{
    76113  global $pwg_event_handlers;
    77   if ($event!='pre_trigger_event' and $event!='post_trigger_event')
    78   {// special case
    79     trigger_event('pre_trigger_event',
    80         array('event'=>$event, 'data'=>$data) );
    81     if ( !isset($pwg_event_handlers[$event]) )
    82     {
    83       trigger_event('post_trigger_event',
    84           array('event'=>$event, 'data'=>$data) );
    85     }
    86   }
     114
     115  // just for debugging
     116  trigger_action('pre_trigger_event',
     117        array('event'=>$event, 'data'=>$data) );
    87118
    88119  if ( !isset($pwg_event_handlers[$event]) )
    89120  {
     121    trigger_action('post_trigger_event',
     122        array('event'=>$event, 'data'=>$data) );
    90123    return $data;
    91124  }
     
    115148    }
    116149  }
    117 
    118   if ($event!='pre_trigger_event' and $event!='post_trigger_event')
    119   {
    120     trigger_event('post_trigger_event',
    121         array('event'=>$event, 'data'=>$data) );
    122   }
    123 
     150  trigger_action('post_trigger_event',
     151        array('event'=>$event, 'data'=>$data) );
    124152  return $data;
    125153}
    126154
    127155
     156function trigger_action($event, $data=null)
     157{
     158  global $pwg_event_handlers;
     159  if ($event!='pre_trigger_event'
     160    and $event!='post_trigger_event'
     161    and $event!='trigger_action')
     162  {// special case for debugging - avoid recursive calls
     163    trigger_action('trigger_action',
     164        array('event'=>$event, 'data'=>$data) );
     165  }
     166
     167  if ( !isset($pwg_event_handlers[$event]) )
     168  {
     169    return;
     170  }
     171  $args = array_slice(func_get_args(), 2);
     172
     173  foreach ($pwg_event_handlers[$event] as $priority => $handlers)
     174  {
     175    if ( !is_null($handlers) )
     176    {
     177      foreach($handlers as $handler)
     178      {
     179        $all_args = array_merge( array($data), $args);
     180        $function_name = $handler['function'];
     181        $accepted_args = $handler['accepted_args'];
     182
     183        if ( $accepted_args == 1 )
     184          $the_args = array($data);
     185        elseif ( $accepted_args > 1 )
     186          $the_args = array_slice($all_args, 0, $accepted_args);
     187        elseif ( $accepted_args == 0 )
     188          $the_args = NULL;
     189        else
     190          $the_args = $all_args;
     191
     192        call_user_func_array($function_name, $the_args);
     193      }
     194    }
     195  }
     196}
    128197
    129198
     
    174243  foreach( $plugins as $plugin)
    175244  {
    176     @include_once( PHPWG_PLUGINS_PATH.$plugin['id'].'/index.php' );
    177   }
    178   trigger_event('plugins_loaded');
     245    $file_name = PHPWG_PLUGINS_PATH.$plugin['id'].'/main.inc.php';
     246    if ( file_exists($file_name) )
     247    {
     248      include_once( $file_name );
     249    }
     250  }
     251  trigger_action('plugins_loaded');
    179252}
    180253?>
Note: See TracChangeset for help on using the changeset viewer.