You are here

function notifications_user_get_subscriptions in Notifications 6.4

Same name and namespace in other branches
  1. 5 notifications.module \notifications_user_get_subscriptions()
  2. 6 notifications.module \notifications_user_get_subscriptions()
  3. 6.2 notifications.module \notifications_user_get_subscriptions()
  4. 6.3 notifications.module \notifications_user_get_subscriptions()

Get active subscriptions for a given user to some object

This is a shorthand function to quickly check some types of subscriptions.

Parameters

$account: User id or user account object to check subscriptions for

$type: Object type

$field: Field type for the subscription. I.e. for a node it will be nid

$value: Field value to check subscriptions to. I.e. $node

Return value

Array of subscriptions for this user and object indexed by sid

1 call to notifications_user_get_subscriptions()
NotificationsContentTests::testNotificationsContent in tests/notifications_content.test
Play with creating, retrieving, deleting a pair subscriptions

File

./notifications.module, line 911
Notifications module

Code

function notifications_user_get_subscriptions($account, $type, $object, $refresh = FALSE) {
  $subscriptions =& messaging_static(__FUNCTION__);
  $uid = messaging_user_uid($account);

  // No subscriptions for anonymous users
  if (!$uid) {
    return array();
  }

  // Check the object exists, is loaded and we've got a key field
  $object = notifications_object_load($type, $object);
  $key = notifications_object_type($type, 'key_field');
  if (!$object || !$key || empty($object->{$key})) {
    return array();
  }

  // Now we've got the key fields for caching, try the cache or find subscriptions
  if ($refresh || !isset($subscriptions[$uid][$type][$object->{$key}])) {

    // Collect subscription types for this object and this account
    // Get allowed subscription options for this account to this object
    $user_subscriptions = array();
    $subscribe_options = notifications_object_subscribe_options($type, $object, messaging_user_object($account));
    foreach ($subscribe_options as $template) {
      $type_subscriptions = notifications_get_subscriptions(array(
        'uid' => $uid,
        'type' => $template['type'],
        'status' => Notifications_Subscription::STATUS_ACTIVE,
      ), $template['fields'], TRUE);
      if ($type_subscriptions) {
        $user_subscriptions += $type_subscriptions;

        //array_merge($user_subscriptions, $type_subscriptions);
      }
    }
    $subscriptions[$uid][$type][$object->{$key}] = $user_subscriptions;
  }
  return $subscriptions[$uid][$type][$object->{$key}];
}