20) { return substr($name, 0, 20) . '...'; } else { return $name; } } /** * helper function */ function _messenger_get_users() { global $user; $items = array(); $time_period = variable_get('user_block_seconds_online', 600); // display a list of currently online users for selection $users = db_query(' SELECT uid, name, IF(access >= %d, 1, 2) as status FROM {users} WHERE uid != 0 AND uid != %d AND status = 1 AND login != 0 ORDER BY status, name', time() - $time_period, $user->uid); while($uid = db_fetch_object($users)) { if($uid->status == 1) { $items[t('on-line users')][$uid->uid] = shorter_name($uid->name); } else { $items[t('off-line users')][$uid->uid] = shorter_name($uid->name); } } return $items; } /** * Retrieve a pipe delimited string of autocomplete suggestions for existing users */ function messenger_autocomplete($string = '') { global $user; $matches = array(); if($string){ $result = db_query_range(' SELECT name FROM {users} WHERE uid != 0 AND status = 1 AND login != 0 AND LOWER(name) != LOWER(\'%s\') AND LOWER(name) LIKE LOWER(\'%s%%\')' , $user->name, $string, 0, 10); while($row = db_fetch_object($result)) { $matches[$row->name] = check_plain($row->name); } } print drupal_to_js($matches); exit(); } /** * implementation of hook_block() */ function messenger_block_form() { _messenger_init(); global $user; $selected_rid = isset($_SESSION['messenger_rid'])? 0 + $_SESSION['messenger_rid']: 0; $selected_user = isset($_SESSION['messenger_user'])? $_SESSION['messenger_user']: ''; $autocomplete = variable_get('messenger_autocomplete', 0); $num_messages = 0; if($row = db_fetch_object(db_query('SELECT count(mid) as num_messages FROM {messenger} WHERE rid = %d AND is_read = 0', $user->uid))) { $num_messages = $row->num_messages; } if($num_messages == 0) { $class = 'messenger_none'; $notify_message = t('You have no messages'); } else if($num_messages == 1) { $class = 'messenger_alert'; $notify_message = t('You have 1 new message'); } else { $class = 'messenger_alert'; $notify_message = t('You have @num_messages new messages', array('@num_messages' => $num_messages)); } // block form $form['notify_message'] = array( '#value' => '
' . l($notify_message, 'messenger', array('attributes' => array('title' => 'read messages'), 'html' => TRUE)) . '
', '#weight' => 0, ); $form['messenger_autocomplete_flag'] = array( '#type' => 'hidden', '#value' => $autocomplete, '#weight' => -1, ); if($autocomplete) { $form['messenger_user'] = array( '#type' => 'textfield', '#title' => t('User'), '#size' => 20, '#maxlength' => 60, '#autocomplete_path' => 'messenger/autocomplete', '#default_value' => $selected_user, '#weight' => 1, ); } else { $form['messenger_rid'] = array( '#type' => 'select', '#title' => t('User'), '#options' => _messenger_get_users(), '#default_value' => $selected_rid, '#weight' => 1, ); } $form['messenger_message'] = array( '#type' => 'textfield', '#title' => t('Quick message'), '#default_value' => '', '#size' => '20', '#weight' => 2, ); $form['messenger_loader_image'] = array( '#type' => 'item', '#value' => theme_image(drupal_get_path('module', 'messenger') . '/messenger_loader.gif' , '' , t('Please wait') , array('id' => 'messenger_loader_image', 'style' => 'display:none;')), '#weight' => 3, ); $form['messenger_submit'] = array( '#type' => 'button', '#value' => t('Submit'), '#attributes' => array('onclick' => 'return messenger_submit_click()'), '#weight' => 4, ); return $form; } /** * the form on the messenger's page */ function messenger_form() { _messenger_init(); $selected_rid = isset($_SESSION['messenger_rid'])? 0 + $_SESSION['messenger_rid']: 0; $selected_user = isset($_SESSION['messenger_user'])? $_SESSION['messenger_user']: ''; $autocomplete = variable_get('messenger_autocomplete', 0); $form['fieldset'] = array( '#type' => 'fieldset', '#attributes' => array('style' => 'margin: 1em 0em;'), ); $form['fieldset']['messenger_autocomplete_flag'] = array( '#type' => 'hidden', '#value' => $autocomplete, '#weight' => -1, ); if($autocomplete) { $form['fieldset']['messenger_user'] = array( '#type' => 'textfield', '#title' => t('User'), '#size' => 30, '#maxlength' => 60, '#autocomplete_path' => 'messenger/autocomplete', '#default_value' => $selected_user, '#weight' => 0, ); } else { $form['fieldset']['messenger_rid'] = array( '#type' => 'select', '#title' => t('User'), '#options' => _messenger_get_users(), '#default_value' => $selected_rid, '#weight' => 0, ); } $form['fieldset']['messenger_message'] = array( '#type' => 'textarea', '#title' => t('Message'), '#default_value' => '', '#rows' => '10', '#weight' => 1, ); $form['fieldset']['messenger_loader_image'] = array( '#type' => 'item', '#value' => theme_image(drupal_get_path('module', 'messenger') . '/messenger_loader.gif' , '' , t('Please wait') , array('id' => 'messenger_loader_image', 'style' => 'display:none;')), '#weight' => 2, ); $form['fieldset']['messenger_submit'] = array( '#type' => 'button', '#value' => t('Submit'), '#attributes' => array('onclick' => 'return messenger_submit_click()'), '#weight' => 3, ); return $form; } /** * handle the block's onclick event */ function _messenger_form() { $rid = $_SESSION['messenger_rid'] = $_POST['rid']; $username = $_SESSION['messenger_user'] = $_POST['username']; $message = $_POST['message']; $output = ''; $user_found = FALSE; $error = FALSE; if(trim($message) == '') { $output = '
' . t('No message sent') . '
'; $error = TRUE; } else { if($rid == -1) { if(!($receiver_user = user_load(array('name' => $username)))) { $output = '
' . t('The username %name does not exist.', array('%name' => $username)) . '
'; $error = TRUE; } else { $rid = $receiver_user->uid; } } else { if(!user_load(array('uid' => $rid))) { $output = '
' . t('The user %uid does not exist.', array('%uid' => $rid)) . '
'; $error = TRUE; } } } if(!$error) { _messenger_submit($rid, $message); $output = '
' . t('Message sent') . '
'; } print($output); exit(); } /** * helper function for form_submit() */ function _messenger_submit($rid, $message) { global $user; db_query(' INSERT INTO {messenger} ( sid, rid, message, timestamp ) VALUES ( %d, %d, \'%s\', %d )', $user->uid, $rid, $message, time()); } /** * returns the messages table also for ajax */ function messenger_table($arg1 = '', $arg2 = '') { global $user; $output = ''; $messenger_format = variable_get('messenger_format', FILTER_FORMAT_DEFAULT); // the $user->uid is needed for security if($arg1 == 'delete') { db_query('DELETE FROM {messenger} WHERE mid = %d AND rid = %d', $arg2, $user->uid); } else if($arg1 == 'delete_all') { db_query('DELETE FROM {messenger} WHERE rid = %d AND is_read = 1', $user->uid); } else if($arg1 == 'mark_as_read') { db_query('UPDATE {messenger} SET is_read = 1 WHERE mid = %d AND rid = %d', $arg2, $user->uid); } else if($arg1 == 'mark_all_as_read') { db_query('UPDATE {messenger} SET is_read = 1 WHERE rid = %d AND is_read = 0', $user->uid); } $query = ' SELECT m.mid, m.sid, m.message, m.timestamp, u.uid, u.name FROM {messenger} m LEFT OUTER JOIN {users} u ON (m.sid = u.uid) WHERE m.rid = %d AND is_read = %d ORDER BY m.timestamp'; // get all the 'Unread messages' sent to this user $result = db_query($query, $user->uid, 0); if($msg = db_fetch_object($result)) { // title $output .= '
'; $output .= '

' . t('Unread messages') . '

'; // the "mark all as read" link $output .= '
'; $output .= '[' . t('Mark all as read') . ']'; $output .= '
'; do { if($msg->name) { $account->uid = $msg->sid; $account->name = $msg->name; $output .= '
'; $output .= '
'; $output .= '
'; $output .= theme_username($account) . ' - ' . format_date($msg->timestamp); $output .= '
'; $output .= '
'; $output .= '' . t('Reply') . '' . ' - ' . '' . t('Mark as read') . ''; $output .= '
'; $output .= ' '; $output .= '
'; $output .= '
'; $output .= check_markup($msg->message, $messenger_format, FALSE); $output .= '
'; $output .= '
'; } else { $output .= '
'; $output .= '
'; $output .= '
'; $output .= variable_get('anonymous', t('Anonymous')) . ' - ' . format_date($msg->timestamp); $output .= '
'; $output .= '
'; $output .= '' . t('Mark as read') . ''; $output .= '
'; $output .= ' '; $output .= '
'; $output .= '
'; $output .= check_markup($msg->message, $messenger_format, FALSE); $output .= '
'; $output .= '
'; } } while($msg = db_fetch_object($result)); $output .= '
'; } // // get the 'Message archive' // $result = db_query($query, $user->uid, 1); if($msg = db_fetch_object($result)) { // title $output .= '
'; $output .= '

' . t('Message archive') . '

'; // "delete all" link $output .= '
'; $output .= '[' . t('Delete all') . ']'; $output .= '
'; do { if($msg->name) { $account->uid = $msg->sid; $account->name = $msg->name; $output .= '
'; $output .= '
'; $output .= '
'; $output .= theme_username($account) . ' - ' . format_date($msg->timestamp); $output .= '
'; $output .= '
'; $output .= '' . t('Reply') . '' . ' - ' . '' . t('Delete') . ''; $output .= '
'; $output .= ' '; $output .= '
'; $output .= '
'; $output .= check_markup($msg->message, $messenger_format, FALSE); $output .= '
'; $output .= '
'; } else { $output .= '
'; $output .= '
'; $output .= '
'; $output .= variable_get('anonymous', t('Anonymous')) . ' - ' . format_date($msg->timestamp); $output .= '
'; $output .= '
'; $output .= '' . t('Delete') . ''; $output .= '
'; $output .= ' '; $output .= '
'; $output .= '
'; $output .= check_markup($msg->message, $messenger_format, FALSE); $output .= '
'; $output .= '
'; } } while($msg = db_fetch_object($result)); $output .= '
'; } return $output; } /** * print out the table for the "display-messenger-table" div */ function _messenger_table($arg1 = '', $arg2 = '') { print messenger_table($arg1, $arg2); exit(); } /** * hook_user(), delete the deleted user's messages */ function messenger_user($op, &$edit, &$account, $category = NULL) { if($op == 'delete') { db_query('DELETE FROM {messenger} WHERE sid = %d OR rid = %d', $account->uid, $account->uid); drupal_set_message(t('%user\'s messenger messages deleted', array('%user' => $account->name))); } } /** * implementation of hook_block() */ function messenger_block($op = 'list', $delta = 0) { $block = array(); if($op == 'list') { $block['0']['info'] = t('Messenger'); $block['0']['cache'] = BLOCK_NO_CACHE; return $block; } else if($op == 'view') { // the block is not displayed on the messenger page $path = $_GET['q'] . ''; if(user_access('access messenger') && !preg_match('/(messenger\/|messenger$)/i', $path)) { $block['content'] = '
' . drupal_get_form('messenger_block_form'); $block['subject'] = t('Messenger'); } return $block; } } /** * menu callback, display the messenger page */ function messenger_page($rid = 0) { $rid = $rid + 0; if($rid) { $_SESSION['messenger_rid'] = $rid; if($row = db_fetch_object(db_query('SELECT name FROM {users} where uid = %d', $rid))) { $_SESSION['messenger_user'] = $row->name; } } $output = ''; $output .= '
'; $output .= drupal_get_form('messenger_form'); $output .= '

' . t('Refresh messages') . '

'; $output .= '
'; $output .= messenger_table(); $output .= '
'; return $output; } /** * Implementation of hook_settings(). Menu callback */ function messenger_settings() { $form['messenger_autocomplete'] = array( '#type' => 'checkbox', '#title' => t('Autocomplete text field for the users list'), '#return_value' => 1, '#default_value' => variable_get('messenger_autocomplete', 0), '#description' => t('Use an autocomplete text field for the users list instead of the default combo box.'), ); $raw_formats = filter_formats(); $formats = array(); foreach($raw_formats as $format) { $formats[$format->format] = $format->name; } $form['messenger_format'] = array( '#type' => 'select', '#title' => t('Input format'), '#options' => $formats, '#default_value' => variable_get('messenger_format', FILTER_FORMAT_DEFAULT), ); return system_settings_form($form); } /** * implementation of hook_menu() */ function messenger_menu() { $items = array(); $access = array('access messenger'); $items['messenger'] = array( 'title' => t('Messenger'), 'page callback' => 'messenger_page', 'access arguments' => $access, 'type' => MENU_SUGGESTED_ITEM, ); $items['messenger/user/%'] = array( 'title' => t('Messenger'), 'page callback' => 'messenger_page', 'page arguments' => array(2), 'access arguments' => $access, 'type' => MENU_CALLBACK, ); $items['messenger/autocomplete'] = array( 'title' => t('Messenger'), 'page callback' => 'messenger_autocomplete', 'access arguments' => $access, 'type' => MENU_CALLBACK, ); $items['messenger/send'] = array( 'title' => t('Send message'), 'page callback' => '_messenger_form', 'access arguments' => $access, 'type' => MENU_CALLBACK, ); $items['admin/settings/messenger'] = array( 'title' => t('Messenger'), 'description' => t('Messenger configuration'), 'page callback' => 'drupal_get_form', 'page arguments' => array('messenger_settings'), 'access arguments' => array('administer messenger'), 'type' => MENU_NORMAL_ITEM, ); $items['messenger/delete/%'] = array( 'title' => t('Delete'), 'page callback' => '_messenger_table', 'page arguments' => array('delete', 2), 'access arguments' => $access, 'type' => MENU_CALLBACK, ); $items['messenger/delete_all'] = array( 'title' => t('Delete all'), 'page callback' => '_messenger_table', 'page arguments' => array('delete_all'), 'access arguments' => $access, 'type' => MENU_CALLBACK, ); $items['messenger/mark_as_read/%'] = array( 'title' => t('Mark as read'), 'page callback' => '_messenger_table', 'page arguments' => array('mark_as_read', 2), 'access arguments' => $access, 'type' => MENU_CALLBACK, ); $items['messenger/mark_all_as_read'] = array( 'title' => t('Mark all as read'), 'page callback' => '_messenger_table', 'page arguments' => array('mark_all_as_read'), 'access arguments' => $access, 'type' => MENU_CALLBACK, ); $items['messenger/refresh'] = array( 'title' => t('Send message'), 'page callback' => '_messenger_table', 'page arguments' => array('refresh'), 'access arguments' => $access, 'type' => MENU_CALLBACK, ); $items['user/%user/messages'] = array( 'title' => 'Messages', 'page callback' => 'messenger_page', 'page arguments' => array(1, TRUE), 'access callback' => 'user_access', 'access arguments' => $access, 'type' => MENU_LOCAL_TASK, 'weight' => '3' ); return $items; }