Index: privatemsg.module =================================================================== RCS file: /cvs/drupal/contributions/modules/privatemsg/privatemsg.module,v retrieving revision 1.70.2.30.2.91.2.25 diff -u -p -r1.70.2.30.2.91.2.25 privatemsg.module --- privatemsg.module 22 Feb 2009 21:21:08 -0000 1.70.2.30.2.91.2.25 +++ privatemsg.module 18 Mar 2009 19:44:52 -0000 @@ -193,18 +193,63 @@ function privatemsg_view_access() { } /** - * This function is called by the menu system through the %privatemsg_thread wildcard + * Load the messages and participants of a thread. + * + * This function is called by the menu system through the + * %privatemsg_thread wildcard. + * + * @param $thread_id + * Thread id of the thread that should be loaded. + * @param $account + * User object for which the thread should be loaded, defaults to + * the current user. + * + * @return + * $thread object, with keys messages, participants, title and user. messages + * contains an array of messages, participants an array of user, subject the + * subject of the thread and user the user viewing the thread. + * + * If no messages are found, or the thread_id is invalid, the function returns + * FALSE. * - * TODO: Really load the full thread in here */ -function privatemsg_thread_load($thread_id) { +function privatemsg_thread_load($thread_id, $account = NULL) { if ((int)$thread_id > 0) { - return $thread_id; + $thread = array(); + + if (is_null($account)) { + global $user; + $account = drupal_clone($user); + } + // load messages returned by the messages query with _privatemsg_load(). + $query = _privatemsg_assemble_query('messages', $thread_id, $account); + $conversation = db_query($query['query']); + while ($result = db_fetch_array($conversation)) { + if ($message = _privatemsg_load($result['mid'], $account)) { + $thread['messages'][$result['mid']] = $message; + } + } + // if there are no messages, don't allow access to the thread. + if (empty($thread['messages'])) { + return FALSE; + } + + // general data, assume subject is the same for all messages of that thread. + $thread['user'] = $account; + $message = current($thread['messages']); + $thread['subject'] = $message['subject']; + + // Load the list of participants. + $query = _privatemsg_assemble_query('participants', $thread_id); + $participants = db_query($query['query']); + while ($result = db_fetch_array($participants)) { + $thread['participants'][$result['uid']] = user_load($result['uid']); + } + return $thread; } return FALSE; } - function private_message_view_options() { $options = module_invoke_all('privatemsg_view_template'); return $options; @@ -502,60 +547,30 @@ function privatemsg_set_new_status($acco } } -function privatemsg_view($thread_id) { - global $user; - $output = ''; +function privatemsg_view($thread) { + drupal_set_title(check_plain($thread['subject'])); - // Load the list of participants. - $query = _privatemsg_assemble_query('participants', $thread_id); - $participants = db_query($query['query']); - while ($result = db_fetch_object($participants)) { - $message['participants'][] = user_load($result->uid); - } - $content['participants']['content'] = theme('privatemsg_to', $message); + // render the participants + $content['participants']['#value'] = theme('privatemsg_to', $thread); $content['participants']['#weight'] = -5; - // Load the messages. - $query = _privatemsg_assemble_query('messages', $thread_id, $user); - $conversation = db_query($query['query']); - $message_count = 0; - while ($result = db_fetch_array($conversation)) { - $pmid = $result['mid']; - $message = _privatemsg_load($pmid, $user); - privatemsg_set_new_status($user, $pmid); - $message['author'] = user_load($message['author']); - // Some tasks only need to be done once - on the first message of a thread. - if ($message_count == 0) { - // Set the page title to the message subject. - drupal_set_title(check_plain($message['subject'])); - } + // render the messages + $output = ''; + foreach ($thread['messages'] as $pmid => $message) { + privatemsg_set_new_status($thread['user'], $pmid); $output .= theme('privatemsg_view', $message); - - $message_count++; } - - if ($message_count == 0) { - drupal_set_message(t('There are no messages available to display.'), 'error'); - } - - $content['messages']['content'] = $output; + $content['messages']['#value'] = $output; $content['messages']['#weight'] = 0; - if ($message_count > 0) { - $content['reply']['content'] = drupal_get_form('privatemsg_new'); + if (privatemsg_user_access('write privatemsg')) { + $content['reply']['#value'] = drupal_get_form('privatemsg_new'); $content['reply']['#weight'] = 5; } //allow other modules to hook into the $content array and alter it - drupal_alter('privatemsg_view_messages', $content, $message_count); - - uasort($content, 'element_sort'); - - $expand = ''; - foreach ($content as $element) { - $expand .= $element['content']; - } - return $expand; + drupal_alter('privatemsg_view_messages', $content, $thread); + return drupal_render($content); } @@ -843,8 +858,15 @@ function privatemsg_sql_load(&$fragments $fragments['inner_join'][] = 'INNER JOIN {pm_index} pmi ON pm.mid = pmi.mid'; $fragments['where'][] = 'pmi.mid = %d'; $fragments['query_args'][] = $pmid; - $fragments['where'][] = 'pmi.uid = %d'; - $fragments['query_args'][] = $account->uid; + if (privatemsg_user_access('read all private messages', $account)) { + // if user has read all permission, try to get the message he is the recipient, if not possible, just load the first. + $fragments['where'][] = 'pmi.uid = %d OR pmi.uid = pmi.uid'; + $fragments['query_args'][] = $account->uid; + } + else { + $fragments['where'][] = 'pmi.uid = %d'; + $fragments['query_args'][] = $account->uid; + } } function privatemsg_sql_messages(&$fragments, $thread_id, $account) { @@ -853,8 +875,15 @@ function privatemsg_sql_messages(&$fragm $fragments['select'][] = 'DISTINCT(pmi.mid) as mid'; $fragments['where'][] = 'pmi.thread_id = %d'; $fragments['query_args'][] = $thread_id; - $fragments['where'][] = 'pmi.uid = %d'; - $fragments['query_args'][] = $account->uid; + if (privatemsg_user_access('read all private messages', $account)) { + // if user has read all permission, try to get the messages he is the recipient, if not possible, just load the first. + $fragments['where'][] = 'pmi.uid = %d OR pmi.uid = pmi.uid'; + $fragments['query_args'][] = $account->uid; + } + else { + $fragments['where'][] = 'pmi.uid = %d'; + $fragments['query_args'][] = $account->uid; + } $fragments['where'][] = 'pmi.deleted = 0'; $fragments['order_by'][] = 'pmi.mid ASC'; } @@ -1055,7 +1084,8 @@ function privatemsg_form_alter(&$form, $ // 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 ($to) { + $recipients = ''; + if (!empty($to)) { $recipients = implode(', ', $to); } @@ -1334,6 +1364,7 @@ function _privatemsg_load($pmid, $accoun $result = db_query($query['query']); $message = db_fetch_array($result); $message['user'] = $account; + $message['author'] = user_load($message['author']); $returned = module_invoke_all('privatemsg_message_load', $message); if (!empty($returned)) { $message = array_merge_recursive($returned, $message); Index: privatemsg_filter/privatemsg_filter.module =================================================================== RCS file: /cvs/drupal/contributions/modules/privatemsg/privatemsg_filter/privatemsg_filter.module,v retrieving revision 1.1.2.8 diff -u -p -r1.1.2.8 privatemsg_filter.module --- privatemsg_filter/privatemsg_filter.module 5 Mar 2009 18:09:02 -0000 1.1.2.8 +++ privatemsg_filter/privatemsg_filter.module 18 Mar 2009 19:44:53 -0000 @@ -452,9 +452,9 @@ function privatemsg_filter_privatemsg_sq } } -function privatemsg_filter_privatemsg_view_messages_alter(&$content, $count) { - if ($count > 0 && db_result(db_query('SELECT COUNT(*) FROM {pm_tags}')) > 0) { - $content['tags']['content'] = drupal_get_form('privatemsg_filter_form'); +function privatemsg_filter_privatemsg_view_messages_alter(&$content, $thread) { + if (count($thread['messages']) > 0 && db_result(db_query('SELECT COUNT(*) FROM {pm_tags}')) > 0) { + $content['tags']['#value'] = drupal_get_form('privatemsg_filter_form'); $content['tags']['#weight'] = 10; } }