View source
<?php
function privatemsg_realname_privatemsg_name_lookup($string) {
if (preg_match('/\\[user:(.+)\\]/', $string, $match)) {
$account = user_load(array(
variable_get('privatemsg_realname_unique_identifier', 'name') => trim($match[1]),
));
$account->type = 'user';
$account->recipient = $account->uid;
return array(
$account,
);
}
$result = db_query("SELECT r.uid FROM {realname} r WHERE r.realname = '%s'", $string);
$accounts = array();
while ($row = db_fetch_object($result)) {
if ($account = user_load($row->uid)) {
$account->type = 'user';
$account->recipient = $account->uid;
$accounts[] = $account;
}
}
return $accounts;
}
function privatemsg_realname_theme() {
return array(
'privatemsg_realname_username' => array(
'arguments' => array(
'recipient' => NULL,
'options' => array(),
),
),
);
}
function privatemsg_realname_privatemsg_sql_autocomplete_alter(&$fragments, $search, $names) {
$fragments['inner_join'][] = 'LEFT JOIN {realname} r ON (r.uid = u.uid)';
if (variable_get('privatemsg_realname_search_username', TRUE)) {
$fragments['where'][1] = "(r.realname LIKE '%s' OR u.name LIKE '%s')";
array_unshift($fragments['query_args']['where'], $search . '%%');
}
else {
$fragments['where'][1] = "r.realname LIKE '%s'";
}
if (!empty($names)) {
$fragments['where'][] = "r.realname NOT IN (" . db_placeholders($names, 'text') . ") OR r.realname IS NULL";
$fragments['query_args']['where'] = array_merge($fragments['query_args']['where'], array_values($names));
}
}
function privatemsg_realname_privatemsg_recipient_type_info_alter(&$recipients) {
$recipients['user']['format'] = 'privatemsg_realname_username';
}
function theme_privatemsg_realname_username($recipient, $options) {
if (!isset($recipient->uid)) {
$recipient->uid = $recipient->recipient;
}
if (!empty($options['plain'])) {
$name = theme('username', $recipient, array(
'plain' => TRUE,
));
if (!empty($options['unique'])) {
$identifier = variable_get('privatemsg_realname_unique_identifier', 'name');
$name .= ' [user: ' . $recipient->{$identifier} . ']';
}
return $name;
}
else {
return theme('username', $recipient);
}
}
function privatemsg_realname_privatemsg_sql_participants_alter(&$fragments, $thread_id) {
$fragments['select'][] = 'r.realname';
$fragments['inner_join'][] = 'LEFT JOIN {realname} r ON (r.uid = u.uid)';
$fragments['group_by'][] = 'r.realname';
}
function privatemsg_realname_form_privatemsg_admin_settings_alter(&$form, &$form_state) {
$form['realname'] = array(
'#type' => 'fieldset',
'#title' => t('Realname integration'),
'#collapsed' => TRUE,
'#collapsible' => TRUE,
'#weight' => 25,
);
$form['realname']['privatemsg_realname_unique_identifier'] = array(
'#type' => 'radios',
'#title' => t('Field to use as a unique identifier'),
'#description' => t('Real names are often not unique. Choose which field should be used as a unique identifier when sending private messages.'),
'#default_value' => variable_get('privatemsg_realname_unique_identifier', 'name'),
'#options' => array(
'name' => t('Username'),
'uid' => t('UID'),
'mail' => t('E-mail'),
),
);
$form['realname']['privatemsg_realname_search_username'] = array(
'#type' => 'checkbox',
'#title' => t('Search usernames for autocomplete suggestions'),
'#default_value' => variable_get('privatemsg_realname_search_username', TRUE),
);
}