You are here

function newsletter_manage_subscriptions_form in Newsletter 7

1 string reference to 'newsletter_manage_subscriptions_form'
newsletter_menu in ./newsletter.module
Implements hook_menu().

File

includes/newsletter.pages.inc, line 12
Functions and forms for the frontend part of newsletter module.

Code

function newsletter_manage_subscriptions_form($form, &$form_state, $user = NULL) {
  if (!isset($user)) {
    global $user;
    $subscriber = entity_get_controller('newsletter_subscriber')
      ->create();
    $subscriber->email = isset($_GET['email']) ? check_plain($_GET['email']) : @$user->mail;
  }
  elseif (arg(0) == 'newsletter' && arg(1) == 'edit') {
    unset($user);
    global $user;
    $subscribers = newsletter_subscriber_load(array(), array(
      'hash' => arg(2),
    ));
    $subscriber = !empty($subscribers) ? array_pop($subscribers) : entity_get_controller('newsletter_subscriber')
      ->create();
  }
  else {
    $subscribers = isset($user->uid) ? newsletter_subscriber_load(array(), array(
      'uid' => $user->uid,
    )) : array();
    $subscriber = !empty($subscribers) ? array_pop($subscribers) : entity_get_controller('newsletter_subscriber')
      ->create();
  }
  newsletter_add_js();
  $show_email = variable_get('newsletter_show_email', TRUE);
  $form['uid'] = array(
    '#type' => 'hidden',
    '#value' => isset($user->uid) ? $user->uid : NULL,
  );
  $form['nsid'] = array(
    '#type' => 'hidden',
    '#value' => isset($subscriber->nsid) ? $subscriber->nsid : NULL,
  );
  if ($show_email) {
    $form['mail'] = array(
      '#attributes' => array(
        'class' => array(
          'container-inline',
        ),
      ),
      '#type' => 'fieldset',
      '#title' => t('E-mail'),
      '#required' => TRUE,
      '#weight' => -10,
    );
  }
  $form['mail']['email'] = array(
    '#type' => $show_email ? 'textfield' : 'hidden',
    '#default_value' => !empty($subscriber->email) ? $subscriber->email : @$user->mail,
    '#required' => TRUE,
    '#size' => 20,
  );
  $personal_info_form = variable_get('newsletter_personal_info_form');
  if (in_array('show', $personal_info_form) || in_array('require', $personal_info_form)) {
    $form['info'] = array(
      '#type' => 'fieldset',
      '#title' => t('Personal info'),
      '#weight' => 5,
    );
  }
  foreach ($personal_info_form as $info_type => $value) {
    if ($value != 'hidden') {
      switch ($info_type) {
        case 'firstname':
          $form['info']['firstname'] = array(
            '#type' => 'textfield',
            '#title' => t('First name'),
            '#size' => 40,
            '#required' => (bool) ($value == 'require'),
            '#default_value' => $subscriber->firstname,
          );
          break;
        case 'lastname':
          $form['info']['lastname'] = array(
            '#type' => 'textfield',
            '#title' => t('Last name'),
            '#size' => 40,
            '#required' => (bool) ($value == 'require'),
            '#default_value' => $subscriber->lastname,
          );
          break;
        case 'gender':
          $form['info']['gender'] = array(
            '#type' => 'select',
            '#title' => t('Gender'),
            '#options' => array(
              'male' => t('Male'),
              'female' => t('Female'),
              'other' => t('Other'),
            ),
            '#empty_option' => t('- Select -'),
            '#required' => (bool) ($value == 'require'),
            '#default_value' => $subscriber->gender,
          );
          break;
        case 'receive_format':
          $form['info']['receive_format'] = array(
            '#type' => 'select',
            '#title' => t('Preferred format'),
            '#options' => array(
              'html' => t('HTML'),
              'plain' => t('Plain Text'),
            ),
            '#empty_option' => t('- Select -'),
            '#required' => (bool) ($value == 'require'),
            '#default_value' => $subscriber->receive_format,
          );
          break;
      }
    }
  }
  field_attach_form('newsletter_subscriber', $subscriber, $form, $form_state);
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => isset($subscriber->nsid) ? t('Update subscription') : t('Subscribe'),
    '#weight' => 10,
  );
  if (isset($subscriber->nsid)) {
    $form['unsubscribe'] = array(
      '#type' => 'link',
      '#title' => t('Unsubscribe'),
      '#href' => 'newsletter/unsubscribe/' . $subscriber->hash,
      '#weight' => 10,
    );
  }
  return $form;
}