function mailchimp_signup_subscribe_form_submit in Mailchimp 7.4
Same name and namespace in other branches
- 7.5 modules/mailchimp_signup/mailchimp_signup.module \mailchimp_signup_subscribe_form_submit()
- 7.3 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 408 - 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.
$mergevars = array_filter($form_state['values']['mergevars']);
// Format mergevar values.
foreach ($mergevars as $mergevar_key => $mergevar_value) {
if (isset($form['mergevars'][$mergevar_key]['#attributes']['type']) && $form['mergevars'][$mergevar_key]['#attributes']['type'] == 'tel') {
$mergevars[$mergevar_key] = mailchimp_format_phone_number($mergevar_value);
}
}
$email = $mergevars['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'];
$interest_groups = isset($list_choices['interest_groups']) ? $list_choices['interest_groups'] : array();
// Parse interests from interest groups.
$interests = array();
foreach ($interest_groups as $interest_group) {
// Check if interest group is an array. Checkbox options will appear in an
// array, and unset options will be an empty array.
if (is_array($interest_group)) {
foreach ($interest_group as $interest_id => $interest_status) {
$interests[$interest_id] = $interest_status !== 0;
}
}
else {
if ($interest_group != 0) {
$interests[$interest_group] = TRUE;
}
}
}
$result = mailchimp_subscribe($list_id, $email, $mergevars, $interests, $signup->settings['doublein']);
// Let other modules act on the results in hook_form_alter.
$form_state['mailchimp_results'][$list_id] = $result;
if (empty($result) || isset($result->success) && $result->success === FALSE) {
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) && !empty($signup->settings['confirmation_message'])) {
$message = mailchimp_signup_tt("mailchimp_signup:mailchimp_signup:{$signup->name}:confirmation_message", check_plain($signup->settings['confirmation_message']));
drupal_set_message($message, 'status');
}
if (!empty($signup->settings['destination'])) {
$form_state['redirect'] = $signup->settings['destination'];
}
}