You are here

function _mailchimp_subscribe_user in Mailchimp 6.2

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

Subscribe or update a user in a givne list

3 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 in ./mailchimp.module
Implementation of hook_user

File

./mailchimp.module, line 960
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 %email\'s settings in the %list list.', array(
          '%email' => $email,
          '%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 %email to the %list list.', array(
          '%email' => $email,
          '%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', '@email has failed to subscribed to @list.', array(
      '@email' => $merge_vars['EMAIL'],
      '@list' => $list->name,
    ), WATCHDOG_WARNING);
  }
  return $success;
}