From e6ed69adc1c41b9065272b3f6605157c33e27d03 Mon Sep 17 00:00:00 2001
From: Atindra Chandel <achandel@masplantiz.com>
Date: Wed, 16 Mar 2011 10:57:14 +0530
Subject: [PATCH] Ported code to drupal 7

---
 README.txt               |   13 ++
 webform_mailchimp.inc    |  369 +++++++++++++++++++++++-----------------------
 webform_mailchimp.info   |   11 +-
 webform_mailchimp.module |  107 ++++++++------
 4 files changed, 268 insertions(+), 232 deletions(-)
 create mode 100644 README.txt

diff --git a/README.txt b/README.txt
new file mode 100644
index 0000000..35130ac
--- /dev/null
+++ b/README.txt
@@ -0,0 +1,13 @@
+Welcome to Mailchimp Webform. Please see the advanced help for more information.
+
+If you're having trouble installing this module, please ensure that your
+tar program is not flattening the directory tree, truncating filenames
+or losing files.
+
+Installing:
+
+Place the entirety of this directory in sites/all/modules/webform_mailchimp
+
+Navigate to administer >> build >> modules. Enable webform_mailchimp and mailchimp module.
+
+If you are using mergevariable in your list, presently only Merge variable |FNAME| and |LNAME| have been supported. You will have to create the field 'first_name' and 'last_name' in the webform for these to be send to the subscription list. If you donot create these two fields in the form they are set as '' in the mailing list.
\ No newline at end of file
diff --git a/webform_mailchimp.inc b/webform_mailchimp.inc
index 9508a9e..a69f4c0 100644
--- a/webform_mailchimp.inc
+++ b/webform_mailchimp.inc
@@ -1,4 +1,5 @@
 <?php
+// $Id: webform_mailchimp.inc,v 1.7 2011/01/27 12:29:10 blackdog Exp $
 
 /**
  * @file
@@ -9,7 +10,7 @@
  * Implementation of _webform_defaults_component().
  */
 function _webform_defaults_mailchimp() {
-  return array(
+    return array(
     'name' => '',
     'form_key' => NULL,
     'pid' => 0,
@@ -17,189 +18,189 @@ function _webform_defaults_mailchimp() {
     'value' => '',
     'mandatory' => 0,
     'extra' => array(
-      'title_display' => 0,
-      'description' => '',
-      'attributes' => array(),
-      'mailchimp_list' => '',
-      'use_existing_email_field' => '',
+    'field_prefix' => '',
+    'field_suffix' => '',
+    'title_display' => 0,
+    'description' => '',
+    'attributes' => array(),
+    'mailchimp_list' => '',
+    'use_existing_email_field' => '',
     ),
-  );
+    );
 }
 
 /**
  * Implementation of _webform_theme_component().
  */
 function _webform_theme_mailchimp() {
-  return array(
+    return array(
     'webform_display_mailchimp' => array(
-      'arguments' => array('element' => NULL),
+    'render element' => 'element'
     ),
     'webform_mailchimp_email' => array(
-      'arguments' => array('element' => NULL),
+    'render element' => 'element'
     ),
-  );
+    );
 }
 
 /**
  * Implementation of _webform_edit_component().
  */
 function _webform_edit_mailchimp($component) {
-  module_load_include('php', 'mailchimp', 'MCAPI.class');
-  
-  drupal_add_js(drupal_get_path('module', 'webform_mailchimp') . '/webform_mailchimp.js');
-
-  $node = node_load($component['nid']);
-  $form = array();
-
-  // Only show the list selection forms if a Mailchimp API key is set.
-  $api_key = variable_get('mailchimp_api_key', FALSE);
-  $options = array('none' => 'Select list');
-  if ($api_key) {
-    $q = new MCAPI($api_key);
-
-    if (!$q->errorCode) {
-      $lists = $q->lists();
-      if (!empty($lists)) {
-        foreach ($lists as $list) {
-          $options[$list['id']] = $list['name'];
+    module_load_include('php', 'mailchimp', 'MCAPI.class');
+
+    drupal_add_js(drupal_get_path('module', 'webform_mailchimp') . '/webform_mailchimp.js');
+
+    $node = node_load($component['nid']);
+    $form = array();
+
+    // Only show the list selection forms if a Mailchimp API key is set.
+    $api_key = variable_get('mailchimp_api_key', FALSE);
+    $options = array('none' => 'Select list');
+    if ($api_key) {
+        $q = new MCAPI($api_key);
+
+        if (!$q->errorCode) {
+            $lists = $q->lists();
+            if (!empty($lists)) {
+                foreach ($lists as $list) {
+                    $options[$list['id']] = $list['name'];
+                }
+            }
+            else {
+                drupal_set_message(t('You do not have any Mailchimp lists defined.', 'error'));
+            }
         }
-      }
-      else {
-        drupal_set_message(t('You do not have any Mailchimp lists defined.', 'error'));
-      }
     }
-  }
-  else {
-    drupal_set_message(t('You need to configure your Mailchimp API key before using this component.'), 'error');
-  }
+    else {
+        drupal_set_message(t('You need to configure your Mailchimp API key before using this component.'), 'error');
+    }
+
+    $form['extra']['mailchimp_list'] = array(
+        '#type' => 'select',
+        '#title' => t('Choose list'),
+        '#default_value' => !empty($component['extra']['mailchimp_list']) ? $component['extra']['mailchimp_list'] : 0,
+        '#description' => t('Choose which list that the user can subscribe to.'),
+        '#options' => $options,
+        '#element_validate' => array('_webform_mailchimp_list_validate'),
+    );
+
+    $form['user_email'] = array(
+        '#type' => 'checkbox',
+        '#title' => t('User email as default'),
+        '#default_value' => $component['value'] == '%useremail' ? 1 : 0,
+        '#description' => t('Set the default value of this field to the user email, if he/she is logged in.'),
+        '#weight' => 0,
+        '#element_validate' => array('_webform_mailchimp_user_email_validate'),
+    );
 
-  $form['extra']['mailchimp_list'] = array(
-    '#type' => 'select',
-    '#title' => t('Choose list'),
-    '#default_value' => !empty($component['extra']['mailchimp_list']) ? $component['extra']['mailchimp_list'] : 0,
-    '#description' => t('Choose which list that the user can subscribe to.'),
-    '#options' => $options,
-    '#element_validate' => array('_webform_mailchimp_list_validate'),
-  );
-
-  $form['user_email'] = array(
-    '#type' => 'checkbox',
-    '#title' => t('User email as default'),
-    '#default_value' => $component['value'] == '%useremail' ? 1 : 0,
-    '#description' => t('Set the default value of this field to the user email, if he/she is logged in.'),
-    '#weight' => 0,
-    '#element_validate' => array('_webform_mailchimp_user_email_validate'),
-  );
-
-
-  $options = array('mailchimp_field' => 'Create field');
-
-  // Fetches existing components, checks if any of them are e-mail fields.
-  // Let's the user choose which field to use for the newsletter e-mail address.
-  if (!empty($node->webform['components'])) {
-    foreach ($node->webform['components'] AS $field) {
-      if ($field['type'] == 'email') {
-        $options[$field['form_key']] = $field['name'];
-      }
+
+    $options = array('mailchimp_field' => 'Create field');
+
+    // Fetches existing components, checks if any of them are e-mail fields.
+    // Let's the user choose which field to use for the newsletter e-mail address.
+    if (!empty($node->webform['components'])) {
+        foreach ($node->webform['components'] AS $field) {
+            if ($field['type'] == 'email') {
+                $options[$field['form_key']] = $field['name'];
+            }
+        }
     }
-  }
 
-  $form['extra']['use_existing_email_field'] = array(
-    '#type' => 'select',
-    '#title' => t('Select an existing e-mail field, or create one'),
-    '#default_value' => !empty($component['extra']['use_existing_email_field']) ? $component['extra']['use_existing_email_field'] : 'create',
-    '#description' => t('If you select an existing email field, that fields input will be used. If not, a field will be shown to the user.'),
-    '#weight' => 1,
-    '#options' => $options,
-  );
-
-  $form['extra']['checkbox_label'] = array(
-    '#type' => 'textfield',
-    '#title' => t('Checkbox label'),
-    '#default_value' => !empty($component['extra']['checkbox_label']) ? $component['extra']['checkbox_label'] : t('Subsribe to newsletter'),
-    '#description' => t('If using an existing field you can edit the default label that\'s printed here.'),
-    '#weight' => 2,
-    '#prefix' => '<div id="field_settings" style="display:none;">',
-  );
-
-  $form['extra']['checkbox_checked_by_default'] = array(
-    '#type' => 'checkbox',
-    '#title' => t('Checked by default.'),
-    '#description' => t('If using an existing field, make the checkbox checked by default.'),
-    '#default_value' => !empty($component['extra']['checkbox_checked_by_default']) ? $component['extra']['checkbox_checked_by_default'] : 0,
-    '#weight' => 3,
-    '#suffix' => '</div>',
-  );
-
-  return $form;
+    $form['extra']['use_existing_email_field'] = array(
+        '#type' => 'select',
+        '#title' => t('Select an existing e-mail field, or create one'),
+        '#default_value' => !empty($component['extra']['use_existing_email_field']) ? $component['extra']['use_existing_email_field'] : 'create',
+        '#description' => t('If you select an existing email field, that fields input will be used. If not, a field will be shown to the user.'),
+        '#weight' => 1,
+        '#options' => $options,
+    );
+
+    $form['extra']['checkbox_label'] = array(
+        '#type' => 'textfield',
+        '#title' => t('Checkbox label'),
+        '#default_value' => !empty($component['extra']['checkbox_label']) ? $component['extra']['checkbox_label'] : t('Subsribe to newsletter'),
+        '#description' => t('If using an existing field you can edit the default label that\'s printed here.'),
+        '#weight' => 2,
+    );
+
+    $form['extra']['checkbox_checked_by_default'] = array(
+        '#type' => 'checkbox',
+        '#title' => t('Checked by default.'),
+        '#description' => t('If using an existing field, make the checkbox checked by default.'),
+        '#default_value' => !empty($component['extra']['checkbox_checked_by_default']) ? $component['extra']['checkbox_checked_by_default'] : 0,
+        '#weight' => 3,
+    );
+
+    return $form;
 }
 
 /**
  * Validation function for the email edit form.
  */
 function _webform_mailchimp_user_email_validate($element, &$form_state) {
-  if ($form_state['values']['user_email']) {
-    $form_state['values']['value'] = '%useremail';
-  }
+    if ($form_state['values']['user_email']) {
+        $form_state['values']['value'] = '%useremail';
+    }
 }
 
 function _webform_mailchimp_list_validate($element, &$form_state) {
-  if ($form_state['values']['extra']['mailchimp_list'] == 'none') {
-    form_error($element, t('You need to select a Mailchimp list.'));
-  }
+    if ($form_state['values']['extra']['mailchimp_list'] == 'none') {
+        form_error($element, t('You need to select a Mailchimp list.'));
+    }
 }
 
 /**
  * Implementation of _webform_render_component().
  */
 function _webform_render_mailchimp($component, $value = NULL, $filter = TRUE) {
-  if ($value[0]) {
-    $default_value = $value[0];
-  }
-  else {
-    $default_value = _webform_filter_values($component['value']);
-  }
+    if ($value[0]) {
+        $default_value = $value[0];
+    }
+    else {
+        $default_value = _webform_filter_values($component['value']);
+    }
 
-  $element = array(
-    '#title'             => htmlspecialchars($component['name'], ENT_QUOTES),
-    '#title_display'     => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
-    '#required'          => $component['mandatory'],
-    '#weight'            => $component['weight'],
-    '#prefix'            => '<div class="webform-component-'. $component['type'] .'" id="webform-component-'. $component['form_key'] .'">',
-    '#suffix'            => '</div>',
-    '#webform_component' => $component,
-    '#theme'             => 'webform_mailchimp_email',
-    '#pre_render'        => array('webform_element_title_display'),
-    '#element_validate'  => array('_webform_validate_mailchimp_email'),
-  );
-
-  // Creates a field if the user hasn't chosen another email field.
-  if ($component['extra']['use_existing_email_field'] == 'mailchimp_field') {
-    $element['mailchimp_email_address'] = array(
-      '#type'              => 'textfield',
-      '#description'       => 'Enter your e-mailaddress.',
-      '#default_value'     => _webform_filter_values($component['value']),
-      '#attributes'        => $component['extra']['attributes'],
-      '#size'              => $size,
-    );
-  }
-  else {
-    $element['mailchimp_signup'] = array(
-      '#type'              => 'checkbox',
-      '#title'             => !empty($component['extra']['checkbox_label']) ? htmlspecialchars($component['extra']['checkbox_label'], ENT_QUOTES) : t('Subscribe to newsletter'),
-      '#default_value'     => !empty($component['extra']['checkbox_checked_by_default']) ? $component['extra']['checkbox_checked_by_default'] : 0,
-      '#attributes'        => $component['extra']['attributes'],
+    $element = array(
+        '#title' => $component['name'],
+        '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
+        '#required' => $component['mandatory'],
+        '#weight' => $component['weight'],
+        '#prefix'            => '<div class="webform-component-'. $component['type'] .'" id="webform-component-'. $component['form_key'] .'"><div class="form-item">',
+        '#suffix'            => '</div></div>',
+        '#webform_component' => $component,
+        '#theme' => 'webform_mailchimp_email',
+        '#theme_wrappers' => array('webform_element'),
+        '#pre_render'        => array('webform_element_title_display'),
+        '#element_validate'  => array('_webform_validate_mailchimp_email'),
     );
-  }
 
-  $mailchimp_list = $component['extra']['mailchimp_list'];
-  $element['mailchimp_list'] = array(
-      '#prefix' => '',
-      '#type' => 'value',
-      '#value' => $mailchimp_list,
+    // Creates a field if the user hasn't chosen another email field.
+    if ($component['extra']['use_existing_email_field'] == 'mailchimp_field') {
+        $element['mailchimp_email_address'] = array(
+            '#type'              => 'textfield',
+            '#description'       => 'Enter your e-mailaddress.',
+            '#default_value'     => _webform_filter_values($component['value']),
+            '#attributes'        => $component['extra']['attributes'],
+        );
+    }
+    else {
+        $element['mailchimp_signup'] = array(
+            '#type'              => 'checkbox',
+            '#title'             => !empty($component['extra']['checkbox_label']) ? htmlspecialchars($component['extra']['checkbox_label'], ENT_QUOTES) : t('Subscribe to newsletter'),
+            '#default_value'     => !empty($component['extra']['checkbox_checked_by_default']) ? $component['extra']['checkbox_checked_by_default'] : 0,
+            '#attributes'        => $component['extra']['attributes'],
+        );
+    }
+
+    $mailchimp_list = $component['extra']['mailchimp_list'];
+    $element['mailchimp_list'] = array(
+        '#prefix' => '',
+        '#type' => 'value',
+        '#value' => $mailchimp_list,
     );
 
-  return $element;
+    return $element;
 }
 
 /**
@@ -213,23 +214,24 @@ function _webform_render_mailchimp($component, $value = NULL, $filter = TRUE) {
  *   None. Calls a form_set_error if the e-mail is not valid.
  */
 function _webform_validate_mailchimp_email($form_element, $form_state) {
-  $component = $form_element['#webform_component'];
-  if (!empty($form_element['mailchimp_email_address']['#value']) && !valid_email_address($form_element['mailchimp_email_address']['#value'])) {
-    form_error($form_element, t('%value is not a valid email address.', array('%value' => $form_element['mailchimp_email_address']['#value'])));
-  }
-  elseif (empty($form_element['mailchimp_email_address']['#value']) && $form_element['#required']) {
-    form_error($form_element, t('E-mail address for newsletter "%name" is required.', array('%name' => $component['name'])));
-  }
+    $component = $form_element['#webform_component'];
+    if (!empty($form_element['mailchimp_email_address']['#value']) && !valid_email_address($form_element['mailchimp_email_address']['#value'])) {
+        form_error($form_element, t('%value is not a valid email address.', array('%value' => $form_element['mailchimp_email_address']['#value'])));
+    }
+    elseif (empty($form_element['mailchimp_email_address']['#value']) && $form_element['#required']) {
+        form_error($form_element, t('E-mail address for newsletter "%name" is required.', array('%name' => $component['name'])));
+    }
 }
 
 /**
  * Implementation of _webform_display_component().
  */
 function _webform_display_mailchimp($component, $value, $format = 'html') {
-  return array(
+    return array(
     '#title' => $component['name'],
     '#weight' => $component['weight'],
     '#theme' => 'webform_display_textfield',
+//    '#theme_wrappers' => $format == 'html' ? array('webform_element') : array('webform_element_text'),
     '#theme_wrappers' => $format == 'html' ? array('webform_element', 'webform_element_wrapper') : array('webform_element_text'),
     '#post_render' => array('webform_element_wrapper'),
     '#field_prefix' => $component['extra']['field_prefix'],
@@ -237,56 +239,61 @@ function _webform_display_mailchimp($component, $value, $format = 'html') {
     '#format' => $format,
     '#value' => isset($value[0]) ? $value[0] : '',
     '#webform_component' => $component,
-  );
+    );
 }
 
 /**
  * Theme the webform element.
  */
-function theme_webform_mailchimp_email($element) {
-  if (form_get_error($element)) {
-    $element['mailchimp_email_address']['#attributes']['class'] = 'error';
-  }
-  $output = '<div class="form-item">' . drupal_render($element) . '</div>';
-  $element['#type'] = 'element';
-  return theme('form_element', $element, $output);
+
+function theme_webform_mailchimp_email($variables) {
+    $element = $variables['element'];
+    if (form_get_error($element)) {
+        $element['mailchimp_email_address']['#attributes']['class'] = 'error';
+    }
+    $element['#type'] = 'element';
+    theme_form_element($variables);
 }
 
 /**
  * Format the output of data for this component.
  */
-function theme_webform_display_mailchimp($element) {
-  $value = $element['#format'] == 'html' ? check_plain($element['#value']) : $element['#value'];
-  return $value !== '' ? ($value) : ' ';
+function theme_webform_display_mailchimp($variables) {
+    $element = $variables['element'];
+    $value = $element['#format'] == 'html' ? check_plain($element['#value']) : $element['#value'];
+    return $value !== '' ? ($value) : ' ';
 }
 
 /**
  * Implementation of _webform_submit_component().
  */
 function _webform_submit_mailchimp($component, $value) {
-  if (!empty($value['mailchimp_email_address'])) {
-    $return_val = array(0 => $value['mailchimp_email_address']);
-  }
-
-  return $return_val;
+    if (!empty($value['mailchimp_email_address'])) {
+        $return_val = array(0 => $value['mailchimp_email_address']);
+        return $return_val;
+    }
+    return $value;
 }
 
 /**
  * Implementation of _webform_analysis_component().
  */
 function _webform_analysis_mailchimp($component, $sids = array()) {
-  $placeholders = count($sids) ? array_fill(0, count($sids), "'%s'") : array();
-  $sidfilter = count($sids) ? " AND sid in (" . implode(",", $placeholders) . ")" : "";
-  $query = 'SELECT data ' .
-    ' FROM {webform_submitted_data} ' .
-    ' WHERE nid = %d ' .
-    ' AND cid = %d ' . $sidfilter;
+$query = db_select('webform_submitted_data', 'wsd', array('fetch' => PDO::FETCH_ASSOC))
+    ->fields('wsd', array('no', 'data'))
+    ->condition('nid', $component['nid'])
+    ->condition('cid', $component['cid']);
+
+  if (count($sids)) {
+    $query->condition('sid', $sids, 'IN');
+  }
+
   $nonblanks = 0;
   $submissions = 0;
   $wordcount = 0;
 
-  $result = db_query($query, array_merge(array($component['nid'], $component['cid']), $sids));
-  while ($data = db_fetch_array($result)) {
+  $result = $query->execute();
+  foreach ($result as $data) {
     if (drupal_strlen(trim($data['data'])) > 0) {
       $nonblanks++;
       $wordcount += str_word_count(trim($data['data']));
@@ -304,23 +311,23 @@ function _webform_analysis_mailchimp($component, $sids = array()) {
  * Implementation of _webform_table_component().
  */
 function _webform_table_mailchimp($component, $value) {
-  return check_plain(empty($value[0]) ? '' : $value[0]);
+    return check_plain(empty($value[0]) ? '' : $value[0]);
 }
 
 /**
  * Implementation of _webform_csv_headers_component().
  */
 function _webform_csv_headers_mailchimp($component, $export_options) {
-  $header = array();
-  $header[0] = '';
-  $header[1] = '';
-  $header[2] = $component['name'];
-  return $header;
+    $header = array();
+    $header[0] = '';
+    $header[1] = '';
+    $header[2] = $component['name'];
+    return $header;
 }
 
 /**
  * Implementation of _webform_csv_data_component().
  */
 function _webform_csv_data_mailchimp($component, $export_options, $value) {
-  return !isset($value[0]) ? '' : $value[0];
+    return !isset($value[0]) ? '' : $value[0];
 }
diff --git a/webform_mailchimp.info b/webform_mailchimp.info
index 8aceb62..05eb38c 100644
--- a/webform_mailchimp.info
+++ b/webform_mailchimp.info
@@ -1,7 +1,10 @@
-
 name = Webform Mailchimp
 description = Creates a Mailchimp Webform component.
-core = 6.x
+package = Webform
+core = 7.x
+
+files[] = webform_mailchimp.inc
+files[] = webform_mailchimp.module
+
 dependencies[] = webform
-dependencies[] = mailchimp
-package = Webform
\ No newline at end of file
+dependencies[] = mailchimp
\ No newline at end of file
diff --git a/webform_mailchimp.module b/webform_mailchimp.module
index e29cf7c..b1a6d42 100644
--- a/webform_mailchimp.module
+++ b/webform_mailchimp.module
@@ -1,66 +1,79 @@
 <?php
+// $Id: webform_mailchimp.module,v 1.1 2010/12/17 15:13:21 blackdog Exp $
 /**
  * Implementation of hook_webform_component_info().
  */
 function webform_mailchimp_webform_component_info() {
-  $components = array();
+    $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',
-  );
+    $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;
+    return $components;
 }
 
 /**
  * Implementation of hook_webform_submission_insert().
  */
 function webform_mailchimp_webform_submission_insert($node, $submission) {
-  global $user;
+    global $user;
+    $first_name = "";
+    $last_name ="";
+    $email_address="";
+    if (!empty($node->webform['components'])) {
+        foreach ($node->webform['components'] AS $key => $field) {
+            if ($field['type'] == 'mailchimp') {
+                $mailchimp_list = $field['extra']['mailchimp_list'];
+                // Need to know if we should look for our own email field or another.
+                if ($field['extra']['use_existing_email_field'] != 'mailchimp_field') {
+                // If using existing field, check if the checkbox was checked
+                    if ($submission->data[$key]['value']['mailchimp_signup'] == 1) {
+                    // Loop through components again to find our email field
+                        foreach($node->webform['components'] AS $key2 => $field2) {
+                            if ($field2['form_key'] == $field['extra']['use_existing_email_field']) {
+                            // Get the form value
+                                $email_address = $submission->data[$key2]['value'][0];
+                            }
+                            if ($field2['form_key'] == 'first_name') {
+                            // Get the form value for first name
+                                $first_name = $submission->data[$key2]['value'][0];
+                            }
+                            if ($field2['form_key'] == 'last_name') {
+                            // Get the form value for last name
+                                $last_name = $submission->data[$key2]['value'][0];
+                            }
+                        
+                        }
 
-  if (!empty($node->webform['components'])) {
-    foreach ($node->webform['components'] AS $key => $field) {
-      if ($field['type'] == 'mailchimp') {
-        $mailchimp_list = $field['extra']['mailchimp_list'];
-        // Need to know if we should look for our own email field or another.
-        if ($field['extra']['use_existing_email_field'] != 'mailchimp_field') {
-          // If using existing field, check if the checkbox was checked
-          if ($submission->data[$key]['value']['mailchimp_signup'] == 1) {
-            // Loop through components again to find our email field
-            foreach($node->webform['components'] AS $key2 => $field2) {
-              if ($field2['form_key'] == $field['extra']['use_existing_email_field']) {
-                // Get the form value
-                $email_address = $submission->data[$key2]['value'][0];
-              }
+                    }
+                }
+                // We have our own email field
+                else {
+                    $email_address = $submission->data[$key]['value'][0];
+                }
             }
-          }
         }
-        // We have our own email field
-        else {
-          $email_address = $submission->data[$key]['value'][0];
+        // If we have an email address and a list is set, try to subscribe the user.
+        if ($email_address != '' && $mailchimp_list != '') {
+        // Fetches available lists, so we get the list object.
+            $lists = _mailchimp_get_available_lists($user);
+            if (isset($lists[$mailchimp_list])) {
+                $merge_vars = array('EMAIL' => $email_address, 'FNAME' => $first_name, 'LNAME' => $last_name);
+                _mailchimp_subscribe_user($lists[$mailchimp_list], $email_address, $merge_vars, TRUE, NULL);
+            }
         }
-      }
-    }
-
-    // If we have an email address and a list is set, try to subscribe the user.
-    if ($email_address != '' && $mailchimp_list != '') {
-      // Fetches available lists, so we get the list object.
-      $lists = _mailchimp_get_available_lists($user);
-      if (isset($lists[$mailchimp_list])) {
-        _mailchimp_subscribe_user($lists[$mailchimp_list], $email_address, array(), TRUE, NULL);
-      }
     }
-  }
 }
-- 
1.7.4.msysgit.0

