Notify by node-type

cercamon - January 25, 2007 - 12:26
Project:Notify
Version:HEAD
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:won't fix
Description

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.

#1

lilou - February 1, 2007 - 23:09
Title:Control notification by node» Notify by node-type
Category:feature request» task
Assigned to:Anonymous» lilou
Status:active» patch (code needs review)

I modify this module to add this feature.

This is my code :

1°) Add before function notify_help :

<?php
define
('NOTIFY_NODE_TYPE', 'notify_node_type_');
?>

2°) Remplace notify_admin_settings by :

<?php

/**
* 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 :

<?php
/**
* 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-)

AttachmentSize
notify-by-node-type.txt22.25 KB

#2

kevinwalsh - February 4, 2007 - 00:15

subscribing

#3

RobRoy - February 10, 2007 - 01:08
Status:patch (code needs review)» patch (code needs work)

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

#4

lilou - February 10, 2007 - 11:34
Version:4.7.x-1.x-dev» HEAD
Status:patch (code needs work)» patch (reviewed & tested by the community)

It's my first patch.

AttachmentSize
notify_7.patch4.08 KB

#5

stella - March 27, 2007 - 13:59

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

#6

stella - May 10, 2007 - 10:20

Marked #140518 as a duplicate of this one.

#7

stella - May 10, 2007 - 10:24

Marked #140518 as a duplicate of this one.

#8

derieppe - May 28, 2007 - 19:53

Hi,

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

Thanks a lot.

#9

beginner - July 9, 2007 - 07:55
Status:patch (reviewed & tested by the community)» patch (code 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.

#10

gracearoha - August 4, 2007 - 05:13

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

#11

DesignWork - August 4, 2007 - 06:29

just tracking too

Dirk

#12

beginner - August 4, 2007 - 06:41
Category:task» feature request

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.

#13

goose2000 - October 5, 2007 - 19:15

tracking, thanks.

#14

oprior - October 25, 2007 - 03:37

subscribing

#15

oprior - October 25, 2007 - 04:24

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?

#16

oprior - October 25, 2007 - 05:38

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?

#17

goose2000 - October 26, 2007 - 16:41

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.

#18

ccalnan - December 5, 2007 - 20:33

subscribing :)

#19

elraun - December 11, 2007 - 00:49

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

#20

beginner - December 16, 2007 - 11:43

see #12

#21

lilou - December 16, 2007 - 12:46
Assigned to:lilou» Anonymous

#22

enxox - January 1, 2008 - 16:05

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

#23

enxox - January 1, 2008 - 16:05

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

#24

mdowsett - April 9, 2008 - 14:37

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.

#25

mdowsett - April 11, 2008 - 13:28

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).

#26

mdowsett - May 21, 2008 - 16:30

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?

#27

matt2000 - August 23, 2008 - 19:32
Status:patch (code needs work)» won't fix

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

Closing due to inactivity.

 
 

Drupal is a registered trademark of Dries Buytaert.