? .cvsignore
? privatemsg.improve_validate.patch
? translations
Index: privatemsg.module
===================================================================
RCS file: /cvs/drupal/contributions/modules/privatemsg/privatemsg.module,v
retrieving revision 1.70.2.30.2.91.2.16
diff -u -p -r1.70.2.30.2.91.2.16 privatemsg.module
--- privatemsg.module	18 Feb 2009 01:36:46 -0000	1.70.2.30.2.91.2.16
+++ privatemsg.module	18 Feb 2009 12:16:47 -0000
@@ -613,7 +613,6 @@ function privatemsg_new(&$form_state, $a
 }
 
 function pm_send_validate($form, &$form_state) {
-//  drupal_set_message('<pre>'. print_r($form_state['values'], 1) . '</pre>');
   // The actual message that is being sent, we create this during validation and pass to submit to send out.
   $message = array();
 
@@ -654,37 +653,29 @@ function pm_send_validate($form, &$form_
       $invalid[$name] = $name;
     }
   }
+  if (!empty($invalid)) {
+    drupal_set_message(t('The following recipients are invalid: @invalid', array('@invalid' => implode(", ", $invalid))), 'error');
+  }
 
-  // Verify that our recipients are valid.
-  /**
-   * VALIDATE NAMES
-   * 1) Make sure the name exists.
-   * 2) Make sure he accepts private messages.
-   * 3) Make sure the sender is not on block list of the recipient.
-   */
-  /**
-   * BUILD VALID RECIPIENT LIST
-   * 1) Names that were not valid from previous step will be stripped out.
-   * 2) Names that remain will be put into a recipients array.
-   */
-
-  $errors = _privatemsg_validate_message($message, $message['author'], TRUE);
-  if (!empty($errors)) {
-      foreach ($errors as $error) {
-        form_set_error('body', $error);
-      }
+  $validated = _privatemsg_validate_message($message);
+  foreach($validated['errors'] as $error) {
+    form_set_error('body', $error);
+  }
+  foreach ($validated['warnings'] as $warning) {
+    drupal_set_message($warning, 'warning');
   }
 
   $form_state['validate_built_message'] = $message;
-  if (!empty($invalid)) {
-    drupal_set_message(t('The following users will not receive this private message: @invalid', array('@invalid' => implode(", ", $invalid))), 'error');
-  }
-  $form_state['values']['recipient'] = implode(', ', array_diff($valid, $invalid));
 }
 
 function pm_send($form, &$form_state) {
   if (_privatemsg_send($form_state['validate_built_message'])) {
-    drupal_set_message(t('A message has been sent to @recipients.', array('@recipients' => $form_state['values']['recipient'])));
+    // Load usernames to which the message was sent to
+    $recipient_names = array();
+    foreach ($form_state['validate_built_message']['recipients'] as $recipient) {
+      $recipient_names[] = theme('username', $recipient);
+    }
+    drupal_set_message(t('A message has been sent to @recipients.', array('@recipients' => implode(', ', $recipient_names))));
   }
 }
 
@@ -1126,13 +1117,11 @@ function privatemsg_new_thread($subject,
   $message['timestamp'] = time();
   $message['recipients'] = $recipients;
 
-  $errors = _privatemsg_validate_message($message, $author);
-  if (!empty($errors)) {
-    return $errors;
-  }
-  else {
-    return _privatemsg_send($message);
+  $validated = _privatemsg_validate_message($message);
+  if ($validated['success']) {
+    $validated['success'] = _privatemsg_send($message);
   }
+  return $validated;
 }
 /**
  * Send a reply message
@@ -1174,23 +1163,22 @@ function privatemsg_reply($thread_id, $b
 
   $message['subject'] = $first_message['subject'];
 
-  $errors = _privatemsg_validate_message($message, $author);
-  if (!empty($errors)) {
-    return $errors;
-  }
-  else {
-    return _privatemsg_send($message);
+  $validated = _privatemsg_validate_message($message);
+  if ($validated['success']) {
+    $validated['success'] = _privatemsg_send($message);
   }
+  return $validated;
 }
 
-function _privatemsg_validate_message($message, $author, $show_warnings = FALSE) {
+function _privatemsg_validate_message(&$message) {
 
-  $errors = array();
-  if (!user_access('write privatemsg', $author)) {
+  if (!user_access('write privatemsg', $message['author'])) {
     // no need to do further checks in this case...
-    return array(t('User @user is not allowed to write messages', array('@user' => $author->name)));
+    return array('errors' => array(t('User @user is not allowed to write messages', array('@user' => $author->name))));
   }
 
+  $errors = array();
+  $warnings = array();
   if (empty($message['subject'])) {
     $errors[] = t('Disallowed to send a message without subject');
   }
@@ -1206,14 +1194,10 @@ function _privatemsg_validate_message($m
 
   if (!empty($message['recipients']) && is_array($message['recipients'])) {
     foreach ($message['recipients'] as $uid => $recipient) {
-      $block_results = module_invoke_all('privatemsg_block_message', $author, $recipient);
+      $block_results = module_invoke_all('privatemsg_block_message', $message['author'], $recipient);
       if (count($block_results) > 0) {
         unset($message['recipients'][$uid]);
-        if ($show_warnings) {
-          foreach ($block_results as $block_result) {
-            drupal_set_message($block_result);
-          }
-        }
+        $warnings += $block_results;
       }
     }
   }
@@ -1223,7 +1207,11 @@ function _privatemsg_validate_message($m
     $errors[] = t('Disallowed to send message because all recipients are blocked');
   }
 
-  return $errors += module_invoke_all('privatemsg_message_validate', $message);
+  $errors += module_invoke_all('privatemsg_message_validate', $message);
+  return array('success'  => !(bool) count($errors),
+               'errors'   => $errors,
+               'warnings' => $warnings,
+  );
 }
 
 function _privatemsg_send($message) {
