You are here

function notifications_queue in Notifications 6.3

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

Queue events for notifications adding query conditions from plug-ins

This is an example of the resulting query

INSERT INTO {notifications_queue} (uid, sid, module, eid, send_interval, send_method, cron, created, conditions) SELECT DISTINCT s.uid, s.sid, s.module, 34, s.send_interval, s.send_method, s.cron, 1230578161, s.conditions FROM notifications s INNER JOIN notifications_fields f ON s.sid = f.sid WHERE s.status = 1 AND s.event_type = 'node' AND s.send_interval >= 0 AND ((f.field = 'nid' AND f.value = '2') OR (f.field = 'type' AND f.value = 'story') OR (f.field = 'author' AND f.value = '1')) GROUP BY s.uid, s.sid, s.module, s.send_interval, s.send_method, s.cron, s.conditions HAVING s.conditions = count(f.sid)

Parameters

$event: Event array.

1 call to notifications_queue()
notifications_event in ./notifications.module
Process subscriptions events
12 string references to 'notifications_queue'
NotificationsContentTests::testNotificationsContent in tests/notifications_content.test
Play with creating, retrieving, deleting a pair subscriptions
NotificationsLiteTests::sendLiteMessages in tests/notifications_lite.test
NotificationsLiteTests::testNotificationsLite in tests/notifications_lite.test
Test simple sending cases
notifications_delete_subscription in ./notifications.module
Delete subscription and clean up related data.
notifications_delete_subscriptions in ./notifications.module
Delete multiple subscriptions and clean up related data (pending notifications, fields).

... See full list

File

./notifications.module, line 451
Notifications module

Code

function notifications_queue($event) {
  $query = array();

  // Build big insert query using the query builder. The fields for this event type will be added by the plug-ins.
  // If no arguments retrieved, skip this step
  if ($query_args = notifications_module_information('query', 'event', $event->type, $event)) {
    $query['insert'] = array(
      'uid',
      'destination',
      'language',
      'sid',
      'module',
      'eid',
      'send_interval',
      'send_method',
      'cron',
      'created',
      'conditions',
    );
    $query['into'] = '{notifications_queue}';
    $query['distinct'] = TRUE;
    $query['select'] = array(
      's.uid',
      's.destination',
      's.language',
      's.sid',
      's.module',
      '%d',
      's.send_interval',
      's.send_method',
      's.cron',
      '%d',
      's.conditions',
    );
    $query['from'] = array(
      '{notifications} s',
    );
    $query['select args'] = array(
      $event->eid,
      $event->created,
    );

    // We do a left join instead of inner join to allow subscriptions with no fields to work
    $query['join'] = array(
      'LEFT JOIN {notifications_fields} f ON s.sid = f.sid',
    );
    $query['where'] = array(
      's.status = 1',
      "s.event_type = '%s'",
      's.send_interval >= 0',
    );
    $query['where args'] = array(
      $event->type,
    );

    // Add one more condition if we don't send notifications on own posts
    if (!variable_get('notifications_sendself', 0) && !empty($event->uid)) {
      $query['where'][] = 's.uid <> %d';
      $query['where args'][] = $event->uid;
    }

    // Some group by fields are not really needed but added for pgsql compatibility
    $query['group'] = array(
      's.uid',
      's.destination',
      's.language',
      's.sid',
      's.module',
      's.send_interval',
      's.send_method',
      's.cron',
      's.conditions',
    );

    // We throw in all the conditions and check the number of matching conditions
    // that must be equal to the subscription conditions number
    $query['having'] = array(
      's.conditions = count(f.sid)',
    );

    // We add parameters for each module separately
    foreach ($query_args as $query_params) {
      $query = notifications_query_build($query_params, $query);
    }

    // Give a chance to other modules to alter the query or empty it so we don't throw it
    drupal_alter('notifications_query', $query);

    // Finally we build the SELECT part of the query and glue it to the INSERT
    if ($query) {
      list($sql, $args) = notifications_query_sql($query);
      db_query($sql, $args);
    }
  }

  // Modules can do cleanup operations or modify the queue
  notifications_module_invoke('event queued', $event, $query);

  // Now update event counter with rows in notifications_queue or delete if no rows
  if ($count = db_result(db_query('SELECT COUNT(*) FROM {notifications_queue} WHERE eid = %d', $event->eid))) {
    db_query('UPDATE {notifications_event} SET counter = %d WHERE eid = %d', $count, $event->eid);

    // If immediate sending enabled, store eid for sending on page exit.
    notifications_send_immediate($event->eid);
  }
  else {
    db_query('DELETE FROM {notifications_event} WHERE eid = %d', $event->eid);
  }
}