You are here

function _notify_send in Notify 5.2

Same name and namespace in other branches
  1. 8 notify.module \_notify_send()
  2. 5 notify.module \_notify_send()
  3. 6 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 352

Code

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

  // Store current user
  // Fetch users with notify enabled
  $uresult = db_query('SELECT u.uid, u.name, u.mail, 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);

    // Fetch all new nodes and 'load' it to get proper body, etc.
    $nresult = db_query(db_rewrite_sql('SELECT n.nid FROM {node} n WHERE (n.status = 1 OR n.moderate = 1) AND n.created > %d AND n.created <= %d ORDER BY n.created'), $period, time());
    $nodes = array();
    while ($node = db_fetch_object($nresult)) {
      $nodes[$node->nid] = node_load($node->nid);
    }

    // Fetch new comments.
    $cresult = db_query(db_rewrite_sql('SELECT c.nid, c.cid, c.subject, c.name FROM {comments} c WHERE c.status = %d AND c.timestamp > %d AND c.timestamp <= %d ORDER BY c.nid, c.timestamp', 'c'), COMMENT_PUBLISHED, $period, time());
    $comments = array();
    while ($comment = db_fetch_object($cresult)) {
      $comments[$comment->nid][] = $comment;
    }
    $node_body = '';
    $comment_body = '';

    // Write new node content to e-mail if user has permissions and nodes are
    // ready to be sent.
    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->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, NULL, NULL, 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])) {
          $nodes[$nid] = node_load($nid);
        }

        // Don't show comments if we're not allowed to view this node.
        if (!node_access('view', $nodes[$nid], $user->uid)) {
          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' => $nodes[$nid]->title,
          '!type' => node_get_types('name', $nodes[$nid]),
          '!author' => $nodes[$nid]->name ? $nodes[$nid]->name : variable_get('anonymous', 'Anonymous'),
        )) . "\n";
        $comment_count = 0;
        foreach ($comment as $c) {
          $comment_body .= '   ' . ++$comment_count . '. ' . t('!title by !author', array(
            '!title' => $c->subject,
            '!author' => $c->name ? $c->name : variable_get(anonymous, 'Anonymous'),
          )) . "\n" . '     ' . url('node/' . $nid, NULL, 'comment-' . $c->cid, 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.
      $from = variable_get('site_mail', ini_get('sendmail_from'));
      $from_name = variable_get('site_name', 'Drupal');
      $subject = t('!sitename new content notification for !username', array(
        '!username' => $user->name,
        '!sitename' => variable_get('site_name', 'Drupal'),
      ));
      $body = t('Greetings !user,', array(
        '!user' => $user->name,
      )) . "\n\n" . $body;
      $body .= "\n-- \n";
      $body .= t('This is an automatic e-mail from !sitename.', array(
        '!sitename' => variable_get('site_name', 'Drupal'),
      )) . "\n";
      $body .= t('To stop receiving these e-mails, change your notification preferences at !notify-url', array(
        '!notify-url' => url("user/{$user->uid}/notify", NULL, NULL, TRUE),
      )) . "\n";
      $headers = array();

      //'From' => "$from_name <$from>");
      if (!drupal_mail('notify_mail', $user->mail, $subject, wordwrap($body, 72), $from, $headers)) {
        $num_failed++;
        db_query('UPDATE {notify} SET attempts = attempts + 1 WHERE uid = %d', $user->uid);
        watchdog('error', t('Notify: User %name (%mail) could not be notified. Mail error.', array(
          '%name' => $user->name,
          '%mail' => $user->mail,
        )));
      }
      else {
        $num_sent++;
        watchdog('user', t('Notify: User %name (%mail) notified successfully.', array(
          '%name' => $user->name,
          '%mail' => $user->mail,
        )));
      }
    }
  }

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