Index: invite.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/invite/Attic/invite.install,v
retrieving revision 1.3.2.4
diff -u -r1.3.2.4 invite.install
--- invite.install	10 Mar 2007 20:32:51 -0000	1.3.2.4
+++ invite.install	15 Mar 2007 17:37:58 -0000
@@ -85,14 +85,16 @@
  * Clean up invitations for deleted users.
  */
 function invite_update_3() {
+  $ret = array();
   $result = db_query("SELECT DISTINCT i.uid FROM {invite} i LEFT JOIN {users} u ON i.uid = u.uid WHERE u.uid IS NULL");
-  $num = db_num_rows($result);
+  $count = db_num_rows($result);
   while ($referrer = db_fetch_object($result)) {
     db_query("DELETE FROM {invite} WHERE uid = %d", $referrer->uid);
   }
-  if ($num) {
-    drupal_set_message(t('@num orphaned invites have been deleted.'), array('@num' => $num));
+  if ($count) {
+    $ret[] = array('success' => TRUE, 'query' => t('@count orphaned invite(s) have been deleted.'), array('@count' => $count));
   }
+  return $ret;
 }
 
 /**
@@ -113,3 +115,18 @@
   $ret[] = update_sql("UPDATE {invite} SET received = 1 WHERE mid != 0");
   return $ret;
 }
+
+/**
+ * Switch to token.module.
+ */
+function invite_update_5() {
+  $ret = array();
+  if ($template = variable_get('invite_default_mail_template', NULL)) {
+    $old = array('@site', '@join_link', '@homepage', '@message', '@inviter');
+    $new = array('[site-name]', '[join-link]', '[site-url]', '[inviter-message]', '[inviter-name]');
+    $template = str_replace($old, $new, $template);
+    variable_set('invite_default_mail_template', $template);
+    $ret[] = array('success' => TRUE, 'query' => t('The message tokens for the invite module have been successfully converted.'));
+  }
+  return $ret;
+}
Index: invite.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/invite/Attic/invite.module,v
retrieving revision 1.10.2.33
diff -u -r1.10.2.33 invite.module
--- invite.module	14 Mar 2007 13:14:16 -0000	1.10.2.33
+++ invite.module	15 Mar 2007 19:21:21 -0000
@@ -108,7 +108,7 @@
     '#default_value' => variable_get('invite_subject', t("You've been invited")),
     '#size' => 20,
     '#maxlength' => 64,
-    '#description' => t('Type the subject of the invitation email.'),
+    '#description' => t('Type the subject of the invitation email. Use the syntax [token] if you want to insert a replacement pattern.'),
     '#required' => TRUE,
   );
   
@@ -117,7 +117,16 @@
     '#title' => t('Mail template'),
     '#default_value' => _invite_get_mail_template(),
     '#required' => TRUE,
-    '#description' => t('Use the following placeholders: @site, @homepage, @join_link, @message, @inviter.'),
+    '#description' => t('Use the syntax [token] if you want to insert a replacement pattern.'),
+  );
+  $form['email_settings']['token_help'] = array(
+    '#title' => t('Replacement patterns'),
+    '#type' => 'fieldset',
+    '#collapsible' => TRUE,
+    '#collapsed' => TRUE,
+  );
+  $form['email_settings']['token_help']['help'] = array(
+    '#value' => theme('token_help', array('user', 'profile', 'invite')),
   );
 
   $form['email_settings']['invite_use_users_email'] = array(
@@ -432,7 +441,7 @@
 
 function _invite_form_validate($form_id, &$edit) {
   global $user;
-  
+
   if (!valid_email_address(trim($edit['email']))) {
     form_set_error('email', t('The email address does not appear to be valid syntax.'));
   }
@@ -445,13 +454,17 @@
 
 function _invite_form_submit($form_id, $edit) {
   global $user;
-  
-  // generate code
-  $code = _invite_create_regcode();
+
+  // create the invite object
+  $invite          = new stdClass();
+  $invite->email   = trim($edit['email']);
+  $invite->inviter = $user->name;
+  $invite->message = trim($edit['message']);
+  $invite->code    = _invite_create_regcode();
 
   // send email
-  if ($success = _invite_send_invite('mail', $user->name, trim($edit['email']), $code, $edit['message'])) {
-    db_query("INSERT INTO {invite} (email, reg_code, uid, expiry) VALUES ('%s', '%s', %d, %d)", trim($edit['email']), $code,$user->uid, time()+(variable_get('invite_expiry', 30)*60*60*24));  
+  if (_invite_send_invite($invite)) {
+    db_query("INSERT INTO {invite} (email, reg_code, uid, expiry) VALUES ('%s', '%s', %d, %d)", $invite->email, $invite->code, $user->uid, time() + (variable_get('invite_expiry', 30) * 60 * 60 * 24));
 
     // notify other modules
     $args = array('inviter' => $user);
@@ -459,6 +472,33 @@
   }
 }
 
+/**
+ * Implementation of hook_token_values().
+ */
+function invite_token_values($type = 'all', $object = NULL) {
+  $values = array();
+  if ($type == 'invite' && is_object($object)) {
+    $values['inviter-name']    = $object->inviter;
+    $values['inviter-message'] = check_plain($object->message);
+    $values['inviter-mail']    = $object->email;
+    $values['join-link']       = url('user/register/'. $object->code, NULL, NULL, TRUE);
+  }
+  return $values;
+}
+
+/**
+ * Implementation of hook_token_list().
+ */
+function invite_token_list($type = 'all') {
+  if ($type == 'invite' || $type == 'all') {
+    $tokens['invite']['inviter-name']    = t('The name of the inviting user. <em>Note:</em> This is the same as [user]. Any <em>user</em>, <em>profile</em> and <em>global [user-*]</em> tokens can be used to refer to the inviting user.');
+    $tokens['invite']['inviter-message'] = t('The message text of the inviting user for the invitee.');
+    $tokens['invite']['invitee-mail']    = t('The email address of the user to be invited.');
+    $tokens['invite']['join-link']       = t('The registration url of the current Drupal website.');
+    return $tokens;
+  }
+}
+
 function invite_delete($email) {
   global $user;
 
@@ -580,8 +620,8 @@
   return $reg_code;
 }
 
-function _invite_send_invite($op, $user_name = "[username]", $email ="[recipient-email]", $code = "registration-code", $message = '[your message]'){
-  global $base_url, $user;
+function _invite_send_invite($invite){
+  global $user;
 
   // this next bit sets From and Reply-To headers
   if(!$from = variable_get('invite_manual_from', NULL)) {
@@ -600,39 +640,37 @@
       $replyto = variable_get('site_mail', ini_get('sendmail_from'));
     }
   }
-  $subject = variable_get('invite_subject',t("You've been invited"));
+  $headers = array('Reply-To' => $replyto);
 
-  $template = _invite_get_mail_template();
-  $site = variable_get('site_name', t('Drupal'));
-  $join_link = url('user/register/'. $code, NULL, NULL, TRUE);
-  $homepage = $base_url;
-  $inviter = $user_name;
-
-  $body = t($template, array('@site' => $site, '@join_link' => $join_link, '@homepage' => $homepage, '@message' => $message, '@inviter' => $inviter));
-
-  switch ($op){
-    case "mail":
-      if ($success = drupal_mail('invite-mail', $email, $subject, wordwrap($body, 72), $from)) {
-        drupal_set_message(t('Your invitation was sent successfully.'));
-      }
-      else {
-        drupal_set_message(t('Problems occurred sending the invitation. Please contact the site administrator.'), 'error');
-        watchdog('invite', t("Failed sending invitation. To: @email From: @from", array('@email' => $email, '@from' => $from)));
-      }
-      return $success;
+  $subject = variable_get('invite_subject', t("You've been invited"));
+  $body = _invite_get_mail_template();
+
+  // perform token replacement (NULL is current user)
+  $types = array('user' => NULL, 'profile' => NULL, 'invite' => $invite);
+  $subject = token_replace_multiple($subject, $types);
+  $body = token_replace_multiple($body, $types);
+
+  if ($success = drupal_mail('invite-mail', $invite->email, $subject, wordwrap($body, 72), $from, $headers)) {
+    drupal_set_message(t('Your invitation was sent successfully.'));
   }
+  else {
+    drupal_set_message(t('Problems occurred sending the invitation. Please contact the site administrator.'));
+    watchdog('invite', t('Failed sending invitation: To: @email From: @from.', array('@email' => '<'. $invite->email .'>', '@from' => '<'. $from .'>')));
+  }
+
+  return $success;
 }
 
 function _invite_get_mail_template() {
-  $template = t("Your friend, @inviter has invited you to join @site [@homepage].
+  $template = t("Your friend, [inviter-name] has invited you to join [site-name] at [site-url].
 
-To become a member of @site, click the link below or paste it into the address bar of your browser. 
+To become a member of [site-name], click the link below or paste it into the address bar of your browser. 
 
-@join_link
+[join-link]
 
 ----------
 
-@message");
+[inviter-message]");
 
   return variable_get('invite_default_mail_template', $template);
 }
