- Timestamp:
- Feb 6, 2014, 2:02:22 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/functions_plugins.inc.php
r27221 r27238 173 173 * @param Callable $func the callback function 174 174 * @param int $priority greater priority will be executed at last 175 * @param string $include_path file to include before executing the callback 176 * @return bool false is handler already exists 175 177 */ 176 178 function add_event_handler($event, $func, 177 $priority=EVENT_HANDLER_PRIORITY_NEUTRAL, $ accepted_args=1)179 $priority=EVENT_HANDLER_PRIORITY_NEUTRAL, $include_path=null) 178 180 { 179 181 global $pwg_event_handlers; 180 182 181 if ( isset($pwg_event_handlers[$event][$priority]))182 { 183 foreach ($pwg_event_handlers[$event][$priority] as $handler)184 { 185 if ( $handler['function'] == $func)183 if (isset($pwg_event_handlers[$event][$priority])) 184 { 185 foreach ($pwg_event_handlers[$event][$priority] as $handler) 186 { 187 if ($handler['function'] == $func) 186 188 { 187 189 return false; … … 190 192 } 191 193 192 $pwg_event_handlers[$event][$priority][] = 193 array( 194 'function'=>$func, 195 'accepted_args'=>$accepted_args); 196 ksort( $pwg_event_handlers[$event] ); 194 $pwg_event_handlers[$event][$priority][] = array( 195 'function' => $func, 196 'include_path' => is_string($include_path) ? $include_path : null, 197 ); 198 199 ksort($pwg_event_handlers[$event]); 197 200 return true; 198 201 } … … 211 214 global $pwg_event_handlers; 212 215 213 if (!isset( $pwg_event_handlers[$event][$priority] ))216 if (!isset($pwg_event_handlers[$event][$priority])) 214 217 { 215 218 return false; … … 223 226 array_values($pwg_event_handlers[$event][$priority]); 224 227 225 if ( empty($pwg_event_handlers[$event][$priority]))226 { 227 unset( $pwg_event_handlers[$event][$priority]);228 if (empty( $pwg_event_handlers[$event] ))228 if (empty($pwg_event_handlers[$event][$priority])) 229 { 230 unset($pwg_event_handlers[$event][$priority]); 231 if (empty($pwg_event_handlers[$event])) 229 232 { 230 unset( $pwg_event_handlers[$event]);233 unset($pwg_event_handlers[$event]); 231 234 } 232 235 } … … 265 268 global $pwg_event_handlers; 266 269 267 if ( isset($pwg_event_handlers['trigger']))268 {// just fordebugging270 if (isset($pwg_event_handlers['trigger'])) 271 {// debugging 269 272 trigger_action('trigger', 270 array('type'=>'event', 'event'=>$event, 'data'=>$data) ); 271 } 272 273 if ( !isset($pwg_event_handlers[$event]) ) 273 array('type'=>'event', 'event'=>$event, 'data'=>$data) 274 ); 275 } 276 277 if (!isset($pwg_event_handlers[$event])) 274 278 { 275 279 return $data; … … 279 283 foreach ($pwg_event_handlers[$event] as $priority => $handlers) 280 284 { 281 foreach($handlers as $handler) 282 { 283 $function_name = $handler['function']; 284 $accepted_args = $handler['accepted_args']; 285 foreach ($handlers as $handler) 286 { 285 287 $args[1] = $data; 286 $data = call_user_func_array($function_name, array_slice($args,1,$accepted_args) ); 287 } 288 } 289 trigger_action('trigger', 290 array('type'=>'post_event', 'event'=>$event, 'data'=>$data) ); 288 289 if (!empty($handler['include_path'])) 290 { 291 include_once($handler['include_path']); 292 } 293 294 $data = call_user_func_array($handler['function'], array_slice($args, 1)); 295 } 296 } 297 298 if (isset($pwg_event_handlers['trigger'])) 299 {// debugging 300 trigger_action('trigger', 301 array('type'=>'post_event', 'event'=>$event, 'data'=>$data) 302 ); 303 } 304 291 305 return $data; 292 306 } … … 315 329 { 316 330 global $pwg_event_handlers; 317 if ( isset($pwg_event_handlers['trigger']) and $event!='trigger' ) 318 {// special case for debugging - avoid recursive calls 331 332 if (isset($pwg_event_handlers['trigger']) and $event!='trigger') 333 {// debugging - avoid recursive calls 319 334 trigger_action('trigger', 320 array('type'=>'action', 'event'=>$event, 'data'=>null) ); 321 } 322 323 if ( !isset($pwg_event_handlers[$event]) ) 335 array('type'=>'action', 'event'=>$event, 'data'=>null) 336 ); 337 } 338 339 if (!isset($pwg_event_handlers[$event])) 324 340 { 325 341 return; … … 329 345 foreach ($pwg_event_handlers[$event] as $priority => $handlers) 330 346 { 331 foreach($handlers as $handler) 332 { 333 $function_name = $handler['function']; 334 $accepted_args = $handler['accepted_args']; 335 336 call_user_func_array($function_name, array_slice($args,1,$accepted_args) ); 347 foreach ($handlers as $handler) 348 { 349 if (!empty($handler['include_path'])) 350 { 351 include_once($handler['include_path']); 352 } 353 354 call_user_func_array($handler['function'], array_slice($args,1)); 337 355 } 338 356 }
Note: See TracChangeset
for help on using the changeset viewer.