You are here

function simplenews_subscribe_user in Simplenews 6.2

Same name and namespace in other branches
  1. 5 simplenews.module \simplenews_subscribe_user()
  2. 6 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

This function does NOT update language information if account already exists.

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.

string $source: Indication for source of subscription. Simplenews uses these sources: website: via any website form (with or without confirmation email) mass subscribe: mass admin UI mass unsubscribe: mass admin UI action: Drupal actions

Return value

$subscription

10 calls to simplenews_subscribe_user()
SimpleNewsAdministrationTestCase::testSubscriptionManagement in tests/simplenews.test
Test newsletter subscription management.
simplenews_block_form_submit in includes/simplenews.subscription.inc
simplenews_confirm_add_form_submit in includes/simplenews.subscription.inc
FAPI CONFIRM ADD form_submit.
simplenews_confirm_subscription in includes/simplenews.subscription.inc
Menu callback: confirm the user's (un)subscription request
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

... See full list

File

./simplenews.module, line 1415
Simplenews node handling, sent email, newsletter block and general hooks

Code

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

  // 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("\n      INSERT INTO {simplenews_subscriptions}\n      (mail, uid, language, activated, timestamp)\n      VALUES ('%s', %d, '%s', %d, %d)", $mail, $account->uid, $preferred_language, 1, time());
    $subscription = simplenews_get_subscription($account);
  }
  if ($confirm) {
    module_load_include('inc', 'simplenews', 'includes/simplenews.mail');

    // 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])) {

    // Add user to newsletter relationship if not already subscribed.
    // Check if user is (un)subscribed to this newsletter.
    // Resubscribe or add new subscription.
    if (isset($subscription->newsletter_subscription[$tid])) {
      db_query("\n        UPDATE {simplenews_snid_tid}\n        SET status = %d,\n          timestamp = %d,\n          source = '%s'\n        WHERE snid = %d\n          AND tid = %d", SIMPLENEWS_SUBSCRIPTION_STATUS_SUBSCRIBED, time(), $source, $subscription->snid, $tid);
    }
    else {
      db_query("\n        INSERT INTO {simplenews_snid_tid}\n        (snid, tid, status, timestamp, source)\n        VALUES (%d, %d, %d, %d, '%s')", $subscription->snid, $tid, SIMPLENEWS_SUBSCRIPTION_STATUS_SUBSCRIBED, time(), $source);
    }

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