You are here

function simplenews_get_subscription in Simplenews 6

Same name and namespace in other branches
  1. 5 simplenews.module \simplenews_get_subscription()
  2. 6.2 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 newsletter tid's uid : user id. 0 if account is anonymous user mail : user email address. empty if email is unknown name : always empty. Added for compatebility with user account object language : language object. User preferred or default language )

7 calls to simplenews_get_subscription()
simplenews_mail_mail in ./simplenews.module
Send a node to an email address.
simplenews_send_node in ./simplenews.module
Send newsletter node to subcribers.
simplenews_subscribe_user in ./simplenews.module
Subscribe a user to a newsletter or send a confirmation mail.
simplenews_subscription_manager_form in ./simplenews.subscription.inc
Menu callback: Generates the subscription form for users.
simplenews_subscription_manager_form_validate in ./simplenews.subscription.inc

... See full list

File

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

Code

function simplenews_get_subscription($account) {

  // Load subscription data based on available account informatioin
  // 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("SELECT s.* FROM {simplenews_subscriptions} s LEFT JOIN {users} u ON u.uid = s.uid WHERE s.snid = %d", $account->snid));
  }
  elseif (isset($account->mail)) {
    $subscription = db_fetch_object(db_query("SELECT s.* FROM {simplenews_subscriptions} s LEFT JOIN {users} u ON u.uid = s.uid WHERE s.mail = '%s'", $account->mail));
  }
  elseif (isset($account->uid) && $account->uid > 0) {
    $subscription = db_fetch_object(db_query("SELECT s.* FROM {simplenews_subscriptions} s LEFT JOIN {users} u ON u.uid = s.uid WHERE s.uid = %d", $account->uid));
  }
  if (!empty($subscription)) {
    $result = db_query("SELECT tid FROM {simplenews_snid_tid} t WHERE t.snid = %d", $subscription->snid);
    $subscription->tids = array();
    while ($newsletter = db_fetch_object($result)) {
      $subscription->tids[$newsletter->tid] = $newsletter->tid;
    }
    $subscription->name = '';
    $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();
  }
  return $subscription;
}