From c18bdad712a64f7b715b628bdcae48d4b376885e Mon Sep 17 00:00:00 2001
From: scottrigby <scott@basekamp.com>
Date: Tue, 7 Jun 2011 01:14:57 -0400
Subject: [PATCH] Issue #290305: port stBorchert's patch to 6.x-1.x

---
 includes/broadcast.inc |   29 ++++---
 signup.info            |    2 +
 signup.module          |  247 +++++++++++++++++++++++++++++-------------------
 3 files changed, 170 insertions(+), 108 deletions(-)

diff --git a/includes/broadcast.inc b/includes/broadcast.inc
index b97ad14..0c23a0b 100644
--- a/includes/broadcast.inc
+++ b/includes/broadcast.inc
@@ -144,21 +144,26 @@ function signup_send_broadcast($nid, $subject, $body, $from, $copy = FALSE, $sig
   if (!empty($signups)) {
     $node = node_load($nid);
     foreach ($signups as $signup) {
-      $user_mail = _signup_get_email($signup);
-      $params = array(
-        'subject' => $subject,
-        'body' => $body,
-        'node' => $node,
-        'signup' => $signup,
+      $account = _signup_get_user($signup);
+      $tokens = array('node' => $node, 'signup' => $signup, 'user' => $signup, 'global' => NULL);
+      $message = array(
+        'type' => 'signup',
+        'subject' => token_replace_multiple($subject, $tokens),
+        'body' => token_replace_multiple($body, $tokens),
+        'language' => user_preferred_language($account),
       );
-      if (module_exists('token')) {
-        $params['body'] = token_replace_multiple($params['body'], array('node' => $node, 'signup' => $signup, 'global' => NULL));
-      }
-      $language = user_preferred_language($signup);
-      drupal_mail('signup', 'signup_broadcast_mail', $user_mail, $language, $params, $from);
-      watchdog('signup', 'Broadcast email for %title sent to %email.', array('%title' => $node->title, '%email' => $user_mail), WATCHDOG_NOTICE, l(t('view'), 'node/'. $node->nid));
+      messaging_message_send_user($account, $message);
+      watchdog('signup', 'Broadcast email for %title sent to %email.', array('%title' => $node->title, '%email' => $account->mail), WATCHDOG_NOTICE, l(t('view'), 'node/'. $node->nid));
     }
     if ($copy) {
+      $tokens = array('node' => $node, 'signup' => $user, 'user' => $user, 'global' => NULL);
+      $message = array(
+        'type' => 'signup',
+        'subject' => token_replace_multiple(messaging_message_part('signup-message', 'subject'), $tokens),
+        'body' => token_replace_multiple($node->signup_confirmation_email, $tokens),
+        'language' => user_preferred_language($account),
+      );
+      messaging_message_send_user($account, $message);
       $sender_email = _signup_get_email($user);
       $signup_tokens = _signup_get_email_tokens($node, $user);
       $message = strtr($body, $signup_tokens);
diff --git a/signup.info b/signup.info
index 3dfab7d..8c4b1e3 100644
--- a/signup.info
+++ b/signup.info
@@ -3,4 +3,6 @@ description = "Allow users to sign up for content (especially events)."
 package = Signup
 dependencies[] = views
 core = 6.x
+dependencies[] = messaging
+dependencies[] = token
 
diff --git a/signup.module b/signup.module
index 00dfce9..afde86a 100644
--- a/signup.module
+++ b/signup.module
@@ -967,6 +967,80 @@ function signup_object_info() {
 }
 
 /**
+ * Implementation of hook_messaging().
+ *
+ * @param $op
+ *   Operation, type of information to retrieve
+ * @param $arg1, $arg2...
+ *   Different parameters depending on $op
+ */
+function signup_messaging($op, $arg1 = NULL, $arg2 = NULL, $arg3 = NULL, $arg4 = NULL) {
+  switch ($op) {
+    case 'message types':
+      $info['signup'] = array(
+        'name' => t('Signup messages'),
+        'description' => t('Messages coming from event signups')
+      );
+      return $info;
+    case 'message groups':
+      // Generic signup message
+      $info['signup-message'] = array(
+        'module' => 'signup',
+        'name' => t('Signup message'),
+        'description' => t('Common parts for all signup messages. This is useful for defining a common header and/or footer for all these messages.'),
+      );
+      // Signup forwarding message
+      $info['signup-forward'] = array(
+        'module' => 'signup',
+        'name' => t('Signup forward message'),
+        'description' => t('Common parts for signup forwarding messages. This is useful for defining a common header and/or footer for all these messages.'),
+      );
+      return $info;
+    case 'message keys':
+      $type = $arg1;
+      switch ($type) {
+        case 'signup-message':
+        case 'signup-forward':
+          return array(
+            'subject' => t('Subject'),
+            'header' => t('Header'),
+            'main' => t('Content'),
+            'footer' => t('Footer'),
+          );
+          break;
+      }
+      break;
+    case 'messages':
+      $type = $arg1;
+      if ($type == 'signup-message') {
+        return array(
+          'subject' => t('Signup confirmation for [node-type]: [node-title]'),
+          'header' => t("Greetings [user],"),
+          'main' => t("..."),
+          'footer' => t('This is an automatic message from [site-name]'),
+        );
+      }
+      if ($type == 'signup-forward') {
+        return array(
+          'subject' => t('Signup confirmation for [node-type]: [node-title]'),
+          'header' => t('The following information was submitted as a signup for [node-title]'),
+          'main' => t("Date/Time: [node-signup-time]\n\rUsername: [signup-user-name] ([signup-email])\n\r[signup-user-data-raw]"),
+          'footer' =>t('This is an automatic message from [site-name]'),
+        );
+      }
+      break;
+    case 'tokens':
+      $type = explode('-', $arg1);
+      $tokens = array();
+      // These are the token groups that will be used for this module's messages
+      if ($type[0] == 'signup') {
+        $tokens = array('signup', 'node', 'user');
+      }
+      return $tokens;
+  }
+}
+
+/**
  * Load a $signup object from the given Signup ID (sid).
  *
  * In addition to pulling all the fields from the {signup_log} table, this
@@ -1296,11 +1370,11 @@ function signup_sign_up_user($signup_form, $notify_user = TRUE, $reset_node_load
     if ($notify_user) {
       // Confirmation e-mail to the user who signed up.
       if ($node->signup_send_confirmation) {
-        signup_send_confirmation_mail($signup, $node);
+        signup_send_confirmation($signup, $node);
       }
 
-      // Notification email to an administrator or coordinator.
-      signup_send_forwarding_mail($signup, $node);
+      // Notification message to an administrator or coordinator.
+      signup_send_forwarding_msg($signup, $node);
 
       // Message to the screen for the user who signed up.
       // First, allow other modules to alter the messages.
@@ -1340,30 +1414,30 @@ function signup_sign_up_user($signup_form, $notify_user = TRUE, $reset_node_load
  *   The fully-loaded node object which the user signed up to.
  *
  * @return
- *   The return value from drupal_mail() if the mail is sent, or FALSE if the
- *   confirmation was aborted for some reason (node not configured to send it,
- *   user doesn't have a valid e-mail address, etc).
+ *   The return value from messaging_message_send_user() if the confirmation is
+ *   sent, or FALSE if the confirmation was aborted for some reason (node not
+ *   configured to send it, user doesn't have a valid e-mail address, etc).
  *
  * @see signup_sign_up_user()
- * @see drupal_mail()
+ * @see messaging_message_send_user()
  */
-function signup_send_confirmation_mail($signup, $node) {
-  $user_mail = _signup_get_email($signup);
-  if (empty($user_mail)) {
+function signup_send_confirmation($signup, $node) {
+  $account = _signup_get_user($signup);
+  if (empty($account->mail)) {
     return FALSE;
   }
-  $node_type_name = node_get_types('name', $node->type);
-  $params = array(
-    'subject' => t('Signup confirmation for !node_type: !title', array('!node_type' => $node_type_name, '!title' => $node->title)),
-    'body' => $node->signup_confirmation_email,
-    'node' => $node,
-    'signup' => $signup,
+  $tokens = array('node' => $node, 'signup' => $signup, 'user' => $signup, 'global' => NULL);
+  $message = array(
+    'type' => 'signup',
+    'subject' => token_replace_multiple(messaging_message_part('signup-message', 'subject'), $tokens),
+    'body' => array(
+      'header' => token_replace_multiple(messaging_message_part('signup-message', 'header'), $tokens),
+      'content' => token_replace_multiple($node->signup_confirmation_email, $tokens),
+      'footer' => token_replace_multiple(messaging_message_part('signup-message', 'footer'), $tokens),
+    ),
+    'language' => user_preferred_language($account),
   );
-  if (module_exists('token')) {
-    $params['body'] = token_replace_multiple($params['body'], array('node' => $node, 'signup' => $signup, 'global' => NULL));
-  }
-  $language = user_preferred_language($signup);
-  return  drupal_mail('signup', 'signup_confirmation_mail', $user_mail, $language, $params);
+  return messaging_message_send_user($account, $message);
 }
 
 /**
@@ -1375,52 +1449,39 @@ function signup_send_confirmation_mail($signup, $node) {
  *   The fully-loaded node object which the user signed up to.
  *
  * @return
- *   The return value from drupal_mail() if the mail is sent, or FALSE if the
- *   messsage is aborted for some reason (node not configured to send it,
- *   user doesn't have a valid e-mail address, etc).
+ *   The return value from messaging_message_send_user() if the message is sent,
+ *   or FALSE if the messsage is aborted for some reason (node not configured to
+ *   send it, user doesn't have a valid e-mail address, etc).
  *
  * @see signup_sign_up_user()
- * @see drupal_mail()
+ * @see messaging_message_send_user()
  */
-function signup_send_forwarding_mail($signup, $node) {
+function signup_send_forwarding_msg($signup, $node) {
   if (!$node->signup_forwarding_email) {
     return FALSE;
   }
-  $user_mail = _signup_get_email($signup);
-  if (empty($user_mail)) {
+  $account = _signup_get_user($signup);
+  if (empty($account->mail)) {
     return FALSE;
   }
-  $message = t('The following information was submitted as a signup for !title', array('!title' => $node->title));
-  if (_signup_get_node_scheduler($node) != 'none') {
-    $message .= "\n\r". t('Date/Time: !time', array('!time' => signup_format_date($node)));
-  }
-
-  $message .= "\n\r\n\r". t('Username: !name', array('!name' => empty($signup->uid) ? variable_get('anonymous', t('Anonymous')) : $signup->name));
-  if (!empty($signup->uid)) {
-    // For authenticated users, just include a link to their profile page.
-    $message .= "\n\r". t('Profile page: !url', array('!url' => url('user/'. $signup->uid, array('absolute' => TRUE))));
-  }
-  else {
-    // For anonymous users, their email is all we've got, so disclose it.
-    $message .= "\n\r". t('E-mail: !email', array('!email' => $user_mail));
-  }
-  if (!empty($signup->form_data)) {
-    $message .= "\n\r\n\r". theme('signup_email_token_custom_data', $signup->form_data);
-  }
-  $node_type_name = node_get_types('name', $node->type);
-  $from = variable_get('site_mail', ini_get('sendmail_from'));
-  $params = array(
-    'subject' => t('Signup confirmation for !node_type: !title', array('!node_type' => $node_type_name, '!title' => $node->title)),
-    'body' => $message,
-    'node' => $node,
-    'signup' => $signup,
-    'header' => array('From' => t('New !node_type Signup', array('!node_type' => $node_type_name)) ."<$from>"),
+  $tokens = array('node' => $node, 'signup' => $signup, 'user' => $account, 'global' => NULL);
+  $message = array(
+    'type' => 'signup',
+    'subject' => token_replace_multiple(messaging_message_part('signup-forward', 'subject'), $tokens),
+    'body' => array(
+      'header' => token_replace_multiple(messaging_message_part('signup-forward', 'header'), $tokens),
+      'content' => token_replace_multiple(messaging_message_part('signup-forward', 'main'), $tokens),
+      'footer' => token_replace_multiple(messaging_message_part('signup-forward', 'footer'), $tokens),
+    ),
+    'language' => user_preferred_language($account),
   );
-  if (module_exists('token')) {
-    $params['body'] = token_replace_multiple($params['body'], array('node' => $node, 'signup' => $signup, 'global' => NULL));
-  }
-  $language = user_preferred_language($signup);
-  return drupal_mail('signup', 'signup_forwarding_mail', $node->signup_forwarding_email, $language, $params, $from);
+  // Build a dummy user object with forwarding mail.
+  // @todo this needs to be a regular user so messaging can send messages not
+  //   only by mail!
+  $user = new stdClass();
+  $user->mail = $node->signup_forwarding_email;
+
+  return messaging_message_send_user($user, $message);
 }
 
 /**
@@ -1519,41 +1580,6 @@ function signup_views_api() {
 }
 
 /**
- * Implementation of hook_mail().
- *
- * Constructs all of the email messages generated by the signup module.
- *
- * @param $key
- *   Unique key to indicate what message to build.
- * @param $message
- *   Reference to the message array being built.
- * @param $params
- *   Array of parameters to indicate what text to include in the message body.
- *   If $params['ignore_tokens'] is TRUE, none of the signup-provided tokens
- *   in the message body will be replaced, otherwise, tokens are replaced
- *   using the node passed in as $params['node'] and the signup data from
- *   $params['signup'].
- *
- * @see drupal_mail()
- * @see _signup_cron_send_reminders()
- * @see signup_sign_up_user()
- * @see signup_broadcast_form_submit()
- */
-function signup_mail($key, &$message, $params) {
-  if (empty($params['ignore_tokens'])) {
-    $tokens = _signup_get_email_tokens($params['node'], $params['signup']);
-    $body = strtr($params['body'], $tokens);
-    $subject = strtr($params['subject'], $tokens);
-  }
-  else {
-    $body = $params['body'];
-    $subject = $params['subject'];
-  }
-  $message['subject'] .= str_replace(array("\r", "\n"), '', $subject);
-  $message['body'][] = $body;
-}
-
-/**
  * Helper function that returns an array of tokens for signup emails.
  *
  * These are the hard-coded tokens provided by signup.module which are
@@ -1617,6 +1643,26 @@ function _signup_get_email($signup) {
 }
 
 /**
+ * Get the user object to use for a given signup.
+ *
+ * @param $signup
+ *   An object containing signup data, in particular 'mail' and 'anon_mail'.
+ *
+ * @return
+ *   The loaded user object to use to contact the signed up user.
+ */
+function _signup_get_user($signup) {
+  $user = new stdClass();
+  $user->uid = 0;
+  $user->language = FALSE;
+  $user->mail = $signup->anon_mail;
+  if (empty($signup->anon_mail)) {
+    $user = user_load(array('mail' => $signup->mail));
+  }
+  return $user;
+}
+
+/**
  * Get the name to use for a given signup.
  *
  * @param $signup
@@ -1725,14 +1771,18 @@ function signup_token_list($type) {
   $tokens = array();
   if ($type == 'node') {
     $tokens['node'] = array(
-      'node-signup-enabled' => t('True/False for if signups are enabled on a node'),
+      'node-signup-enabled' => t('True/False for if signups are enabled on a node.'),
       'node-signup-status' => t("Are signups 'open' or 'closed' for this node."),
-      'node-signup-limit' => t('The signup limit for this node'),
+      'node-signup-limit' => t('The signup limit for this node.'),
+      'node-signup-time' => t('The date and time of the thing the user signed up for.'),
+      'node-type' => t('The type of the node the user signed up.'),
+      'node-title' => t('The title of the node the user signed up.'),
     );
   }
   elseif ($type == 'signup') {
     $tokens['signup'] = array(
-      'signup-sid' => t("The signup's unique ID number"),
+      'signup-sid' => t("The signup's unique ID number."),
+      'signup-user-name' => t('The name of the user who signed up.'),
       'signup-user-data' => t('Any custom fields the user filled out when signing up, rendered as HTML.'),
       'signup-user-data-raw' => t('Any custom fields the user filled out when signing up, rendered as unfiltered plain text (e.g. for ASCII e-mail).'),
       'signup-date-short' => t('The date/time when the user signed up, short date format.'),
@@ -1754,15 +1804,19 @@ function signup_token_list($type) {
 function signup_token_values($type = 'all', $object = NULL) {
   $values = array();
   if ($type == 'node') {
+    $values['node-type'] = node_get_types('name', $object->type);
+    $values['node-title'] = $object->title;
     if (empty($object->signup)) {
       $values['node-signup-enabled'] = t('disabled');
       $values['node-signup-status'] = '';
       $values['node-signup-limit'] = '';
+      $values['node-signup-time'] = '';
     }
     else {
       $values['node-signup-enabled'] = t('enabled');
       $values['node-signup-status'] = $object->signup_status ? t('open') : t('closed');
       $values['node-signup-limit'] = (int)$object->signup_close_signup_limit;
+      $values['node-signup-time'] = signup_format_date($object);
     }
   }
   elseif ($type == 'signup') {
@@ -1776,6 +1830,7 @@ function signup_token_values($type = 'all', $object = NULL) {
       }
     }
     $values['signup-sid'] = $object->sid;
+    $values['signup-user-name'] = empty($object->uid) ? variable_get('anonymous', t('Anonymous')) : $object->name;
     $values['signup-user-data'] = theme('signup_custom_data', $signup_data);
     $values['signup-user-data-raw'] = theme('signup_email_token_custom_data', $signup_data);
     $values['signup-date-short'] = format_date($object->signup_time, 'small');
-- 
1.7.2.3

