You are here

function constant_contact_unsubscribe_confirm_submit in Constant Contact 5

Same name and namespace in other branches
  1. 6 constant_contact.module \constant_contact_unsubscribe_confirm_submit()

Implementation of hook_form_submit() process confirm unsubscribe

File

./constant_contact.module, line 340

Code

function constant_contact_unsubscribe_confirm_submit($form_id, $values) {
  global $user;
  if (!constant_contact_check_user($values['list'])) {
    if (empty($user->uid)) {
      drupal_set_message(t('You must be logged in to unsubscribe.'), 'error');
      return drupal_goto('user/user', 'destination=constant_contact/unsubscribe/' . urlencode($values['list']));
    }
    else {
      drupal_set_message(t('You are logged in, but your email does not appear to be subscribed to that list.'), 'error');
      return drupal_goto($_GET['destination'] ? $_GET['destination'] : '<front>');
    }
  }
  $lists = constant_contact_get_lists();
  if (empty($values['list']) || !array_search($values['list'], $lists)) {
    drupal_set_message(t('Invalid list name.'), 'error');
    return drupal_goto($_GET['destination'] ? $_GET['destination'] : '<front>');
  }
  require_once 'ConstantContact.php';
  $constant_contact = new ConstantContact();
  $constant_contact
    ->setUsername(variable_get('constant_contact_username', ''));
  $constant_contact
    ->setPassword(variable_get('constant_contact_password', ''));
  $constant_contact
    ->setCategory($values['list']);
  if ($constant_contact
    ->remove($user->mail)) {
    $type = 'status';
    $message = t('You have been removed from %list mailing list.', array(
      '%list' => $values['list'],
    ));
    constant_contact_save_user($user, $values['list'], FALSE);
  }
  else {
    $type = 'error';
    $message = t('You could not be removed from %list mailing list.', array(
      '%list' => $values['list'],
    ));
    $_GET['destination'] = 'constant_contact/unsubscribe/' . urlencode($values['list']);
  }
  drupal_set_message($message, $type);
  return drupal_goto($_GET['destination'] ? $_GET['destination'] : '<front>');
}