You are here

function simplenews_get_subscription in Simplenews 6.2

Same name and namespace in other branches
  1. 5 simplenews.module \simplenews_get_subscription()
  2. 6 simplenews.module \simplenews_get_subscription()

Get the subscription object for the given account.

Account is defined by (in order of preference) snid, email address or uid. If the account is not subscribed a default subscription object is returned containing all available account info.

Parameters

object $account account details. Containing one or none of these items:: object( snid : subscription id mail : email address uid : user id )

Return value

subscription object object( snid : subscription id. 0 if account is not subscribed tids : array of tid's of active subscriptions newsletter_subscriptions : array of newsletter subscription objects uid : user id. 0 if account is anonymous user mail : user email address. empty if email is unknown name : always empty. Added for compatibility with user account object language : language object. User preferred or default language )

13 calls to simplenews_get_subscription()
SimpleNewsAdministrationTestCase::testSubscriptionManagement in tests/simplenews.test
Test newsletter subscription management.
SimplenewsSubscribeTestCase::testSubscribeAnonymousSingle in tests/simplenews.test
Test anonymous subscription with single opt in.
simplenews_mail_mail in includes/simplenews.mail.inc
Send a node to an email address.
simplenews_send_node in includes/simplenews.mail.inc
Send newsletter node to subscribers.
simplenews_subscribe_user in ./simplenews.module
Subscribe a user to a newsletter or send a confirmation mail.

... See full list

File

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

Code

function simplenews_get_subscription($account) {

  // Load subscription data based on available account information
  // NOTE that the order of checking for snid, mail and uid is critical. mail must be checked *before* uid. See simplenews_subscribe_user()
  if (isset($account->snid)) {
    $subscription = db_fetch_object(db_query("\n      SELECT s.*, u.name\n      FROM {simplenews_subscriptions} s\n      LEFT JOIN {users} u\n        ON u.uid = s.uid\n      WHERE s.snid = %d", $account->snid));
  }
  elseif (isset($account->mail)) {
    $subscription = db_fetch_object(db_query("\n      SELECT s.*, u.name\n      FROM {simplenews_subscriptions} s\n      LEFT JOIN {users} u\n        ON u.uid = s.uid\n      WHERE LOWER(s.mail) = LOWER('%s')", $account->mail));
  }
  elseif (isset($account->uid) && $account->uid > 0) {
    $subscription = db_fetch_object(db_query("\n      SELECT s.*, u.name\n      FROM {simplenews_subscriptions} s\n      LEFT JOIN {users} u\n        ON u.uid = s.uid\n      WHERE s.uid = %d", $account->uid));
  }
  if (!empty($subscription)) {
    $result = db_query("\n      SELECT tid, status, timestamp, source\n      FROM {simplenews_snid_tid} t\n      WHERE t.snid = %d", $subscription->snid);
    $subscription->tids = array();
    while ($newsletter_subscription = db_fetch_object($result)) {
      if ($newsletter_subscription->status == SIMPLENEWS_SUBSCRIPTION_STATUS_SUBSCRIBED) {
        $subscription->tids[$newsletter_subscription->tid] = $newsletter_subscription->tid;
      }
      $subscription->newsletter_subscription[$newsletter_subscription->tid] = $newsletter_subscription;
    }

    // simplenews_subscription matches account language if registered.
    $subscription->language = user_preferred_language($subscription);
  }
  else {

    // Account is unknown in subscription table. Create default subscription object
    $subscription = new stdClass();
    $subscription->name = '';
    $subscription->uid = isset($account->uid) ? $account->uid : 0;
    $subscription->mail = isset($account->mail) ? $account->mail : '';
    $subscription->language = language_default();
    $subscription->snid = 0;
    $subscription->tids = array();
    $subscription->newsletter_subscription = array();
  }
  return $subscription;
}