? fieldable_messages.png ? privatemsg_fieldable.patch Index: privatemsg.api.php =================================================================== RCS file: /cvs/drupal/contributions/modules/privatemsg/privatemsg.api.php,v retrieving revision 1.1.2.5.2.3 diff -u -p -r1.1.2.5.2.3 privatemsg.api.php --- privatemsg.api.php 27 Oct 2009 12:56:16 -0000 1.1.2.5.2.3 +++ privatemsg.api.php 19 Jan 2010 12:13:37 -0000 @@ -307,7 +307,7 @@ function hook_privatemsg_message_validat $errors = array(); - foreach ($message['recipients'] as $recipient) { + foreach ($message->recipients as $recipient) { if ($recipient->name == 'blocked user') { $_privatemsg_invalid_recipients[] = $recipient->uid; $errors[] = t('%name has chosen to not recieve any more messages from you.', array('%name' => $recipient->name)); @@ -328,7 +328,7 @@ function hook_privatemsg_message_presave // delete recipients which have been marked as invalid global $_privatemsg_invalid_recipients; foreach ($_privatemsg_invalid_recipients as $invalid) { - unset($message['recipients'][$invalid]); + unset($message->recipients[$invalid]); } } /** @@ -355,7 +355,7 @@ function hook_privatemsg_message_view_al * Message array */ function hook_privatemsg_message_insert($message) { - _mymodule_save_data($message['mid']); + _mymodule_save_data($message->mid); } /** Index: privatemsg.module =================================================================== RCS file: /cvs/drupal/contributions/modules/privatemsg/privatemsg.module,v retrieving revision 1.70.2.30.2.91.2.64.2.44 diff -u -p -r1.70.2.30.2.91.2.64.2.44 privatemsg.module --- privatemsg.module 7 Jan 2010 21:16:00 -0000 1.70.2.30.2.91.2.64.2.44 +++ privatemsg.module 19 Jan 2010 12:13:41 -0000 @@ -355,9 +355,8 @@ function privatemsg_thread_load($thread_ // Load messages returned by the messages query with privatemsg_message_load_multiple(). $query = _privatemsg_assemble_query('messages', array($thread_id), $thread['read_all'] ? NULL : $account); // Override count query to use DISTINCT. - $countQuery = $query->countquery(); - $expressions = &$countQuery->getExpressions(); - $expressions['expression']['expression'] = 'COUNT(DISTINCT pmi.mid)'; + $countQuery = db_select($query); + $countQuery->addExpression('COUNT(*)'); $thread['message_count'] = $thread['to'] = $countQuery->execute()->fetchField(); $thread['from'] = 1; // Check if we need to limit the messages. @@ -438,7 +437,7 @@ function privatemsg_thread_load($thread_ // General data, assume subject is the same for all messages of that thread. $thread['user'] = $account; $message = current($thread['messages']); - $thread['subject'] = $message['subject']; + $thread['subject'] = $message->subject; } $threads[$account->uid][$thread_id] = $thread; } @@ -535,20 +534,24 @@ function privatemsg_preprocess_privatems // drupal_set_message('
'. print_r($vars,1 ) . ''); $message = $vars['message']; - $vars['mid'] = isset($message['mid']) ? $message['mid'] : NULL; - $vars['thread_id'] = isset($message['thread_id']) ? $message['thread_id'] : NULL; - $vars['author_picture'] = theme('user_picture', array('account' => $message['author'])); - $vars['author_name_link'] = theme('username', array('account' => $message['author'])); + $vars['mid'] = isset($message->mid) ? $message->mid : NULL; + $vars['thread_id'] = isset($message->thread_id) ? $message->thread_id : NULL; + $vars['author_picture'] = theme('user_picture', array('account' => $message->author)); + $vars['author_name_link'] = theme('username', array('account' => $message->author)); /** * @todo perhaps make this timestamp configurable via admin UI? */ - $vars['message_timestamp'] = format_date($message['timestamp'], 'small'); - $vars['message_body'] = check_markup($message['body'], $message['format']); + $vars['message_timestamp'] = format_date($message->timestamp, 'small'); + // Build fields content. + field_attach_prepare_view('privatemsg_message', array($vars['mid'] => $message)); + $content = drupal_render(field_attach_view('privatemsg_message', $message)); + + $vars['message_body'] = check_markup($message->body, $message->format) . $content; if (isset($vars['mid']) && isset($vars['thread_id']) && privatemsg_user_access('delete privatemsg')) { $vars['message_actions'][] = array('title' => t('Delete message'), 'href' => 'messages/delete/' . $vars['thread_id'] . '/' . $vars['mid']); } $vars['message_anchors'][] = 'privatemsg-mid-' . $vars['mid']; - if (!empty($message['is_new'])) { + if (!empty($message->is_new)) { $vars['message_anchors'][] = 'new'; $vars['new'] = drupal_ucfirst(t('new')); } @@ -1144,25 +1147,25 @@ function privatemsg_new_thread($recipien global $user; $author = clone $user; - $message = array(); - $message['subject'] = $subject; - $message['body'] = $body; + $message = (object)$options; + $message->subject = $subject; + $message->body = $body; // Make sure that recipients are keyed by user id and are not added // multiple times. foreach ($recipients as $recipient) { - $message['recipients'][$recipient->uid] = $recipient; + $message->recipients[$recipient->uid] = $recipient; } - // Set custom options, if any. - if (!empty($options)) { - $message += $options; - } // Apply defaults - this will not overwrite existing keys. - $message += array( - 'author' => $author, - 'timestamp' => time(), - 'format' => filter_default_format($author), - ); + if (!isset($message->author)) { + $message->author = $author; + } + if (!isset($message->timestamp)) { + $message->timestamp = time(); + } + if (!isset($message->format)) { + $message->format = filter_default_format($author); + } $validated = _privatemsg_validate_message($message); if ($validated['success']) { @@ -1209,23 +1212,23 @@ function privatemsg_reply($thread_id, $b global $user; $author = clone $user; - $message = array(); - $message['body'] = $body; + $message = (object)$options; + $message->body = $body; - // set custom options, if any - if (!empty($options)) { - $message += $options; - } - // apply defaults - $message += array( - 'author' => $author, - 'timestamp' => time(), - 'format' => filter_default_format($author), - ); + // Apply defaults - this will not overwrite existing keys. + if (!isset($message->author)) { + $message->author = $author; + } + if (!isset($message->timestamp)) { + $message->timestamp = time(); + } + if (!isset($message->format)) { + $message->format = filter_default_format($author); + } // We don't know the subject and the recipients, so we need to load them.. // thread_id == mid on the first message of the thread - $first_message = privatemsg_message_load($thread_id, $message['author']); + $first_message = privatemsg_message_load($thread_id, $message->author); if (!$first_message) { return array( 'success' => FALSE, @@ -1233,16 +1236,14 @@ function privatemsg_reply($thread_id, $b ); } - $message['thread_id'] = $thread_id; + $message->thread_id = $thread_id; - // Load participants. - $message['recipients'] = _privatemsg_load_thread_participants($thread_id); + $message->recipients = _privatemsg_load_thread_participants($thread_id); // Remove author. - if (isset($message['recipients'][$message['author']->uid]) && count($message['recipients']) > 1) { - unset($message['recipients'][$message['author']->uid]); + if (isset($message->recipients[$message->author->uid]) && count($message->recipients) > 1) { + unset($message->recipients[$message->author->uid]); } - $message['subject'] = $first_message['subject']; - + $message->subject = $first_message->subject; $validated = _privatemsg_validate_message($message); if ($validated['success']) { $validated['message'] = _privatemsg_send($message); @@ -1252,17 +1253,17 @@ function privatemsg_reply($thread_id, $b function _privatemsg_validate_message(&$message, $form = FALSE) { $messages = array('error' => array(), 'warning' => array()); - if (!privatemsg_user_access('write privatemsg', $message['author'])) { + if (!privatemsg_user_access('write privatemsg', $message->author)) { // no need to do further checks in this case... if ($form) { - form_set_error('author', t('User @user is not allowed to write messages', array('@user' => $message['author']->name))); + form_set_error('author', t('User @user is not allowed to write messages', array('@user' => $message->author->name))); return array( 'success' => FALSE, 'messages' => $messages, ); } else { - $messages['error'][] = t('User @user is not allowed to write messages', array('@user' => $message['author']->name)); + $messages['error'][] = t('User @user is not allowed to write messages', array('@user' => $message->author->name)); return array( 'success' => FALSE, 'messages' => $messages, @@ -1270,7 +1271,7 @@ function _privatemsg_validate_message(&$ } } - if (empty($message['subject'])) { + if (empty($message->subject)) { if ($form) { form_set_error('subject', t('Disallowed to send a message without subject')); } @@ -1280,7 +1281,7 @@ function _privatemsg_validate_message(&$ } // Don't allow replies without a body. - if (!empty($message['thread_id']) && empty($message['body'])) { + if (!empty($message->thread_id) && empty($message->body)) { if ($form) { form_set_error('body', t('Disallowed to send reply without a message.')); } @@ -1290,16 +1291,16 @@ function _privatemsg_validate_message(&$ } // Check if an allowed format is used. - if (!filter_access(filter_format_load($message['format']), $message['author'])) { + if (!filter_access(filter_format_load($message->format), $message->author)) { if ($form) { form_set_error('format', t('You are not allowed to use the specified input format.')); } else { - $messages['error'][] = t('User @user is not allowed to use the specified input format.', array('@user' => $message['author']->name)); + $messages['error'][] = t('User @user is not allowed to use the specified input format.', array('@user' => $message->author->name)); } } - if (empty($message['recipients']) || !is_array($message['recipients'])) { + if (empty($message->recipients) || !is_array($message->recipients)) { if ($form) { form_set_error('to', t('Disallowed to send a message without atleast one valid recipient')); } @@ -1308,9 +1309,9 @@ function _privatemsg_validate_message(&$ } } - if (!empty($message['recipients']) && is_array($message['recipients'])) { - foreach(module_invoke_all('privatemsg_block_message', $message['author'], $message['recipients']) as $blocked) { - unset($message['recipients'][$blocked['uid']]); + if (!empty($message->recipients) && is_array($message->recipients)) { + foreach(module_invoke_all('privatemsg_block_message', $message->author, $message->recipients) as $blocked) { + unset($message->recipients[$blocked['uid']]); if ($form) { drupal_set_message($blocked['message'], 'warning'); } else { @@ -1320,7 +1321,7 @@ function _privatemsg_validate_message(&$ } // Check again, give another error message if all recipients are blocked - if (empty($message['recipients'])) { + if (empty($message->recipients)) { if ($form) { form_set_error('to', t('Disallowed to send message because all recipients are blocked')); } @@ -1351,18 +1352,19 @@ function _privatemsg_validate_message(&$ */ function _privatemsg_send($message) { - drupal_alter('privatemsg_message_presave', $message); + module_invoke_all('privatemsg_message_presave', $message); + field_attach_presave('privatemsg_message', $message); $query = db_insert('pm_index')->fields(array('mid', 'thread_id', 'uid', 'is_new', 'deleted')); - if (isset($message['read_all']) && $message['read_all']) { + if (isset($message->read_all) && $message->read_all) { // The message was sent in read all mode, add the author as recipient to all // existing messages. - $query_messages = _privatemsg_assemble_query('messages', array($message['thread_id']), NULL); + $query_messages = _privatemsg_assemble_query('messages', array($message->thread_id), NULL); foreach ($query_messages->execute()->fetchCol() as $mid) { $query->values(array( 'mid' => $mid, - 'thread_id' => $message['thread_id'], - 'uid' => $message['author']->uid, + 'thread_id' => $message->thread_id, + 'uid' => $message->author->uid, 'is_new' => 0, 'deleted' => 0, )); @@ -1371,27 +1373,27 @@ function _privatemsg_send($message) { // 1) Save the message body first. $args = array(); - $args['subject'] = $message['subject']; - $args['author'] = $message['author']->uid; - $args['body'] = $message['body']; - $args['format'] = $message['format']; - $args['timestamp'] = $message['timestamp']; + $args['subject'] = $message->subject; + $args['author'] = $message->author->uid; + $args['body'] = $message->body; + $args['format'] = $message->format; + $args['timestamp'] = $message->timestamp; $mid = db_insert('pm_message') ->fields($args) ->execute(); - $message['mid'] = $mid; + $message->mid = $mid; // Thread ID is the same as the mid if it's the first message in the thread. - if (!isset($message['thread_id'])) { - $message['thread_id'] = $mid; + if (!isset($message->thread_id)) { + $message->thread_id = $mid; } // 2) Save message to recipients. // Each recipient gets a record in the pm_index table. - foreach ($message['recipients'] as $recipient) { + foreach ($message->recipients as $recipient) { $query->values(array( 'mid' => $mid, - 'thread_id' => $message['thread_id'], + 'thread_id' => $message->thread_id, 'uid' => $recipient->uid, 'is_new' => 1, 'deleted' => 0, @@ -1400,18 +1402,19 @@ function _privatemsg_send($message) { // When author is also the recipient, we want to set message to UNREAD. // All other times the message is set to READ. - $is_new = isset($message['recipients'][$message['author']->uid]) ? 1 : 0; + $is_new = isset($message->recipients[$message->author->uid]) ? 1 : 0; // Also add a record for the author to the pm_index table. $query->values(array( 'mid' => $mid, - 'thread_id' => $message['thread_id'], - 'uid' => $message['author']->uid, + 'thread_id' => $message->thread_id, + 'uid' => $message->author->uid, 'is_new' => $is_new, 'deleted' => 0, )); $query->execute(); + field_attach_insert('privatemsg_message', $message); module_invoke_all('privatemsg_message_insert', $message); // If we reached here that means we were successful at writing all messages to db. @@ -1506,19 +1509,20 @@ function privatemsg_message_load_multipl $result = _privatemsg_assemble_query('load', $pmids, $account)->execute(); $messages = array(); - while ($message = $result->fetchAssoc()) { - $message['user'] = $account; + while ($message = $result->fetchObject()) { + $message->user = $account; // Load author of message. - if (!($message['author'] = user_load($message['author']))) { + if (!($message->author = user_load($message->author))) { // If user does not exist, load anonymous user. - $message['author'] = user_load(array('uid' => 0)); + $message->author = user_load(array('uid' => 0)); } - $returned = module_invoke_all('privatemsg_message_load', $message); if (!empty($returned)) { $message = array_merge_recursive($returned, $message); } - $messages[$message['mid']] = $message; + $messages[$message->mid] = $message; } + field_attach_load('privatemsg_message', $messages); + module_invoke_all('privatemsg_message_load', $messages); return $messages; } @@ -1529,7 +1533,7 @@ function privatemsg_message_load_multipl * Either be a string ('some_id') or an array('group_name', 'query_id'), * if a string is supplied, group_name defaults to 'privatemsg'. * - * @return + * @return SelectQuery * Array with the keys query and count. count can be used to count the * elements which would be returned by query. count can be used together * with pager_query(). @@ -1821,6 +1825,37 @@ function privatemsg_privatemsg_thread_op return $operations; } +function privatemsg_entity_info() { + $return = array( + 'privatemsg_message' => array( + 'label' => t('Privatemsg'), + 'base table' => 'pm_message', + 'fieldable' => TRUE, + 'object keys' => array( + 'id' => 'mid', + ), + 'bundles' => array( + 'privatemsg_message' => array( + 'label' => t('Privatemsg message'), + 'admin' => array( + 'path' => 'admin/config/people/messages', + 'access arguments' => array('administer privatemsg'), + ), + ), + ), + ), + ); + return $return; +} + +function privatemsg_build_modes($obj_type) { + $modes = array(); + if ($obj_type == 'privatemsg_message') { + $modes['message'] = t('Message'); + } + return $modes; +} + /** * Implements hook_node_view(). */ @@ -1868,4 +1903,4 @@ function privatemsg_views_api() { 'api' => 2, 'path' => drupal_get_path('module', 'privatemsg') . '/views', ); -} \ No newline at end of file +} Index: privatemsg.pages.inc =================================================================== RCS file: /cvs/drupal/contributions/modules/privatemsg/privatemsg.pages.inc,v retrieving revision 1.1.2.2 diff -u -p -r1.1.2.2 privatemsg.pages.inc --- privatemsg.pages.inc 6 Jan 2010 19:29:21 -0000 1.1.2.2 +++ privatemsg.pages.inc 19 Jan 2010 12:13:43 -0000 @@ -66,11 +66,11 @@ function _privatemsg_action_form() { function privatemsg_delete($form, $form_state, $thread, $message) { $form['pmid'] = array( '#type' => 'value', - '#value' => $message['mid'], + '#value' => $message->mid, ); $form['delete_destination'] = array( '#type' => 'value', - '#value' => count($thread['messages']) > 1 ? 'messages/view/' . $message['thread_id'] : 'messages', + '#value' => count($thread['messages']) > 1 ? 'messages/view/' . $message->thread_id : 'messages', ); if (privatemsg_user_access('read all private messages')) { @@ -83,7 +83,7 @@ function privatemsg_delete($form, $form_ } return confirm_form($form, t('Are you sure you want to delete this message?'), - isset($_GET['destination']) ? $_GET['destination'] : 'messages/view/'. $message['thread_id'], + isset($_GET['destination']) ? $_GET['destination'] : 'messages/view/'. $message->thread_id, t('This action cannot be undone.'), t('Delete'), t('Cancel') @@ -285,16 +285,16 @@ function privatemsg_new($form, &$form_st ); $form['message_header']['message_preview'] = $form_state['privatemsg_preview']; } - $form['privatemsg'] = array( + $form = array( '#type' => 'fieldset', '#access' => privatemsg_user_access('write privatemsg'), ); - $form['privatemsg']['author'] = array( + $form['author'] = array( '#type' => 'value', '#value' => $user, ); if (is_null($thread_id)) { - $form['privatemsg']['recipient'] = array( + $form['recipient'] = array( '#type' => 'textfield', '#title' => t('To'), '#description' => t('Separate multiple names with commas.'), @@ -306,7 +306,7 @@ function privatemsg_new($form, &$form_st // Do not hardcode #maxlength, make it configurable by number of recipients, not their name length. ); } - $form['privatemsg']['subject'] = array( + $form['subject'] = array( '#type' => 'textfield', '#title' => t('Subject'), '#size' => 50, @@ -320,7 +320,7 @@ function privatemsg_new($form, &$form_st if (isset($form_state['values']) && array_key_exists('body_format', $form_state['values'])) { $format = $form_state['values']['body_format']; } - $form['privatemsg']['body'] = array( + $form['body'] = array( '#type' => 'textarea', '#title' => t('Message'), '#rows' => 6, @@ -329,13 +329,13 @@ function privatemsg_new($form, &$form_st '#resizable' => TRUE, '#text_format' => isset($format) ? $format : filter_default_format(), ); - $form['privatemsg']['preview'] = array( + $form['preview'] = array( '#type' => 'submit', '#value' => t('Preview message'), '#submit' => array('privatemsg_new_preview'), '#weight' => 10, ); - $form['privatemsg']['submit'] = array( + $form['submit'] = array( '#type' => 'submit', '#value' => t('Send message'), '#weight' => 15, @@ -350,57 +350,63 @@ function privatemsg_new($form, &$form_st $title = t('Clear'); } - $form['privatemsg']['cancel'] = array( + $form['cancel'] = array( '#value' => l($title, $url, array('attributes' => array('id' => 'edit-cancel'))), '#weight' => 20, ); if (!is_null($thread_id)) { - $form['privatemsg']['thread_id'] = array( + $form['thread_id'] = array( '#type' => 'value', '#value' => $thread_id, ); - $form['privatemsg']['subject'] = array( + $form['subject'] = array( '#type' => 'value', '#default_value' => $subject, ); $recipients_string_themed = implode(', ', $to_themed); - $form['privatemsg']['recipient_display'] = array( + $form['recipient_display'] = array( '#value' => '
'. t('Reply to thread:
Recipients: !to', array('!to' => $recipients_string_themed)) .'