You are here

function notifications_query_build in Notifications 6.4

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

Query builder for subscriptions

This adds up query elements into a big array so they can be later rendered as SQL

Both the $params and the $query can have some special values

  • fields, array of field => value conditions
  • fields args, array of field arguments, to be added into the query after 'where args'
  • fields sql, sql condition for fields to be added as is
  • fields index, if present, current index to alias and join the fields table

Parameters

$params: Array of conditions to add.

$query: Base query to build upon

See also

notifications_query_sql()

3 calls to notifications_query_build()
NotificationsBasicTests::testNotificationsQueryBuilder in tests/notifications_api.test
Test query builder
notifications_query_subscriptions in includes/query.inc
Run query for subscriptions table with field conditions
Notifications_Queue::queue_event in includes/notifications_queue.class.inc
Queue events for notifications adding query conditions from plug-ins

File

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

Code

function notifications_query_build($params, $query = array()) {
  foreach ($params as $name => $elements) {
    if ($name == 'fields') {

      // Fields elements have some special handling, they have the form: field => value
      foreach ($elements as $field => $value) {
        if (is_numeric($field)) {
          $field = $value['field'];
          $value = $value['value'];
        }

        // Use field definition provided by hook_notifications('subscription fields') and handle array values with IN conditions
        // Workaround to have a valid one because not all modules provide the information yet (og?)
        if (notifications_subscription_fields($field, 'type') == 'int') {
          $type = 'int';
          $fieldval = 'intval';
        }
        else {
          $type = 'char';
          $fieldval = 'value';
        }
        if (isset($query['fields index'])) {
          $alias = 'f' . $query['fields index']++;
          $query['join'][] = "INNER JOIN {notifications_fields} {$alias} ON {$alias}.sid = s.sid";
        }
        else {
          $alias = 'f';
        }
        if (is_array($value)) {
          $query['fields'][] = "{$alias}.field = '%s' AND {$alias}.{$fieldval} IN (" . db_placeholders($value, $type) . ")";
          $query['fields args'][] = $field;
          $query['fields args'] = empty($query['fields args']) ? $value : array_merge($query['fields args'], $value);
        }
        else {
          $query['fields'][] = "{$alias}.field = '%s' AND {$alias}.{$fieldval} = " . db_type_placeholder($type);
          $query['fields args'][] = $field;
          $query['fields args'][] = $value;
        }
      }
    }
    else {
      if ($name == 'fields sql') {

        // These are added as 'fields' parameters without further parsing
        $name = 'fields';
      }
      if (is_array($elements)) {
        $query[$name] = empty($query[$name]) ? $elements : array_merge($query[$name], $elements);
      }
      else {
        $query[$name][] = $elements;
      }
    }
  }
  return $query;
}