Add new comment

OpenCart Event system. Part 2 of 3 - adding a new handler.

 
Important notice! The Event system only appeared in the OpenCart and it is under development. So it is possible that it will change in future. If you find some changes, please write about them in the comments.
 
How to add a new Event handler in your module?
 
There is a model admin/model/extension/event.php for this, here is the code:

class ModelExtensionEvent extends Model {
    public function addEvent($code, $trigger, $action) {
        $this->db->query("INSERT INTO " . DB_PREFIX . "event SET `code` = '" . $this->db->escape($code) . "', `trigger` = '" . $this->db->escape($trigger) . "', `action` = '" . $this->db->escape($action) . "'");
    }
 
    public function deleteEvent($code) {
        $this->db->query("DELETE FROM " . DB_PREFIX . "event WHERE `code` = '" . $this->db->escape($code) . "'");
    }
}

 
As we can see, the model contains only 2 methods:
1. addEvent - for adding new handler. It has 3 parameters:
-  $code - code  or identifier of the handler, usually the module name
-  $trigger - the Event which we want to handle
-  $action - the handler which will be called when the Event will trigger.
2. deleteEvent - for deleting the handler.
 
 
We can add and delete a handler in the install and uninstall methods of our module. 
 
The example of adding a new Event handler
 
For example, we can take the code of the pp_login module where this has already been implemented:
 

public function install() {
  $this->load->model('extension/event');
  $this->model_extension_event->addEvent('pp_login', 'post.customer.logout', 'module/pp_login/logout');
}

 
We can see that first the Event model is loaded after that the handler is added. 
It means that when in the catalog/controller/account/logout.php will be triggered 'post.customer.logout' Event:
 

$this->event->trigger('post.customer.logout');

will invoke the method logout() of this module in the module/pp_login/logout:

public function logout() {
  if (isset($this->session->data['pp_login'])) {
    unset($this->session->data['pp_login']);
  }
}

which will delete pp_login from the session.
 
The example of deleting an Event handler

public function uninstall() {
  $this->load->model('extension/event');
  $this->model_extension_event->deleteEvent('pp_login');
}

For deleting the handler you need to give only a code or identifier of the handler.
 
Conclusion
 
As we can see despite of the many words everything is very simple. In the module in the install method we should add the handler with the controller method which will be invoked when the Event will be triggered. 
 
The Events make OpenCart more flexible and get rid of the dozen conflicts created by the vQmodAt the moment OpenCart Event system is under development and not cover all OpenCart methods, but it is very pleased that  so extremely needed system appears in the OpenCart. I hope that over time the Event system will completely replace the vQmod. 
 
The list of the existing Events read in the third part of this article.

 

CAPTCHA
Spam protection
Target Image