You are here

function _webform_edit_mailchimp in Webform Mailchimp 6

Same name and namespace in other branches
  1. 7.4 webform_mailchimp.inc \_webform_edit_mailchimp()
  2. 7 webform_mailchimp.inc \_webform_edit_mailchimp()
  3. 7.2 webform_mailchimp.inc \_webform_edit_mailchimp()

Implementation of _webform_edit_component().

File

./webform_mailchimp.inc, line 46
Webform module Mailchimp component.

Code

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'];
        }
      }
      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');
  }
  $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'];
      }
    }
  }
  $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('Subscribe 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,
  );
  $form['extra']['checkbox_hidden'] = array(
    '#type' => 'checkbox',
    '#title' => t('Hide the checkbox.'),
    '#description' => t('Check this if users do not need to be able to set or unset wether they want to subscribe. Note that you still need to check wether the checkbox should be checked by default.'),
    '#default_value' => !empty($component['extra']['checkbox_hidden']) ? $component['extra']['checkbox_hidden'] : 0,
    '#weight' => 4,
    '#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;
}