You are here

function _notify_select_content in Notify 2.0.x

Same name and namespace in other branches
  1. 8 notify.module \_notify_select_content()
  2. 7 notify.module \_notify_select_content()
  3. 1.0.x notify.module \_notify_select_content()

Helper function to set up query objects to select content for counting and sending.

Return array has six values:

  • ordinary published entities: nodes, comments;
  • in unpublished queue: published nodes, published comments, unpublished nodes, unpublished comments,

Return value

array res_nodes, res_comms, res_nopub, res_copub, res_nounp, res_counp.

4 calls to _notify_select_content()
QueueForm::submitForm in src/Form/QueueForm.php
Form submission handler.
SkipForm::buildForm in src/Form/SkipForm.php
Form constructor.
_notify_count in ./notify.module
Count the various types of content.
_notify_send in ./notify.module
Helper function to send the notification e-mail batch.

File

./notify.module, line 272
Hooks for Notify module.

Code

function _notify_select_content() {
  $config = \Drupal::config('notify.settings');
  $users = $config
    ->get('notify_users');
  $batch_remain = $users ? count($users) : 0;
  $since = $config
    ->get('notify_send_last');
  if ($batch_remain) {
    $send_start = $config
      ->get('notify_send_start');
  }
  else {
    $send_start = \Drupal::time()
      ->getRequestTime();
  }
  if (!$since) {
    $period = $config
      ->get('notify_period');
    if ($period > 0) {
      $since = $send_start - $period;
    }
  }
  $all = NodeType::loadMultiple();
  $ntype = [];
  foreach ($all as $type => $object) {
    $ntype[] = $type;
  }

  // Build query object to fetch new nodes.
  $q = \Drupal::database()
    ->select('node', 'n');
  $q
    ->join('node_field_data', 'u', 'n.nid = u.nid');
  $q
    ->fields('n', [
    'nid',
  ]);
  if (count($ntype) >= 1) {
    $q
      ->condition('n.type', $ntype, 'IN');
  }
  if ($config
    ->get('notify_include_updates')) {
    $q
      ->condition((new Condition('OR'))
      ->condition((new Condition('AND'))
      ->condition('u.created', $since, '>')
      ->condition('u.created', $send_start, '<='))
      ->condition((new Condition('AND'))
      ->condition('u.changed', $since, '>')
      ->condition('u.changed', $send_start, '<=')));
    $q
      ->orderBy('u.created', 'asc');
    $q->allowRowCount = TRUE;
    $res_nodes = $q
      ->execute()
      ->fetchAll();
  }
  else {
    $q
      ->condition('u.created', $since, '>');
    $q
      ->condition('u.created', $send_start, '<=');
    $q
      ->orderBy('u.created', 'asc');
    $q->allowRowCount = TRUE;
    $res_nodes = $q
      ->execute()
      ->fetchAll();
  }

  // Get published nodes in unpublished queue.
  $q = \Drupal::database()
    ->select('notify_unpublished_queue', 'q');
  $q
    ->join('node', 'n', 'q.nid = n.nid');
  $q
    ->join('node_field_data', 'u', 'q.nid = u.nid');
  $q
    ->fields('q', [
    'nid',
    'cid',
  ]);
  $q
    ->condition('u.status', 1, '=');
  $q
    ->condition('q.cid', 0, '=');
  $q
    ->orderBy('q.nid', 'asc');
  $q->allowRowCount = TRUE;
  $res_nopub = $q
    ->execute()
    ->fetchAll();

  // Get unpublished nodes in unpublished queue.
  $q = \Drupal::database()
    ->select('notify_unpublished_queue', 'q');
  $q
    ->join('node', 'n', 'q.nid = n.nid');
  $q
    ->join('node_field_data', 'u', 'q.nid = u.nid');
  $q
    ->fields('q', [
    'nid',
    'cid',
  ]);
  $q
    ->condition('u.status', 0, '=');
  $q
    ->condition('q.cid', 0, '=');
  $q
    ->orderBy('q.nid', 'asc');
  $q->allowRowCount = TRUE;
  $res_nounp = $q
    ->execute()
    ->fetchAll();
  if (\Drupal::service('module_handler')
    ->moduleExists('comment')) {

    // Fetch new published comments.
    $q = \Drupal::database()
      ->select('comment', 'c');
    $q
      ->join('comment_field_data', 'v', 'c.cid = v.cid');
    $q
      ->join('node', 'n', 'v.entity_id = n.nid');
    $q
      ->fields('c', [
      'cid',
    ]);
    if (count($ntype) >= 1) {
      $q
        ->condition('n.type', $ntype, 'IN');
    }

    // ->condition(((new Condition('AND'))->condition(true)
    // (AND () (OR (AND ()())  (AND ()())))
    if ($config
      ->get('notify_include_updates')) {

      // dpm('incl. updates', 'comms');
      // Only comments attached to nodes are called 'comment'.
      $q
        ->condition((new Condition('AND'))
        ->condition('c.comment_type', 'comment', '=')
        ->condition((new Condition('OR'))
        ->condition((new Condition('AND'))
        ->condition('v.created', $since, '>')
        ->condition('v.created', $send_start, '<='))
        ->condition((new Condition('AND'))
        ->condition('v.changed', $since, '>')
        ->condition('v.changed', $send_start, '<='))));
      $q
        ->orderBy('v.created', 'asc');
      $q->allowRowCount = TRUE;
      $res_comms = $q
        ->execute()
        ->fetchAll();
    }
    else {

      // dpm('no updates', 'comms');.
      $q
        ->condition('c.comment_type', 'comment', '=');
      $q
        ->condition('v.created', $since, '>');
      $q
        ->condition('v.created', $send_start, '<=');
      $q
        ->orderBy('v.created', 'asc');
      $q->allowRowCount = TRUE;
      $res_comms = $q
        ->execute()
        ->fetchAll();
    }

    // Foreach ($res_comms as $row) {
    // dpm($row, 'comms');
    // }
    // Get published comments in unpublished queue.
    $q = \Drupal::database()
      ->select('notify_unpublished_queue', 'q');
    $q
      ->join('comment', 'c', 'q.cid = c.cid');
    $q
      ->join('comment_field_data', 'v', 'q.cid = v.cid');
    $q
      ->fields('q', [
      'nid',
      'cid',
    ]);
    $q
      ->condition('v.status', 1, '=');
    $q
      ->orderBy('q.cid', 'asc');
    $q->allowRowCount = TRUE;
    $res_copub = $q
      ->execute()
      ->fetchAll();

    // Foreach ($res_copub as $row) {
    // dpm($row, 'copub');
    // }
    // Get unpublished comments in unpublished queue.
    $q = \Drupal::database()
      ->select('notify_unpublished_queue', 'q');
    $q
      ->join('comment', 'c', 'q.cid = c.cid');
    $q
      ->join('comment_field_data', 'v', 'q.cid = v.cid');
    $q
      ->fields('q', [
      'nid',
      'cid',
    ]);
    $q
      ->condition('v.status', 0, '=');
    $q
      ->orderBy('q.cid', 'asc');
    $q->allowRowCount = TRUE;
    $res_counp = $q
      ->execute()
      ->fetchAll();

    // Foreach ($res_counp as $row) {
    // dpm($row, 'counp');
    // }.
  }
  else {
    $res_comms = $res_copub = $res_counp = NULL;
  }
  return [
    $res_nodes,
    $res_comms,
    $res_nopub,
    $res_copub,
    $res_nounp,
    $res_counp,
  ];
}