Index: notify.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/notify/notify.module,v
retrieving revision 2.62
diff -u -u -F^f -r2.62 notify.module
--- notify.module	11 Aug 2006 21:24:47 -0000	2.62
+++ notify.module	18 Dec 2006 17:11:20 -0000
@@ -7,9 +7,49 @@
 function notify_help($section) {
   switch ($section) {
     case 'admin/help#notify':
-      $output = '<p>'. t('The notification module allows users to subscribe to periodic e-mails which include all new or revised content and/or comments much like the daily news letters sent by some websites.  Even if this feature is not configured for normal site users, it can be a useful feature for an administrator of a site to monitor content submissions and comment posts.') .'</p>';
-      $output .= '<p>'. t('The administrator sets the frequency of the e-mails in the notify administration interface. They can also set how many e-mail failures should occur before notify stops sending notifications.  Note that cron must be enabled for notifications to be sent out.') .'</p>';
-      $output .= t('<p>You can</p><ul><li>set up your site to run tasks automatically at required intervals. For more information, see <a href="%admin-help-system">cron</a>.</li><li>administer notify <a href="%admin-settings-notify">administer &gt;&gt; settings &gt;&gt; notify</a>.</li></ul>', array('%admin-help-system' => url('admin/help/system'), '%admin-settings-notify' => url('admin/settings/notify')));
+      $output = '<p>'. t('The notification module allows users to subscribe to periodic e-mails which include all new content and/or comments, much like the daily news letters sent by some websites.  Even if this feature is not configured for normal site users, it can be a useful feature for an administrator of a site to monitor content submissions and comment posts.') .'</p>';
+      $output .= '<p>'. t('Note that cron, which lets your site run tasks automatically at required intervals, must be enabled for notifications to be sent out. For more information, see <a href="%admin-help-system">cron</a>.', array('%admin-help-system' => url('admin/help/system'))) .'</p>';
+      $output .= '<p>' . t('You can configure the following settings from <a href="%admin-settings-notify">administer &gt;&gt; settings &gt;&gt; notify</a>:', array('%admin-settings-notify' => url('admin/settings/notify'))) . '</p>';
+      $output .= '<ul><li>' . t('The frequency of notification e-mails') . '</li>';
+      $output .= '<li>' . t('How many e-mail failures should occur before notify stops sending notifications') . '</li>';
+      $output .= '<li>' . t('The format of the notification e-mail.') . '</li></ul>';
+      $output .= '<p><strong>' . t('Configuring the notification e-mail') . '</strong></p>';
+      $output .= '<p>' . t('The administration page presents you with a template for each section of the e-mail. You can alter these templates to suit whatever format you prefer. You can customize the following fields:') . '</p>';
+      $output .= '<ul><li>' . t('From: the address the e-mail will appear to come from.') . '</li>';
+      $output .= '<li>' . t('Subject: the subject line for the e-mail.') . '</li>';
+      $output .= '<li>' . t('Greeting: the text that will start the e-mail.') . '</li>';
+      $output .= '<li>' . t('Signature: the text that will be added to the bottom of each e-mail.') . '</li>';
+      $output .= '<li>' . t('Node summary: a summary of the new nodes.') . '</li>';
+      $output .= '<li>' . t('Node item: the format for reporting individual nodes.') . '</li>';
+      $output .= '<li>' . t('Node separator: the text to separate nodes from each other.') . '</li>';
+      $output .= '<li>' . t('Comment summary: a summary of the new comments.') . '</li>';
+      $output .= '<li>' . t('Comment item: the format for reporting individual comments.') . '</li></ul>';
+      $output .= '<p>' . t('A variety of different variables are available for you to configure what information you want to present to the user. You can put any of the following variables into the e-mail template:') . '</p>';
+      $output .= '<ul><li>' . t('%comment_age -- The time since the comment was created') . '</li>';
+      $output .= '<li>' . t('%comment_author -- The author of the comment') . '</li>';
+      $output .= '<li>' . t('%comment_count -- The message "%comment_total new comment(s)", appropriately pluralized') . '</li>';
+      $output .= '<li>' . t('%comment_node_count -- The message "%comment_node_total new comment(s)", appropriately pluralized') . '</li>';
+      $output .= '<li>' . t('%comment_node_total -- Total number of new comments') . '</li>';
+      $output .= '<li>' . t('%comment_number -- The linear order of the comment (the n\'th item attached to a particular node)') . '</li>';
+      $output .= '<li>' . t('%comment_total -- Total number of new comments attached to this node') . '</li>';
+      $output .= '<li>' . t('%comment_title -- The title of the comment') . '</li>';
+      $output .= '<li>' . t('%comment_url -- The URL to the new comment') . '</li>';
+      $output .= '<li>' . t('%node_age -- The time since the node was created') . '</li>';
+      $output .= '<li>' . t('%node_author -- The author of the node') . '</li>';
+      $output .= '<li>' . t('%node_content -- The content of the node formatted according to the user\'s preferences. If the user has selected "title only", this variable will be an empty string. Otherwise, it contains either the teaser or the full body, as appropriate for each user.') . '</li>';
+      $output .= '<li>' . t('%node_count -- The message "%node_total new node(s)", appropriately pluralized') . '</li>';
+      $output .= '<li>' . t('%node_number -- The linear order of the node (the n\'th item).') . '</li>';
+      $output .= '<li>' . t('%node_status -- Published/Queued/Unplublished') . '</li>';
+      $output .= '<li>' . t('%node_title -- The title of the item') . '</li>';
+      $output .= '<li>' . t('%node_total -- The total number of new nodes') . '</li>';
+      $output .= '<li>' . t('%node_type -- The type of the item (book page, story, etc.)') . '</li>';
+      $output .= '<li>' . t('%node_url -- The URL to the new item') . '</li>';
+      $output .= '<li>' . t('%notify_url -- The URL of the user\'s notify settings page') . '</li>';
+      $output .= '<li>' . t('%site_mail -- The e-mail address of the site') . '</li>';
+      $output .= '<li>' . t('%site_name -- The name of the site') . '</li>';
+      $output .= '<li>' . t('%user_id: The user\'s unique ID number') . '</li>';
+      $output .= '<li>' . t('%user_name -- The user\'s login name.') . '</li></ul>';
+
       $output .= '<p>'. t('For more information please read the configuration and customization handbook <a href="%notify">Notify page</a>.', array('%notify' => 'http://www.drupal.org/handbook/modules/notify/')) .'</p>';
       return $output;
     case 'admin/modules#description':
@@ -59,6 +99,113 @@ function notify_settings() {
     '#options' => array(t('Disabled'), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20),
   );
 
+  $values = _notify_get_mail_template();
+
+  $form['mail_template'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('E-mail format'),
+    '#collapsible' => TRUE,
+    '#collapsed' => TRUE,
+  );
+
+  $form['mail_template']['notify_email_from'] = array(
+    '#type' => 'textfield',
+    '#title' => t('From'),
+    '#default_value' => $values->notify_email_from,
+    '#size' => 50,
+    '#maxlength' => 128,
+    '#description' => t('The e-mail will appear to be from this address.')
+  );
+
+  $form['mail_template']['notify_email_subject'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Subject'),
+    '#default_value' => $values->notify_email_subject,
+    '#size' => 50,
+    '#maxlength' => 128,
+    '#description' => t('The subject line for the e-mail.')
+  );
+
+  $form['mail_template']['notify_email_greeting'] = array(
+    '#type' => 'textarea',
+    '#title' => t('E-mail greeting'),
+    '#default_value' => $values->notify_email_greeting,
+    '#cols' => 80,
+    '#rows' => 4,
+    '#maxlength' => 1024,
+    '#description' => t('Each e-mail will start with this text.')
+  );
+
+  $form['mail_template']['notify_email_signature'] = array(
+    '#type' => 'textarea',
+    '#title' => t('E-mail signature'),
+    '#default_value' => $values->notify_email_signature,
+    '#cols' => 80,
+    '#rows' => 4,
+    '#maxlength' => 1024,
+    '#description' => t('This text will be added to the bottom of each e-mail.')
+  );
+
+  $form['mail_template']['notify_node_summary'] = array(
+    '#type' => 'textarea',
+    '#title' => t('Node summary'),
+    '#default_value' => $values->notify_node_summary,
+    '#cols' => 80,
+    '#rows' => 4,
+    '#maxlength' => 1024,
+    '#description' => t('A summary of the node changes.'),
+  );
+
+  $form['mail_template']['notify_node_item'] = array(
+    '#type' => 'textarea',
+    '#title' => t('Node item'),
+    '#default_value' => $values->notify_node_item,
+    '#cols' => 80,
+    '#rows' => 4,
+    '#maxlength' => 1024,
+    '#description' => t('A template for each node. If you use the field %node_content, it will be filled in with the user\'s choice of content (teaser or body).'),
+  );
+
+  $form['mail_template']['notify_node_separator'] = array(
+    '#type' => 'textarea',
+    '#title' => t('Node separator'),
+    '#default_value' => $values->notify_node_separator,
+    '#cols' => 80,
+    '#rows' => 2,
+    '#maxlength' => 1024,
+    '#description' => t('Node entries will be separated by this text.'),
+  );
+
+  $form['mail_template']['notify_comment_summary'] = array(
+    '#type' => 'textarea',
+    '#title' => t('Comment summary'),
+    '#default_value' => $values->notify_comment_summary,
+    '#cols' => 80,
+    '#rows' => 4,
+    '#maxlength' => 1024,
+    '#description' => t('A summary of the comment changes.'),
+  );
+
+  $form['mail_template']['notify_comment_node'] = array(
+    '#type' => 'textarea',
+    '#title' => t('Comment node'),
+    '#default_value' => $values->notify_comment_node,
+    '#cols' => 80,
+    '#rows' => 4,
+    '#maxlength' => 1024,
+    '#description' => t('Details about the node that has new comments.'),
+  );
+
+  $form['mail_template']['notify_comment_item'] = array(
+    '#type' => 'textarea',
+    '#title' => t('Comment item'),
+    '#default_value' => $values->notify_comment_item,
+    '#cols' => 80,
+    '#rows' => 4,
+    '#maxlength' => 1024,
+    '#description' => t('A template for each comment.'),
+  );
+
   return $form;
 }
 
@@ -294,8 +441,13 @@ function _notify_content($node, $notify)
 
 function _notify_send() {
   $period = variable_get('notify_send_last', time() - variable_get('notify_send', 86400));
-  $separator = '------------------------------------------------------------------------------';
-  $mini_separator = '---';
+
+  $substitute = array(
+    '%site_mail' => variable_get('site_mail', ini_get('sendmail_from')),
+    '%site_name' => mime_header_encode(variable_get('site_name', 'drupal')),
+  );
+
+  $templates = _notify_get_mail_template();
 
   $ret = array('sent' => 0, 'failed' => 0);
 
@@ -319,7 +471,7 @@ function _notify_send() {
     }
 
     // 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();
@@ -333,7 +485,11 @@ function _notify_send() {
     // 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)) {
@@ -342,29 +498,36 @@ function _notify_send() {
 
         // 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');
         }
+        // This condition should never be true, given the query. Delete?
         elseif ($node->status == 0) {
-          $status = t('Unpublished');
+          $substitute['%node_status'] = t('Unpublished');
         }
 
-        if ($node_count > 0) {
-          $node_body .= $mini_separator . "\n\n";
+        if ($node_count++ > 0) {
+          $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";
+
+        $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);
+        $substitute["%node_content"] = _notify_content($node, $user);
+
+        $node_body .= strtr($templates->notify_node_item, $substitute);
       }
 
       // 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;
+        $substitute['%node_total'] = $node_count;
+        $substitute['%node_count'] = format_plural($node_count, '1 new node', '%count new nodes');
+        $node_body = strtr($templates->notify_node_summary, $substitute) . $node_body;
       }
     }
 
@@ -385,29 +548,38 @@ function _notify_send() {
           }
 
           if ($nid_old > 0) {
-            $comment_body .= $mini_separator . "\n\n";
+            $comment_body .= strtr($templates['notify_node_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";
-
+	  $node = $nodes[$nid];
+          $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);
+          $substitute['%comment_node_total'] = count($comment);
+          $substitute['%comment_node_count'] = format_plural(count($comment), '1 new comment', '%count new comments');
+          $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";
           $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;
+        $substitute['%comment_count'] = format_plural($total_comment_count, '1 new comment', '%count new comments');
+        $comment_body = strtr($templates->notify_comment_summary, $substitute) . $comment_body;
       }
     }
 
@@ -416,17 +588,13 @@ function _notify_send() {
     // 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 = 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')));
-
-      $body = t('Greetings %user,', array("%user" => $user->name))."\n\n$body";
+      $from      = mime_header_encode(strtr($templates->notify_email_from, $substitute));
+      $headers = "From: $from\nReply-to: $from\nReturn-path: $from\nX-Mailer: Drupal\nErrors-to: $from\n";
+      $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";
-
-      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, wordwrap($greeting . $body . $signature, 72), $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' => '<em>'. $user->name .'</em>', '%mail' => $user->mail)));
@@ -518,3 +686,27 @@ function _notify_switch_user($uid = NULL
     $orig_user[] = $user;
   }
 }
+
+function _notify_get_mail_template() {
+  static $defs;
+  if (isset($defs)) {
+    return $defs;
+  }
+  $defs = new stdClass();
+  $separator = "------------------------------------------------------------------------------";
+
+  $defs->notify_email_from      = t(variable_get('notify_email_from', "%site_name <%site_mail>"));
+  $defs->notify_email_subject   = t(variable_get('notify_email_subject', "%site_name new content notification for %user_name"));
+  $defs->notify_email_greeting  = t(variable_get('notify_email_greeting', "Greetings %user_name,\n\n"));
+  $defs->notify_email_signature = t(variable_get('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   = t(variable_get('notify_node_summary', "$separator\nRecent content - %node_count\n$separator\n\n"));
+  $defs->notify_node_item      = t(variable_get('notify_node_item', "%node_number. %node_title\n%node_status %node_type by %node_author\n[ %node_url ]\n%node_content\n"));
+  $defs->notify_node_separator = t(variable_get('notify_node_separator', "---\n\n"));
+
+  $defs->notify_comment_summary   = t(variable_get('notify_comment_summary', "$separator\nRecent comments - %comment_count\n$separator\n\n"));
+  $defs->notify_comment_node      = t(variable_get('notify_comment_node', "%comment_node_count attached to %node_type posted by %node_author: %node_title\n"));
+  $defs->notify_comment_item      = t(variable_get('notify_comment_item', "   %comment_number. %comment_title by %comment_author\n     %comment_url\n\n"));
+
+  return $defs;
+}
