Index: privatemsg.author-pane.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/privatemsg/Attic/privatemsg.author-pane.inc,v
retrieving revision 1.1.2.2
diff -u -r1.1.2.2 privatemsg.author-pane.inc
--- privatemsg.author-pane.inc	18 Feb 2009 01:36:46 -0000	1.1.2.2
+++ privatemsg.author-pane.inc	17 Apr 2009 19:05:42 -0000
@@ -12,7 +12,7 @@
 function privatemsg_preprocess_author_pane(&$variables) {
   $image_path = $variables['image_path'];
   // Send private message
-  if ($url = privatemsg_get_link($variables['account'])) {
+  if ($url = privatemsg_get_link(array($variables['account']))) {
     $img = theme('image', "$image_path/private-message.png", t('Send private message'), t('Send private message'), NULL, TRUE);
     $variables['privatemsg'] = l($img, $url, array('absolute' => TRUE, 'html' => TRUE));
     $variables['privatemsg_link'] = l(t('Send PM'), $url);
Index: privatemsg.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/privatemsg/privatemsg.module,v
retrieving revision 1.70.2.30.2.91.2.35
diff -u -r1.70.2.30.2.91.2.35 privatemsg.module
--- privatemsg.module	17 Apr 2009 18:12:54 -0000	1.70.2.30.2.91.2.35
+++ privatemsg.module	17 Apr 2009 19:15:23 -0000
@@ -37,18 +37,23 @@
  * @return
  *   Array with user objects.
  */
-function _privatemsg_generate_user_array($userstring) {
+function _privatemsg_generate_user_array($userstring, $slice = NULL) {
   static $user_cache = array();
 
-  // convert user uid list (uid1,uid2,uid3) into an array and only load the last 4 uids.
-  $users = array_slice(explode(',', $userstring), -4);
+  // Convert user uid list (uid1,uid2,uid3) into an array. If $slice is not NULL
+  // pass that as argument to array_slice(). For example, -4 will only load the
+  // last four users.
+  $users = explode(',', $userstring);
+  if (!is_null($slice)) {
+    $users = array_slice($users, $slice);
+  }
   $participants = array();
-  foreach ($users as $user) {
-    if (isset($user_cache[$user])) {
-      $participants[$user] = $user_cache[$user];
+  foreach ($users as $uid) {
+    if (!array_key_exists($uid, $user_cache)) {
+      $user_cache[$uid] = user_load($uid);
     }
-    else {
-      $participants[$user] = $user_cache[$user] = user_load($user);
+    if (is_object($user_cache[$uid])) {
+      $participants[$uid] = $user_cache[$uid];
     }
   }
   return $participants;
@@ -141,7 +146,7 @@
   $items['messages/new'] = array(
     'title'            => 'Write new message',
     'page callback'    => 'drupal_get_form',
-    'page arguments'   => array('privatemsg_new', 2),
+    'page arguments'   => array('privatemsg_new', 2, 3, NULL),
     'access callback'  => 'privatemsg_user_access',
     'access arguments' => array('write privatemsg'),
     'type'             => MENU_LOCAL_TASK,
@@ -513,7 +518,7 @@
   $query = _privatemsg_assemble_query('participants', $thread_id);
   $participants = db_query($query['query']);
   while ($result = db_fetch_object($participants)) {
-    $message['participants'][] = user_load($result->uid);
+    $recipients[] = $message['participants'][] = user_load($result->uid);
   }
   $content['participants']['content'] = theme('privatemsg_to', $message);
   $content['participants']['#weight'] = -5;
@@ -545,7 +550,7 @@
   $content['messages']['#weight'] = 0;
 
   if ($message_count > 0) {
-    $content['reply']['content'] = drupal_get_form('privatemsg_new');
+    $content['reply']['content'] = drupal_get_form('privatemsg_new', $recipients, $message['subject'], $thread_id);
     $content['reply']['#weight'] = 5;
   }
 
@@ -562,18 +567,53 @@
 }
 
 
-function privatemsg_new(&$form_state, $account = NULL) {
+function privatemsg_new(&$form_state, $recipients = array(), $subject = '', $thread_id = NULL) {
   global $user;
 
-  $recipient = '';
-  $subject   = '';
+  $recipients_string = '';
   $body      = '';
 
-  if ((int)$account > 0 && $account = user_load($account)) {
-    $recipient  = $account->name .', ';
+  // convert recipients to array of user objects
+  if (!empty($recipients) && is_string($recipients) || is_int($recipients)) {
+    $recipients = _privatemsg_generate_user_array($recipients);
+  }
+  elseif (is_object($recipients)) {
+    $recipients = array($recipients);
+  }
+  elseif (empty($recipients) && is_string($recipients)) {
+    $recipients = array();
+  }
+
+  $usercount = 0;
+  $to = array();
+  foreach ($recipients as $recipient) {
+    if (in_array($recipient->name, $to)) {
+      // We already added the recipient to the list, skip him.
+      continue;
+    }
+    // Check if another module is blocking the sending of messages to the recipient by current user.
+    $user_blocked = module_invoke_all('privatemsg_block_message', $user, array($recipient));
+    if (!count($user_blocked) <> 0 && $recipient->uid) {
+        if ($recipient->uid == $user->uid) {
+          $usercount++;
+          // Skip putting author in the recipients list for now.
+          continue;
+        }
+      $to[] = $recipient->name;
+    }
+  }
+
+  if (empty($to) && $usercount >= 1) {
+    // Assume the user sent message to own account as if the usercount is one or less, then the user sent a message but not to self.
+    $to[] = $user->name;
   }
+
+  if (!empty($to)) {
+    $recipients_string = implode(', ', $to);
+  }
+
   if (isset($form_state['values'])) {
-    $recipient = $form_state['values']['recipient'];
+    $recipients_string = $form_state['values']['recipient'];
     $subject   = $form_state['values']['subject'];
     $body      = $form_state['values']['body'];
   }
@@ -600,7 +640,7 @@
     '#type'               => 'textfield',
     '#title'              => t('To'),
     '#description'        => t('Separate multiple names with commas.'),
-    '#default_value'      => $recipient, // populate this later
+    '#default_value'      => $recipients_string,
     '#required'           => TRUE,
     '#weight'             => -10,
     '#size'               => 50,
@@ -639,12 +679,38 @@
   if (isset($_REQUEST['destination'])) {
     $url = $_REQUEST['destination'];
   }
+  elseif (!is_null($thread_id)) {
+    $url = $_GET['q'];
+  }
 
   $form['privatemsg']['cancel'] = array(
     '#value'              => l(t('Cancel'), $url, array('attributes' => array('id' => 'edit-cancel'))),
     '#weight'             => 20,
   );
   $form['#validate'][]    = 'pm_send_validate';
+
+  if (!is_null($thread_id)) {
+    $form['privatemsg']['thread_id'] = array(
+      '#type' => 'value',
+      '#value' => $thread_id,
+    );
+    $form['privatemsg']['subject'] = array(
+          '#type' => 'value',
+          '#default_value' => $subject,
+    );
+    $form['privatemsg']['recipient_display'] = array(
+      '#value' =>  '<p>'. t('Reply to: %to', array('%to' => $recipients_string)) .'</p>',
+      '#weight' => -10,
+    );
+    $form['privatemsg']['recipient']  = array(
+      '#type' => 'value',
+      '#default_value' => $recipients_string,
+    );
+    if (empty($recipients_string)) {
+      // If there are no valid recipients, unset the message reply form.
+      $form['privatemsg']['#access'] = FALSE;
+    }
+  }
   return $form;
 }
 
@@ -913,7 +979,7 @@
 
   switch ($op) {
     case 'view':
-      if ($url = privatemsg_get_link($account)) {
+      if ($url = privatemsg_get_link(array($account))) {
         $account->content['privatemsg_send_new_message'] = array(
           '#type'   => 'markup',
           '#value'  => l(t('Send this user a message'), $url, array('query' => drupal_get_destination())),
@@ -1006,61 +1072,6 @@
   return $block;
 }
 
-function privatemsg_form_alter(&$form, $form_state, $form_id) {
-  switch ($form_id) {
-    case 'privatemsg_new':
-      if (arg(1) == 'view') {
-        global $user;
-        $subject = db_result(db_query('SELECT subject FROM {pm_message} WHERE mid = %d', arg(2)));
-        $thread = db_result(db_query('SELECT thread_id FROM {pm_index} WHERE mid = %d', arg(2)));
-        $query = _privatemsg_assemble_query('participants', $thread);
-        $participants = db_query($query['query']);
-        $usercount = 0;
-        while ($result = db_fetch_object($participants)) {
-          if ($result->uid == $user->uid) {
-            $usercount++;
-            // Skip putting author in the recipients list for now.
-            continue;
-          }
-          $to[$result->uid] = user_load($result->uid)->name;
-        }
-        if (empty($to) && $usercount >= 1) {
-          // Assume the user sent message to own account as if the usercount is one or less, then the user sent a message but not to self.
-          $to[$user->uid] = $user->name;
-        }
-        // Check if another module is blocking the sending of messages to the recipient by current user.
-        foreach(module_invoke_all('privatemsg_block_message', $user, $to) as $blocked) {
-          unset($to[$blocked['uid']]);
-        }
-
-        $form['privatemsg']['thread_id'] = array(
-          '#type' => 'value',
-          '#value' => $thread,
-        );
-        $form['privatemsg']['subject'] = array(
-          '#type' => 'value',
-          '#default_value' => $subject,
-        );
-        $form['privatemsg']['recipient']  = array(
-          '#type' => 'value',
-          '#default_value' => implode(', ', $to),
-        );
-        $form['privatemsg']['body']['#title'] = t('Send reply to %recipient', array('%recipient' => implode(', ', $to)));
-        $form['privatemsg']['body']['#required'] = TRUE;
-
-        if (empty($to)) {
-          // If there are no valid recipients, unset the message reply form.
-          $form['privatemsg']['#access'] = FALSE;
-        }
-        else {
-          // Hide the cancel button on the reply form - this is only needed on the new message form, not for a reply.
-          $form['privatemsg']['cancel']['#access'] = FALSE;
-        }
-      }
-    break;
-  }
-}
-
 function privatemsg_delete($form_state, $pmid) {
   global $user;
 
@@ -1301,24 +1312,38 @@
  *
  * @ingroup api
  */
-function privatemsg_get_link($recipient, $account = NULL) {
+function privatemsg_get_link($recipients, $account = array(), $subject = NULL) {
   if ($account == NULL) {
     global $user;
     $account = $user;
   }
+  
+  if (!is_array($recipients)) {
+    $recipients = array($recipients);
+  }
 
-  if (!privatemsg_user_access('write privatemsg', $account)
-      || !privatemsg_user_access('read privatemsg', $recipient)
-      || $recipient->uid == 0   /* << These two are redundant because privatemsg_user_access checks for ->uid == 0 */
-      || $account->uid == 0) {
+  if (!privatemsg_user_access('write privatemsg', $account) || $account->uid == 0) {
     return FALSE;
   }
 
-  if (count(module_invoke_all('privatemsg_block_message', $account, array($recipient))) > 0) {
+  $validated = array();
+  foreach ($recipients as $recipient) {
+    if (!privatemsg_user_access('read privatemsg', $recipient)) {
+      continue;
+    }
+    if (count(module_invoke_all('privatemsg_block_message', $account, array($recipient))) > 0) {
+      continue;
+    }
+    $validated[] = $recipient->uid;
+  }
+  if (empty($validated)) {
     return FALSE;
   }
-
-  return 'messages/new/'. $recipient->uid;
+  $url = 'messages/new/'. implode(',', $validated);
+  if (!is_null($subject)) {
+    $url .= '/'. $subject;
+  }
+  return $url;
 }
 
 /**
Index: privatemsg.theme.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/privatemsg/Attic/privatemsg.theme.inc,v
retrieving revision 1.1.2.3
diff -u -r1.1.2.3 privatemsg.theme.inc
--- privatemsg.theme.inc	16 Apr 2009 20:13:22 -0000	1.1.2.3
+++ privatemsg.theme.inc	17 Apr 2009 19:05:42 -0000
@@ -13,7 +13,7 @@
   if (empty($thread['participants'])) {
     return;
   }
-  $participants = _privatemsg_generate_user_array($thread['participants']);
+  $participants = _privatemsg_generate_user_array($thread['participants'], -4);
   $field = array();
   $field['data'] = _privatemsg_format_participants($participants, 3, TRUE);
   $field['class'] = 'privatemsg-list-participants';

