You are here

constant_contact.module in Constant Contact 5

File

constant_contact.module
View source
<?php

// $Id

/**
 * Implementation of hook_perm().
 */
function constant_contact_perm() {
  return array(
    'admininister constant_contact',
  );
}

/**
 * Implementation of hook_menu().
 */
function constant_contact_menu($may_cache) {
  $items = array();
  $items[] = array(
    'path' => 'admin/settings/constant_contact',
    'title' => t('Constant Contact Settings'),
    'description' => t('Configure settings for Constant Contact'),
    'callback' => 'drupal_get_form',
    'callback arguments' => array(
      'constant_contact_admin_settings',
    ),
    'access' => user_access('admininister constant_contact'),
    'type' => MENU_NORMAL_ITEM,
  );
  $items[] = array(
    'path' => 'admin/settings/constant_contact_import',
    'title' => t('Constant Contact Import Users'),
    'description' => t('Import existing Drupal users to Constant Contact'),
    'callback' => 'drupal_get_form',
    'callback arguments' => array(
      'constant_contact_import',
    ),
    'access' => user_access('admininister constant_contact'),
    'type' => MENU_NORMAL_ITEM,
  );
  $items[] = array(
    'path' => 'constant_contact/unsubscribe',
    'title' => t('List Unsubscribe'),
    'description' => t('Unsubscribe from a list'),
    'callback' => 'drupal_get_form',
    'callback arguments' => array(
      'constant_contact_unsubscribe_confirm',
    ),
    'type' => MENU_CALLBACK,
    'access' => TRUE,
  );
  return $items;
}

/**
* Implementation of hook_help().
*/
function constant_contact_help($section) {
  switch ($section) {
    case 'admin/help/constant_contact':
    case 'admin/help#constant_contact':

      // Return a line-break version of the module README
      return filter_filter('process', 2, NULL, file_get_contents(dirname(__FILE__) . "/README.txt"));
  }
}

/**
 * Implementation of hook_block().
 */
function constant_contact_block($op = 'list', $delta = 0) {
  switch ($op) {
    case 'list':
      $blocks = array();
      $lists = constant_contact_get_lists();
      foreach ($lists as $i => $list) {
        $blocks[] = array(
          'info' => t('Constant Contact: ' . $list),
        );
      }
      return $blocks;
      break;
    case 'view':
      global $user;
      if (empty($user->uid) && !variable_get('constant_contact_allowanon', TRUE)) {
        return;
      }
      $lists = constant_contact_get_lists();
      $lists_arr = array_values($lists);
      $keyname = constant_contact_escape_listname($lists_arr[$delta]);
      $list = $lists[$keyname];
      if (constant_contact_check_user($list)) {
        return;
      }
      $block['content'] = drupal_get_form('constant_contact_block_form', $delta);
      return $block;
      break;
  }
}

/**
 * Implementation of hook_form()
 */
function constant_contact_admin_settings() {
  $form = array();
  $form['constant_contact_server'] = array(
    '#type' => 'fieldset',
    '#title' => t('Server Settings'),
    '#description' => t('Put your Constant Contact login info in here'),
    '#collapsible' => true,
  );
  $form['constant_contact_server']['constant_contact_username'] = array(
    '#type' => 'textfield',
    '#title' => t('Username'),
    '#default_value' => variable_get('constant_contact_username', ''),
    '#size' => 30,
    '#description' => t('The username you use to login to Constant Contact'),
  );
  $form['constant_contact_server']['constant_contact_password'] = array(
    '#type' => 'textfield',
    '#title' => t('Password'),
    '#default_value' => variable_get('constant_contact_password', ''),
    '#size' => 30,
    '#description' => t('The password you use to login to Constant Contact'),
  );
  $form['constant_contact_server']['advanced'] = array(
    '#type' => 'fieldset',
    '#title' => t('Advanced'),
    '#collapsible' => true,
    '#collapsed' => true,
  );
  $form['constant_contact_server']['advanced']['constant_contact_addurl'] = array(
    '#type' => 'textfield',
    '#title' => t('Add Url'),
    '#default_value' => variable_get('constant_contact_addurl', 'http://ui.constantcontact.com/roving/wdk/API_AddSiteVisitor.jsp'),
    '#description' => t('The url of the add server'),
  );
  $form['constant_contact_server']['advanced']['constant_contact_delurl'] = array(
    '#type' => 'textfield',
    '#title' => t('Remove Url'),
    '#default_value' => variable_get('constant_contact_delurl', 'http://ui.constantcontact.com/roving/wdk/API_UnsubscribeSiteVisitor.jsp'),
    '#description' => t('The url of the remove server'),
  );
  $form['email'] = array(
    '#type' => 'fieldset',
    '#title' => t('Confirmation Email'),
    '#description' => t('Customize the e-mail that is sent when the user registers. Available variables are: !list_name, !uri_list_unsubscribe, !username, !site, !password, !uri, !uri_brief, !mailto, !date, !login_uri, !login_url.'),
    '#collapsible' => true,
  );
  $form['email']['constant_contact_sendemail'] = array(
    '#type' => 'checkbox',
    '#title' => t('Send Email'),
    '#default_value' => variable_get('constant_contact_sendemail', TRUE),
  );
  $form['email']['constant_contact_email_subject'] = array(
    '#type' => 'textfield',
    '#title' => t('Subject'),
    '#default_value' => variable_get('constant_contact_email_subject', 'Welcome to !list_name Mailing List!'),
  );
  $form['email']['constant_contact_email_body'] = array(
    '#type' => 'textarea',
    '#title' => t('Body'),
    '#default_value' => variable_get('constant_contact_email_body', "Dear !username,\nThanks for joining the !list_name mailing list at !uri. If you would like to unsubscribe, use the following URL: !uri_list_unsubscribe\nThanks,\nThe !site Team"),
  );
  $form['constant_contact_lists'] = array(
    '#type' => 'fieldset',
    '#title' => t('List Settings'),
    '#description' => t('Settings for particular mailing lists'),
    '#collapsible' => true,
  );
  $form['constant_contact_lists']['constant_contact_allowanon'] = array(
    '#type' => 'checkbox',
    '#title' => t('Allow anonymous Users'),
    '#default_value' => variable_get('constant_contact_allowanon', TRUE),
    '#description' => t('Can non-logged-in users add themselves to lists?  If so, a form will be displayed that will allow them to enter their email address.'),
  );
  $form['constant_contact_lists']['constant_contact_lists'] = array(
    '#type' => 'textarea',
    '#title' => t('Lists'),
    '#default_value' => variable_get('constant_contact_lists', ''),
    '#size' => 30,
    '#description' => t('Put all your Constant Contact mailing lists that you want integration for in here, one per line. I recommend using names shorter then 21 characters, so the keys will be distinct.'),
  );
  $form['constant_contact_lists']['blocktext'] = array(
    '#type' => 'fieldset',
    '#title' => t('Block Text'),
    '#description' => t('The text that appears in the signup block'),
    '#collapsible' => true,
    '#collapsed' => true,
  );
  $lists = constant_contact_get_lists();
  foreach ($lists as $keyname => $list) {
    $form['constant_contact_lists']['blocktext'][$keyname] = array(
      '#type' => 'textfield',
      '#title' => $list . t(' text'),
      '#default_value' => variable_get($keyname, "Join {$list} Mailing List"),
      '#size' => 30,
    );
  }
  return system_settings_form($form);
}

/**
 * Implementation of hook_form()
 * creates the join mailing list block
 */
function constant_contact_block_form($delta = 0, $op = 'list', $edit = array()) {
  global $user;
  $lists = constant_contact_get_lists();
  $lists_arr = array_values($lists);
  $keyname = constant_contact_escape_listname($lists_arr[$delta]);
  $list = $lists[$keyname];
  if (constant_contact_check_user($list)) {
    return;
  }
  $form = array();
  $form[] = array(
    '#value' => '<div id="' . $keyname . '" class="constant_contact_join">' . variable_get($keyname, "Join {$list} Mailing List") . '</div>',
  );

  // give anonymous users an email field
  if (empty($user->uid)) {
    $form['email'] = array(
      '#type' => 'textfield',
      '#title' => t('Email Address'),
      '#size' => 10,
    );
  }
  $form['list'] = array(
    '#type' => 'hidden',
    '#value' => $list,
  );
  $form[] = array(
    '#type' => 'submit',
    '#value' => t('Join'),
  );
  return $form;
}

/**
 * Implementation of hook_form_validate()
 * validate the join mailing list block
 */
function constant_contact_block_form_validate($form_id, $values) {
  global $user;
  $ok = empty($user->uid) && !empty($values['email']) || !empty($user->uid);
  if (!$ok) {
    drupal_set_message(t('You need to set your email address.'), 'error');
  }
  return $ok;
}

/**
 * Implementation of hook_form_submit()
 * process the join mailing list block
 */
function constant_contact_block_form_submit($form_id, $values) {
  global $user;
  $data = array();
  if ($user->uid) {
    $email = $user->mail;
    $data['Custom_field_1'] = $user->name;
    $data['Custom_field_2'] = $user->uid;
    $data['Custom_field_3'] = date('r');
  }
  else {
    $email = $values['email'];
  }
  require_once 'ConstantContact.php';
  $constant_contact = new ConstantContact();
  $constant_contact
    ->setUsername(variable_get('constant_contact_username', ''));
  $constant_contact
    ->setPassword(variable_get('constant_contact_password', ''));
  $constant_contact
    ->setCategory($values['list']);
  if ($constant_contact
    ->add($email, $data)) {
    $type = 'status';
    $message = 'You have been added to our list.';
    if (variable_get('constant_contact_sendemail', TRUE)) {
      constant_contact_confirmation_email($email, $values['list']);
    }
    constant_contact_save_user($user, $values['list']);
  }
  else {
    $type = 'error';
    $message = 'There was a problem adding you to the list';
  }
  drupal_set_message(t($message), $type);
}

/**
 * Implementation of hook_form()
 * confirm unsubscribe
 */
function constant_contact_unsubscribe_confirm($list = NULL) {
  $lists = constant_contact_get_lists();
  $list = urldecode($list);
  if (empty($list) || !array_search($list, $lists)) {
    drupal_set_message(t('Invalid list name.'), 'error');
    return drupal_goto($_GET['destination'] ? $_GET['destination'] : '<front>');
  }
  $form['list'] = array(
    '#type' => 'hidden',
    '#value' => $list,
  );
  $form = confirm_form($form, t('Are you sure you want to unsubscribe from %list?', array(
    '%list' => $list,
  )), $_GET['destination'] ? $_GET['destination'] : '<front>', t('You must be logged in and subscribed under your account email, for this to work.'), t('Unsubscribe'), t('Cancel'));
  return $form;
}

/**
 * Implementation of hook_form_submit()
 * process confirm unsubscribe
 */
function constant_contact_unsubscribe_confirm_submit($form_id, $values) {
  global $user;
  if (!constant_contact_check_user($values['list'])) {
    if (empty($user->uid)) {
      drupal_set_message(t('You must be logged in to unsubscribe.'), 'error');
      return drupal_goto('user/user', 'destination=constant_contact/unsubscribe/' . urlencode($values['list']));
    }
    else {
      drupal_set_message(t('You are logged in, but your email does not appear to be subscribed to that list.'), 'error');
      return drupal_goto($_GET['destination'] ? $_GET['destination'] : '<front>');
    }
  }
  $lists = constant_contact_get_lists();
  if (empty($values['list']) || !array_search($values['list'], $lists)) {
    drupal_set_message(t('Invalid list name.'), 'error');
    return drupal_goto($_GET['destination'] ? $_GET['destination'] : '<front>');
  }
  require_once 'ConstantContact.php';
  $constant_contact = new ConstantContact();
  $constant_contact
    ->setUsername(variable_get('constant_contact_username', ''));
  $constant_contact
    ->setPassword(variable_get('constant_contact_password', ''));
  $constant_contact
    ->setCategory($values['list']);
  if ($constant_contact
    ->remove($user->mail)) {
    $type = 'status';
    $message = t('You have been removed from %list mailing list.', array(
      '%list' => $values['list'],
    ));
    constant_contact_save_user($user, $values['list'], FALSE);
  }
  else {
    $type = 'error';
    $message = t('You could not be removed from %list mailing list.', array(
      '%list' => $values['list'],
    ));
    $_GET['destination'] = 'constant_contact/unsubscribe/' . urlencode($values['list']);
  }
  drupal_set_message($message, $type);
  return drupal_goto($_GET['destination'] ? $_GET['destination'] : '<front>');
}

/**
 * Implementation of hook_form()
 * import users
 */
function constant_contact_import() {
  $form = array(
    array(
      '#value' => '<p>' . t("This will import all your drupal users into your Constant Contact account. It doesn't send confirmation emails, so if you want that, you will need to send a mailing-list email, on the Constant Contact site.") . '</p>',
    ),
  );
  $lists = constant_contact_get_lists();
  $form['list'] = array(
    '#type' => 'select',
    '#title' => t('List'),
    '#options' => array_combine(array_values($lists), array_values($lists)),
    '#description' => t('Choose the mailing list to import them to.'),
  );
  $form[] = array(
    '#type' => 'submit',
    '#value' => t('Import'),
  );
  return $form;
}

/**
 * Implementation of hook_form_submit()
 * process import users
 */
function constant_contact_import_submit($form_id, $values) {
  $lists = constant_contact_get_lists();
  $rs = db_query("SELECT * FROM {users}");
  $fail_count = $success_count = 0;
  require_once 'ConstantContact.php';
  $constant_contact = new ConstantContact();
  $constant_contact
    ->setUsername(variable_get('constant_contact_username', ''));
  $constant_contact
    ->setPassword(variable_get('constant_contact_password', ''));
  $constant_contact
    ->setCategory($values['list']);
  while ($user = db_fetch_object($rs)) {
    if (empty($user->uid)) {
      continue;
    }
    $data = array(
      'Custom_field_1' => $user->name,
      'Custom_field_2' => $user->uid,
      'Custom_field_3' => date('r'),
    );
    if ($constant_contact
      ->add($user->mail, $data)) {
      constant_contact_save_user($user, $values['list']);
      $success_count++;
    }
    else {
      $fail_count++;
    }
  }
  if ($success_count) {
    drupal_set_message(t('!sc users sucessfully imported.', array(
      '!sc' => $success_count,
    )));
  }
  if ($fail_count) {
    drupal_set_message(t('!sc users failed to be imported.', array(
      '!sc' => $fail_count,
    )), 'error');
  }
}

/**
 * Store list in user-data, or session for anon users
 * @param string $list is the friendly name of the list
 * @param boolean $subscribe indicates whether we are adding or removing
 * @return boolean success status
 */
function constant_contact_save_user(&$user, $list, $subscribe = TRUE) {

  // save in session for anon users, and when the user logs out
  $_SESSION['constant_contact_lists'] = isset($user->constant_contact_lists) ? $user->constant_contact_lists : array();
  $_SESSION['constant_contact_lists'][$list] = $subscribe;
  if (empty($user->uid)) {
    return TRUE;
  }
  $data = array();
  $data['constant_contact_lists'] = isset($user->constant_contact_lists) ? $user->constant_contact_lists : array();
  $data['constant_contact_lists'][$list] = $subscribe;
  $u = user_save($user, $data);
  if (!empty($u)) {
    $user = $u;
    return TRUE;
  }
  else {
    drupal_set_message(t('Error storing subscribtion information.'), 'error');
    return FALSE;
  }
}

/**
 * returns boolean of whether or not the user (or anon subscriber) has signed up
 * @param string $list the friendly name of the list to check
 * @return boolean whether or not the user (or anon subscriber) has signed up
 */
function constant_contact_check_user($list) {
  global $user;
  return isset($_SESSION['constant_contact_lists']) && isset($_SESSION['constant_contact_lists'][$list]) && $_SESSION['constant_contact_lists'][$list] || isset($user->constant_contact_lists) && isset($user->constant_contact_lists[$list]) && $user->constant_contact_lists[$list];
}

/**
 * Send a confirmation email
 * @param string $address email to send to
 * @param string $list_name friedly name of list that is being subscribed to
 * @return boolean success status
 */
function constant_contact_confirmation_email($address, $list_name) {
  global $user, $base_url;
  $variables = array(
    '!list_name' => $list_name,
    '!username' => $user->name,
    '!site' => variable_get('site_name', 'Drupal'),
    '!login_url' => user_pass_reset_url($account),
    '!uri' => $base_url,
    '!uri_brief' => substr($base_url, strlen('http://')),
    '!mailto' => $address,
    '!date' => format_date(time()),
    '!login_uri' => url('user', NULL, NULL, TRUE),
    // version 6 is url('user', array('absolute' => TRUE)),
    '!uri_list_unsubscribe' => url('constant_contact/unsubscribe/' . urlencode($list_name), NULL, NULL, TRUE),
  );
  return drupal_mail('constant_contact-confirmation', $address, t(variable_get('constant_contact_email_subject', ''), $variables), t(variable_get('constant_contact_email_body', ''), $variables), variable_get('site_mail', ini_get('sendmail_from')));
}

/**
 * returns an array of available lists
 * @return array list of available lists, based on constant_contact_lists variable
 */
function constant_contact_get_lists() {
  $lists = explode("\n", variable_get('constant_contact_lists', ''));
  $newlists = array();
  foreach ($lists as $i => $list) {
    $list = trim($list);
    if (empty($list)) {
      continue;
    }
    $newlists[constant_contact_escape_listname($list)] = $list;
  }
  return $newlists;
}

/**
 * makes the list name key for a mailing-list
 * @param string $string a "friendly" name of a list
 * @return string a key name of a list
 */
function constant_contact_escape_listname($string) {
  return substr("constant_contact_blocktext_" . form_clean_id($string), 0, 48);
}

/**
 * Theme the mailing list block.
 */
function theme_constant_contact_block_form($form) {
  return drupal_render($form);
}

/**
 * PHP4 compatability functions
 */
if (!function_exists('array_combine')) {
  function array_combine($arr1, $arr2) {
    $out = array();
    foreach ($arr1 as $key1 => $value1) {
      $out[$value1] = $arr2[$key1];
    }
    return $out;
  }
}

Functions

Namesort descending Description
constant_contact_admin_settings Implementation of hook_form()
constant_contact_block Implementation of hook_block().
constant_contact_block_form Implementation of hook_form() creates the join mailing list block
constant_contact_block_form_submit Implementation of hook_form_submit() process the join mailing list block
constant_contact_block_form_validate Implementation of hook_form_validate() validate the join mailing list block
constant_contact_check_user returns boolean of whether or not the user (or anon subscriber) has signed up
constant_contact_confirmation_email Send a confirmation email
constant_contact_escape_listname makes the list name key for a mailing-list
constant_contact_get_lists returns an array of available lists
constant_contact_help Implementation of hook_help().
constant_contact_import Implementation of hook_form() import users
constant_contact_import_submit Implementation of hook_form_submit() process import users
constant_contact_menu Implementation of hook_menu().
constant_contact_perm Implementation of hook_perm().
constant_contact_save_user Store list in user-data, or session for anon users
constant_contact_unsubscribe_confirm Implementation of hook_form() confirm unsubscribe
constant_contact_unsubscribe_confirm_submit Implementation of hook_form_submit() process confirm unsubscribe
theme_constant_contact_block_form Theme the mailing list block.