You are here

function _mailchimp_subscribe_user in Mailchimp 7

Same name and namespace in other branches
  1. 5.2 mailchimp.module \_mailchimp_subscribe_user()
  2. 5 mailchimp.module \_mailchimp_subscribe_user()
  3. 6.2 mailchimp.module \_mailchimp_subscribe_user()
  4. 6 mailchimp.module \_mailchimp_subscribe_user()

Subscribe or update a user in a given list

4 calls to _mailchimp_subscribe_user()
mailchimp_subscribe_anon_form_submit in ./mailchimp.module
Submit handler for anonymous subscription form
mailchimp_subscribe_auth_form_submit in ./mailchimp.module
Submit handler to add users to lists when editing/creating a user
mailchimp_user_insert in ./mailchimp.module
Implements hook_user_insert().
mailchimp_user_update in ./mailchimp.module
Implements hook_user_update().

File

./mailchimp.module, line 805
Mailchimp module.

Code

function _mailchimp_subscribe_user($list, $email, $merge_vars, $message = TRUE, $q = NULL) {
  if ($q || ($q = _mailchimp_get_api_object())) {
    $double_optin = $list->doublein;

    // required by MC API. An empty array will fail.
    if (empty($merge_vars)) {
      $merge_vars = '';
    }
    if (_mailchimp_is_subscribed($list->id, $email, $q)) {
      $action = 'updated in';
      $success = $q
        ->listUpdateMember($list->id, $email, $merge_vars);
      if ($success && $message) {
        drupal_set_message(t('You have updated your settings in %list.', array(
          '%list' => $list->name,
        )));
      }
    }
    else {
      $action = 'subscribed to';
      $success = $q
        ->listSubscribe($list->id, $email, $merge_vars, 'html', $double_optin, TRUE);
      if ($message && $success && $double_optin) {
        drupal_set_message(t('You have chosen to subscribe to %list. An email will be sent to your address. Click the link in the email to confirm the subscription.', array(
          '%list' => $list->name,
        )));
      }
      elseif ($message && $success) {
        drupal_set_message(t('You have subscribed to %list.', array(
          '%list' => $list->name,
        )));
      }
    }
  }
  if ($success) {
    watchdog('mailchimp', '@email was @action list @list.', array(
      '@action' => $action,
      '@email' => $merge_vars['EMAIL'],
      '@list' => $list->name,
    ), WATCHDOG_NOTICE);
  }
  else {
    watchdog('mailchimp', 'A problem occurred subscribing @email to list @list. Message: @msg', array(
      '@email' => $merge_vars['EMAIL'],
      '@list' => $list->name,
      '@msg' => $q->errorMessage,
    ), WATCHDOG_WARNING);
  }
  return $success;
}