summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaroly Negyesi2007-07-19 14:37:30 (GMT)
committer Karoly Negyesi2007-07-19 14:37:30 (GMT)
commit50ad9db08e66c9410a4c2f7292fa600f2aaaeb57 (patch)
tree1071729739727b54b15b4e60f01f1a70864cf494
parent06a1ecf5a2033ea3fa83ebff841cf4b1a53d1e45 (diff)
#140881 add a privatemsg api
-rw-r--r--privatemsg.module297
1 files changed, 178 insertions, 119 deletions
diff --git a/privatemsg.module b/privatemsg.module
index 5d2e7d8..ed6066d 100644
--- a/privatemsg.module
+++ b/privatemsg.module
@@ -162,30 +162,30 @@ function privatemsg_user($type, &$edit, &$user, $category = NULL) {
if (user_access('access private messages') && $category == 'account') {
$form = array();
$form['privatemsg_settings'] = array(
- '#type' => 'fieldset',
- '#title' => t('Private message settings'),
- '#weight' => 4,
- '#collapsible' => TRUE
- );
+ '#type' => 'fieldset',
+ '#title' => t('Private message settings'),
+ '#weight' => 4,
+ '#collapsible' => TRUE,
+ );
$form['privatemsg_settings']['privatemsg_allow'] = array(
- '#type' => 'checkbox',
- '#title' => t('Allow private messages'),
- '#default_value' => isset($edit['privatemsg_allow']) ? $edit['privatemsg_allow'] : 1,
- '#description' => t('Check this box to allow users to send you private messages.')
- );
+ '#type' => 'checkbox',
+ '#title' => t('Allow private messages'),
+ '#default_value' => isset($edit['privatemsg_allow']) ? $edit['privatemsg_allow'] : 1,
+ '#description' => t('Check this box to allow users to send you private messages.'),
+ );
$form['privatemsg_settings']['privatemsg_setmessage_notify'] = array(
- '#type' => 'checkbox',
- '#title' => t('Aggressive notification of new messages'),
- '#default_value' => isset($edit['privatemsg_setmessage_notify']) ? $edit['privatemsg_setmessage_notify'] : 1,
- '#description' => t('Show status message on every page until new messages are read.')
- );
+ '#type' => 'checkbox',
+ '#title' => t('Aggressive notification of new messages'),
+ '#default_value' => isset($edit['privatemsg_setmessage_notify']) ? $edit['privatemsg_setmessage_notify'] : 1,
+ '#description' => t('Show status message on every page until new messages are read.'),
+ );
$form['privatemsg_settings']['privatemsg_mailalert'] = array(
- '#type' => 'radios',
- '#title' => t('Receive e-mail for unread messages'),
- '#options' => array(PRIVATEMSG_MAIL_NONE => t('Never'), PRIVATEMSG_MAIL_DIGEST => t('Daily'), PRIVATEMSG_MAIL_INDIVIDUAL => t('Every message')),
- '#default_value' => isset($edit['privatemsg_mailalert']) ? $edit['privatemsg_mailalert'] : PRIVATEMSG_MAIL_NONE,
- '#description' => t('Select when to receive e-mail notification for unread messages.')
- );
+ '#type' => 'radios',
+ '#title' => t('Receive e-mail for unread messages'),
+ '#options' => array(PRIVATEMSG_MAIL_NONE => t('Never'), PRIVATEMSG_MAIL_DIGEST => t('Daily'), PRIVATEMSG_MAIL_INDIVIDUAL => t('Every message')),
+ '#default_value' => isset($edit['privatemsg_mailalert']) ? $edit['privatemsg_mailalert'] : variable_get('privatemsg_mailalert_default', PRIVATEMSG_MAIL_NONE),
+ '#description' => t('Select when to receive e-mail notification for unread messages.'),
+ );
return $form;
}
@@ -275,70 +275,81 @@ function _privatemsg_block_inbox() {
function privatemsg_configure() {
$form = array();
+ $form['email'] = array(
+ '#type' => 'fieldset',
+ '#title' => t('Email defaults'),
+ );
+ $form['email']['privatemsg_mailalert_default'] = array(
+ '#type' => 'radios',
+ '#title' => t('Receive e-mail for unread messages'),
+ '#options' => array(PRIVATEMSG_MAIL_NONE => t('Never'), PRIVATEMSG_MAIL_DIGEST => t('Daily'), PRIVATEMSG_MAIL_INDIVIDUAL => t('Every message')),
+ '#default_value' => variable_get('privatemsg_mailalert_default', PRIVATEMSG_MAIL_NONE),
+ '#description' => t('Select the default for when users will receive e-mail notification for unread messages.'),
+ );
$form['privatemsg_max_rate'] = array(
- '#type' => 'select',
- '#title' => t('Private messaging max rate'),
- '#default_value' => variable_get('privatemsg_max_rate', 15),
- '#options' => drupal_map_assoc(array(5, 10, 15, 20, 30, 60), 'format_interval'),
- '#description' => t('Max submit rate for private messaging. To prevent abuse.')
- );
+ '#type' => 'select',
+ '#title' => t('Private messaging max rate'),
+ '#default_value' => variable_get('privatemsg_max_rate', 15),
+ '#options' => drupal_map_assoc(array(5, 10, 15, 20, 30, 60), 'format_interval'),
+ '#description' => t('Max submit rate for private messaging. To prevent abuse.'),
+ );
$form['privatemsg_sent_status'] = array(
- '#type' => 'select',
- '#title' => t('Sent message status'),
- '#default_value' => variable_get('privatemsg_sent_status', 1),
- '#options' => array(t('Disabled'), t('Enabled')),
- '#description' => t('If enabled users can see whether a message has been read or not.')
- );
+ '#type' => 'select',
+ '#title' => t('Sent message status'),
+ '#default_value' => variable_get('privatemsg_sent_status', 1),
+ '#options' => array(t('Disabled'), t('Enabled')),
+ '#description' => t('If enabled users can see whether a message has been read or not.'),
+ );
$form['privatemsg_per_page'] = array(
- '#type' => 'select',
- '#title' => t('Messages per page'),
- '#default_value' => variable_get('privatemsg_per_page', 10),
- '#options' => drupal_map_assoc(array(5, 10, 15, 20, 25, 30, 35, 40, 50, 60, 80, 100)),
- '#description' => t('The maximum number of messages displayed per page; links to browse messages automatically appear.')
- );
+ '#type' => 'select',
+ '#title' => t('Messages per page'),
+ '#default_value' => variable_get('privatemsg_per_page', 10),
+ '#options' => drupal_map_assoc(array(5, 10, 15, 20, 25, 30, 35, 40, 50, 60, 80, 100)),
+ '#description' => t('The maximum number of messages displayed per page; links to browse messages automatically appear.'),
+ );
$form['privatemsg_menu_link'] = array(
- '#type' => 'textfield',
- '#title' => t('Menu link'),
- '#default_value' => variable_get('privatemsg_menu_link', t('My inbox')),
- '#description' => t("Text shown in link to user's inbox."),
- );
+ '#type' => 'textfield',
+ '#title' => t('Menu link'),
+ '#default_value' => variable_get('privatemsg_menu_link', t('My inbox')),
+ '#description' => t("Text shown in link to user's inbox."),
+ );
$form['links'] = array(
- '#type' => 'fieldset',
- '#title' => t('"Write to author" links'),
- '#description' => t('Edit <a href="!settings-url">content types</a> to select where these links are displayed.', array('!settings-url' => url('admin/content/types'))),
- );
+ '#type' => 'fieldset',
+ '#title' => t('"Write to author" links'),
+ '#description' => t('Edit <a href="!settings-url">content types</a> to select where these links are displayed.', array('!settings-url' => url('admin/content/types'))),
+ );
$form['welcome'] = array(
- '#type' => 'fieldset',
- '#title' => t('Welcome message for new users')
- );
+ '#type' => 'fieldset',
+ '#title' => t('Welcome message for new users'),
+ );
$form['welcome']['subject'] = array(
- '#type' => 'textfield',
- '#title' => t('Subject'),
- '#default_value' => variable_get('privatemsg_welcome_subject', t('Welcome')),
- '#size' => 50,
- '#maxlength' => 64
- );
+ '#type' => 'textfield',
+ '#title' => t('Subject'),
+ '#default_value' => variable_get('privatemsg_welcome_subject', t('Welcome')),
+ '#size' => 50,
+ '#maxlength' => 64,
+ );
$form['welcome']['privatemsg_welcome_message'] = array(
- '#type' => 'textarea',
- '#title' => t('Message'),
- '#default_value' => variable_get('privatemsg_welcome_message', ''),
- '#cols' => 80,
- '#rows' => 5,
- '#description' => t('Leave blank to disable this feature.')
- );
+ '#type' => 'textarea',
+ '#title' => t('Message'),
+ '#default_value' => variable_get('privatemsg_welcome_message', ''),
+ '#cols' => 80,
+ '#rows' => 5,
+ '#description' => t('Leave blank to disable this feature.'),
+ );
$form['welcome'][] = filter_form(variable_get('privatemsg_welcome_format', FILTER_FORMAT_DEFAULT));
$sender = user_load(array('uid' => variable_get('privatemsg_welcome_sender', 1)));
$form['welcome']['privatemsg_welcome_sender'] = array(
- '#type' => 'textfield',
- '#title' => t('Sender'),
- '#default_value' => $sender->name,
- '#autocomplete_path' => 'privatemsg/autocomplete',
- '#size' => 50,
- '#maxlength' => 64,
- '#required' => TRUE
- );
+ '#type' => 'textfield',
+ '#title' => t('Sender'),
+ '#default_value' => $sender->name,
+ '#autocomplete_path' => 'privatemsg/autocomplete',
+ '#size' => 50,
+ '#maxlength' => 64,
+ '#required' => TRUE,
+ );
return system_settings_form($form);
}
@@ -380,6 +391,9 @@ function _privatemsg_prune() {
while ($message = db_fetch_object($result)) {
db_query("INSERT INTO {privatemsg_archive} (id, author, recipient, subject, message, timestamp, hostname, format, folder) VALUES (%d, %d, %d, '%s', '%s', %d, '%s', %d, %d)", $message->id, $message->author, $message->recipient, $message->subject, $message->message, $message->timestamp, $message->hostname, $message->format, $message->folder);
db_query('DELETE FROM {privatemsg} WHERE id = %d', $message->id);
+
+ // Notify other modules that a message is pruned.
+ privatemsg_invoke_privatemsg($message, 'prune');
}
// this is MySQL-specific
@@ -590,15 +604,15 @@ function privatemsg_new() {
$form = array('preview' => array());
$form['recipient'] = array(
- '#prefix' => '<div class="container-inline">',
- '#type' => 'textfield',
- '#title' => t('To'),
- '#default_value' => $message->recipient,
- '#autocomplete_path' => 'privatemsg/autocomplete',
- '#size' => 50,
- '#maxlength' => 64,
- '#required' => TRUE
- );
+ '#prefix' => '<div class="container-inline">',
+ '#type' => 'textfield',
+ '#title' => t('To'),
+ '#default_value' => $message->recipient,
+ '#autocomplete_path' => 'privatemsg/autocomplete',
+ '#size' => 50,
+ '#maxlength' => 64,
+ '#required' => TRUE,
+ );
$result = db_query('SELECT DISTINCT(name) AS name FROM {privatemsg} p, {users} u WHERE p.author = u.uid AND recipient = %d AND p.timestamp > (UNIX_TIMESTAMP(NOW()) - (3600 * 24 * 30)) ORDER BY name', $user->uid);
$contacts = array();
while ($name = db_fetch_object($result)) {
@@ -615,39 +629,40 @@ function privatemsg_new() {
}
array_unshift($contacts, t('Contacts'));
$form['quick'] = array(
- '#type' => 'select',
- '#options' => $contacts,
- '#attributes' => array('style' => 'display: none;'), // hidden unless JS is working
- '#suffix' => '</div>'
- );
+ '#type' => 'select',
+ '#options' => $contacts,
+ '#attributes' => array('style' => 'display: none;'), // hidden unless JS is working
+ '#suffix' => '</div>',
+ );
$form['subject'] = array(
- '#type' => 'textfield',
- '#title' => t('Subject'),
- '#default_value' => $message->subject,
- '#size' => 50,
- '#maxlength' => 64,
- '#required' => TRUE
- );
+ '#type' => 'textfield',
+ '#title' => t('Subject'),
+ '#default_value' => $message->subject,
+ '#size' => 50,
+ '#maxlength' => 64,
+ '#required' => TRUE,
+ );
$form['privatemsgbody'] = array(
- '#type' => 'textarea',
- '#title' => t('Message'),
- '#default_value' => $message->message,
- '#cols' => 80,
- '#rows' => 5,
- '#required' => TRUE
- );
+ '#type' => 'textarea',
+ '#title' => t('Message'),
+ '#default_value' => $message->message,
+ '#cols' => 80,
+ '#rows' => 5,
+ '#required' => TRUE,
+ );
$form[] = filter_form($message->format);
$form[] = array(
- '#type' => 'submit',
- '#value' => t('Preview')
- );
+ '#type' => 'submit',
+ '#value' => t('Preview'),
+ '#weight' => 20,
+ );
$form[] = array(
- '#type' => 'submit',
- '#value' => t('Send private message')
- );
+ '#type' => 'submit',
+ '#value' => t('Send private message'),
+ '#weight' => 20,
+ );
$form['#after_build'] = array('_privatemsg_new_preview');
-
return $form;
}
@@ -693,11 +708,16 @@ function privatemsg_new_validate($form_id, $form_values) {
function privatemsg_new_submit($form_id, $form_values) {
global $user;
if ($form_values['op'] != t('Send private message')) {
- return false;
+ return FALSE;
}
$recipient = user_load(array('name' => $form_values['recipient']));
- $result = _privatemsg_send($user, $recipient, $form_values['subject'], $form_values['privatemsgbody'], $form_values['format']);
+ $pmid = _privatemsg_send($user, $recipient, $form_values['subject'], $form_values['privatemsgbody'], $form_values['format']);
+ $form_values['pmid'] = $pmid;
+
+ // Tell the other modules a new private message has been sent.
+ privatemsg_invoke_privatemsg($form_values, 'sent');
+
drupal_set_message(t('Message sent.'));
drupal_goto($user->uid ? 'privatemsg' : '');
}
@@ -709,21 +729,20 @@ function privatemsg_send_privatemsg($recipient, $subject, $body, $format = FILTE
}
function _privatemsg_send($sender, $recipient, $subject, $body, $format) {
- $result = db_query("INSERT INTO {privatemsg} (id, author, recipient, subject, message, timestamp, newmsg, hostname, format) VALUES (%d, %d, %d, '%s', '%s', %d, %d, '%s', %d)", db_next_id('{privatemsg}_id'), $sender->uid, $recipient->uid, $subject, $body, time(), 1, getenv('REMOTE_ADDR'), $format);
- if ($result !== false && isset($recipient->privatemsg_mailalert) && $recipient->privatemsg_mailalert == PRIVATEMSG_MAIL_INDIVIDUAL) {
+ $pmid = db_next_id('{privatemsg}_id');
+ $result = db_query("INSERT INTO {privatemsg} (id, author, recipient, subject, message, timestamp, newmsg, hostname, format) VALUES (%d, %d, %d, '%s', '%s', %d, %d, '%s', %d)", $pmid, $sender->uid, $recipient->uid, $subject, $body, time(), 1, getenv('REMOTE_ADDR'), $format);
+ if ($result !== FALSE && isset($recipient->privatemsg_mailalert) && $recipient->privatemsg_mailalert == PRIVATEMSG_MAIL_INDIVIDUAL) {
_privatemsg_sendmail($recipient, 1);
}
if ($points = variable_get('privatemsg_userpoints', 0)) {
module_invoke('userpoints', 'userpointsapi', 'points', $points, $sender->uid, 'privatemsg');
}
- return $result;
+ return $result ? $pmid : $result;
}
function privatemsg_view($message_id) {
- global $user;
- $result = db_query('SELECT p.id, u.uid, u.name, p.author, p.timestamp, p.subject, p.message, p.newmsg, p.recipient, p.folder, p.format FROM {privatemsg} p, {users} u WHERE (recipient = %d OR author = %d) AND author = u.uid AND id = %d', $user->uid, $user->uid, $message_id);
+ $message = privatemsg_load($message_id);
- $message = db_fetch_object($result);
if ($message->newmsg && $user->uid == $message->recipient) {
$result = db_query('UPDATE {privatemsg} SET newmsg = 0 WHERE id = %d', $message_id);
}
@@ -732,7 +751,7 @@ function privatemsg_view($message_id) {
}
function privatemsg_delete() {
- if (($folder = _privatemsg_delete(arg(2))) !== false) {
+ if (($folder = _privatemsg_delete(arg(2))) !== FALSE) {
drupal_set_message(t('Message deleted.'));
}
drupal_goto($folder ? 'privatemsg/list/'. $folder : 'privatemsg');
@@ -750,9 +769,13 @@ function _privatemsg_delete($id) {
if ($message->recipient == $user->uid) {
db_query('UPDATE {privatemsg} SET recipient_del = 1 WHERE id = %d', $id);
}
+
+ // Notify other modules that a message is deleted.
+ privatemsg_invoke_privatemsg($message, 'delete');
+
return $message->folder;
}
- return false;
+ return FALSE;
}
function _privatemsg_get_new_messages($uid = 0) {
@@ -825,13 +848,18 @@ function theme_privatemsg_view($message) {
global $user;
if ($message) {
+ $message->message = check_markup($message->message, $message->format, FALSE);
+
+ // Allow other modules to change the message.
+ privatemsg_invoke_privatemsg($message, 'view');
+
$body = '
<p><strong>'. t('From') .':</strong> '. theme('username', $message) .'<br />
<strong>'. t('To') .':</strong> '. theme('username', user_load(array('uid' => $message->recipient))) .'<br />
<strong>'. t('Subject') .':</strong> '.check_plain($message->subject) .'<br />
<strong>'. t('Date') .':</strong> '. format_date($message->timestamp) .'</p>
<div class="privatemsgbody">
- '. check_markup($message->message, $message->format, FALSE) .'</div>';
+ '. $message->message .'</div>';
$links = array();
if ($message->recipient == $user->uid) {
$author = user_load(array('uid' => $message->uid));
@@ -989,4 +1017,35 @@ function theme_privatemsg_message_table($form) {
/** @} End of addtogroup theme_system **/
-?>
+/**
+ * Invoke a hook_privatemsg() operation in all modules.
+ *
+ * @param &$message
+ * A message object.
+ * @param $op
+ * A string containing the name of the message operation.
+ * @return
+ * The returned value of the invoked hooks.
+ */
+function privatemsg_invoke_privatemsg(&$message, $op) {
+ $return = array();
+ foreach (module_implements('privatemsg') as $name) {
+ $function = $name .'_privatemsg';
+ $result = $function($message, $op);
+ if (isset($result) && is_array($result)) {
+ $return = array_merge($return, $result);
+ }
+ else if (isset($result)) {
+ $return[] = $result;
+ }
+ }
+ return $return;
+}
+
+function privatemsg_load($message_id) {
+ global $user;
+ $result = db_query('SELECT p.id, u.uid, u.name, p.author, p.timestamp, p.subject, p.message, p.newmsg, p.recipient, p.folder, p.format FROM {privatemsg} p, {users} u WHERE (recipient = %d OR author = %d) AND author = u.uid AND id = %d', $user->uid, $user->uid, $message_id);
+
+ $message = db_fetch_object($result);
+ return $message;
+}