diff --git a/application/core/LSYii_Application.php b/application/core/LSYii_Application.php index 7b81e90ed59..03de9f0909b 100644 --- a/application/core/LSYii_Application.php +++ b/application/core/LSYii_Application.php @@ -129,7 +129,7 @@ public function __construct($config = null) $this->pluginManager->loadPlugins(); // @TODO: REMOVE TEST CODE BELOW - $this->getPluginManager()->dispatchEvent('dummyEvent'); + $this->getPluginManager()->dispatchEvent(new PluginEvent('dummyEvent', $this)); } /** diff --git a/application/extensions/Menu/MenuWidget.php b/application/extensions/Menu/MenuWidget.php index 932bf0f3c99..923c5a41267 100644 --- a/application/extensions/Menu/MenuWidget.php +++ b/application/extensions/Menu/MenuWidget.php @@ -83,12 +83,13 @@ public function run() 'alt' => $this->gT('LimeSurvey online manual'), 'image' => 'showhelp.png' ); + + $event = new PluginEvent('afterAdminMenuLoaded', $this); + $event->set('menu', $menu); - $this->menu = $menu; - - App()->getPluginManager()->dispatchEvent('afterAdminMenuLoaded', array($this)); + $result = App()->getPluginManager()->dispatchEvent($event); - $menu = $this->menu; + $menu = $result->get('menu'); $this->render('adminmenu', compact('menu', 'imageUrl')); } diff --git a/application/libraries/PluginManager/PluginEvent.php b/application/libraries/PluginManager/PluginEvent.php new file mode 100644 index 00000000000..e2b38c1a988 --- /dev/null +++ b/application/libraries/PluginManager/PluginEvent.php @@ -0,0 +1,69 @@ +_sender = $sender; + } + + $this->_event = $event; + + return $this; + } + + public function get($key, $default = null) + { + if (!array_key_exists($key, $this->_parameters)) + { + return $default; + } else { + return $this->_parameters[$key]; + } + } + + public function getEventName() + { + return $this->_event; + } + + public function getSender() + { + if (!is_null($this->_sender)) { + return $this->_sender; + } else { + return false; + } + } + + public function set($key, $value) + { + $this->_parameters[$key] = $value; + + return $this; + } + + public function stop($bool = null) + { + if (!is_null($bool)) { + $this->_stop = (bool) $bool; + } + + return (bool) $this->_stop; + } +} \ No newline at end of file diff --git a/application/libraries/PluginManager/PluginManager.php b/application/libraries/PluginManager/PluginManager.php index d7a238b0313..2cea359ab3c 100644 --- a/application/libraries/PluginManager/PluginManager.php +++ b/application/libraries/PluginManager/PluginManager.php @@ -79,20 +79,30 @@ public function unsubscribe(iPlugin $plugin, $event) /** * This function dispatches an event to all registered plugins. - * @param type $event Name of the event. - * @param type $params Parameters to be passed to the event handlers. + * @param PluginEvent $event Object holding all event properties + * @param string|array $target Optional name of plugin to fire the event on + * + * @return PluginEvent */ - public function dispatchEvent($event, $params = array()) + public function dispatchEvent(PluginEvent $event, $target = array()) { - $eventResults = array(); - if (isset($this->subscriptions[$event])) + $eventName = $event->getEventName(); + if (is_string($target)) { + $target = array($target); + } + if (isset($this->subscriptions[$eventName])) { - foreach($this->subscriptions[$event] as $subscription) + foreach($this->subscriptions[$eventName] as $subscription) { - $eventResults[get_class($subscription[0])] = call_user_func_array($subscription, $params); + if (!$event->stop() + && (empty($target) || in_array($subscription[0], $target))) + { + call_user_func($subscription, $event); + } } } - + + return $event; } /** @@ -174,7 +184,8 @@ public function loadPlugins() { $this->loadPlugin($pluginName, $id); } - $this->dispatchEvent('afterPluginLoad'); // Alow plugins to do stuff after all plugins are loaded + + //$this->dispatchEvent(new PluginEvent('afterPluginLoad', $this)); // Alow plugins to do stuff after all plugins are loaded } } ?> diff --git a/plugins/Example/Example.php b/plugins/Example/Example.php index 80cb025be5e..6a49d8dd56b 100644 --- a/plugins/Example/Example.php +++ b/plugins/Example/Example.php @@ -12,7 +12,7 @@ public function __construct(PluginManager $pluginManager, $id) $this->subscribe('afterAdminMenuLoaded'); } - public function helloWorld() + public function helloWorld(PluginEvent $event) { $count = (int) $this->get('count'); if ($count === false) $count = 0; @@ -21,13 +21,16 @@ public function helloWorld() $this->set('count', $count); } - public function afterAdminMenuLoaded(MenuWidget $menu) + public function afterAdminMenuLoaded(PluginEvent $event) { - $menu->menu['left'][]=array( + $menu = $event->get('menu', array()); + $menu['left'][]=array( 'href' => "http://docs.limesurvey.org", - 'alt' => $menu->gT('LimeSurvey online manual'), + 'alt' => $event->getSender()->gT('LimeSurvey online manual'), 'image' => 'showhelp.png' ); + + $event->set('menu', $menu); } }