events_dispatcher.class.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. include_once api_get_path(CONFIGURATION_PATH).'events.conf.php';
  3. /**
  4. *
  5. * Entry point for every event in the application.
  6. * Fires the functions linked to the events according to the event's conf.
  7. * Every function got its own filter, it's fired inside the functiones fired
  8. * by this class. The filter config is next to the event config, in conf/events.conf.php
  9. *
  10. */
  11. class EventsDispatcher
  12. {
  13. public static function events($event_name, $event_data = array())
  14. {
  15. global $event_config;
  16. // get the config for the event passed in parameter ($event_name)
  17. // and execute every actions with the values
  18. foreach ($event_config[$event_name]["actions"] as $func) {
  19. $execute = true;
  20. if (!function_exists($func)) // if the function doesn't exist, we log
  21. {
  22. error_log("EventsDispatcher warning : ".$func." does not exist.");
  23. $execute = false;
  24. }
  25. // check if the event's got a filter
  26. if (function_exists($event_name."_".$func."_filter_func"))
  27. {
  28. $filter = $event_name."_".$func."_filter_func";
  29. // if it does, we execute the filter (which changes the data
  30. // in-place and returns true on success or false on error)
  31. $execute = $filter($event_data);
  32. }
  33. else // if there's no filter
  34. {
  35. error_log("EventsDispatcher warning : ".$event_name."_".$func."_filter_func does not exist.");
  36. }
  37. if (!$execute) // if the filter says we cannot send the mail, we get out of here
  38. {
  39. return false;
  40. }
  41. // finally, if the filter says yes (or the filter doesn't exist),
  42. // we execute the in-between function that will call the needed
  43. // function
  44. $func($event_name, $event_data);
  45. }
  46. }
  47. }