From 5ad4b98cbd3e09f5f4c30b581f65dfa4288f8601 Mon Sep 17 00:00:00 2001 From: ben Date: Tue, 12 Feb 2008 19:19:22 +0000 Subject: Adding priority and a little more description to the events API. git-svn-id: https://code.elgg.org/elgg/trunk@9 36083f99-b078-4883-b0ff-0f9b5a30f544 --- engine/lib/elgglib.php | 47 ++++++++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 19 deletions(-) (limited to 'engine') diff --git a/engine/lib/elgglib.php b/engine/lib/elgglib.php index 0cb246d35..e6930091c 100644 --- a/engine/lib/elgglib.php +++ b/engine/lib/elgglib.php @@ -251,19 +251,28 @@ * * And must return true or false depending on success. A false will halt the event in its tracks and no more functions will be called. * - * You can then simply register them using the alias function: + * You can then simply register them using the following function. Optionally, this can be called with a priority nominally from 0 to 1000, where functions with lower priority values are called first (note that priorities CANNOT be negative): * - * register_event_handler('event', 'object_type', 'function_name'); + * register_event_handler($event, $object_type, $function_name [, $priority = 500]); + * + * Note that you can also use 'all' in place of both the event and object type. + * + * To trigger an event properly, you should always use: + * + * trigger_event($event, $object_type [, $object]); + * + * Where $object is optional, and represents the $object_type the event concerns. This will return true if successful, or false if it fails. * * @param string $event The type of event (eg 'init', 'update', 'delete') * @param string $object_type The type of object (eg 'system', 'blog', 'user') * @param string $function The name of the function that will handle the event + * @param int $priority A priority to add new event handlers at. Lower numbers will be called first (default 500) * @param boolean $call Set to true to call the event rather than add to it (default false) * @param mixed $object Optionally, the object the event is being performed on (eg a user) * @return true|false Depending on success */ - function events($event = "", $object_type = "", $function = "", $call = false, $object = null) { + function events($event = "", $object_type = "", $function = "", $priority = 500, $call = false, $object = null) { static $events; @@ -278,7 +287,13 @@ if (!$call) { if (!empty($event) && !empty($object_type) && is_callable($function)) { - $events[$event][$object_type][] = $function; + $priority = (int) $priority; + if ($priority < 0) $priority = 0; + while (isset($events[$event][$object_type][$priority])) { + $priority++; + } + $events[$event][$object_type][$priority] = $function; + ksort($events[$event][$object_type]); return true; } else { return false; @@ -292,18 +307,6 @@ return false; } } - if ($event != 'all' && !empty($events['all'][$object_type]) && is_array($events['all'][$object_type])) - foreach($events['all'][$object_type] as $eventfunction) { - if (!$eventfunction('all', $object_type, $object)) { - return false; - } - } - if ($object_type != 'all' && !empty($events[$event]['all']) && is_array($events[$event]['all'])) - foreach($events[$event]['all'] as $eventfunction) { - if (!$eventfunction($event, 'all', $object)) { - return false; - } - } return true; } @@ -322,8 +325,8 @@ * @return true|false Depending on success */ - function register_event_handler($event, $object_type, $function) { - return events($event, $object_type, $function); + function register_event_handler($event, $object_type, $function, $priority = 500) { + return events($event, $object_type, $function, $priority); } /** @@ -335,7 +338,13 @@ * @return true|false Depending on success */ function trigger_event($event, $object_type, $object = null) { - return events($event, $object_type, "", true, $object); + if (events($event, $object_type, "", null, true, $object) + && events('all', $object_type, "", null, true, $object) + && events($event, 'all', "", null, true, $object) + && events('all', 'all', "", null, true, $object)) { + return true; + } + return false; } ?> \ No newline at end of file -- cgit v1.2.3