You are here

function subscriptions_queue in Subscriptions 2.0.x

Same name and namespace in other branches
  1. 5.2 subscriptions.module \subscriptions_queue()
  2. 6 subscriptions.module \subscriptions_queue()
  3. 7 subscriptions.module \subscriptions_queue()

Queues events for notifications.

Parameters

array $event: Event array.

3 calls to subscriptions_queue()
subscriptions_content_comment_insert in subscriptions_content/subscriptions_content.module
Implements hook_comment_insert().
subscriptions_content_comment_update in subscriptions_content/subscriptions_content.module
Implements hook_comment_update().
subscriptions_content_node_insert in subscriptions_content/subscriptions_content.module
Implements hook_node_insert().
2 string references to 'subscriptions_queue'
subscriptions_taxonomy_taxonomy_term_delete in subscriptions_taxonomy/subscriptions_taxonomy.module
Implements hook_taxonomy_term_delete().
_subscriptions_content_load in subscriptions_content/subscriptions_content.notify.inc
Returns a node if published, including any comments that are still queued, but limited by the given subscriptions queue ID.

File

./subscriptions.module.old.php, line 487
Subscriptions module.

Code

function subscriptions_queue(array $event) {
  global $user;
  if (isset($event['node']->nid) && strpos('  ' . variable_get('subscriptions_blocked_nodes', '') . ' ', ' ' . $event['node']->nid . ' ')) {
    return;
  }
  $event += array(
    'uid' => $user->uid,
    'load_args' => '',
  );
  foreach (module_implements('subscriptions_queue_alter') as $module) {
    $function = $module . '_subscriptions_queue_alter';
    $function($event);
    if (empty($event)) {
      return;

      // $event was cleared, forget it
    }
  }
  if (is_array($event['load_args'])) {
    $event['load_args'] = serialize($event['load_args']);
  }
  foreach (module_implements('subscriptions') as $subs_module) {
    $subs_module_query = module_invoke($subs_module, 'subscriptions', 'queue', $event);

    // Allow other modules to alter the data.
    drupal_alter('subscriptions_queue_query', $subs_module_query);
    if (!isset($subs_module_query)) {
      continue;
    }
    foreach ($subs_module_query as $module => $module_query) {
      foreach ($module_query as $field => $query) {
        $select = db_select('subscriptions', 's');
        $select
          ->innerJoin('subscriptions_user', 'su', 's.recipient_uid = su.uid');
        $select
          ->innerJoin('users', 'u', 'su.uid = u.uid');
        $select
          ->leftJoin('subscriptions_last_sent', 'sls', 'su.uid = sls.uid AND s.send_interval = sls.send_interval');
        if (!empty($query['join'])) {
          $joins = isset($query['join']['table']) ? array(
            $query['join'],
          ) : $query['join'];
          foreach ($joins as $join) {
            $select
              ->innerJoin($join['table'], $join['alias'], $join['on']);
          }
        }
        $select
          ->fields('u', array(
          'uid',
          'name',
          'language',
        ))
          ->fields('s', array(
          'module',
          'field',
          'value',
          'author_uid',
          'send_interval',
        ))
          ->fields('su', array(
          'digest',
          'suspended',
        ));
        $select
          ->addExpression('COALESCE(sls.last_sent, 0)', 'last_sent');
        $select
          ->addExpression("'" . $event['load_function'] . "'", 'load_function');
        $select
          ->addExpression("'" . $event['load_args'] . "'", 'load_args');
        $select
          ->addExpression((int) $event['is_new'], 'is_new');
        $select
          ->condition('s.module', $module)
          ->condition('s.field', $field)
          ->condition('s.author_uid', array(
          $module == 'node' && $event['type'] == 'comment' && isset($event['node']->uid) ? $event['node']->uid : $event['uid'],
          -1,
        ), 'IN')
          ->groupBy('u.uid');

        // #2798015: "only_full_group_by" becomes standard in MySQL 5.7,
        // just as for pgsql and sqlsrv.
        $select
          ->groupBy('u.uid')
          ->groupBy('u.name')
          ->groupBy('u.language')
          ->groupBy('s.module')
          ->groupBy('s.field')
          ->groupBy('s.value')
          ->groupBy('s.author_uid')
          ->groupBy('s.send_interval')
          ->groupBy('su.digest')
          ->groupBy('su.suspended')
          ->groupBy('last_sent');
        if (!empty($query['where'])) {
          foreach ($query['where'] as $cond) {
            $select
              ->condition($cond[0], $cond[1], $cond[2]);
          }
        }
        if (!empty($query['value'])) {
          $select
            ->condition('s.value', $query['value']);
        }
        if ($user->uid && !_subscriptions_get_setting('send_self', $user)) {
          $select
            ->condition('s.recipient_uid', $user->uid, '<>');
        }
        if (!empty($event['noqueue_uids'])) {

          // Allow hook_subscriptions_queue_alter() modules to set uids that won't get any notifications queued:
          $select
            ->condition('s.recipient_uid', $event['noqueue_uids'], 'NOT IN');
        }
        if (!empty($query['groupby'])) {
          $select
            ->groupBy($query['groupby']);
        }
        $insert = db_insert('subscriptions_queue', array(
          'return' => Database::RETURN_NULL,
        ))
          ->from($select);

        /*  for debugging:
            $sqid = $insert->execute();
            drupal_set_message($insert->__toString() . '<br />ID=' . $sqid . ' inserted.');
            /*/
        $insert
          ->execute();

        /**/
      }
    }
  }
}