You are here

function simplenews_unsubscribe_user in Simplenews 6

Same name and namespace in other branches
  1. 5 simplenews.module \simplenews_unsubscribe_user()
  2. 6.2 simplenews.module \simplenews_unsubscribe_user()
  3. 7 simplenews.module \simplenews_unsubscribe_user()

Unsubscribe a user from a newsletter or send a confirmation mail.

The $confirm parameter determines the action: FALSE = The user is unsubscribed TRUE = User receives an email to verify the address and complete the unsubscription The subscription account is deleted when the user is unsubscribed to the last newsletter

Parameters

string $mail The email address to unsubscribe from the newsletter.:

integer $tid The term ID of the newsletter.:

boolean $confirm TRUE = send confirmation mail; FALSE = unsubscribe immediate from the newsletter:

5 calls to simplenews_unsubscribe_user()
simplenews_block_form_submit in ./simplenews.module
simplenews_confirm_removal_form_submit in ./simplenews.subscription.inc
simplenews_subscription_list_remove_submit in ./simplenews.admin.inc
simplenews_subscription_manager_form_submit in ./simplenews.subscription.inc
simplenews_user in ./simplenews.module
Implementation of hook_user().

File

./simplenews.module, line 1065
Simplnews node handling, sent email, newsletter block and general hooks

Code

function simplenews_unsubscribe_user($mail, $tid, $confirm = TRUE) {

  //Prevent mismatches from accidental capitals in mail address
  $mail = strtolower($mail);
  $account = (object) array(
    'mail' => $mail,
  );
  $subscription = simplenews_get_subscription($account);

  // The unlikely case that a user is subscribed from a non existing newsletter is logged
  if (!($newsletter = taxonomy_get_term($tid))) {
    watchdog('simplenews', 'Attempt to unsubscribe from non existing newsletter term ID %id', array(
      '%id' => $tid,
    ), WATCHDOG_ERROR);
    return FALSE;
  }
  if ($confirm) {

    // Send confirmation email to user to complete unsubscription
    // or to tell them that he or she is not subscribed
    // Confirmation mail is in the user preferred language.
    $params['from'] = _simplenews_set_from();
    $params['context']['newsletter'] = $newsletter;
    $params['context']['account'] = $subscription;
    drupal_mail('simplenews', 'unsubscribe', $mail, $subscription->language, $params, $params['from']['address']);
  }
  elseif (isset($subscription->tids[$tid])) {

    // OR remove the user from the newsletter.
    db_query('DELETE FROM {simplenews_snid_tid} WHERE snid = %d AND tid = %d', $subscription->snid, $tid);

    // Clean up subscription account if user is not subscribed to any newsletter anymore
    if (!db_result(db_query("SELECT COUNT(*) FROM {simplenews_snid_tid} t WHERE t.snid = %d", $subscription->snid))) {
      db_query('DELETE FROM {simplenews_subscriptions} WHERE snid = %d', $subscription->snid);
    }

    // Execute simplenews unsubscribe trigger
    simplenews_call_actions('unsubscribe', $subscription);
  }
  return TRUE;
}