events_dispatcher.class.php 1.8 KB

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