You are here

function simplenews_subscribe_user in Simplenews 5

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

API function; subscribes a user to a newsletter.

Parameters

$mail: The e-mail address to subscribe to the newsletter.

$tid: The term ID of the newsletter.

$confirm: Whether we should send a confirmation e-mail and hold off adding this user to the newsletter until he or she clicks the confirm link in the e-mail.

5 calls to simplenews_subscribe_user()
simplenews_block_form_submit in ./simplenews.module
Forms API callback; handles block form (un)subscribe submissions.
simplenews_confirm_add_form_submit in ./simplenews.module
Forms API callback; handles form submission for a user confirming subscribe request.
simplenews_subscription_list_add_submit in ./simplenews.module
simplenews_subscription_manager_form_submit in ./simplenews.module
Forms API callback; submit handler for subscription form.
simplenews_user in ./simplenews.module
Implementation of hook_user().

File

./simplenews.module, line 813

Code

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

  //Prevent mismatches from accidental capitals in mail address
  $mail = strtolower($mail);
  $subscription = simplenews_get_user_subscription($mail);

  // If user is not subscribed to ANY newsletter, add basic info first.
  if (!$subscription) {
    $account = _simplenews_user_load($mail);
    db_query("INSERT INTO {simplenews_subscriptions} (mail, uid, a_status) VALUES ('%s', %d, 1)", $mail, $account->uid);
    $subscription = simplenews_get_user_subscription($mail);
  }
  $newsletter = taxonomy_get_term($tid);
  if ($confirm) {

    // Send confirmation e-mail to user to complete subscription or to tell
    // them that he or she is already subscribed.
    simplenews_mail_confirm($mail, $newsletter, $subscription ? $subscription->snid : NULL, 'subscribe');
  }
  elseif (!isset($subscription->tids[$tid])) {

    // Then, add user to newsletter relationship if not already subscribed.
    db_query("INSERT INTO {simplenews_snid_tid} (snid, tid) VALUES (%d, %d)", $subscription->snid, $tid);
  }
  return TRUE;
}