You are here

function simplenews_subscriptions_page_form_submit in Simplenews 6.2

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

FAPI PAGE subscription form_submit.

2 string references to 'simplenews_subscriptions_page_form_submit'
simplenews_subscriptions_multi_block_form in includes/simplenews.subscription.inc
FAPI MULTI BLOCK subscription form.
simplenews_subscriptions_page_form in includes/simplenews.subscription.inc
FAPI PAGE subscription form.

File

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

Code

function simplenews_subscriptions_page_form_submit($form, &$form_state) {
  global $user;
  if (isset($user->mail) && $user->mail == $form_state['values']['mail']) {
    $account = $user;
  }
  else {
    $account = (object) array(
      'mail' => $form_state['values']['mail'],
    );
  }
  switch ($form_state['values']['op']) {
    case t('Update'):

      // We first subscribe, then unsubscribe. This prevents deletion of subscriptions
      // when unsubscribed from the
      arsort($form_state['values']['newsletters'], SORT_NUMERIC);
      foreach ($form_state['values']['newsletters'] as $tid => $checked) {
        if ($checked) {
          simplenews_subscribe_user($form_state['values']['mail'], $tid, FALSE, 'website');
        }
        else {
          simplenews_unsubscribe_user($form_state['values']['mail'], $tid, FALSE, 'website');
        }
      }
      drupal_set_message(t('The newsletter subscriptions for %mail have been updated.', array(
        '%mail' => $form_state['values']['mail'],
      )));
      break;
    case t('Subscribe'):
      $confirm_any = FALSE;
      foreach ($form_state['values']['newsletters'] as $tid => $checked) {
        $confirm = FALSE;
        if ($checked) {
          if (empty($account->uid)) {
            $confirm = variable_get('simplenews_opt_inout_' . $tid, 'double') == 'double';
            $confirm_any |= $confirm;
          }
          simplenews_subscribe_user($form_state['values']['mail'], $tid, $confirm, 'website');
        }
      }
      if ($confirm_any) {
        drupal_set_message(t('You will receive a confirmation email shortly containing further instructions on how to complete your subscription.'));
      }
      else {
        drupal_set_message(t('You were added to the chosen mailing list.'));
      }
      break;
    case t('Unsubscribe'):
      $confirm_any = FALSE;
      foreach ($form_state['values']['newsletters'] as $tid => $checked) {
        $confirm = FALSE;
        if ($checked) {
          if (!$account->uid) {
            $confirm = variable_get('simplenews_opt_inout_' . $tid, 'double') == 'double';
            $confirm_any |= $confirm;
          }
          simplenews_unsubscribe_user($form_state['values']['mail'], $tid, $confirm, 'website');
        }
      }
      if ($confirm_any) {
        drupal_set_message(t('You will receive a confirmation email shortly containing further instructions on how to cancel your subscription.'));
      }
      else {
        drupal_set_message(t('You were added to the chosen mailing list.'));
      }
      break;
  }
}