You are here

function mailchimp_auth_newsletter_form in Mailchimp 6.2

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

Called from mailchimp_subscribe_auth_form() and embeds the form elements for a single newseltter.

Parameters

string $form :

string $list :

string $account :

string $q :

2 calls to mailchimp_auth_newsletter_form()
mailchimp_subscribe_auth_form in ./mailchimp.module
Maichimp authenticated user subscription form.
mailchimp_user in ./mailchimp.module
Implementation of hook_user

File

./mailchimp.module, line 226
Mailchimp module.

Code

function mailchimp_auth_newsletter_form(&$form, $list, $account, $q) {
  $is_subscribed = FALSE;
  $member_interests = array();

  // ignore required lists, handled by hook_user
  if ($list->listtype !== MAILCHIMP_LISTTYPE_REQUIRED) {
    if ($account && $account->uid) {
      $memberinfo = $q
        ->listMemberInfo($list->id, $account->mail);
      $is_subscribed = $memberinfo['status'] == 'subscribed';
      $member_interests = isset($memberinfo['merges']['GROUPINGS']) ? $memberinfo['merges']['GROUPINGS'] : array();
      $default_value = $is_subscribed;
    }
    else {
      $default_value = $is_subscribed = $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' => t($list->description),
      '#attributes' => array(
        'class' => 'mailchimp-newsletter-checkbox-' . $list->id,
      ),
    );

    // present interest groups
    if (variable_get('mailchimp_interest_groups_user_forms', FALSE)) {
      $interests = _mailchimp_interest_groups_element($q, $list->id, $member_interests, $is_subscribed);
      if (!empty($interests)) {
        $form['wrapper' . $list->id] = array_merge($form['wrapper' . $list->id], $interests);
        drupal_add_js('Drupal.behaviors.mailchimp = function(context) {
          $(".mailchimp-newsletter-checkbox-' . $list->id . '").click(function(){
            Drupal.toggleFieldset(".mailchimp-newsletter-interests-' . $list->id . '");
          });
        }', 'inline');
      }
    }
  }
  return $form;
}