You are here

function simplenews_subscribe_user in Simplenews 6

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

Subscribe a user to a newsletter or send a confirmation mail.

The $confirm parameter determines the action: FALSE = The user is subscribed TRUE = User receives an email to verify the address and complete the subscription A new subscription account is created when the user is subscribed to the first newsletter

Parameters

string $mail: The email address to subscribe to the newsletter.

integer $tid: The term ID of the newsletter.

boolean $confirm: TRUE = send confirmation mail; FALSE = subscribe immediate to the newsletter

string $preferred_language: The language code (i.e. 'en', 'nl') of the user preferred language. Use '' for the site default language. Use NULL for the language of the current page.

6 calls to simplenews_subscribe_user()
simplenews_block_form_submit in ./simplenews.module
simplenews_confirm_add_form_submit in ./simplenews.subscription.inc
simplenews_subscribe_user_action in simplenews_action/simplenews_action.module
A configurable Drupal action. Subscribe the user to a newsletter hook = user: Subscribe this user to selected newsletter
simplenews_subscription_list_add_submit in ./simplenews.admin.inc
simplenews_subscription_manager_form_submit in ./simplenews.subscription.inc

... See full list

File

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

Code

function simplenews_subscribe_user($mail, $tid, $confirm = TRUE, $preferred_language = NULL) {
  global $language;

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

  // Get current subscriptions if any.
  $account = (object) array(
    'mail' => $mail,
  );
  $subscription = simplenews_get_subscription($account);

  // If user is not subscribed to ANY newsletter, create a subscription account
  if ($subscription->snid == 0) {

    // To subscribe a user:
    //   - Fetch the users uid.
    //   - Determine the user preferred language.
    //   - Add the user to the database.
    //   - Get the full subscription object based on the mail address.
    // Note that step 3 gets subscription data based on mail address because the uid can be 0 (for anonymous users)
    $account = _simplenews_user_load($mail);

    // If the site is multilingual:
    //  - Anonymous users are subscribed with their preferred language
    //    equal to the language of the current page.
    //  - Registered users will be subscribed with their default language as
    //    set in their account settings.
    // By default the preferred language is not set.
    if (variable_get('language_count', 1) > 1) {
      if ($account->uid) {
        $preferred_language = $account->language;
      }
      else {
        $preferred_language = isset($preferred_language) ? $preferred_language : $language->language;
      }
    }
    db_query("INSERT INTO {simplenews_subscriptions} (mail, uid, language, activated) VALUES ('%s', %d, '%s', %d)", $mail, $account->uid, $preferred_language, 1);
    $subscription = simplenews_get_subscription($account);
  }
  if ($confirm) {

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

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

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