Index: simplenews.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/simplenews/simplenews.module,v
retrieving revision 1.48.2.9
diff -u -p -r1.48.2.9 simplenews.module
--- simplenews.module	7 May 2007 19:29:13 -0000	1.48.2.9
+++ simplenews.module	14 May 2007 00:15:18 -0000
@@ -226,13 +226,26 @@ function simplenews_form(&$node) {
     '#maxlength' => 128,
     '#required' => TRUE,
   );
+  
+  $fields = _simplenews_get_profile_fields();
+  if (!empty($fields)) {
+    $profile_fields = '<br />'. t('Available user profile fields:');
+    $profile_field_substitution = array();
+    $c = 0;
+    $cc = count($fields);
+    foreach ($fields as $name => $title) {
+      $profile_field_substitution['@'. $name] = $title;
+      $profile_fields .= ' @'. $name .' ('. $title .')'. ($c < $cc - 1 ? ',' : '.');
+      ++$c;
+    }
+  }
   $form['body'] = array(
     '#type' => 'textarea',
     '#title' => t('Message'),
     '#default_value' => $node->body,
     '#rows' => 20,
     '#required' => TRUE,
-    '#description' => t('This will be the body of your newsletter. Available variables are:') . ' %site ' . t('(the name of your website),') . ' %uri ' . t('(a link to your homepage),') . ' %uri_brief ' . t('(homepage link without the http://),') . ' %mymail ' . t('(your e-mail address),') . ' %date ' . t('(today\'s date),') . ' %login_uri ' . t('(link to login page).'),
+    '#description' => t('This will be the body of your newsletter. Available variables are:') . ' %site ' . t('(the name of your website),') . ' %uri ' . t('(a link to your homepage),') . ' %uri_brief ' . t('(homepage link without the http://),') . ' %mymail ' . t('(your e-mail address),') . ' %date ' . t('(today\'s date),') . ' %login_uri ' . t('(link to login page).') . (isset($profile_fields) ? $profile_fields : ''),
   );
 
   $form['format'] = filter_form($node->format);
@@ -461,7 +474,18 @@ function simplenews_taxonomy($op, $type,
  * Implementation of hook_view().
  */
 function simplenews_view(&$node, $teaser = FALSE) {
+  global $user;
+  
   $node = simplenews_replace_vars($node, TRUE);
+  if (!isset($node->simplenews_prepare_mail)) {
+    // Replace user profile variables upon node view with data from current user.
+    if (!$teaser) {
+      $node->body = simplenews_replace_profile_vars($node->body, $user);
+    }
+    else {
+      $node->teaser = simplenews_replace_profile_vars($node->teaser, $user);
+    }
+  }
   $node = node_prepare($node, $teaser);
   return $node;
 }
@@ -1014,6 +1038,9 @@ function simplenews_node_prepare($nid, $
   $node = node_load(array('nid' => $nid), NULL, TRUE);
   $node = simplenews_replace_vars($node, FALSE);
 
+  // Prevent user profile replacements in hook_view().
+  $node->simplenews_prepare_mail = 1;
+  
   // To play well with other modules that add content to the node,
   // simplenews_node_prepare() must mimic node_view() as far as possible.
   // Following is adapted from node_view().
@@ -1169,9 +1196,12 @@ function simplenews_mail_confirm($email,
  * to plug in an extra module
  *
  * @param $mail
- *   An object with at least $mail->to, $mail->subject, and $mail->message.
+ *   An object with at least $mail->to, $mail->subject, and $mail->body.
  */
 function simplenews_mail_send($mail) {
+  // Replace user profile variables.
+  $mail->body = simplenews_replace_profile_vars($mail->body, $mail->to);
+  
   $from_email = isset($mail->from_address) ? $mail->from_address : variable_get('site_mail', ini_get('sendmail_from'));
   $from = isset($mail->from_name) ? '"'. addslashes($mail->from_name).'" <'. $from_email .'>' : $from_email;
 
@@ -2578,6 +2608,55 @@ function simplenews_replace_vars($node, 
   return $node;
 }
 
+/**
+ * Replace user profile fields in newsletter text.
+ * 
+ * @param string $body
+ *   A newsletter text to process.
+ * @param string $user
+ *   Either an user object or an email address for user profile information
+ *   substitution.
+ */
+function simplenews_replace_profile_vars($text, $user) {
+  if (!is_object($user)) {
+    $user = _simplenews_user_load($user);
+  }
+  if (module_exists('profile')) {
+    profile_load_profile($user);
+    $fields = _simplenews_get_profile_fields();
+    foreach ($fields as $name => $title) {
+      if (isset($user->$name)) {
+        $profile_field_substitution['@'. $name] = $user->$name;
+      }
+      else {
+        $profile_field_substitution['@'. $name] = '';
+      }
+    }
+    $text = strtr($text, $profile_field_substitution);
+  }
+  
+  return $text;
+}
+
+/**
+ * Helper function to fetch user profile fields.
+ */
+function _simplenews_get_profile_fields() {
+  $fields = array();
+  if (module_exists('profile')) {
+    $results = db_query("SELECT name, title FROM {profile_fields} ORDER BY category, weight");
+    
+    while ($row = db_fetch_object($results)) { 
+      // Don't include private fields.
+      if ($row->visibility != PROFILE_PRIVATE) {
+        $fields[$row->name] = $row->title;
+      }
+    }
+  }
+  
+  return $fields;
+}
+
 function simplenews_time() {
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
