You are here

function constant_contact_signup_form in Constant Contact 7.3

Same name and namespace in other branches
  1. 6.3 constant_contact.module \constant_contact_signup_form()
  2. 6.2 constant_contact.module \constant_contact_signup_form()

Form builder for the custom signup form, added using a block.

1 string reference to 'constant_contact_signup_form'
constant_contact_forms in ./constant_contact.module
Implements hook_forms().

File

./constant_contact.module, line 998

Code

function constant_contact_signup_form($form, &$form_state, $delta) {
  $cc = constant_contact_create_object();
  $show_selection = variable_get('cc_block_show_list_selection_' . $delta, CC_BLOCK_SHOW_LIST_SELECTION);
  $selection_format = variable_get('cc_block_list_selection_format_' . $delta, CC_LIST_SELECTION_FORMAT);
  $show_format_choice = variable_get('cc_show_format_choice_' . $delta, CC_SHOW_FORMAT_CHOICE);
  $default_subscribe_format = variable_get('cc_subscribe_format_' . $delta, CC_SUBSCRIBE_FORMAT);
  $form_block_fields = variable_get('cc_form_block_fields_' . $delta, array());
  $email_field_position = variable_get('cc_email_field_position_' . $delta, 1);
  if (is_array($form_block_fields)) {

    // Dummy entry so we get enough for the email field.
    $form_block_fields['_email_' . $delta] = 1;
    $current_pos = 1;
    foreach ($form_block_fields as $field => $enabled) {
      if ($enabled) {
        if ($current_pos == $email_field_position) {
          $form['cc_email_' . $delta] = array(
            '#type' => 'textfield',
            '#title' => t('Email'),
            '#size' => 30,
            '#required' => TRUE,
          );
        }
        if ($field != '_email_' . $delta) {
          $fieldname = str_replace(' ', '', $field);
          $form["cc_{$fieldname}_{$delta}"] = array(
            '#type' => 'textfield',
            '#title' => check_plain($field),
            '#size' => 30,
            '#required' => TRUE,
            '#default_value' => isset($form_state['input']["cc_{$fieldname}_{$delta}"]) ? $form_state['input']["cc_{$fieldname}_{$delta}"] : '',
          );
        }
        ++$current_pos;
      }
    }
  }
  if ($show_selection && is_object($cc)) {
    $show_lists = variable_get('cc_block_lists_' . $delta, array());
    $lists = constant_contact_get_lists($cc);
    $options = array();
    foreach ($lists as $list_id => $list_name) {
      if (in_array($list_id, $show_lists)) {
        $options[$list_id] = $list_name;
      }
    }
    if (count($options) > 0) {
      if ($selection_format == 'select') {
        $field_type = 'select';
      }
      else {
        $field_type = 'checkboxes';
      }
      $form['cc_newsletter_lists_' . $delta] = array(
        '#type' => $field_type,
        '#title' => check_plain(variable_get('cc_list_selection_title_' . $delta, CC_SIGNUP_TITLE)),
        '#description' => check_plain(variable_get('cc_list_selection_description_' . $delta, CC_SIGNUP_DESCRIPTION)),
        '#options' => $options,
        '#required' => TRUE,
      );
      if ($selection_format == 'select') {
        $field_size = $options && count($options) > 25 ? 25 : count($options);
        $form['cc_newsletter_lists_' . $delta]['#multiple'] = TRUE;
        $form['cc_newsletter_lists_' . $delta]['#size'] = $field_size;
      }
    }
  }
  if ($show_format_choice) {
    $form['cc_email_format_' . $delta] = array(
      '#type' => 'radios',
      '#title' => t('Email Format'),
      '#description' => 'You can receive emails in Text or HTML format',
      '#default_value' => $default_subscribe_format,
      '#options' => $default_subscribe_format == 'HTML' ? array(
        'HTML' => t('HTML'),
        'Text' => t('Text'),
      ) : array(
        'Text' => t('Text'),
        'HTML' => t('HTML'),
      ),
    );
  }
  $form['#id'] = "cc_signup_form_{$delta}";
  $form['delta'] = array(
    '#type' => 'hidden',
    '#value' => $delta,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#name' => "submit_{$delta}",
    '#value' => t('Sign up'),
  );
  return $form;
}