Search code examples
phpoxwall

oxwall OW_EventManager bind and trigger


I'm using oxwall and I want to know how EventManager works on this platform for registering new methods on specific events with a simple example for triggering and binding an event to a process.


Solution

  • Here is an example for triggering and binding an event to a process. My formatting sucks a bit...

    Say you are creating a plugin for creating user account.

    /**
    * Method to save user
    */
    public function saveUser( $username, $password, $email, $accountType )
    {
        $userService = BOL_UserService::getInstance();
        $user = $userService->createUser( $username, $password, $email, $accountType );
    
        //Set new event parameters
        $event = new OW_Event('plugin_key_custom_event_name', array(
            'userDto' => $user
        ));
        //Trigger an event for after registering user
        OW::getEventManager()->trigger($event );
    }
    

    Bind Event to Listener

    You should do this part from your event handler class or from your plugin init.php file.

    /**
    *
    * Bind Event
    */
    class PLUGINKEY_CLASS_EventHandler
    {
        public function sendUserWelcome( OW_Event $e )
        {
             $params = $e->getParams();
             $user = $params['userDto'];
    
             BOL_UserService::getInstance()->sendWellcomeLetter($user);         
        }
    
        public function init()
        {
            //bind sendUserWelcome() method to 'plugin_key_custom_event_name' event
            OW::getEventManager()->bind('plugin_key_custom_event_name', array($this, 'sendUserWelcome'));
        }
    }
    

    Finally, you can initialize the event handler from your init.php file.

    $eventHandler = new PLUGINKEY_CLASS_EventHandler();
    $eventHandler->init();