events_dispatcher.class.php 2.0 KB

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