View source
<?php
function webform_mailchimp_webform_component_info() {
$components = array();
$components['mailchimp'] = array(
'label' => t('Mailchimp'),
'description' => t('Sign up to a newsletter.'),
'features' => array(
'csv' => TRUE,
'email' => FALSE,
'email_address' => TRUE,
'email_name' => FALSE,
'required' => FALSE,
'conditional' => FALSE,
'group' => FALSE,
'attachment' => FALSE,
),
'file' => 'webform_mailchimp.inc',
);
return $components;
}
function webform_mailchimp_webform_submission_insert($node, $submission) {
global $user;
$mailchimp_lists = array();
if (!empty($node->webform['components'])) {
foreach ($node->webform['components'] as $key => $field) {
if ($field['type'] == 'mailchimp') {
$mailchimp_lists[] = $field['extra']['mailchimp_list'];
if ($field['extra']['use_existing_email_field'] != 'mailchimp_field') {
if ($submission->data[$key]['value'][0] == 1) {
foreach ($node->webform['components'] as $key2 => $field2) {
if ($field2['form_key'] == $field['extra']['use_existing_email_field']) {
$email_address = $submission->data[$key2]['value'][0];
}
}
}
}
else {
$email_address = $submission->data[$key]['value'][0];
}
if (!empty($field['extra']['mergefields'])) {
$mergefields_key_array = array();
$keyvaluepairs = explode("\n", $field['extra']['mergefields']);
foreach ($keyvaluepairs as $keyvalue) {
$keyvalue = trim($keyvalue);
$keyvalue = explode('|', $keyvalue);
if (is_array($keyvalue) && !empty($keyvalue[0]) && !empty($keyvalue[1])) {
$mergefields_key_array[$keyvalue[1]] = $keyvalue[0];
}
}
}
if (!empty($field['extra']['interestfields'])) {
$groupfields_key_array = array();
$keyvaluepairs = explode("\n", $field['extra']['interestfields']);
foreach ($keyvaluepairs as $keyvalue) {
$keyvalue = trim($keyvalue);
$keyvalue = explode('|', $keyvalue);
if (is_array($keyvalue) && !empty($keyvalue[0]) && !empty($keyvalue[1])) {
$groupfields_key_array[$keyvalue[1]] = $keyvalue[0];
}
}
}
}
}
$mergefields_replacements = array();
if (!empty($mergefields_key_array) && is_array($mergefields_key_array)) {
foreach ($node->webform['components'] as $key => $field) {
if (!empty($mergefields_key_array[$field['form_key']])) {
$mergefields_replacements[$mergefields_key_array[$field['form_key']]] = $submission->data[$key]['value'][0];
}
}
}
$groupfields_replacements = array();
if (!empty($groupfields_key_array) && is_array($groupfields_key_array)) {
foreach ($node->webform['components'] as $key => $field) {
if (!empty($groupfields_key_array[$field['form_key']])) {
$choices = explode("\n", $field['extra']['items']);
$sorted_choices = array();
foreach ($choices as $choice_key => $choice_val) {
$id_name = explode('|', trim($choice_val));
$sorted_choices[$id_name[0]] = $id_name[1];
}
foreach ($submission->data[$key]['value'] as $filled_out_value) {
$groupfields_replacements[$groupfields_key_array[$field['form_key']]][] = str_replace(",", "\\,", $sorted_choices[$filled_out_value]);
}
}
}
foreach ($groupfields_replacements as $groupname => $values_array) {
$mergefields_replacements['GROUPINGS'][] = array(
'name' => $groupname,
'groups' => implode(',', $values_array),
);
}
}
if ($email_address != '' && !empty($mailchimp_lists)) {
$lists = _mailchimp_get_available_lists($user);
foreach ($mailchimp_lists as $mailchimp_list) {
if (isset($lists[$mailchimp_list])) {
_mailchimp_subscribe_user($lists[$mailchimp_list], $email_address, $mergefields_replacements, TRUE, NULL);
}
}
}
}
}