You are here

function _notify_send in Notify 6

Same name and namespace in other branches
  1. 8 notify.module \_notify_send()
  2. 5.2 notify.module \_notify_send()
  3. 5 notify.module \_notify_send()
  4. 7 notify.module \_notify_send()
  5. 2.0.x notify.module \_notify_send()
  6. 1.0.x notify.module \_notify_send()

Helper function to send the notification email.

TODO: Needs some cleanup and themability.

2 calls to _notify_send()
notify_admin_users_submit in ./notify.module
Submit for the notify_admin form.
notify_cron in ./notify.module
Implementation of hook_cron().

File

./notify.module, line 440
Notify module sends email digests of new content and comments.

Code

function _notify_send($send_start = NULL) {
  $send_start = $send_start ? $send_start : time();
  $period = variable_get('notify_send_last', $send_start - variable_get('notify_send', 86400));
  $separator = '------------------------------------------------------------------------------';
  $mini_separator = '---';
  $num_sent = 0;
  $num_failed = 0;
  _notify_switch_user();

  // Store current user
  // Fetch all node type authorized by notify settings

  //Note that queries use 'nn' alias to avoid conflicts when query is rewritten by access control modules
  $ntype = array();
  foreach (node_get_types() as $type => $name) {
    if (variable_get(NOTIFY_NODE_TYPE . $type, 0)) {
      $ntype[] = $type;
    }
    if (count($ntype) >= 1) {
      $nodetypes_query = "AND (nn.type = '" . implode("' OR nn.type = '", $ntype) . "') ";
    }
    else {
      $nodetypes_query = '';
    }
  }

  // build query to fetch desired nodes.
  $q = 'SELECT nn.nid FROM {node} nn WHERE (nn.status = 1 OR nn.moderate = 1) ' . $nodetypes_query . ' AND ((nn.created > %d AND nn.created <= %d)';

  //include updated nodes?
  $q .= variable_get('notify_include_updates', 1) ? ' OR (nn.changed > %d AND nn.changed <= %d))' : ')';
  $q .= ' ORDER BY nn.created ASC';

  //Run the query, and load the nodes
  _notify_switch_user(1);

  //Query as if user 1, we check for individual user access later.
  $nresult = db_query(db_rewrite_sql($q, 'nn'), $period, $send_start, $period, $send_start);
  $nodes = array();
  while ($node = db_fetch_object($nresult)) {
    $nodes[$node->nid] = node_load($node->nid);
  }

  // Fetch new comments.
  $comments = array();
  if (module_exists('comment')) {
    $cresult = db_query(db_rewrite_sql('SELECT c.nid, c.cid, c.subject, c.name, c.status FROM {comments} c INNER JOIN {node} nn ON c.nid = nn.nid WHERE c.timestamp > %d AND c.timestamp <= %d ' . $nodetypes_query . ' ORDER BY c.nid, c.timestamp', 'c'), $period, $send_start);
    while ($comment = db_fetch_object($cresult)) {
      $comments[$comment->nid][] = $comment;
    }
  }
  if (count($nodes) || count($comments)) {

    // Fetch users with notify enabled
    $uresult = db_query('SELECT u.uid, u.name, u.mail, u.language, n.status, n.node, n.teasers, n.comment FROM {notify} n INNER JOIN {users} u ON n.uid = u.uid WHERE n.status = 1 AND u.status = 1 AND n.attempts <= %d', variable_get('notify_attempts', 5));
    while ($user = db_fetch_object($uresult)) {

      // Switch current user to this account to use node_access functions, etc.
      _notify_switch_user($user->uid);
      $node_body = '';
      $comment_body = '';

      // Write new node content to e-mail if user has permissions and nodes are
      // ready to be sent. @TODO - cache by role
      if ($user->node && user_access('access content') && count($nodes)) {
        $node_count = 0;
        foreach ($nodes as $node) {

          // Skip to next if this user is NOT allowed to view this node.
          if (!node_access('view', $node)) {
            continue;
          }

          // @TODO: Add functionality to hook into moderation modules?
          if ($node->moderate == 1) {
            $status = t('Queued');
          }
          elseif ($node->status == 1) {
            $status = t('Published');
          }
          elseif ($node->status == 0) {
            $status = t('Unpublished');
          }
          if ($node_count > 0) {
            $node_body .= $mini_separator . "\n\n";
          }
          $node_body .= ++$node_count . '. ' . $node->title . "\n";
          $node_body .= t('!status !type by !author', array(
            '!status' => $status,
            '!type' => node_get_types('name', $node),
            '!author' => $node->name ? $node->name : variable_get('anonymous', 'Anonymous'),
          )) . "\n";
          $node_body .= '[ ' . url('node/' . $node->nid, array(
            'absolute' => TRUE,
          )) . ' ]' . "\n\n";
          $node_body .= _notify_content($node, $user) . "\n";
        }

        // Prepend node e-mail header as long as user could access at least one node.
        if ($node_count > 0) {
          $node_body = $separator . "\n" . t('Recent content - !count', array(
            '!count' => format_plural(count($nodes), '1 new post', '@count new posts'),
          )) . "\n" . $separator . "\n\n" . $node_body;
        }
      }

      // Write new comments to e-mail if user has permissions and there are
      // comments to be sent.
      if ($user->comment && user_access('access comments') && count($comments)) {
        $total_comment_count = 0;
        foreach ($comments as $nid => $comment) {

          // If we don't already have the node, fetch it.
          if (!isset($nodes[$nid])) {
            $node = node_load($nid);
          }
          else {
            $node = $nodes[$nid];
          }

          // Don't show comments if we're not allowed to view this node.
          if (!node_access('view', $node)) {
            continue;
          }
          if ($comment_body) {
            $comment_body .= $mini_separator . "\n\n";
          }
          $comment_body .= t('!count attached to !type posted by !author: !title', array(
            '!count' => format_plural(count($comment), '1 new comment', '@count new comments'),
            '!title' => $node->title,
            '!type' => node_get_types('name', $node),
            '!author' => $node->name ? $node->name : variable_get('anonymous', 'Anonymous'),
          )) . "\n";
          $comment_count = 0;
          foreach ($comment as $c) {

            //Determine whether to show comment status
            if (user_access('administer comments')) {
              $status = $c->status == COMMENT_PUBLISHED ? t('Published') : t('Unpublished');
              $status = " [{$status}]";
            }
            $comment_body .= '   ' . ++$comment_count . '.' . t('!status !title by !author', array(
              '!status' => $status,
              '!title' => $c->subject,
              '!author' => $c->name ? $c->name : variable_get('anonymous', 'Anonymous'),
            )) . "\n" . '     ' . url('node/' . $nid, array(
              'fragment' => 'comment-' . $c->cid,
              'absolute' => TRUE,
            )) . "\n\n";
            $total_comment_count++;
          }
        }
        if ($total_comment_count > 0) {
          $comment_body = $separator . "\n" . t('Recent comments - !count', array(
            '!count' => format_plural($total_comment_count, '1 new comment', '@count new comments'),
          )) . "\n" . $separator . "\n\n" . $comment_body;
        }
      }
      $body = $node_body . $comment_body;

      // If there was anything new, send mail.
      if ($body) {

        // Set up initial values for e-mail.
        $headers = array();

        //'From' => "$from_name <$from>");
        if (!drupal_mail('notify', 'send', $user->mail, user_preferred_language($user), array(
          'content' => $body,
        ))) {
          $num_failed++;
          db_query('UPDATE {notify} SET attempts = attempts + 1 WHERE uid = %d', $user->uid);
          watchdog('notify', 'User %name (%mail) could not be notified. Mail error.', array(
            '%name' => $user->name,
            '%mail' => $user->mail,
          ), WATCHDOG_ERROR);
        }
        else {
          $num_sent++;
          watchdog('notify', 'User %name (%mail) notified successfully.', array(
            '%name' => $user->name,
            '%mail' => $user->mail,
          ), WATCHDOG_INFO);
        }
      }
    }
  }

  // Restore user.
  _notify_switch_user();
  return array(
    $num_sent,
    $num_failed,
  );
}