? .cvsignore
? privatemsg_paging.patch
? privatemsg_paging2.patch
Index: privatemsg.module
===================================================================
RCS file: /cvs/drupal/contributions/modules/privatemsg/privatemsg.module,v
retrieving revision 1.70.2.30.2.91.2.76
diff -u -p -r1.70.2.30.2.91.2.76 privatemsg.module
--- privatemsg.module	14 Oct 2009 15:24:44 -0000	1.70.2.30.2.91.2.76
+++ privatemsg.module	16 Oct 2009 15:01:29 -0000
@@ -251,6 +251,9 @@ function privatemsg_view_access() {
  * @param $account
  *   User object for which the thread should be loaded, defaults to
  *   the current user.
+ * @param $limit
+ *   How many messages should be loaded. Unread messages will always be
+ *   displayed and counting starts from the end.
  *
  * @return
  *   $thread object, with keys messages, participants, title and user. messages
@@ -262,7 +265,7 @@ function privatemsg_view_access() {
 
  * @ingroup api
  */
-function privatemsg_thread_load($thread_id, $account = NULL) {
+function privatemsg_thread_load($thread_id, $account = NULL, $limit = NULL) {
   static $threads = array();
   if ((int)$thread_id > 0) {
     $thread = array('thread_id' => $thread_id);
@@ -289,19 +292,37 @@ function privatemsg_thread_load($thread_
         $thread['read_all'] = TRUE;
       }
 
+      // If limit is NULL, select based on get params
+      if (is_null($limit)) {
+        if (isset($_GET['show']) && (int)$_GET['show'] > 0) {
+          $limit = (int)$_GET['show'];
+        }
+        else {
+          $limit = variable_get('privatemsg_thread_messages', 10);
+        }
+      }
+      $thread['limit'] = $limit;
+
       // load messages returned by the messages query with _privatemsg_load().
       $query = _privatemsg_assemble_query('messages', array($thread_id), $thread['read_all'] ? NULL : $account);
-      $conversation = db_query($query['query']);
+      $thread['message_count'] = db_result(db_query($query['count']));
+      // Check if we need to limit the messages.
+      $startwith = 0;
+      if ($thread['message_count'] > $limit) {
+        $startwith = $thread['message_count'] - $limit;
+      }
+      $conversation = db_query_range($query['query'], $startwith, variable_get('privatemsg_thread_messages_hardlimit', 100));
       while ($result = db_fetch_array($conversation)) {
         if ($message = _privatemsg_load($result['mid'], $thread['read_all'] ? NULL : $account)) {
           $thread['messages'][$result['mid']] = $message;
         }
       }
-      // if there are no messages, don't allow access to the thread.
+      // If there are no messages, don't allow access to the thread.
       if (empty($thread['messages'])) {
         $thread = FALSE;
-      } else {
-        // general data, assume subject is the same for all messages of that thread.
+      }
+      else {
+        // General data, assume subject is the same for all messages of that thread.
         $thread['user'] = $account;
         $message = current($thread['messages']);
         $thread['subject'] = $message['subject'];
@@ -416,6 +437,20 @@ function private_message_settings() {
     '#default_value' => variable_get('privatemsg_display_fields', array('participants')),
   );
 
+  $form['privatemsg_listing']['privatemsg_thread_messages'] = array(
+    '#type'          => 'textfield',
+    '#title'         => t('Display limit for messages inside threads'),
+    '#default_value' => variable_get('privatemsg_thread_messages', 10),
+    '#description'   => t('Define how many messages should be displayed by default. It does display the last messages and earlier messages can be displayed incrementally by the same amount.'),
+  );
+
+  $form['privatemsg_listing']['privatemsg_thread_messages_hardlimit'] = array(
+    '#type'          => 'textfield',
+    '#title'         => t('Hard display limit for messages inside threads'),
+    '#default_value' => variable_get('privatemsg_thread_messages_hardlimit', 100),
+    '#description'   => t('Threads will never display more messages than defined here at the same time. It is still possible to view all messages of a thread, but not at the same time.'),
+  );
+
   $form['#submit'][] = 'private_message_settings_submit';
   return system_settings_form($form);
 }
@@ -651,6 +686,14 @@ function privatemsg_unread_count($accoun
 function privatemsg_view($thread) {
   drupal_set_title(check_plain($thread['subject']));
 
+  // Display a link to load all messages.
+  if ($thread['message_count'] > count($thread['messages'])) {
+    $title = t('Displaying @display from @total messages. Show earlier messages.', array('@display' => $thread['limit'], '@total' => $thread['message_count']));
+    $options = array('query' => array('show' => $thread['limit'] + variable_get('privatemsg_thread_messages', 10)));
+    $content['display_all']['#value'] = l($title, 'messages/view/' . $thread['thread_id'], $options);
+    $content['display_all']['#weight'] = -10;
+  }
+
   // Render the participants.
   $content['participants']['#value'] = theme('privatemsg_recipients', $thread);
   $content['participants']['#weight'] = -5;
@@ -1093,7 +1136,7 @@ function privatemsg_sql_load(&$fragments
 function privatemsg_sql_messages(&$fragments, $threads, $account = NULL, $load_all = FALSE) {
   $fragments['primary_table'] = '{pm_index} pmi';
 
-  $fragments['select'][]      = 'DISTINCT(pmi.mid) as mid';
+  $fragments['select'][]      = 'pmi.mid';
   $fragments['where'][]       = 'pmi.thread_id IN ('. db_placeholders($threads) .')';
   $fragments['query_args']['where']   += $threads;
   if ($account) {
@@ -1102,8 +1145,9 @@ function privatemsg_sql_messages(&$fragm
     $fragments['query_args']['where'][]  = $account->uid;
   }
   if (!$load_all) {
-    $fragments['where'][]       = 'pmi.deleted = 0';
+    $fragments['where'][]     = 'pmi.deleted = 0';
   }
+  $fragments['group_by'][]    = 'pmi.mid';
   $fragments['order_by'][]    = 'pmi.mid ASC';
 }
 
