Skip to content

Commit

Permalink
Issue #50: Merge pull request #58 by @herbdool
Browse files Browse the repository at this point in the history
  • Loading branch information
herbdool authored Dec 4, 2023
2 parents ff5f8f8 + 896c853 commit 284b209
Show file tree
Hide file tree
Showing 22 changed files with 440 additions and 320 deletions.
2 changes: 1 addition & 1 deletion privatemsg.admin.inc
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ function privatemsg_admin_settings() {

$form['privatemsg_listing']['display_fields'] = array(
'#type' => 'checkboxes',
'#title' => t('Configure fields'),
'#title' => t('Choose fields to display'),
'#description' => t('Select which columns/fields should be displayed in the message listings. Subject and Last updated cannot be disabled.'),
'#options' => array(
'participants' => t('Participants'),
Expand Down
2 changes: 1 addition & 1 deletion privatemsg.controller.inc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Loads private messages.
*/

class PrivatemsgMessageController extends DefaultEntityController {
class PrivatemsgMessageController extends EntityDatabaseStorageController {

/**
* @var User|null
Expand Down
85 changes: 85 additions & 0 deletions privatemsg.entity.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
/**
* @file
* Class for private messages.
*/

/**
* Defines the private messages class.
*/
class PrivatemsgMessage extends Entity {

/**
* The message ID.
*
* @var integer
*/
public $mid;

/**
* The message type (bundle).
*
* @var string
*/
public $type;


/**
* The uid of the user who is associated with the private message.
*
* @var integer
*/
public $author;

/**
* The message subject.
*
* @var string
*/
public $subject;

/**
* Constructor for private message entities.
*/
public function __construct(array $values = array()) {
parent::__construct($values);
}

/**
* Implements EntityInterface::id().
*/
public function id() {
return isset($this->mid) ? $this->mid : NULL;
}

/**
* Implements EntityInterface::entityType().
*/
public function entityType() {
return 'privatemsg_message';
}

/**
* Implements EntityInterface::bundle().
*/
public function bundle() {
return $this->entityType();
}

/**
* Implements EntityInterface::label().
*/
public function label() {
return $this->subject;
}

/**
* Implements EntityInterface::uri().
*/
public function uri() {
return array(
'path' => 'messages/view/' . $this->mid,
'options' => array(),
);
}
}
16 changes: 16 additions & 0 deletions privatemsg.install
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,19 @@ function privatemsg_update_1000() {
update_variable_del('privatemsg_limits_receive_object');
update_variable_del('privatemsg_limits_receive_amount');
}

/**
* Create a role to role_id mapping for privatemsg_roles so roles can be
* used for pm_index.
*/
function privatemsg_update_1001() {
$config = config('privatemsg_roles.settings');
$roles = user_roles(TRUE);

$role_id = 1;
foreach ($roles as $role_name => $role) {
$config->set('role_id.' . $role_name, $role_id);
$role_id++;
}
$config->save();
}
16 changes: 9 additions & 7 deletions privatemsg.module
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ function privatemsg_config_info() {
*/
function privatemsg_autoload_info() {
return array(
'PrivatemsgMessage' => 'privatemsg.entity.inc',
'PrivatemsgMessageController' => 'privatemsg.controller.inc',
'views_handler_field_privatemsg_link' => 'views/views_handler_field_privatemsg_link.inc',
);
Expand Down Expand Up @@ -1690,7 +1691,7 @@ function privatemsg_new_thread($recipients, $subject, $body = NULL, $options = a
global $user;
$author = clone $user;

$message = (object) $options;
$message = entity_create('privatemsg_message', $options);
$message->subject = $subject;
$message->body = $body;
// Make sure that recipients are keyed correctly and are not added
Expand Down Expand Up @@ -1762,7 +1763,7 @@ function privatemsg_reply($thread_id, $body, $options = array()) {
global $user;
$author = clone $user;

$message = (object) $options;
$message = entity_create('privatemsg_message', $options);
$message->body = $body;

// Apply defaults - this will not overwrite existing keys.
Expand Down Expand Up @@ -1968,7 +1969,7 @@ function _privatemsg_send($message) {
}

// We only want to add the author to the pm_index table, if the message has
// not been sent directly to him.
// not been sent directly to them.
if (!isset($message->recipients['user_' . $message->author->uid])) {
$query->values(array(
'mid' => $mid,
Expand Down Expand Up @@ -2277,15 +2278,15 @@ function privatemsg_operation_execute($operation, $threads, $account = NULL) {
}
// Add in callback arguments if present.
if (isset($operation['callback arguments'])) {
$args = array_merge(array($threads), $operation['callback arguments']);
$args = array_merge(array('threads' => $threads), $operation['callback arguments']);
}
else {
$args = array($threads);
}

// Add the user object to the arguments.
if ($account) {
$args[] = $account;
$args['account'] = $account;
}

// Execute the chosen action and pass the defined arguments.
Expand All @@ -2299,10 +2300,10 @@ function privatemsg_operation_execute($operation, $threads, $account = NULL) {
if (isset($operation['undo callback']) && $undo_function = $operation['undo callback']) {
// Add in callback arguments if present.
if (isset($operation['undo callback arguments'])) {
$undo_args = array_merge(array($threads), $operation['undo callback arguments']);
$undo_args = array_merge(array('threads' => $threads), $operation['undo callback arguments']);
}
else {
$undo_args = array($threads);
$undo_args = array('threads' => $threads);
}

// Avoid saving the complete user object in the session.
Expand Down Expand Up @@ -2404,6 +2405,7 @@ function privatemsg_entity_info() {
'base table' => 'pm_message',
'fieldable' => TRUE,
'controller class' => 'PrivatemsgMessageController',
'entity class' => 'PrivatemsgMessage',
'uri callback' => 'privatemsg_message_uri_callback',
'entity keys' => array(
'id' => 'mid',
Expand Down
31 changes: 21 additions & 10 deletions privatemsg.pages.inc
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ function privatemsg_new($form, &$form_state, $recipients = array(), $subject = '
'#value' => $read_all === TRUE,
);
// Attach field widgets.
$message = (object) array();
$message = entity_create('privatemsg_message', array());
if (isset($form_state['validate_built_message'])) {
$message = $form_state['validate_built_message'];
}
Expand All @@ -535,13 +535,23 @@ function privatemsg_check_format_access($element) {
}

function privatemsg_new_validate($form, &$form_state) {
global $user;
// The actual message that is being sent, we create this during validation and
// pass to submit to send out.
$message = (object)$form_state['values'];
$message->mid = 0;
$message->format = $message->body['format'];
$message->body = $message->body['value'];
$message->timestamp = REQUEST_TIME;
$values = $form_state['values'];
$message_info = array(
'mid' => 0,
'author' => $user,
'recipient' => isset($values['recipient']) ? $values['recipient'] : NULL,
'subject' => $values['subject'],
'body' => $values['body']['value'],
'format' => $values['body']['format'],
'thread_id' => isset($values['thread_id']) ? $values['thread_id'] : NULL,
'read_all' => $values['read_all'],
'timestamp' => REQUEST_TIME,
);
$message = entity_create('privatemsg_message', $message_info);

// Avoid subjects which only consist of a space as these can not be clicked.
$message->subject = trim($message->subject);

Expand All @@ -560,7 +570,7 @@ function privatemsg_new_validate($form, &$form_state) {

if (!empty($invalid)) {
// Display information about invalid recipients.
backdrop_set_message(t('The following users will not receive this private message: @invalid.', array('@invalid' => implode(", ", $invalid))), 'error');
backdrop_set_message(t('The following recipients will not receive this private message: @invalid.', array('@invalid' => implode(", ", $invalid))), 'error');
}
if (!empty($denieds)) {
// Display information about denied recipients.
Expand Down Expand Up @@ -609,9 +619,10 @@ function privatemsg_new_preview($form, &$form_state) {
$form_state['privatemsg_preview'] = array(
'#markup' => theme('privatemsg_view', array('message' => $message)),
'#attached' => array(
backdrop_get_path('module', 'privatemsg') . '/css/privatemsg-view.base.css',
backdrop_get_path('module', 'privatemsg') . '/css/privatemsg-view.theme.css',
),
'css' => array(
backdrop_get_path('module', 'privatemsg') . '/css/privatemsg-view.base.css',
backdrop_get_path('module', 'privatemsg') . '/css/privatemsg-view.theme.css',
)),
);
// This forces the form to be rebuilt instead of being submitted.
$form_state['rebuild'] = TRUE;
Expand Down
54 changes: 39 additions & 15 deletions privatemsg_filter/privatemsg_filter.module
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ function privatemsg_filter_form_privatemsg_admin_settings_alter(&$form, $form_st
);

// Add tags to the list of possible columns.
$form['privatemsg_listing']['privatemsg_display_fields']['#options']['tags'] = t('Tags');
$form['privatemsg_listing']['display_fields']['#options']['tags'] = t('Tags');

$form['#submit'][] = 'privatemsg_filter_settings_submit';
}
Expand Down Expand Up @@ -325,7 +325,21 @@ function privatemsg_filter_get_filter($account) {
}

if (!empty($_SESSION['privatemsg_filter'])) {
return $_SESSION['privatemsg_filter'];
$saved_filter = $_SESSION['privatemsg_filter'];
// Hydrate the authors' user accounts.
// Avoid __PHP_Incomplete_Class due to User class not loaded before
// unserialize() gets called.
foreach ($saved_filter['author'] as $key => $author) {
$author = (array) $author;
if (isset($author['uid'])) {
$user = user_load($author['uid']);
$user->recipient = $user->uid;
$user->type = 'user';
$saved_filter['author'][$key] = $user;
}
}

return $saved_filter;
}

}
Expand Down Expand Up @@ -607,10 +621,10 @@ function privatemsg_filter_privatemsg_thread_operations($type) {
$archive = array(
'label' => t('Archive'),
'callback' => 'privatemsg_filter_remove_tags',
'callback arguments' => array('tag_id' => $config->get('inbox_tag')),
'callback arguments' => array('tag_ids' => $config->get('inbox_tag')),
'success message' => t('The messages have been archived.'),
'undo callback' => 'privatemsg_filter_add_tags',
'undo callback arguments' => array('tag_id' => $config->get('inbox_tag')),
'undo callback arguments' => array('tag_ids' => $config->get('inbox_tag')),
);
return array('archive' => $archive);
}
Expand Down Expand Up @@ -675,10 +689,10 @@ function privatemsg_filter_add_tag_submit($form, &$form_state) {

$operation = array(
'callback' => 'privatemsg_filter_add_tags',
'callback arguments' => array('tag_id' => $tag_ids),
'callback arguments' => array('tag_ids' => $tag_ids),
'success message' => t('The selected conversations have been tagged.'),
'undo callback' => 'privatemsg_filter_remove_tags',
'undo callback arguments' => array('tag_id' => $tag_ids),
'undo callback arguments' => array('tag_ids' => $tag_ids),
);
privatemsg_operation_execute($operation, $form_state['values']['list']);
$form_state['rebuild'] = TRUE;
Expand All @@ -691,10 +705,10 @@ function privatemsg_filter_add_tag_submit($form, &$form_state) {
function privatemsg_filter_remove_tag_submit($form, &$form_state) {
$operation = array(
'callback' => 'privatemsg_filter_remove_tags',
'callback arguments' => array('tag_id' => $form_state['values']['tag-remove']),
'callback arguments' => array('tag_ids' => $form_state['values']['tag-remove']),
'success message' => t('The tag has been removed from the selected conversations.'),
'undo callback' => 'privatemsg_filter_add_tags',
'undo callback arguments' => array('tag_id' => $form_state['values']['tag-remove']),
'undo callback arguments' => array('tag_ids' => $form_state['values']['tag-remove']),
);
privatemsg_operation_execute($operation, $form_state['values']['list']);
$form_state['rebuild'] = TRUE;
Expand Down Expand Up @@ -736,12 +750,22 @@ function privatemsg_filter_query_privatemsg_list_alter($query) {

if (isset($filter['author']) && !empty($filter['author'])) {
foreach ($filter['author'] as $author) {
$alias = $query->join('pm_index', 'pmi', '%alias.mid = pm.mid');
$query->condition($alias . '.recipient', $author->uid);
$query->condition($alias . '.type', 'user');
$alias = $count_query->join('pm_index', 'pmi', '%alias.mid = pm.mid');
$count_query->condition($alias . '.recipient', $author->uid);
$count_query->condition($alias . '.type', 'user');
if ($author->type == 'user') {
$alias = $query->join('pm_index', 'pmi', '%alias.mid = pm.mid');
$query->condition($alias . '.recipient', $author->uid);
$query->condition($alias . '.type', 'user');
$alias = $count_query->join('pm_index', 'pmi', '%alias.mid = pm.mid');
$count_query->condition($alias . '.recipient', $author->uid);
$count_query->condition($alias . '.type', 'user');
}
if (module_exists('privatemsg_roles') && $author->type == 'role') {
$alias = $query->join('pm_index', 'pmi', '%alias.mid = pm.mid');
$query->condition($alias . '.recipient', $author->recipient);
$query->condition($alias . '.type', 'role');
$alias = $count_query->join('pm_index', 'pmi', '%alias.mid = pm.mid');
$count_query->condition($alias . '.recipient', $author->recipient);
$count_query->condition($alias . '.type', 'role');
}
}
}

Expand Down Expand Up @@ -1007,7 +1031,7 @@ function privatemsg_filter_user_cancel($edit, $account, $method) {
function privatemsg_filter_privatemsg_message_insert($message) {
foreach ($message->recipients as $recipient) {
if ($recipient->type == 'user' || $recipient->type == 'hidden') {
privatemsg_filter_add_tags(array($message->thread_id), config_get('privatemsg_filter.settings', 'inbox_tag'));
privatemsg_filter_add_tags(array($message->thread_id), config_get('privatemsg_filter.settings', 'inbox_tag'), $recipient);
}
}
}
Expand Down
Loading

0 comments on commit 284b209

Please sign in to comment.