You are here

webform_campaignmonitor.module in Webform Campaign Monitor 6

Same filename and directory in other branches
  1. 7.3 webform_campaignmonitor.module
  2. 7.2 webform_campaignmonitor.module

The brain of Webform Campaign Monitor.

Provides a webform checkbox component that lets users sign up to campaign monitor.

File

webform_campaignmonitor.module
View source
<?php

/**
 * @file
 * The brain of Webform Campaign Monitor.
 *
 * Provides a webform checkbox component that lets users
 * sign up to campaign monitor.
 */

/**
 * Implementation of hook_webform_component_info().
 */
function webform_campaignmonitor_webform_component_info() {
  $components = array();
  $components['campaignmonitor'] = array(
    'label' => t('Campaign Monitor'),
    'description' => t('Allows users subscribe to enewsletters.'),
    'features' => array(
      'title_display' => FALSE,
    ),
    'file' => 'components/campaignmonitor.inc',
  );
  return $components;
}

/**
 * Implementation of hook_form_alter().
 */
function webform_campaignmonitor_form_alter(&$form, &$form_state, $form_id) {
  if (drupal_substr($form_id, 0, 20) == 'webform_client_form_') {
    $form['#submit'][] = '_webform_campaignmonitor_submit';
  }
}

/**
 * Webforms submit handler.
 *
 * @param array $form
 *   The actual form.
 * @param array $form_state
 *   The form state array.
 */
function _webform_campaignmonitor_submit($form, &$form_state) {

  // Get all campaign monitor components in the form.
  $components = _webform_campaignmonitor_list_components_in_form($form_state['webform']['component_tree']['children'], 'campaignmonitor');

  // If there is no components, return.
  if (empty($components)) {
    return;
  }

  // The results of the attempts at trying to subscribe the user.
  $results = array();
  foreach ($components as $component) {

    // When this value is true, the user wants to subscribe.
    $subscribe = $form_state['values']['submitted'][$component['cid']];
    if (!$subscribe) {
      continue;
    }

    // Get the email component the campaign monitor component is linked to.
    if (isset($component['extra']['email_component'])) {
      $email = check_plain($form_state['values']['submitted'][$component['extra']['email_component']]);
    }
    else {
      continue;
    }

    // Get the name component if it has been set.
    if ($component['extra']['textfield_component']) {
      $name = check_plain($form_state['values']['submitted'][$component['extra']['textfield_component']]);
    }
    else {
      $name = '';
    }

    // Subscribe!
    if (!empty($email)) {
      $results = $results + _webform_campaignmonitor_subscribe($email, $name);
    }
  }

  // If we don't have anything to show the user, return.
  if (empty($results)) {
    return;
  }

  // Otherwise, sort out the messages.
  foreach ($results as $key => $value) {
    if ($value['succeed']) {
      drupal_set_message($value['message'], 'status');
    }
    else {
      drupal_set_message($value['message'], 'error');
    }
  }
}

/**
 * Subscribes an email address to one or more mailing lists.
 *
 * @param string $email
 *   The user's email address.
 * @param string $name
 *   The user's name (optional).
 *
 * @return array
 *   An array of messages containing the results.
 */
function _webform_campaignmonitor_subscribe($email, $name = '') {
  $results = array();

  // Get all the CM variables.
  $api_key = variable_get('campaignmonitor_api_key', '');
  $client_id = variable_get('campaignmonitor_client_id', '');
  $list_id = variable_get('campaignmonitor_list_id', '');

  // Subscribe or re-subscribe the user.
  $cm = new CampaignMonitor($api_key, $client_id, NULL, $list_id);
  $result = $cm
    ->subscriberAddAndResubscribe($email, $name);
  if ($result['anyType']['Code'] == 0) {
    $results[$list_id]['message'] = t('You are now subscribed to our mailing list.');
    $results[$list_id]['succeed'] = TRUE;
  }
  else {
    $results[$list_id]['message'] = t('You were not subscribed to our mailing list, please try again.');
    $results[$list_id]['succeed'] = FALSE;
  }
  return $results;
}

/**
 * Lists all components of a given type that belong to a given node nid.
 *
 * @param int $nid
 *   The node id.
 * @param string $type
 *   The component type you would like to list (optional).
 *
 * @return array
 *   Array of components.
 */
function _webform_campaignmonitor_list_components($nid, $type = '') {
  $components = array();
  $result = db_query("SELECT w.cid, w.pid, w.name FROM {webform_component} w WHERE w.nid = '%d' AND w.type = '%s' ORDER BY w.weight ASC", $nid, $type);
  while ($row = db_fetch_array($result)) {
    $components[] = $row;
  }
  return $components;
}

/**
 * Lists all components of a given type.
 *
 * @param array $elements
 *   The form elements.
 * @param string $type
 *   The component type you would like to list.
 * 
 * @return array
 *   Array of components.
 */
function _webform_campaignmonitor_list_components_in_form($elements, $type) {
  $components = array();
  foreach ($elements as $element) {
    if ($element['type'] == $type) {
      $components[$element['cid']] = $element;
    }
    if (isset($element['children'])) {
      $children_components = _webform_campaignmonitor_list_components_in_form($element['children'], $type);
      if ($children_components) {
        $components = $components + $children_components;
      }
    }
  }
  return $components;
}

Functions

Namesort descending Description
webform_campaignmonitor_form_alter Implementation of hook_form_alter().
webform_campaignmonitor_webform_component_info Implementation of hook_webform_component_info().
_webform_campaignmonitor_list_components Lists all components of a given type that belong to a given node nid.
_webform_campaignmonitor_list_components_in_form Lists all components of a given type.
_webform_campaignmonitor_submit Webforms submit handler.
_webform_campaignmonitor_subscribe Subscribes an email address to one or more mailing lists.