You are here

function simplenews_subscriptions_page_form in Simplenews 7

Same name and namespace in other branches
  1. 6.2 includes/simplenews.subscription.inc \simplenews_subscriptions_page_form()
  2. 7.2 includes/simplenews.subscription.inc \simplenews_subscriptions_page_form()

FAPI PAGE subscription form.

See also

simplenews_subscriptions_page_form_validate()

simplenews_subscriptions_page_form_submit()

1 string reference to 'simplenews_subscriptions_page_form'
simplenews_menu in ./simplenews.module
Implements hook_menu().

File

includes/simplenews.subscription.inc, line 208
(Un)subscription and (un)subscription confirmation

Code

function simplenews_subscriptions_page_form($form, &$form_state, $code = NULL) {
  global $user;
  $subscriber = $mail = FALSE;
  if (!empty($user->mail)) {
    $subscriber = simplenews_subscriber_load_by_mail($user->mail);
    $mail = $user->mail;
  }
  elseif ($code) {
    if (!($subscriber = simplenews_subscriber_load_by_hash($code))) {
      drupal_not_found();
      return;
    }
    $mail = $subscriber->mail;
  }
  $form = array();
  $options = array();
  $default_value = array();

  // Get newsletters for subscription form checkboxes.
  // Newsletters with opt-in/out method 'hidden' will not be listed.
  foreach (simplenews_category_get_visible() as $newsletter) {
    $options[$newsletter->tid] = check_plain(_simplenews_newsletter_name($newsletter));
    $default_value[$newsletter->tid] = FALSE;
  }
  if ($subscriber) {

    // If there is an existing subscriber object, use the existing settings.
    $default_value = array_merge($default_value, $subscriber->tids);
  }
  $form['subscriptions'] = array(
    '#type' => 'fieldset',
  );
  $form['subscriptions']['newsletters'] = array(
    '#type' => 'checkboxes',
    '#options' => $options,
    '#default_value' => $default_value,
  );

  // If we have a mail address, which is either from a logged in user or a
  // subscriber identified through the hash code, display the mail address
  // instead of a textfield. Anonymous uses will still have to confirm any
  // changes.
  if ($mail) {
    $form['subscriptions']['#title'] = t('Subscriptions for %mail', array(
      '%mail' => $mail,
    ));
    $form['subscriptions']['#description'] = t('Check the newsletters you want to subscribe to. Uncheck the ones you want to unsubscribe from.');
    $form['subscriptions']['mail'] = array(
      '#type' => 'value',
      '#value' => $mail,
    );
    $form['update'] = array(
      '#type' => 'submit',
      '#value' => t('Update'),
      '#weight' => 20,
    );
  }
  else {
    $form['subscriptions']['#title'] = t('Manage your newsletter subscriptions');
    $form['subscriptions']['#description'] = t('Select the newsletter(s) to which you want to subscribe or unsubscribe.');
    $form['subscriptions']['mail'] = array(
      '#type' => 'textfield',
      '#title' => t('E-mail'),
      '#size' => 20,
      '#maxlength' => 128,
      '#weight' => 10,
      '#required' => TRUE,
    );
    $form['subscribe'] = array(
      '#type' => 'submit',
      '#value' => t('Subscribe'),
      '#weight' => 20,
    );
    $form['unsubscribe'] = array(
      '#type' => 'submit',
      '#value' => t('Unsubscribe'),
      '#weight' => 30,
    );
  }
  $form['#validate'][] = 'simplenews_subscriptions_page_form_validate';
  $form['#submit'][] = 'simplenews_subscriptions_page_form_submit';
  return $form;
}