You are here

function notifications_user_get_subscriptions in Notifications 5

Same name and namespace in other branches
  1. 6.4 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 subscription for a given user

Parameters

$uid: User id

$event_type: Event type

$oid: Object id for caching. I.e. for a node it will be nid

$object: Object to check subscriptions to. I.e. $node

Return value

Array of subscriptions for this user and object indexed by sid

3 calls to notifications_user_get_subscriptions()
notifications_autosubscribe in notifications_autosubscribe/notifications_autosubscribe.module
Subscribes users to content they post, if not already subscribed
Notifications_Content_Tests::testNotificationsContent in tests/notifications_content.test
Play with creating, retrieving, deleting a pair subscriptions
notifications_ui_user_node in notifications_ui/notifications_ui.module
Get list of possible and existing subscriptions for user/node

File

./notifications.module, line 401
Notifications module

Code

function notifications_user_get_subscriptions($uid, $event_type, $oid, $object = NULL, $refresh = FALSE) {
  static $subscriptions;
  if ($refresh || !isset($subscriptions[$uid][$event_type][$oid])) {
    $subscriptions[$uid][$event_type][$oid] = array();
    $query_args = notifications_module_information('query', 'user', $event_type, $object);

    // Base query
    $query = array(
      'args' => array(
        $uid,
        $event_type,
      ),
    );
    foreach ($query_args as $query_params) {
      $query = notifications_query_build($query_params, $query);
    }

    // Build the query merging all the parts
    $sql = 'SELECT s.*, f.* FROM {notifications} s INNER JOIN {notifications_fields} f ON s.sid = f.sid ';
    if (!empty($query['join'])) {
      $sql .= implode(' ', $query['join']);
    }
    $sql .= " WHERE s.uid = %d AND event_type = '%s' ";
    if (!empty($query['where'])) {
      $sql .= " AND ((" . implode(') OR (', $query['where']) . '))';
    }
    $result = db_query($sql, $query['args']);
    while ($sub = db_fetch_object($result)) {
      if (!isset($subscriptions[$uid][$event_type][$oid][$sub->sid])) {
        $subscriptions[$uid][$event_type][$oid][$sub->sid] = $sub;
      }
      $subscriptions[$uid][$event_type][$oid][$sub->sid]->fields[$sub->field] = $sub->value;
    }
  }
  return $subscriptions[$uid][$event_type][$oid];
}