You are here

function mailchimp_auth_newsletter_form in Mailchimp 7

Same name and namespace in other branches
  1. 6.2 mailchimp.module \mailchimp_auth_newsletter_form()

Return a form element for a single newsletter.

1 call to mailchimp_auth_newsletter_form()
mailchimp_subscribe_auth_form in ./mailchimp.module
Add mailchimp form fields to account and registration forms

File

./mailchimp.module, line 211
Mailchimp module.

Code

function mailchimp_auth_newsletter_form(&$form, $list, $account, $q) {
  $is_subscribed = FALSE;
  if ($account && $account->uid > 0) {
    $memberinfo = $q
      ->listMemberInfo($list->id, $account->mail);
    $is_subscribed = $memberinfo['status'] == 'subscribed';
    $default_value = $is_subscribed;
  }
  else {
    $default_value = $list->listtype == MAILCHIMP_LISTTYPE_OPTOUT ? TRUE : FALSE;
  }

  // wrap in a div
  $form['wrapper' . $list->id] = array(
    '#prefix' => '<div class="mailchimp-newsletter-wrapper">',
    '#suffix' => '</div>',
  );
  $form['wrapper' . $list->id]['mailchimp_list_' . $list->id] = array(
    '#type' => 'checkbox',
    '#title' => $list->label ? t($list->label) : t('Subscribe to the @newsletter newsletter', array(
      '@newsletter' => $list->name,
    )),
    '#default_value' => $default_value,
    '#description' => $list->description,
    '#attributes' => array(
      'class' => array(
        'mailchimp-newsletter-checkbox-' . $list->id,
      ),
    ),
  );

  // present interest groups
  if (variable_get('mailchimp_interest_groups_user_forms', FALSE)) {
    if ($intgroup = $q
      ->listInterestGroups($list->id)) {
      switch ($intgroup['form_field']) {
        case 'radio':
          $field_type = 'radios';
          break;
        case 'checkbox':
          $field_type = 'checkboxes';
          break;
        default:
          $field_type = $intgroup['form_field'];
      }
      $options = array();
      foreach ((array) $intgroup['groups'] as $group) {
        $options[$group] = $group;
      }
      $form['wrapper' . $list->id]['mailchimp_list_' . $list->id . '_INTERESTS'] = array(
        '#type' => $field_type,
        '#title' => $intgroup['name'],
        '#options' => $options,
        '#default_value' => $is_subscribed ? explode(",", str_replace(', ', ',', $memberinfo['merges']['INTERESTS'])) : array(),
        '#attributes' => array(
          'class' => array(
            'mailchimp-newsletter-interests-' . $list->id,
          ),
        ),
      );
      drupal_add_js('$(document).ready(function(){
        if($(".mailchimp-newsletter-checkbox-' . $list->id . '").attr("checked")){
          $(".mailchimp-newsletter-interests-' . $list->id . '").parent(".form-item").show();
        } else {
          $(".mailchimp-newsletter-interests-' . $list->id . '").parent(".form-item").hide();
        }
        $(".mailchimp-newsletter-checkbox-' . $list->id . '").click(function(){ $(".mailchimp-newsletter-interests-' . $list->id . '").parent(".form-item").toggle("slow");});
      });', 'inline');
    }
  }
  return $form;
}