You are here

function notifications_subscriptions_query_build in Notifications 6.3

Same name and namespace in other branches
  1. 6 notifications.module \notifications_subscriptions_query_build()
  2. 6.2 notifications.module \notifications_subscriptions_query_build()

Query builder for subscriptions

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

Parameters

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

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

  • single 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

$limit: Whether to limit the result to subscriptions with exactly that condition fields

Return value

array() Structured array with 'join', 'where', 'args' elements

2 calls to notifications_subscriptions_query_build()
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 fit a set of conditions.

File

./notifications.module, line 863
Notifications module

Code

function notifications_subscriptions_query_build($params, $conditions = array(), $limit = FALSE) {
  $join = $where = $args = array();
  $schema = drupal_get_schema('notifications');

  // If we limit this query to the number of conditions add a new param
  if ($limit && $conditions) {
    $params += array(
      'conditions' => count($conditions),
    );
  }

  // Add conditions for main notifications table
  foreach ($params as $field => $value) {
    if (in_array($schema['fields'][$field]['type'], array(
      'serial',
      'int',
    ))) {
      $where[] = 'n.' . $field . " = %d";
    }
    else {
      $where[] = 'n.' . $field . " = '%s'";
    }
    $args[] = $value;
  }

  // Now we need to join once the fields table for each condition
  if ($conditions) {
    $index = 0;
    foreach ($conditions as $key => $data) {
      if (is_array($data)) {
        $field = $data['type'];
        $value = $data['value'];
      }
      else {
        $field = $key;
        $value = $data;
      }
      $alias = 'nf' . $index++;
      $join[] = "INNER JOIN {notifications_fields} {$alias} ON n.sid = {$alias}.sid";
      $where[] = "{$alias}.field = '%s'";
      $args[] = $field;

      // If null value, do not check value, we just check that a condition for this field type exists
      if (!is_null($value)) {
        if (notifications_subscription_fields($field, 'type') == 'int') {
          $where[] = "{$alias}.intval = %d";
        }
        else {
          $where[] = "{$alias}.value = '%s'";
        }
        $args[] = $value;
      }
    }
  }

  // Return query array
  return array(
    'from' => array(
      '{notifications} n',
    ),
    'join' => $join,
    'where' => $where,
    'args' => $args,
  );
}