Drupal 6: Silverpop AddRecipient Module

Submitted by nigel on Tuesday 26th June 2012

This module (which requires the Silverpop XML API module to be installed) provides the facility to add recipients to a Silverpop database. By default this is through a 'Subscribe now' block or node, both of which can be themed. In addition, there is functionality to delete a recipient from a Silverpop database. User permissions (for the nodes) and block permissions (for the blocks) can control the visibility of the forms.

Installation

Installation is by the usual Drupal mechanism. Once the module is installed, permissions should be set by going to admin/user/permissions. It is important to ensure that these are set correctly to prevent unauthorised access through to the Silverpop API.

Usage

To create a form to capture an email address, go to node/add/silverpop-subscribe-now. You will need to provide a title and a database id. This will provide both a unique block and a node. To make the block visible you will need to go to admin/build/block/list and configure as appropriate. The blocks can be identified by their nid or by the titles they were created with.

Those uses with the correct permissions will see a link to silverpop/remove_recipient in the navigation menu (providing this itself is visible on your system). Use the form from this link to delete email addresses from a database.

Limitations

This system captures email addresses which can be useful for newsletters and campaigns. It does not capture other information such as names / titles etc. It would be possible to re-factor the code to do this as an exercise at a later date.

The RemoveRecipient API call assumes there is only one key, of Email, in the database and will not work should this not be the case.

silverpop_addrecipient.info
version = "6.x-1.00"
name = "Silverpop AddRecipient"
description = "Silverpop Add Recipient through a subscribe now form on a block"
core = 6.x
php = 5.2
package = "silverpop"
dependencies[] = "silverpop"
silverpop_addrecipient.install
<?php



/**
 * Implementation of hook_install().
 */
function silverpop_addrecipient_install() {

    
drupal_install_schema('silverpop_addrecipient');

}



/**
* Implementation of hook_uninstall().
*/
function silverpop_addrecipient_uninstall() {

    
drupal_uninstall_schema('silverpop_addrecipient');
}



/**
* Implementation of hook_schema().
*/
function silverpop_addrecipient_schema() {

    
$schema['silverpop_subscribe_now'] = array(
        
'fields' => array(
            
'vid' =>            array('type' => 'int''unsigned' => TRUE'not null' => TRUE'default' => 0),
            
'nid' =>            array('type' => 'int''unsigned' => TRUE'not null' => TRUE'default' => 0),
            
'sp_db' =>          array('type' => 'varchar''length' => 255'not null' => FALSE),
            ),
        
'unique keys' => array(
            
'nid_vid' =>        array('nid''vid'),
            ),
        
'indexes' => array(
            
'nid' =>            array('nid'),
        ),
        
'primary key' => array('vid'),
    );

    return 
$schema;
}
?>
silverpop_addrecipient.module
<?php

// Copyright @badzillacouk http://www.badzilla.co.uk
// Licence GPL. This program may be distributed as per the terms of GPL and all credits
// must be retained
//
// If you find this script useful, please consider a donation to help me fund my web presence
// and encourage me to develop more products to be placed under the terms of GPL
// To donate, go to http://www.badzilla.co.uk and click on the donation button
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 



/**
 * Implements hook_node_info().
 */
function silverpop_addrecipient_node_info() {

    return array(
        
'silverpop_subscribe_now' => array(
            
'name' => t('Silverpop Subscribe Now Block'),
            
'module' => 'silverpop_addrecipient',
            
'description' => t("Silverpop Subscribe Now Block to capture new email addresses and add to Silverpop"),
            
'has_title' => TRUE,
            
'title_label' => t('Silverpop Subscribe Now Block Title'),
            
'has_body' => FALSE,
            ),
        );
}



/**
 * implements hook_insert().
 */
function silverpop_addrecipient_insert($node) {

    
db_query(
        
'INSERT INTO {silverpop_subscribe_now} (vid, nid, sp_db) '
        
."VALUES (%d, %d, '%s')",
            
$node->vid,
            
$node->nid,
            
$node->sp_db);
}



/**
 * Implements hook_delete().
 */
function silverpop_addrecipient_delete($node) {

    
db_query('DELETE FROM {silverpop_subscribe_now} WHERE nid = %d'$node->nid);
}




/**
 * This implementation just handles deleting node revisions.
 * Implements hook_nodeapi().
 */
function silverpop_addrecipient_nodeapi(&$node$op$teaser$page) {

    if (
$op == 'delete revision' and $type->type == 'silverpop_addrecipient'
        
db_query('DELETE FROM {silverpop_subscribe_now} WHERE vid = %d'$node->vid);
}




/**
 * implements hook_update().
 */
function silverpop_addrecipient_update($node) {

    
db_query("UPDATE {silverpop_subscribe_now} SET sp_db = '%s'WHERE vid = '%d'",
                
$node->sp_db);
}



/**
 * Implementation of hook_load().
 */
function silverpop_addrecipient_load($node) {

    return 
db_fetch_object(db_query("SELECT * FROM {silverpop_subscribe_now} WHERE vid = %d"$node->vid));
}



/**
 * Implements hook_perm()
 */
function silverpop_addrecipient_perm() {

    return array(
        
'create silverpop_subscribe_now',
        
'edit silverpop_subscribe_now',
        
'delete silverpop_subscribe_now',
        
'view silverpop_subscribe_now',
    );
}




/**
 * Implements hook_access()
 */
function silverpop_addrecipient_access($op$node$account) {

    switch (
$op) {
        case 
'create':
            return 
user_access('create silverpop_subscribe_now'$account);
        case 
'update':
            return 
user_access('edit silverpop_subscribe_now'$account);
        case 
'delete':
            return 
user_access('delete silverpop_subscribe_now'$account);
        case 
'view':
            return 
user_access('view silverpop_subscribe_now'$account);

    }
}



/**
 * Implementation of hook_view().
 */
function silverpop_addrecipient_view($node$teaser FALSE$page FALSE) {

    if (!
$teaser) {
        
$node node_prepare($node$teaser);
        
$node->content['form'] = array('#value' => drupal_get_form('silverpop_subscribe_now_form'$node));
    }

    return 
$node;
}


/**
 * Implementation of hook_form().
 */
function silverpop_addrecipient_form(&$node$form_state) {

    
$form['title'] = array(
        
'#type' => 'textfield',
        
'#maxlength' => 250,
        
'#title' => $type->title_label,
        
'#required' => TRUE,
        
'#default_value' => $node->title,
        
'#weight' => -10,
    );

    
$form['sp_db'] = array(
        
'#type' => 'textfield',
        
'#maxlength' => 255,
        
'#title' => t('Silverpop database id'),
        
'#required' => TRUE,
        
'#description' => t('Silverpop database id'),
        
'#default_value' => $node->sp_db $node->sp_db '',
        
'#weight' => 0,
    );

    return 
$form;
}



/**
 * Implementation of hook_block().
 */
function silverpop_addrecipient_block($op 'list'$delta 0$edit = array()) {

    switch(
$op) {
        case 
'list':
            
$res db_query("SELECT n.nid, r.title FROM {node} AS n INNER JOIN {node_revisions} AS r
                             USING (vid) WHERE type = 'silverpop_subscribe_now' AND status = 1"
);

            while(
$ret db_fetch_object($res)) 
                
$block[$ret->nid]['info'] = t('Silverpop Subscribe Now: @title [nid:@nid]',
                                    array(
'@title' => $ret->title'@nid' => $ret->nid));
            
            break;

        case 
'view':
            if (
$node node_load($delta)) {
                
$block['content'] = drupal_get_form('silverpop_subscribe_now_form'$node);
                
$block['subject'] = $node->title;
            }

            break;

        default:
            break;
    }

    return 
$block;
}



// Forms for user input 
function silverpop_subscribe_now_form($form_state$node_params) {

    
$form['email_address'] = array(
        
'#type' => 'textfield',
        
'#maxlength' => 255,
        
'#title' => t('Email address'),
        
'#size' => 15,
        
'#required' => TRUE,
    );

    
$form['submit'] = array(
        
'#type' => 'submit',
        
'#value' => t('Subscribe'),
    );

    
$form['#validate'][] = 'silverpop_subscribe_now_form_validate';
    
$form['#submit'][] = 'silverpop_subscribe_now_form_submit';

    return 
$form;
}





function 
silverpop_subscribe_now_form_validate($form, &$form_state) {

    if (!
valid_email_address($form_state['values']['email_address']))
        
form_set_error('email_address't('Please enter a valid Email address'));
}



function 
silverpop_subscribe_now_form_submit($form, &$form_state) {

    
// Construct the XML for the Silverpop API call
    
$dom = new DOMDocument;
    
$list_id $dom->createTextNode($form['#parameters'][2]->sp_db);
    
$list $dom->createElement('LIST_ID');
    
$cfrom_text $dom->createTextNode('1');
    
$cfrom $dom->createElement('CREATED_FROM');
    
$column $dom->createElement('COLUMN');
    
$name_text $dom->createTextNode('EMAIL');
    
$name $dom->createElement('NAME');
    
$value_text $dom->createTextNode($form_state['values']['email_address']);
    
$value $dom->createElement('VALUE');
    
$method $dom->createElement('AddRecipient');

    
$list->appendChild($list_id);
    
$cfrom->appendChild($cfrom_text);
    
$name->appendChild($name_text);
    
$value->appendChild($value_text);
    
$method->appendChild($list);
    
$method->appendChild($cfrom);
    
$method->appendChild($column);
    
$column->appendChild($name);
    
$column->appendChild($value);
    
$dom->appendChild($method);

    
module_load_include('inc''silverpop''silverpop_xml');
    
$addrecipient = new silverpop_xml($domFALSE);
    list(
$retcode$response) = $addrecipient->apicall();

    
$msg '';
    if (
$retcode)
        
$msg t('Thanks! You have now been suscribed to the mailing list');
    else if (
is_object($response) and 
             isset(
$response->getElementsByTagName('errorid')->item(0)->nodeValue) and 
             
$response->getElementsByTagName('errorid')->item(0)->nodeValue == 122)
        
$msg t('Thanks, but you are already subscribed');
    else
        
$msg t('Sorry, unable to subscribe you at this present time');

    
drupal_set_message($msg);
}



/**
 * Implementation of hook_theme().
 */
function silverpop_addrecipient_theme() {

    
$ret['silverpop_subscribe_now_form'] = array(
            
'arguments' => array('form' => NULL));

    
$ret['silverpop_remove_recipient_form'] = array(
            
'arguments' => array('form' => NULL));

    return 
$ret;
}



function 
theme_silverpop_subscribe_now_form($form) {

    
$output '';

    
$output .= "To subscribe to our newsletter, please enter your Email address below";
    
$output .= drupal_render($form['email_address']);
    
$output .= drupal_render($form['submit']);

    
$output .= drupal_render($form);

    return 
$output
}



/**
 * Implementation of hook_menu().
 */
function silverpop_addrecipient_menu() {

    
$items['silverpop/remove_recipient'] = array(
        
'title' => t('Silverpop Remove Recipient'),
        
'description' => t('Removal of recipients from Silverpop'),
        
'page callback' => 'drupal_get_form',
        
'page arguments' => array('silverpop_remove_recipient_form'),
        
'access arguments' => array('delete silverpop_subscribe_now'),
        
'type' => MENU_NORMAL_ITEM,
    );

    return 
$items;
}



function 
silverpop_remove_recipient_form() {

    
$form['sp_db'] = array(
        
'#type' => 'textfield',
        
'#maxlength' => 250,
        
'#title' => t('Silverpop database id'),
        
'#required' => TRUE,
        
'#description' => t('Silverpop database id'),
        
'#default_value' => $node->sp_db $node->sp_db '',
        
'#weight' => 0,
    );

    
$form['email'] = array(
        
'#type' => 'textfield',
        
'#maxlength' => 250,
        
'#title' => t('Email address'),
        
'#description' => t('Email address to remove from the Silverpop database'),
        
'#required' => TRUE,
        
'#weight' => 10,
    );

    
$form['submit'] = array(
        
'#type' => 'submit',
        
'#value' => t('Submit'),
        
'#weight' => 20,
    );

    
$form['#submit'][] = 'silverpop_remove_recipient_form_submit';
    
$form['#validate'][] = 'silverpop_remove_recipient_form_validate';

    return 
$form;
}



function 
silverpop_remove_recipient_form_validate($form, &$form_state) {

    if (!
valid_email_address($form_state['values']['email']))
        
form_set_error('email't('Please enter a valid Email address'));
}



function 
silverpop_remove_recipient_form_submit($form, &$form_state) {

    
// Construct the XML for the Silverpop API call
    
$dom = new DOMDocument;
    
$list_id $dom->createTextNode($form_state['values']['sp_db']);
    
$list $dom->createElement('LIST_ID');

    
$email_text $dom->createTextNode($form_state['values']['email']);
    
$email $dom->createElement('EMAIL');

    
$method $dom->createElement('RemoveRecipient');

    
$list->appendChild($list_id);
    
$email->appendChild($email_text);
    
$method->appendChild($list);
    
$method->appendChild($email);

    
$dom->appendChild($method);

    
module_load_include('inc''silverpop''silverpop_xml');
    
$removerecipient = new silverpop_xml($domTRUE);
    list(
$retcode$response) = $removerecipient->apicall();

    
$msg '';
    if (
$retcode)
        
$msg t('Email address succesfully deleted from the specified database');
    else if (
is_object($response) and 
             isset(
$response->getElementsByTagName('errorid')->item(0)->nodeValue) and 
             
$response->getElementsByTagName('errorid')->item(0)->nodeValue == 138)
        
$msg t('The email address you specified does not exist in the Silverpop database');
    else
        
$msg t('Sorry, the removal of the email address failed. Please contact your administrator');

    
drupal_set_message($msg);
}


function 
theme_silverpop_remove_recipient_form($form) {

    
$output '';

    
$output .= "To remove an email address from a Silverpop database, please use the form below";
    
$output .= drupal_render($form['sp_db']);
    
$output .= drupal_render($form['email']);
    
$output .= drupal_render($form['submit']);

    
$output .= drupal_render($form);

    return 
$output
}
?>
blog terms
Drupal Drupal 6