Commit fedf3cbf authored by Hans Nilsson's avatar Hans Nilsson
Browse files

Feature [#1052290] by weseze - Add merge fields.

parent 3ce008c5
Loading
Loading
Loading
Loading
+16 −0
Original line number Diff line number Diff line
@@ -131,6 +131,22 @@ function _webform_edit_mailchimp($component) {
    '#suffix' => '</div>',
  );

  $form['extra']['mergefields'] = array(
    '#type' => 'textarea',
    '#title' => t('Mailchimp merge fields'),
    '#description' => t('Enter one key|value pair per line. Use the MailChimp field tag as the key and the webform field key as the value. For example "FNAME|firstname"'),
    '#default_value' => !empty($component['extra']['mergefields']) ? $component['extra']['mergefields'] : '',
    '#weight' => 5,
  );

  $form['extra']['interestfields'] = array(
    '#type' => 'textarea',
    '#title' => t('Mailchimp interests fields'),
    '#description' => t('Enter one key|value pair per line. Use the MailChimp group name as the key and the webform field (checkboxes or dropdown) key as the value. For example "FOOD|types_of_food_you_like"'),
    '#default_value' => !empty($component['extra']['interestfields']) ? $component['extra']['interestfields'] : '',
    '#weight' => 6,
  );

  return $form;
}

+68 −1
Original line number Diff line number Diff line
@@ -51,6 +51,73 @@ function webform_mailchimp_webform_submission_insert($node, $submission) {
        else {
          $email_address = $submission->data[$key]['value'][0];
        }

        // Retrieve mergefields and create a merge array with key webform key and value Mailchimp merge tag
        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];
            }
          }
        }

        // Retrieve interestfields and create a merge array with key webform key and value Mailchimp group name
        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();
    // Create the mergefield 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']])) {
          // This is probably a bit to easy... The delta value is not taken into account
          $mergefields_replacements[$mergefields_key_array[$field['form_key']]] = $submission->data[$key]['value'][0];
        }
      }
    }

    $groupfields_replacements = array();
    // Create the mergefield 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']])) {
          // We are dealing with checkboxes, dropdowns and have received the id instead of the raw value
          // So we need to extract the value and send that instead
          $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),
        );
      }
    }

@@ -59,7 +126,7 @@ function webform_mailchimp_webform_submission_insert($node, $submission) {
      // 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);
        _mailchimp_subscribe_user($lists[$mailchimp_list], $email_address, $mergefields_replacements, TRUE, NULL);
      }
    }
  }