It will be very great if the notification e-mail could be enable or disable by node. In my case I have a nodes with embeded imatges with automatic actualitzation, with the actual module I receive e-mails in any change of this nodes and for my will be very good if the administrator can disable this feature in this nodes.

CommentFileSizeAuthor
#4 notify_7.patch4.08 KBlilou
#1 notify-by-node-type.txt22.25 KBlilou

Comments

lilou’s picture

Title: Control notification by node » Notify by node-type
Assigned: Unassigned » lilou
Category: feature » task
Status: Active » Needs review
StatusFileSize
new22.25 KB

I modify this module to add this feature.

This is my code :

1°) Add before function notify_help :

define('NOTIFY_NODE_TYPE', 'notify_node_type_');

2°) Remplace notify_admin_settings by :

/**
 * Menu callback; display notify settings page.
 */
function notify_admin_settings() {
  $period = array(
    900         => format_interval(900),
    1800        => format_interval(1800),
    3600        => format_interval(3600),
    10800       => format_interval(10800),
    21600       => format_interval(21600),
    32400       => format_interval(32400),
    43200       => format_interval(43200),
    86400       => format_interval(86400),
    172800      => format_interval(172800),
    259200      => format_interval(259200),
    604800      => format_interval(604800),
    1209600     => format_interval(1209600),
    2419200     => format_interval(2419200),
    1000000000  => t('Never'),
  );

  $form = array();

  $set = 'global';
  $form[$set] = array(
    '#type' => 'fieldset',
    '#title' => t('Global notification settings'),
    '#collapsible' => true,
    '#collapsed' => true,
  );
    
  $form[$set]['notify_send'] = array(
    '#type' => 'select',
    '#title' => t('Send notifications every'),
    '#default_value' => variable_get('notify_send', 86400),
    '#options' => $period,
    '#description' => t('How often should new content notifications be sent? Requires cron to be running.'),
  );

  $form[$set]['notify_attempts'] = array(
    '#type' => 'select',
    '#title' => t('Number of failed sends after which notifications are disabled'),
    '#default_value' => variable_get('notify_attempts', 5),
    '#options' => array(t('Disabled'), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20),
  );


  $set = 'ntype';
  $form[$set] = array(
    '#type' => 'fieldset',
    '#title' => t('Notification by node type'),
    '#collapsible' => true,
    '#collapsed' => false,
  );
  foreach(node_get_types() as $type) {
    $form[$set][NOTIFY_NODE_TYPE . $type->type] = array(
      '#type' => 'checkbox',
      '#title' => $type->name,
      '#return_value' => 1,
      '#default_value' => variable_get(NOTIFY_NODE_TYPE . $type->type, 0),
    );
  }      
  return system_settings_form($form);
}

3°) And remplace _notify_send by :

/**
 * Helper function to send the notification email.
 * 
 * TODO: Needs some cleanup and themability.
 */
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));

    // Fetch all node type authorized by notify settings
    $ntype = array();
      foreach(node_get_types() as $type) {
    	if ( variable_get(NOTIFY_NODE_TYPE . $type->type, 0) ) {
    		$ntype[] = $type->type;
          }
          if (count($ntype) >= 1){
      	    $reqntype = "AND (n.type = '".implode("' OR n.type = '", $ntype)."') ";
          }else{
          	$reqntype = "";
          }
  	}   

  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) '.$reqntype.' 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());
      $cresult = db_query(db_rewrite_sql('SELECT c.nid, c.cid, c.subject, c.name, n.type FROM {comments} c INNER JOIN {node} n ON n.nid = c.nid WHERE c.status = %d AND c.timestamp > %d AND c.timestamp <= %d '.$reqntype.' 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 .'. '. t('@title', array('@title' => $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);
}

That's all x-)

kevinwalsh’s picture

subscribing

RobRoy’s picture

Status: Needs review » Needs work

Can you please post a patch against CVS according to http://drupal.org/patch? Thanks.

lilou’s picture

Version: 4.7.x-1.x-dev » master
Status: Needs work » Reviewed & tested by the community
StatusFileSize
new4.08 KB

It's my first patch.

stella’s picture

This patch works for me. Other than a few changes to the coding style to match the drupal standards, this patch is good to go.

Cheers,
Stella

stella’s picture

Marked #140518 as a duplicate of this one.

stella’s picture

Marked #140518 as a duplicate of this one.

derieppe’s picture

Hi,

Could theses functions be patched for the notify.module / drupal 5 version and publish here ?

Thanks a lot.

beginner’s picture

Status: Reviewed & tested by the community » Needs work

The patch needs to be re-rolled to match the coding standards: http://drupal.org/coding-standards.

See also: When to set a patch RTBC: http://drupal.org/node/156119

I'll review the patch anyway, but only when I find time.

gracearoha’s picture

just tracking to see when patch has been committed. Thanks!

designwork’s picture

just tracking too

Dirk

beginner’s picture

Category: task » feature

Since you are all tracking, you are certainly aware that issues will be dealt in this order: http://drupal.org/node/159427
How long it will take will depend on how much help I get.
Anyway, this issue is currently assigned to Lilou.

goose2000’s picture

tracking, thanks.

oprior’s picture

subscribing

oprior’s picture

I thought I might just modify line 341 of notify.module like so to restrict it from notifying about certain content types:

$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' AND n.type != 'qc_product_colour' AND n.type != 'content_marketing' AND n.type != 'content_background' AND n.type != 'casetracker_basic_case'), $period, time());

It doesn't seem to be working though, any suggestions at to why?

oprior’s picture

Sorry, I had my extra ANDs after the ORDER By! Seems to be working now!

Does anyone see a reason why it might have been a bad idea for me to have hacked the module this way?

goose2000’s picture

Can anybody offer up a .module of this? I could not get the patch to work with latest release. Or will this be integrated in some future release? Any ideas - thanks.

colincalnan’s picture

subscribing :)

ron collins’s picture

subscribing. notify isn't much use to me with out this.

beginner’s picture

see #12

lilou’s picture

Assigned: lilou » Unassigned
enxox’s picture

This version seems to work very well for me, but I noticed that If I choose a node type made with cck fields, and the title+body option, only text inserted in drupal-body field is notified.
How this can be modified to send info submitted also in other field?

to goose, beginner and others... download the txt in #1 and rename it notify.module

enxox’s picture

This version seems to work very well for me, but I noticed that If I choose a node type made with cck fields, and the title+body option, only text inserted in drupal-body field is notified.
How this can be modified to send info submitted also in other field?

to goose, beginner and others... download the txt in #1 and rename it notify.module

mdowsett’s picture

In response to post #15

I've already applied a few patches to my .module file (since patches don't seem to being committed to new releases of this module) so my line 341 is moved and modified by now.....I was hoping you could confirm some things for me to help with applying your code change.

I'm guessing my existing line to change is:

$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) OR (n.changed > %d AND n.changed <= %d)) ORDER BY n.created'), $period, time(), $period, time());

Is that right? It's now at line 369 so it is close. I've applied patches to notify of edited nodes (the stock module just notifies new nodes) and I think that code is now in this line so I'd want to keep it.

Would you mind modifying my code to be proper (I can change the node types myself)...so you add the node types that you WANT to be sent out right (not list the ones you want blocked from being sent out).

This is my best guess (I am not a coder):

$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) OR (n.changed > %d AND n.changed <= %d)) ORDER BY n.created' AND n.type != 'include_node_type1' AND n.type != 'include_node_type2' AND n.type != 'include_node_type3' AND n.type != 'include_node_type4'), $period, time(), $period, time());

A GUI would be ideal of course but this will get me by.

MANY thanks.

mdowsett’s picture

this didn't work....I thought I'd try it....I got a whole whack of errors.

And I didn't understand post #16....I tried taking out just the one AND (the one immediately after ORDER BY) but that caused the entire site to go white (no errors).

mdowsett’s picture

six weeks later on this issue....I decided to start fresh.

I had to manually change the code in #1 to get the options in the admin settings to select which node types I wanted to restrict the notifications to...and got that menu up.

I haven't yet seen any notifications come thru - this concerns me. I've created new content since the installation and preferences were set and also ran cron (manually).

Is there anywhere that I can see a log of what notifications went out?

matt2000’s picture

Status: Needs work » Closed (won't fix)

See Notify by Views as a means to accomplish this using views to select nodes by type.

Closing due to inactivity.