You are here

function notifications_query_subscriptions in Notifications 6.4

Run query for subscriptions table with field conditions

Note these queries are just for single type subscriptions all all field conditions are AND'd by default, unless set the 'fields operator' property

Builds queries for 'notifications' and 'notifications_fields' tables using schema and fields (subscription fields) information.

Parameters

array $main_conditions: Array of multiple conditions in the notifications table.

array $field_conditions: Array of multiple conditions in the notifications_fields table. The array elements may be

  • array of arrays that are field => value pairs
  • or key => array('type' => field, 'value' => value)

If value is null, it just checks that a condition for the given field type exists

$query: Optional query array to build upon

$full_loading: Build the subscription objects and store them in the cache (We don't want this for deletion queries)

Return value

array() Resulting subscription objects indexed by sid

2 calls to notifications_query_subscriptions()
notifications_delete_subscriptions in ./notifications.module
Delete multiple subscriptions and clean up related data (pending notifications, fields).
notifications_get_subscriptions in ./notifications.module
Get subscriptions that match a set of conditions.

File

includes/query.inc, line 166
Notifications query API - Helper functions to build monster queries

Code

function notifications_query_subscriptions($main_conditions, $field_conditions = array(), $query = array(), $full_loading = TRUE) {

  // Add default values to the query
  $query += array(
    'select' => "s.sid, s.type, s.event_type, s.send_interval, s.cron, s.module, s.status, s.destination, s.mdid, s.language, s.conditions",
    'distinct' => TRUE,
    'fields operator' => 'AND',
    'pager' => NULL,
    'where' => array(),
    'where args' => array(),
  );

  // The messaging query builder knows about table schemas
  $where = _messaging_query_where('notifications', $main_conditions, 's');
  $query['where'] = array_merge($query['where'], $where['where']);
  $query['where args'] = array_merge($query['where args'], $where['args']);
  $query['from'] = array(
    '{notifications} s',
  );

  // Add in field conditions. How the fields table is joined will depend on other parameters
  if ($field_conditions) {
    if ($query['fields operator'] == 'AND') {

      // We must multiple join fields table and the field conditions should be AND
      $query['fields index'] = 0;
    }
    else {

      // The field conditions will be OR'd so we can use a single join
      $query['join'][] = "LEFT JOIN {notifications_fields} f ON s.sid = f.sid";

      // And we need to group by all select fields to be able to use having
      $query['group'][] = implode(', ', $query['select']);
      $query['having'][] = 's.conditions = count(f.sid)';
    }

    // Add in field conditions
    $query = notifications_query_build(array(
      'fields' => $field_conditions,
    ), $query);
  }
  if ($query['pager'] && empty($query['order'])) {
    $query['order'][] = 's.sid';
  }

  // Build the query anhd throw it using simple query or pager query
  $result = notifications_query_sql($query, TRUE);

  // Build list with results
  $subscriptions = array();
  while ($load = db_fetch_object($result)) {
    if ($full_loading) {

      // Using this loader will also place the subscriptions in the static cache
      $subs = notifications_load_subscription($load);
      $subscriptions[$subs->sid] = $subs;
    }
    else {
      $subscriptions[$load->sid] = $load;
    }
  }
  return $subscriptions;
}