privatemsg settings.', array('!settings-url' => url('admin/settings/privatemsg'))); } } switch ($section) { case 'admin/help#privatemsg': $output = '

'. t('The private messaging module allows users to send messages to each other without having to share email addresses. An inbox link will appear in the navigation menu. The "write to author" links are included in posts, allowing users to write a private message instead of commenting openly. Allowing users to communicate directly is an important part of building the strength of the community.') .'

'; $output .= '

'. t('Users can also select whether to receive email notices of new messages by editing their user profile. The contacts list contains only users that you have previously messaged. To contact users not in your list, you need to know their local user name. Administrators can set messaging options such as frequency of emails, message status display, and number of messages to display per page. They can also configure \'Write to Author\' options.') .'

'; $output .= t('

You can

'; $output .= '

'. t('For more information please read the configuration and customization handbook Privatemsg page.', array('!privatemsg' => 'http://drupal.org/handbook/modules/privatemsg/')) .'

'; return $output; } } /** * Implementation of hook_link(). */ function privatemsg_link($type, $node = NULL, $teaser = FALSE) { global $user; static $access = array(); $links = array(); $uid = $node->uid; if ($type == 'comment' && $node->nid) { $node = node_load($node->nid); } if (user_access('access private messages') && in_array($teaser ? 'teaser' : $type, variable_get('privatemsg_link_'. $node->type, array())) && $uid != $user->uid && (isset($user->privatemsg_allow) ? $user->privatemsg_allow : 1)) { if (!isset($access[$uid])) { $author = user_load(array('uid' => $uid)); $access[$uid] = user_access('access private messages', $author) && $author->uid && (isset($author->privatemsg_allow) ? $author->privatemsg_allow : 1); } if ($access[$uid]) { $links['privatemsg_write_to_author'] = array( 'title' => t('Write to author'), 'href' => 'privatemsg/msgto/'. $uid, ); } } return $links; } /** * Implementation of hook_menu(). */ function privatemsg_menu($may_cache) { global $user; $items = array(); if ($may_cache) { $access = user_access('access private messages'); $items[] = array('path' => 'admin/settings/privatemsg', 'title' => t('Privatemsg'), 'description' => t('Configure Privatemsg settings.'), 'callback' => 'drupal_get_form', 'callback arguments' => array('privatemsg_configure')); $items[] = array('path' => 'privatemsg', 'title' => t('Private Messages'), 'callback' => 'privatemsg_list', 'access' => TRUE, // Check access/redirect in callback 'type' => MENU_SUGGESTED_ITEM); $items[] = array('path' => 'privatemsg/new', 'title' => t('Write a new message'), 'callback' => 'drupal_get_form', 'callback arguments' => array('privatemsg_new'), 'access' => $access, 'type' => MENU_CALLBACK); $items[] = array('path' => 'privatemsg/reply', 'title' => t('Write a new message'), 'callback' => 'drupal_get_form', 'callback arguments' => array('privatemsg_new'), 'access' => $access, 'type' => MENU_CALLBACK); $items[] = array('path' => 'privatemsg/msgto', 'title' => t('Write a new message'), 'callback' => 'drupal_get_form', 'callback arguments' => array('privatemsg_new'), 'access' => $access, 'type' => MENU_CALLBACK); $items[] = array('path' => 'privatemsg/autocomplete', 'title' => t('Privatemsg autocomplete'), 'callback' => 'privatemsg_autocomplete', 'access' => $access, 'type' => MENU_CALLBACK); $items[] = array('path' => 'privatemsg/view', 'title' => t('Read message'), 'callback' => 'privatemsg_view', 'access' => $access && $user->uid, // No guest access 'type' => MENU_CALLBACK); $items[] = array('path' => 'privatemsg/newfolder', 'title' => t('Create new folder'), 'callback' => 'drupal_get_form', 'callback arguments' => array('privatemsg_new_folder'), 'access' => $access && $user->uid, // No guest access 'type' => MENU_CALLBACK); $items[] = array('path' => 'privatemsg/delete', 'callback' => 'privatemsg_delete', 'access' => $access && $user->uid, // No guest access 'type' => MENU_CALLBACK); } else { $new = _privatemsg_get_new_messages(); $items[] = array('path' => 'privatemsg/inbox', 'title' => t(variable_get('privatemsg_menu_link', 'My inbox')) . ($new ? ' ('. $new .')' : ''), 'callback' => 'privatemsg_list', 'type' => $user->uid && (isset($user->privatemsg_allow) ? $user->privatemsg_allow : 1) ? MENU_DYNAMIC_ITEM : MENU_CALLBACK); if ($new && strncmp($_GET['q'], 'privatemsg', 10) && (isset($user->privatemsg_setmessage_notify) ? $user->privatemsg_setmessage_notify : 1) && user_access('access private messages')) { $m = drupal_set_message(); if (empty($m)) { drupal_set_message(strtr(format_plural($new, 'You have one new private message.', 'You have @count new private messages.'), array('!url' => url('privatemsg')))); } } } return $items; } /** * Implementation of hook_user(). */ function privatemsg_user($type, &$edit, &$user, $category = NULL) { switch ($type) { case 'view': if (user_access('access private messages') && (isset($user->privatemsg_allow) ? $user->privatemsg_allow : 1)) { return array(t('Private messages') => array(array( 'value' => l(t('Write private message'), 'privatemsg/msgto/'. $user->uid, array( 'title' => t('Send private message to @name', array('@name' => $user->name)))), 'class' => 'send-message'))); } else if ($GLOBALS['user']->uid) { return; } else if (isset($user->privatemsg_allow) ? $user->privatemsg_allow : 1) { if (variable_get('user_register', 1)) { return array(t('Private messages') => array(array( 'value' => t('login or register to send private messages to this user', array('!login' => url('user/login'), '!register' => url('user/register'))), 'class' => 'need-login'))); } else { return array(t('Private messages') => array(array( 'value' => t('login to send private messages to this user', array('!login' => url('user/login'))), 'class' => 'need-login'))); } } break; case 'form': 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 ); $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.') ); $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.') ); $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.') ); return $form; } break; case 'insert': if ($welcome_message = trim(variable_get('privatemsg_welcome_message', ''))) { $subject = variable_get('privatemsg_welcome_subject', t('Welcome')); $format = variable_get('privatemsg_welcome_format', FILTER_FORMAT_DEFAULT); $sender = user_load(array('uid' => variable_get('privatemsg_welcome_sender', 1))); if ($sender->uid) { _privatemsg_send($sender, $user, $subject, $welcome_message, $format); } } break; case 'delete': db_query('DELETE FROM {privatemsg} WHERE recipient = %d', $user->uid); db_query('DELETE FROM {privatemsg_archive} WHERE recipient = %d', $user->uid); db_query('DELETE FROM {privatemsg_folder} WHERE uid = %d', $user->uid); db_query('UPDATE {privatemsg} SET author = 0 WHERE author = %d', $user->uid); db_query('UPDATE {privatemsg_archive} SET author = 0 WHERE author = %d', $user->uid); break; } } /** * Implementation of hook_form_alter(). */ function privatemsg_form_alter($form_id, &$form) { if ($form_id == 'node_type_form') { $link = variable_get('privatemsg_link_'. $form['#node_type']->type, array()); $form['workflow']['privatemsg_link'] = array( '#type' => 'checkboxes', '#title' => t('Private message "Write to author" links'), '#weight' => 30, 'node' => array( '#type' => 'checkbox', '#title' => t('Link on node'), '#default_value' => in_array('node', $link), ), 'teaser' => array( '#type' => 'checkbox', '#title' => t('Link on teaser'), '#default_value' => in_array('teaser', $link), ), 'comment' => array( '#type' => module_exists('comment') ? 'checkbox' : 'hidden', '#title' => t('Link on comments'), '#default_value' => in_array('comment', $link), ), ); } } /** * Implementation of hook_block(). */ function privatemsg_block($op = 'list', $delta = 0, $edit = array()) { if ($op == 'list') { $blocks[0]['info'] = t('Private messages'); return $blocks; } else if ($op == 'view') { switch ($delta) { case 0: return _privatemsg_block_inbox(); } } } function _privatemsg_block_inbox() { global $user; if ($user->uid && user_access('access private messages') && (isset($user->privatemsg_allow) ? $user->privatemsg_allow : 1)) { $new = _privatemsg_get_new_messages(); $items = array( l(t('Inbox') . ($new ? " ($new)" : ''), 'privatemsg'), l(t('Write message'), 'privatemsg/msgto'), ); $block['subject'] = t('Private messages'); $block['content'] = theme('item_list', $items); return $block; } } function privatemsg_configure() { $form = array(); $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.') ); $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.') ); $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.') ); $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."), ); $form['links'] = array( '#type' => 'fieldset', '#title' => t('"Write to author" links'), '#description' => t('Edit content types 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') ); $form['welcome']['subject'] = array( '#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.') ); $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 ); return system_settings_form($form); } function privatemsg_configure_validate($form_id, $form_values, $form) { if (!empty($form_values['privatemsg_welcome_sender'])) { $sender = user_load(array('name' => $form_values['privatemsg_welcome_sender'])); if ($sender->uid) { form_set_value($form['welcome']['privatemsg_welcome_sender'], $sender->uid); } else { form_set_error('privatemsg_welcome_sender', t('The Sender does not exist.')); } } } /** * Implementation of hook_perm(). */ function privatemsg_perm() { return array('access private messages'); } /** * Implementation of hook_cron(). */ function privatemsg_cron() { // perform these actions just once per day if (variable_get('privatemsg_last_cron', 0) < (time() - 3600*24)) { _privatemsg_prune(); _privatemsg_mailalert(); variable_set('privatemsg_last_cron', time()); } } function _privatemsg_prune() { // move deleted message older than 1 month to archive table, and optimize table $result = db_query('SELECT * FROM {privatemsg} WHERE author_del = 1 AND recipient_del = 1 AND timestamp < %d', time() - 3600*24*30); 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); } // this is MySQL-specific if (!strncmp($GLOBALS['db_type'], 'mysql', 5)) { db_query('OPTIMIZE TABLE {privatemsg}'); } } function _privatemsg_mailalert() { $result = db_query('SELECT COUNT(*) AS c, recipient FROM {privatemsg} WHERE newmsg = 1 AND recipient_del = 0 GROUP BY recipient'); while ($alert = db_fetch_object($result)) { $user = user_load(array('uid' => $alert->recipient)); if ((isset($user->privatemsg_allow) ? $user->privatemsg_allow : 1) && isset($user->privatemsg_mailalert) && $user->privatemsg_mailalert == PRIVATEMSG_MAIL_DIGEST && $user->status != 0) { _privatemsg_sendmail($user, $alert->c); } } } function _privatemsg_sendmail($user, $new_count) { global $locale; static $from, $languages; if (!isset($from)) { $from = variable_get('site_mail', ini_get('sendmail_from')); $languages = array(); if (function_exists('locale')) { $languages = locale_supported_languages(); $languages = $languages['name']; } } $save_locale = $locale; // use each user's individual locale if (isset($languages[$user->language])) { $locale = $user->language; } $subject = t('New private messages at @site.', array('@site' => variable_get('site_name', 'drupal'))); // Can't pass tokens directly to format_plural, so strtr+check_plain here $message = strtr(format_plural($new_count, 'Hi @name, This is an automatic reminder from the site @site. You have received a new private message. To read your message, follow this link: !link1 If you don\'t want to receive these emails again, change your preferences here: !link2', 'Hi @name, This is an automatic reminder from the site @site. You have @count unread private messages. To read your messages, follow this link: !link1 If you don\'t want to receive these emails again, change your preferences here: !link2'), array('@name' => check_plain($user->name), '@site' => check_plain(variable_get('site_name', 'drupal')), '!link1' => url('privatemsg', NULL, NULL, 1), '!link2' => url('user/'. $user->uid .'/edit', NULL, NULL, 1))); drupal_mail('privatemsg_mailalert', $user->mail, $subject, $message, $from); // revert to previous (site default) locale $locale = $save_locale; } function privatemsg_autocomplete($string) { $matches = array(); $result = db_query_range("SELECT name FROM {users} u WHERE status <> 0 AND LOWER(name) LIKE LOWER('%s%%') AND data NOT LIKE '%\"privatemsg_allow\";s:0%'", $string, 0, 10); while ($user = db_fetch_object($result)) { $matches[$user->name] = check_plain($user->name); } print drupal_to_js($matches); exit(); } function privatemsg_list() { global $user; // Notification emails link here but we may not be logged in yet if (!$user->uid) { drupal_goto('user/login', 'destination=privatemsg'); } if (!user_access('access private messages') || !(isset($user->privatemsg_allow) ? $user->privatemsg_allow : 1)) { return drupal_access_denied(); } drupal_set_title(t('Private messages')); $current_folder = arg(2); if ($current_folder != 1) { $result = pager_query('SELECT id, subject, p.timestamp, u.uid, u.name, newmsg FROM {privatemsg} p, {users} u WHERE p.author = u.uid AND p.recipient = %d AND folder = %d AND p.recipient_del = 0 ORDER BY p.timestamp DESC', variable_get('privatemsg_per_page', 10), 0, NULL, $user->uid, $current_folder); if ($current_folder > 0) { $folder_name = db_result(db_query('SELECT name FROM {privatemsg_folder} WHERE fid = %d AND uid = %d', $current_folder, $user->uid)); } else { $folder_name = t('Inbox'); } } else { // sent messages $result = pager_query('SELECT id, subject, p.timestamp, u.uid, u.name, newmsg FROM {privatemsg} p, {users} u WHERE p.recipient = u.uid AND p.author = %d AND p.author_del = 0 ORDER BY p.timestamp DESC', variable_get('privatemsg_per_page', 10), 0, NULL, $user->uid); $folder_name = t('Sent messages'); } $messages = array(); while ($message = db_fetch_object($result)) { $messages[] = $message; } $folders[] = array(0, t('Inbox')); $result = db_query('SELECT fid, name FROM {privatemsg_folder} WHERE uid = %d', $user->uid); while ($folder = db_fetch_object($result)) { $folders[] = array($folder->fid, $folder->name); } $folders[] = array(1, t('Sent messages')); return drupal_get_form('privatemsg_list_form', $current_folder, $messages, $folders); } function privatemsg_list_form_submit($form_id, $form_values) { global $user; switch ($_POST['op']) { case t('Write a new message'): return 'privatemsg/new'; case t('Delete messages'): foreach ($form_values['messages'] as $mid => $message) { if ($message['selected']) { _privatemsg_delete($mid); } } break; case t('New folder'): return 'privatemsg/newfolder'; case t('Move to folder'): if (!isset($form_values['folder']) || ($form_values['folder'] != 0 && !db_result(db_query('SELECT fid FROM {privatemsg_folder} WHERE fid = %d AND uid = %d', $form_values['folder'], $user->uid)))) { watchdog('privatemsg', t('Attempted use of unauthorized folder'), WATCHDOG_WARNING); return drupal_access_denied(); } foreach ($form_values['messages'] as $mid => $message) { if ($message['selected']) { db_query('UPDATE {privatemsg} SET folder = %d WHERE id = %d AND recipient = %d', $form_values['folder'], $mid, $user->uid); } } return $form_values['folder'] ? 'privatemsg/list/'. $form_values['folder'] : 'privatemsg'; case t('Empty folder'): $fid = (int)arg(2); if ($fid == 1) { db_query('UPDATE {privatemsg} SET author_del = 1 WHERE author = %d', $user->uid); } else if ($fid > 1 && !db_result(db_query('SELECT fid FROM {privatemsg_folder} WHERE fid = %d AND uid = %d', $fid, $user->uid))) { watchdog('privatemsg', t('Attempted use of unauthorized folder'), WATCHDOG_WARNING); return drupal_access_denied(); } else { db_query('UPDATE {privatemsg} SET recipient_del = 1 WHERE folder = %d AND recipient = %d', $fid, $user->uid); } break; case t('Delete folder'): $fid = (int)arg(2); if (!db_result(db_query('SELECT fid FROM {privatemsg_folder} WHERE fid = %d AND uid = %d', $fid, $user->uid))) { watchdog('privatemsg', t('Attempted use of unauthorized folder'), WATCHDOG_WARNING); return drupal_access_denied(); } db_query('DELETE FROM {privatemsg_folder} WHERE fid = %d', $fid); db_query('UPDATE {privatemsg} SET recipient_del = 1 WHERE folder = %d', $fid); drupal_set_message(t('Folder deleted.')); return 'privatemsg'; } } function _privatemsg_format_folder($current, $fid, $name) { if ($current == $fid) { return array('title' => "$name", 'html' => TRUE); } return array('title' => $name, 'href' => $fid ? 'privatemsg/list/'. $fid : 'privatemsg'); } function privatemsg_new() { global $user; $message = 0; if (($op = arg(1)) == 'reply') { $message = arg(2); } else if ($op == 'msgto' && ($arg = arg(2)) && ($msg->recipient = db_result(db_query('SELECT name FROM {users} WHERE uid = %d', $arg)))) { $message = $msg; } if ($message && !is_object($message)) { // This is a reply to another message $message = db_fetch_object(db_query('SELECT subject, message, u.name AS recipient FROM {privatemsg} p, {users} u WHERE u.uid = p.author AND id = %d AND recipient = %d', $message, $user->uid)); if (!stristr($message->subject, t('Re:'))) { $message->subject = t('Re:') .' '. $message->subject; } // quoting; [quote] if default input format uses bbcode or quote, else > quoting foreach (filter_list_format(filter_resolve_format(FILTER_FORMAT_DEFAULT)) as $filter) { if ($filter->module == 'bbcode' || $filter->module == 'quote') { $bbcode = TRUE; break; } } if (isset($bbcode)) { $message->message = "\n\n[quote=". $message->recipient .']'. $message->message .'[/quote]'; } else { $message->message = "\n". str_replace("\n", "\n> ", "\n". $message->message); } } drupal_add_js(drupal_get_path('module', 'privatemsg') .'/privatemsg.js'); $form = array('preview' => array()); $form['recipient'] = array( '#prefix' => '
', '#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)) { $username = check_plain($name->name); $contacts[$username] = $username; } if (module_exists('buddylist')) { $result = db_query('SELECT u.name FROM {buddylist} b, {users} u WHERE b.buddy = u.uid AND b.uid = %d', $user->uid); while ($name = db_fetch_object($result)) { $buddyname = check_plain($name->name); $contacts[$buddyname] = $buddyname; } asort($contacts); } array_unshift($contacts, t('Contacts')); $form['quick'] = array( '#type' => 'select', '#options' => $contacts, '#attributes' => array('style' => 'display: none;'), // hidden unless JS is working '#suffix' => '
' ); $form['subject'] = array( '#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 ); $form[] = filter_form($message->format); $form[] = array( '#type' => 'submit', '#value' => t('Preview') ); $form[] = array( '#type' => 'submit', '#value' => t('Send private message') ); $form['#after_build'] = array('_privatemsg_new_preview'); return $form; } function _privatemsg_new_preview($form, $form_values) { if ($form_values['op'] == t('Preview')) { $recipient = user_load(array('name' => $form_values['recipient'])); if ($recipient->uid) { global $user; $message->subject = $form_values['subject']; $message->message = $form_values['privatemsgbody']; $message->format = $form_values['format']; $message->timestamp = time(); $message->newmsg = TRUE; $message->folder = 1; $message->uid = $message->author = $user->uid; $message->name = $user->name; $message->recipient = $recipient->uid; $form['preview'] = array( '#prefix' => '
', '#value' => theme('privatemsg_view', $message), '#suffix' => '
'); drupal_set_title(t('Preview message')); } } return $form; } function privatemsg_new_validate($form_id, $form_values) { if (!empty($form_values['recipient'])) { $recipient = user_load(array('name' => $form_values['recipient'])); if (!$recipient->uid) { form_set_error('recipient', t('The Recipient does not exist.')); } else if (!$recipient->status || !(isset($recipient->privatemsg_allow) ? $recipient->privatemsg_allow : 1)) { form_set_error('recipient', t('@name does not accept private messages.', array('@name' => $recipient->name))); } } if (!isset($form_values['format']) || !filter_access($form_values['format'])) { form_set_error('format', t('The supplied input format is invalid.')); } } function privatemsg_new_submit($form_id, $form_values) { global $user; if ($form_values['op'] != t('Send private message')) { return false; } $recipient = user_load(array('name' => $form_values['recipient'])); $result = _privatemsg_send($user, $recipient, $form_values['subject'], $form_values['privatemsgbody'], $form_values['format']); drupal_set_message(t('Message sent.')); drupal_goto($user->uid ? 'privatemsg' : ''); } // API callable from other modules: function privatemsg_send_privatemsg($recipient, $subject, $body, $format = FILTER_FORMAT_DEFAULT) { global $user; return _privatemsg_send($user, $recipient, $subject, $body, $format); } 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) { _privatemsg_sendmail($recipient, 1); } if ($points = variable_get('privatemsg_userpoints', 0)) { module_invoke('userpoints', 'userpointsapi', 'points', $points, $sender->uid, 'privatemsg'); } return $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 = db_fetch_object($result); if ($message->newmsg && $user->uid == $message->recipient) { $result = db_query('UPDATE {privatemsg} SET newmsg = 0 WHERE id = %d', $message_id); } return theme('privatemsg_view', $message); } function privatemsg_delete() { if (($folder = _privatemsg_delete(arg(2))) !== false) { drupal_set_message(t('Message deleted.')); } drupal_goto($folder ? 'privatemsg/list/'. $folder : 'privatemsg'); } function _privatemsg_delete($id) { global $user; $result = db_query('SELECT author, recipient, folder FROM {privatemsg} WHERE (recipient = %d OR author = %d) AND id = %d', $user->uid, $user->uid, $id); if ($message = db_fetch_object($result)) { if ($message->author == $user->uid) { db_query('UPDATE {privatemsg} SET author_del = 1 WHERE id = %d', $id); } if ($message->recipient == $user->uid) { db_query('UPDATE {privatemsg} SET recipient_del = 1 WHERE id = %d', $id); } return $message->folder; } return false; } function _privatemsg_get_new_messages($uid = 0) { global $user; static $cache = array(); if ($uid == 0) { $uid = $user->uid; } if (!isset($cache[$uid])) { $cache[$uid] = (int)db_result(db_query('SELECT COUNT(*) FROM {privatemsg} WHERE recipient = %d AND newmsg = 1 AND recipient_del = 0', $uid)); } return $cache[$uid]; } function privatemsg_new_folder() { $form = array(); $form['name'] = array( '#type' => 'textfield', '#title' => t('Name'), '#size' => 50, '#maxlength' => 64, '#required' => TRUE ); $form['op'] = array( '#type' => 'submit', '#value' => t('Add folder') ); return $form; } function privatemsg_new_folder_validate($form_id, $form_values) { global $user; if (!empty($form_values['name']) && db_result(db_query("SELECT name FROM {privatemsg_folder} WHERE name = '%s' AND uid = %d", $form_values['name'], $user->uid))) { form_set_error('name', t('A folder with this name already exists.')); } } function privatemsg_new_folder_submit($form_id, $form_values) { global $user; db_query("INSERT INTO {privatemsg_folder} (fid, uid, name) VALUES (%d, %d, '%s')", db_next_id('{privatemsg_folder}_fid'), $user->uid, $form_values['name']); drupal_set_message(t('Folder created successfully.')); drupal_goto('privatemsg'); } function privatemsg_userpoints($op, $new_points = 0, $uid = 0, $event = '') { switch ($op) { case 'setting': $form['privatemsg_userpoints'] = array( '#type' => 'textfield', '#title' => t('Points for sending a private message'), '#default_value' => variable_get('privatemsg_userpoints', 0)); return $form; } } /** @addtogroup theme_system Privatemsg module specific theme functions @{ **/ /** Returns content to view a private message @param message **/ function theme_privatemsg_view($message) { global $user; if ($message) { $body = '

'. t('From') .': '. theme('username', $message) .'
'. t('To') .': '. theme('username', user_load(array('uid' => $message->recipient))) .'
'. t('Subject') .': '.check_plain($message->subject) .'
'. t('Date') .': '. format_date($message->timestamp) .'

'. check_markup($message->message, $message->format, FALSE) .'
'; $links = array(); if ($message->recipient == $user->uid) { $author = user_load(array('uid' => $message->uid)); if ($author->uid && (isset($author->privatemsg_allow) ? $author->privatemsg_allow : 1)) { $links['privatemsg_reply'] = array('title' => t('Reply to this message'), 'href' => 'privatemsg/reply/'. $message->id); } else { $links['privatemsg_noreply'] = array('title' => t('Sender does not accept replies'), 'href' => NULL); } } if ($message->recipient == $user->uid || variable_get('privatemsg_sent_status', 1)) { $links['privatemsg_delete'] = array('title' => t('Delete this message'), 'href' => 'privatemsg/delete/'. $message->id, 'attributes' => array('onclick' => "return confirm('". t('Are you sure to delete this message?') ."')")); } $links['privatemsg_list'] = array('title' => t('List messages'), 'href' => ($message->recipient == $user->uid && $message->folder) ? 'privatemsg/list/'. $message->folder : 'privatemsg'); if ($message->id) { $body .= ''; } } else { drupal_set_message(t("Error: you can't read this message."), 'error'); $body = ''; } return $body; } function privatemsg_list_form($current_folder, $messages, $folders) { $form = $extra_folders = array(); foreach ($folders as $folder) { $folder_list[] = _privatemsg_format_folder($current_folder, $folder[0], $folder[1]); if ($folder[0] != 1 && $folder[0] != $current_folder) { $extra_folders[$folder[0]] = $folder[1]; } } $form['folders'] = array( '#value' => theme('links', $folder_list) ); $form['messages'] = array( '#theme' => 'privatemsg_message_table', '#tree' => TRUE ); $form['messages']['current_folder'] = array( '#type' => 'value', '#value' => $current_folder ); foreach ($messages as $message) { if ($current_folder != 1) { $new = $message->newmsg; } else { $new = variable_get('privatemsg_sent_status', 1) ? $message->newmsg : 0; } $form['messages'][$message->id] = array(); $form['messages'][$message->id]['selected'] = array( '#type' => 'checkbox', ); $form['messages'][$message->id]['date'] = array( '#type' => 'value', '#value' => format_date($message->timestamp, 'small') ); $form['messages'][$message->id]['user'] = array( '#type' => 'value', '#value' => theme('username', $message) ); $form['messages'][$message->id]['subject'] = array( '#type' => 'value', '#value' => l($message->subject, 'privatemsg/view/'. $message->id) . ($new ? (' '. theme('mark')) : '') ); } $form[] = array('#type' => 'markup', '#value' => theme('pager', array(), variable_get('privatemsg_per_page', 10))); if (count($messages) > 0) { $form['delete_messages'] = array( '#type' => 'submit', '#value' => t('Delete messages'), '#attributes' => array('onclick' => "return confirm('". t('Are you sure you want to delete these messages?') ."')") ); } // folder management if (count($extra_folders) > 0 && $current_folder != 1 && count($messages) > 0) { $form['folder'] = array( '#prefix' => '
', '#type' => 'select', '#options' => $extra_folders ); $form['move_messages'] = array( '#type' => 'submit', '#value' => t('Move to folder'), '#suffix' => '
' ); } $form[] = array('#type' => 'markup', '#value' => '
'); $form['new_message'] = array( '#type' => 'submit', '#value' => t('Write a new message') ); $form[] = array('#type' => 'markup', '#value' => '
'); if ($current_folder > 1) { // you can't delete Inbox $form['delete_folder'] = array( '#type' => 'submit', '#value' => t('Delete folder'), '#attributes' => array('onclick' => "return confirm('". t('Are you sure you want to delete this folder and all its messages?') ."')") ); } if (count($messages) > 0) { $form['empty_folder'] = array( '#type' => 'submit', '#value' => t('Empty folder'), '#attributes' => array('onclick' => "return confirm('". t('Are you sure you want to delete every message in this folder?') ."')") ); } $form['new_folder'] = array( '#type' => 'submit', '#value' => t('New folder') ); return $form; } function theme_privatemsg_message_table($form) { $rows = array(); foreach (element_children($form) as $key) { if ($key != 'current_folder') { $row = array(); $row[] = drupal_render($form[$key]['selected']); $row[] = $form[$key]['date']['#value']; $row[] = $form[$key]['user']['#value']; $row[] = $form[$key]['subject']['#value']; $rows[] = $row; } } if (count($rows) == 0) { $rows[] = array(array('data' => t('No messages.'), 'colspan' => 4)); } $header = array( NULL, t('Date'), ($form['current_folder']['#value'] == 1 ? t('To') : t('From')), t('Subject'), ); return theme('table', $header, $rows); } /** @} End of addtogroup theme_system **/ ?>