diff -x CVS -urN notify-cvs/notify.inc notify-flatcap/notify.inc --- notify-cvs/notify.inc 2006-06-26 21:51:14.000000000 +0100 +++ notify-flatcap/notify.inc 2006-06-29 17:19:31.000000000 +0100 @@ -163,22 +163,27 @@ function _notify_send() { $period = variable_get('notify_send_last', time() - variable_get('notify_send', 86400)); - $separator = '------------------------------------------------------------------------------'; - $mini_separator = '---'; - + + $templates = notify_email_variables(); + + $substitute = array( + '%site_mail' => variable_get('site_mail', ini_get('sendmail_from')), + '%site_name' => mime_header_encode(variable_get('site_name', 'drupal')), + ); + $ret = array('sent' => 0, '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', + '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 ORDER BY n.created'), $period); @@ -188,7 +193,7 @@ } // Fetch new comments - $cresult = db_query(db_rewrite_sql('SELECT c.nid, c.cid, c.subject, c.pid, u.name FROM {comments} c ' . + $cresult = db_query(db_rewrite_sql('SELECT c.nid, c.cid, c.subject, c.pid, c.timestamp, u.name FROM {comments} c ' . 'INNER JOIN {users} u ON u.uid = c.uid WHERE c.status = %d AND c.timestamp > %d ' . 'ORDER BY c.nid, c.timestamp', 'c'), COMMENT_PUBLISHED, $period); $comments = array(); @@ -197,99 +202,121 @@ } // Set up initial values for e-mail - $from = variable_get('site_mail', ini_get('sendmail_from')); - $from_name = mime_header_encode(variable_get('site_name', 'drupal')); - $subject = t('%sitename new content notification for %username', array('%username' => $user->name, '%sitename' => variable_get('site_name', 'drupal'))); $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)) { - + + $substitute['%user_id'] = $user->uid; + $substitute['%user_name'] = $user->name; + $substitute['%notify_url'] = url("user/$user->uid/notify", NULL, NULL, TRUE); + $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 new 4.7 moderation modules if ($node->moderate) { - $status = t('Queued'); + $substitute['%node_status'] = t('Queued'); } elseif ($node->status == 1) { - $status = t('Published'); + $substitute['%node_status'] = t('Published'); } elseif ($node->status == 0) { - $status = t('Unpublished'); + $substitute['%node_status'] = t('Unpublished'); } - + if ($node_count > 0) { - $node_body .= $mini_separator . "\n\n"; + $node_body .= strtr($templates['notify_node_separator'], $substitute); } - $node_body .= ++$node_count.'. '.t('%title', array('%title' => $node->title)) ."\n"; - $node_body .= t('%status %type by %author', array('%status' => $status, '%type' => _node_names('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 node', '%count new nodes'))) ."\n" - . $separator . "\n\n" . $node_body; - } + + $node_count++; + $substitute['%node_age'] = format_interval(time() - $node->changed); + $substitute['%node_author'] = ($node->name ? $node->name : variable_get('anonymous', 'Anonymous')); + $substitute['%node_number'] = $node_count; + $substitute['%node_title'] = $node->title; + $substitute['%node_type'] = _node_names('name', $node); + $substitute['%node_url'] = url('node/'.$node->nid, NULL, NULL, TRUE); + + $user->teasers = 1; + $substitute["%node_teaser"] = _notify_content($node, $user); + $user->teasers = 2; + $substitute["%node_body"] = _notify_content($node, $user); + + $node_body .= strtr($templates['notify_node_item'], $substitute); + } + + if ($node_body) { + $substitute['%node_total'] = $node_count; + $node_body = strtr($templates['notify_node_summary'], $substitute) . $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)) { + if ($user->comment && user_access('access comments') && count($comments)) { $total_comment_count = 0; $nid_old = 0; foreach ($comments as $nid => $comment) { if ($nid != $nid_old) { // If we don't already have the node, fetch it. - if (!array_key_exists($nid, $nodes)) { + if (!array_key_exists($nid, $nodes)) { $nodes[$node->nid] = node_load($nid); } - + if ($nid_old > 0) { - $comment_body .= $mini_separator . "\n\n"; + $comment_body .= strtr($templates['notify_comment_separator'], $substitute); } - - $comment_body .= t('%count new comments attached to %type posted by %author: %title', - array('%count' => count($comment), '%title' => $nodes[$nid]->title, - '%type' => _node_names('name', $nodes[$nid]), '%author' => - ($nodes[$nid]->name ? $nodes[$nid]->name : variable_get('anonymous', 'Anonymous'))))."\n"; + $substitute['%node_age'] = format_interval(time() - $node->changed); + $substitute['%node_author'] = ($node->name ? $node->name : variable_get('anonymous', 'Anonymous')); + $substitute['%node_number'] = $node_count; + $substitute['%node_title'] = $node->title; + $substitute['%node_type'] = _node_names('name', $node); + $substitute['%node_url'] = url('node/'.$node->nid, NULL, NULL, TRUE); + + $comment_body .= strtr($templates['notify_comment_node'], $substitute); $nid_old = $nid; } - + $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"; + $comment_count++; $total_comment_count++; + + $substitute['%comment_age'] = format_interval(time() - $c->timestamp); + $substitute['%comment_author'] = ($c->name ? $c->name : variable_get('anonymous', 'Anonymous')); + $substitute['%comment_number'] = $comment_count; + $substitute['%comment_title'] = $c->subject; + $substitute['%comment_url'] = url('node/'.$nid, NULL, 'comment-'.$c->cid, TRUE); + + $comment_body .= strtr($templates['notify_comment_item'], $substitute); } } - + 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; + $substitute['%comment_total'] = $total_comment_count; + $comment_body = strtr($templates['notify_comment_summary'], $substitute) . $comment_body; } } - + $body = $node_body . $comment_body; - + // If there was anything new, send mail if ($body) { - $body = t('Greetings %user,', array("%user" => $user->name))."\n\n$body"; + $from = strtr($templates['notify_email_from'], $substitute); + $headers = strtr($templates['notify_email_headers'], $substitute); + $subject = strtr($templates['notify_email_subject'], $substitute); + $greeting = strtr($templates['notify_email_greeting'], $substitute); + $signature = strtr($templates['notify_email_signature'], $substitute); - $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 = "From: " . mime_header_encode($from) . "\n" . $headers; + $body = wordwrap($prefix . $body . $suffix, 72); - if (!user_mail($user->mail, $subject, wordwrap($body, 72), "From: $from_name <$from>\nReply-to: $from_name <$from>\nReturn-path: $from_name <$from>\nX-Mailer: Drupal\nErrors-to: $from\n")) { + if (!user_mail($user->mail, $subject, $body, $headers)) { $ret['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))); @@ -382,3 +409,48 @@ } } +function notify_email_defaults() { + $separator = "------------------------------------------------------------------------------"; + $defs = array(); + + $defs['notify_email_from'] = "%site_name <%site_mail>"; + $defs['notify_email_subject'] = "%site_name new content notification for %user_name"; + $defs['notify_email_headers'] = "Reply-to: %site_name <%site_mail>\nReturn-path: %site_name <%site_mail>\nX-Mailer: Drupal\nErrors-to: %site_mail"; + $defs['notify_email_greeting'] = "Greetings %user_name,\n\n"; + $defs['notify_email_signature'] = "\n-- \nThis is an automatic e-mail from %site_name.\nTo stop receiving these e-mails, change your notification preferences at %notify_url\n"; + + $defs['notify_node_summary'] = "$separator\nRecent content - %node_total new nodes\n$separator\n\n"; + $defs['notify_node_item'] = "%node_number. %node_title\n%node_status %node_type by %node_author\n[ %node_url ]\n\n"; + $defs['notify_node_separator'] = "---\n\n"; + + $defs['notify_comment_summary'] = "$separator\nRecent comments - %comment_total new comments\n$separator\n\n"; + $defs['notify_comment_node'] = "%comment_node_total new comments attached to %node_type posted by %node_author: %node_title\n"; + $defs['notify_comment_item'] = " %comment_number. %comment_title by %comment_author\n %comment_url\n\n"; + $defs['notify_comment_separator'] = "---\n\n"; + + return $defs; +} + +function notify_email_variables() { + $separator = "------------------------------------------------------------------------------"; + $vars = array(); + $defs = notify_email_defaults(); + + $vars['notify_email_from'] = variable_get('notify_email_from', $defs['notify_email_from']); + $vars['notify_email_subject'] = variable_get('notify_email_subject', $defs['notify_email_subject']); + $vars['notify_email_headers'] = variable_get('notify_email_headers', $defs['notify_email_headers']); + $vars['notify_email_greeting'] = variable_get('notify_email_greeting', $defs['notify_email_greeting']); + $vars['notify_email_signature'] = variable_get('notify_email_signature', $defs['notify_email_signature']); + + $vars['notify_node_summary'] = variable_get('notify_node_summary', $defs['notify_node_summary']); + $vars['notify_node_item'] = variable_get('notify_node_item', $defs['notify_node_item']); + $vars['notify_node_separator'] = variable_get('notify_node_separator', $defs['notify_node_separator']); + + $vars['notify_comment_summary'] = variable_get('notify_comment_summary', $defs['notify_comment_summary']); + $vars['notify_comment_node'] = variable_get('notify_comment_node', $defs['notify_comment_node']); + $vars['notify_comment_item'] = variable_get('notify_comment_item', $defs['notify_comment_item']); + $vars['notify_comment_separator'] = variable_get('notify_comment_separator', $defs['notify_comment_separator']); + + return $vars; +} + diff -x CVS -urN notify-cvs/notify.module notify-flatcap/notify.module --- notify-cvs/notify.module 2006-04-21 10:01:37.000000000 +0100 +++ notify-flatcap/notify.module 2006-06-29 17:22:31.000000000 +0100 @@ -38,10 +38,15 @@ 1000000000 => 'Never' ); + include_once drupal_get_path('module', 'notify') . '/notify.inc'; + $vars = notify_email_variables(); + + /* Notification settings */ $form['notify_settings'] = array( '#type' => 'fieldset', '#title' => t('E-mail notification settings'), - '#collapsible' => TRUE + '#collapsible' => TRUE, + '#collapsed' => TRUE, ); $form['notify_settings']['notify_send'] = array( @@ -59,6 +64,162 @@ '#options' => array(t('Disabled'), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20) ); + /* E-mail settings */ + $form['notify_email_settings'] = array( + '#type' => 'fieldset', + '#title' => t('E-mail settings'), + '#collapsible' => TRUE, + '#collapsed' => TRUE, + ); + + $form['notify_email_settings']['notify_email_from'] = array( + '#type' => 'textfield', + '#title' => t('From'), + '#default_value' => $vars['notify_email_from'], + '#size' => 50, + '#maxlength' => 128, + '#description' => t('The e-mail will appear to be from this address.') + ); + + $form['notify_email_settings']['notify_email_subject'] = array( + '#type' => 'textfield', + '#title' => t('Subject'), + '#default_value' => $vars['notify_email_subject'], + '#size' => 50, + '#maxlength' => 128, + '#description' => t('The subject line for the e-mail.') + ); + + $form['notify_email_settings']['notify_email_headers'] = array( + '#type' => 'textarea', + '#title' => t('Extra headers'), + '#default_value' => $vars['notify_email_headers'], + '#cols' => 80, + '#rows' => 4, + '#maxlength' => 1024, + '#description' => t('Add extra headers to the e-mail.') + ); + + $form['notify_email_settings']['notify_email_greeting'] = array( + '#type' => 'textarea', + '#title' => t('E-mail greeting'), + '#default_value' => $vars['notify_email_greeting'], + '#cols' => 80, + '#rows' => 4, + '#maxlength' => 1024, + '#description' => t('Each e-mail will start with this text.') + ); + + $form['notify_email_settings']['notify_email_signature'] = array( + '#type' => 'textarea', + '#title' => t('E-mail signature'), + '#default_value' => $vars['notify_email_signature'], + '#cols' => 80, + '#rows' => 4, + '#maxlength' => 1024, + '#description' => t('This text will be added to the bottom of each e-mail.') + ); + + /* Node settings */ + $form['notify_node_settings'] = array( + '#type' => 'fieldset', + '#title' => t('Node settings'), + '#collapsible' => TRUE, + '#collapsed' => TRUE, + '#description' => t('These settings affect the display of nodes in the email.'), + ); + + $form['notify_node_settings']['notify_node_summary'] = array( + '#type' => 'textarea', + '#title' => t('Node summary'), + '#default_value' => $vars['notify_node_summary'], + '#cols' => 80, + '#rows' => 4, + '#maxlength' => 1024, + '#description' => t('A summary of the node changes.') + ); + + $form['notify_node_settings']['notify_node_item'] = array( + '#type' => 'textarea', + '#title' => t('Node item'), + '#default_value' => $vars['notify_node_item'], + '#cols' => 80, + '#rows' => 4, + '#maxlength' => 1024, + '#description' => t('A template for each node or comment.') + ); + + $form['notify_node_settings']['notify_node_separator'] = array( + '#type' => 'textarea', + '#title' => t('Node separator'), + '#default_value' => $vars['notify_node_separator'], + '#cols' => 80, + '#rows' => 2, + '#maxlength' => 1024, + '#description' => t('Node entries will be separated by this text.') + ); + + /* Comment settings */ + $form['notify_comment_settings'] = array( + '#type' => 'fieldset', + '#title' => t('Comment settings'), + '#collapsible' => TRUE, + '#collapsed' => TRUE, + '#description' => t('These settings affect the display of comments in the email.'), + ); + + $form['notify_comment_settings']['notify_comment_summary'] = array( + '#type' => 'textarea', + '#title' => t('Comment summary'), + '#default_value' => $vars['notify_comment_summary'], + '#cols' => 80, + '#rows' => 4, + '#maxlength' => 1024, + '#description' => t('A summary of the comment changes.') + ); + + $form['notify_comment_settings']['notify_comment_node'] = array( + '#type' => 'textarea', + '#title' => t('Comment node'), + '#default_value' => $vars['notify_comment_node'], + '#cols' => 80, + '#rows' => 4, + '#maxlength' => 1024, + '#description' => t('Details about the node that has new comments.') + ); + + $form['notify_comment_settings']['notify_comment_item'] = array( + '#type' => 'textarea', + '#title' => t('Comment item'), + '#default_value' => $vars['notify_comment_item'], + '#cols' => 80, + '#rows' => 4, + '#maxlength' => 1024, + '#description' => t('A template for each comment or comment.') + ); + + $form['notify_comment_settings']['notify_comment_separator'] = array( + '#type' => 'textarea', + '#title' => t('Comment separator'), + '#default_value' => $vars['notify_comment_separator'], + '#cols' => 80, + '#rows' => 2, + '#maxlength' => 1024, + '#description' => t('Comment entries will be separated by this text.') + ); + + + /* User settings */ + + /* User can: + * Enable/disable node notifications + * Enable/disable comment notifications + * Configure greeting + * Configure signature + * Configure node summary, entry and separator + * Configure comment summary, entry and separator + */ + return $form; }