You are here

function mailchimp_signup_subscribe_form_submit in Mailchimp 7.3

Same name and namespace in other branches
  1. 7.5 modules/mailchimp_signup/mailchimp_signup.module \mailchimp_signup_subscribe_form_submit()
  2. 7.4 modules/mailchimp_signup/mailchimp_signup.module \mailchimp_signup_subscribe_form_submit()

Submit handler to add users to lists on subscription form submission.

File

modules/mailchimp_signup/mailchimp_signup.module, line 357
Mailchimp Signup module. Allows creation of signup forms integrated with Mailchimp.

Code

function mailchimp_signup_subscribe_form_submit($form, &$form_state) {
  $signup = reset($form_state['build_info']['args']);
  $list_details = mailchimp_get_lists($signup->mc_lists);
  $subscribe_lists = array();

  // Filter out blank fields so we don't erase values on the Mailchimp side.
  $merge_values = array_filter($form_state['values']['mergevars']);
  $email = $merge_values['EMAIL'];

  // If we only have one list we won't have checkbox values to investigate:
  if (count($signup->mc_lists) == 1) {
    $subscribe_lists[0] = array(
      'subscribe' => reset($signup->mc_lists),
      'interest_groups' => isset($form_state['values']['mailchimp_lists']['interest_groups']) ? $form_state['values']['mailchimp_lists']['interest_groups'] : NULL,
    );
  }
  else {

    // We can look at the checkbox values now:
    foreach ($form_state['values']['mailchimp_lists'] as $list) {
      if ($list['subscribe']) {
        $subscribe_lists[] = $list;
      }
    }
  }
  $successes = array();

  // Loop through the selected lists and try to subscribe:
  foreach ($subscribe_lists as $list_choices) {
    $list_id = $list_choices['subscribe'];
    $mergevars = $merge_values;
    if (isset($list_choices['interest_groups'])) {
      $mergevars['GROUPINGS'] = mailchimp_reformat_groupings($list_choices['interest_groups']);
    }
    $result = mailchimp_subscribe($list_id, $email, $mergevars, $signup->settings['doublein'], $signup->settings['send_welcome']);

    // Let other modules act on the results in hook_form_alter.
    $form_state['mailchimp_results'] = $result;
    if (empty($result)) {
      drupal_set_message(t('There was a problem with your newsletter signup to %list.', array(
        '%list' => $list_details[$list_id]['name'],
      )), 'warning');
    }
    else {
      $successes[] = $list_details[$list_id]['name'];
    }
  }
  if (count($successes) && isset($signup->settings['confirmation_message']) && strlen($signup->settings['confirmation_message'])) {
    $message = mailchimp_signup_tt("field:mailchimp_signup:form:{$signup->mcs_id}:confirmation_message", check_plain($signup->settings['confirmation_message']));
    drupal_set_message($message, 'status');
  }
  if (!empty($signup->settings['destination'])) {
    $form_state['redirect'] = $signup->settings['destination'];
  }
}