User Guide & Reference Manual

App Hooks Library

Hooks are system actions such as store_order, member_registration, and subscription_new that serve as possible triggers for emails or method/function calls.

All users, even novice users, will configure for emails to be sent by adding/editing emails attached to hooks at Configuration > Emails in the control panel. Learn more about configuring emails.

Developers and advanced users can use the App Hooks library to extend Hero's functionality beyond what it is today by tying it into third-party software, weaving new routines into the software, etc. You can bind any class->method() call to a hook, but you can also bind any simple function() call as long as you specify the $filename during your bind() creation.

Most hooks will automatically pass arguments to your called methods/functions. These are not comprehensive arguments, but rather ID's that can be used to retrieve full details of the relevant system action via other methods like $this->invoice_model->get_invoice().

Default Available Hooks

The following hooks are available in an Hero installation, by default. They are stored in the hooks table of your database.

Creating New Hooks in Custom Modules

You can register a new hook in any of your custom module installation routines by calling this library's register() method documented below.

This hook can then be bound to like anything else. Look at the trigger() method below for how to trigger your system hooks once they've been created. This includes adding data to the hook which can be used in emails or passed to subsequently bound method/function calls.

Initialization

This library is loaded automatically by both Front_controller and Admincp_controller. So, if you are using a standard module or your new module is extending these classes (as it should!), you can access this library with:


$this->app_hooks->...();

Otherwise, you'll have to load the library:


$this->load->library('app_hooks');

Method Reference

int bind (string $hook , string $class , string $method , string $filename)

Bind a class/method or function to a hook. These classes can be CodeIgniter standard models or libraries, but can also be outside classes located in other PHP files. If you are only using a simple function() call that is not part of a class, set $class to FALSE.

The $filename should be relative to your root Hero folder. For example, to bind a method in a library file in your new module, "my_new_module":


$this->app_hooks->bind('member_register', 'My_library', 'create_user_in_my_app', 'app/modules/my_new_module/libraries/my_library.php');

Now, whenever a user registers, that library will be loaded (if not already loaded) and a call will be made by App Hooks like so:


$this->load->library('my_new_module/my_library');
$this->my_library->create_user_in_my_app($user_id, $password);

Note that two arguments were sent to your method. These arguments are specified when triggering the hook and differ from hook to hook. In this case, for a member registration, it makes sense to pass the user's system ID and their password (something you cannot retrieve from a user_model->get_user() call).

array get_hooks ()

Return an alphabetically sorted array of all hooks.

array get_hook (string $name)

Return the configuration of one particular hook. Configuration data:

int register (string $name , string $description , array $email_data , array $other_email_data)

Create a new hook in the system. Configuration details are equivalent to the get_hook() method. This method is likely called in your module definition file as part of an installation/upgrade routine.


// we are accessing the CI superobject via $this->CI because this example comes from a module definition file
$this->CI->app_hooks->register('subscription_renewal_failure','A subscription charge fails.',array('member','subscription','subscription_plan'));

boolean data (string $data , int $id)

Set email data using a general datetype when calling a trigger. This method is called potentially multiple times before a hook is triggered. It is used to initiate the loading of standard data variables into the hook's email variable options. For example, by simply specify $this->app_hooks->data('member', $member_id); (assuming that member ID is valid), we are making variables like *{$member.username}*, *{$member.email}*, *{$member.first_name}*, and *{$member.last_name}* available to emails that are sent with this hook. For more information on emails, check out the guide to configuring emails.


$this->app_hooks->data('member', $user_id);
$this->app_hooks->data('invoice', $invoice_id);
$this->app_hooks->trigger('paid_invoice');

void data_var (string $name , string $value)

Specify a single variable to add to the email data accumulating before a hook call with trigger(). These are one-off variables unique to a hook, not standard data types like member, invoice, or subscription.


$this->app_hooks->data('member', $user_id);
$this->app_hooks->data_var('new_password', $new_password);
$this->app_hooks->trigger('change_password');

void trigger (string $name [, $arguments ])

Trigger a hook. This will send out all emails that match the hooks current data parameters, and also trigger the calling of methods/functions bound to this hook. If additional arguments are passed, they will be passed along to the methods/functions bound to this hook.

This is an example of a real trigger call for the member_change_password hook:


// load the CI superobject
$CI =& get_instance();

// prep the hook with data
$CI->app_hooks->data('member', $user_id);
$CI->app_hooks->data_var('new_password', $new_password);

// trigger with 2 additional arguments
$CI->app_hooks->trigger('member_change_password', $user_id, $new_password);

// be kind, reset the hook's data so that the next hook trigger doesn't accidentally pass email data that doesn't exist
$CI->app_hooks->reset();

void reset ()

Reset the current data being attached to a hook call.