Simple Add-ons

Part II: Extensions

is.gd/EYmSjP

 

Isaac Raway (@airways, airways@mm.st)

MetaSushi / The Nerdery

Custom extensions

Anatomy of an extension

system/expressionengine/third_party/sms_custom_notification/
system/expressionengine/third_party/sms_custom_notification/ext.sms_custom_notification.php
    
class Sms_custom_notification_ext {

ProForm

The hook

HookParametersReturns
proform_insert_end$module, $form_obj, $datanone

Description
This hook is called just after inserting the data for the form submission into the database. This can be used to push the final data array to an external source, generating some sort of custom notification, or a number of other purposes.

The API

Code: Meta data

class Sms_custom_notification_ext {
     public $settings        = array();
     public $description     = 'Sends an SMS notification when a form '.
                               'is submitted.';
     public $docs_url        = '';
     public $name            = 'SMS Custom Notification';
     public $settings_exist  = 'n';
     public $version         = '1.0';

     ...

Code: Settings

require 'Services/Twilio.php';

class Sms_custom_notification_ext {
    ...
    private $twilio_sid    = 'xxxxxxxx';
    private $twilio_token  = 'xxxxxxxx';
    private $twilio_from   = '555678678';
    private $twilio_notify = '111234234';

Code: Extension activation

public function activate_extension() {
    $extensions = array(
        array(
            'class'     => __CLASS__,
            'method'    => 'proform_insert_end',
            'hook'      => 'proform_insert_end',
            'settings'  => serialize($this->settings),
            'version'   => $this->version,
            'enabled'   => 'y'
        ),
    );
    
    foreach($extensions as $data) {
        $this->EE->db->insert('extensions', $data);
    }
}

Code: The hook method

function proform_insert_end($module, $form_obj, $data) {
    // 1. Create a new Twilio API object
    // 2. Send the SMS
    // 3. We haven't change the data, but must return it:
    return $data;
}

Code: Initialize the API class

// 1. Create a new Twilio API object
$client = new Services_Twilio($this->twilio_sid, $this->twilio_token);}

Code: Initialize the API class

$message = $client->account->sms_messages->create(
    $this->twilio_from,          // From Twilio number
    $this->twilio_notify,        // To admin number
    <<<END
New {$form_obj->form_name} post:
  Name: {$data['name']}
  Phone: {$data['phone']}
  Severity: {$data['severity']}
END
);

Code: Putting it together

function proform_insert_end($module, $form_obj, $data) {
    // 1. Create a new Twilio API object
    $client = new Services_Twilio($this->twilio_sid, $this->twilio_token);
     
    // 2. Send the SMS
    $message = $client->account->sms_messages->create(
        $this->twilio_from,          // From Twilio number
        $this->twilio_notify,        // To admin number
        <<<END
New {$form_obj->form_name} post:
  Name: {$data['name']}
  Phone: {$data['phone']}
  Severity: {$data['severity']}
END
);
     
    // 3. We haven't change the data, but we must return it:
    return $data;
}

That's it!

Questions?












is.gd/EYmSjP

Isaac Raway (@airways, airways@mm.st)