Stripping CVS keywords
[project/privatemsg.git] / privatemsg_realname / privatemsg_realname.module
CommitLineData
6fb29165 1<?php
6fb29165
SG
2
3/**
4 * Implements hook_privatemsg_name_lookup().
5 */
6function privatemsg_realname_privatemsg_name_lookup($string) {
7
8 // First, check the unique version.
9 if (preg_match('/\[user:(.+)\]/', $string, $match)) {
10 $account = user_load(array(variable_get('privatemsg_realname_unique_identifier', 'name') => trim($match[1])));
11 $account->type = 'user';
12 $account->recipient = $account->uid;
13 return array($account);
14 }
15
16 // Then try to look it up with the real name.
17 $result = db_query("SELECT r.uid FROM {realname} r WHERE r.realname = '%s'", $string);
18 $accounts = array();
19 while ($row = db_fetch_object($result)) {
20 if ($account = user_load($row->uid)) {
21 $account->type = 'user';
22 $account->recipient = $account->uid;
23 $accounts[] = $account;
24 }
25 }
26 return $accounts;
27}
28
29/**
30 * Implemenets hook_theme().
31 */
32function privatemsg_realname_theme() {
33 return array(
34 'privatemsg_realname_username' => array(
35 'arguments' => array('recipient' => NULL, 'options' => array()),
36 ),
37 );
38}
39
40/**
41 * Implements hook_privatemsg_sql_autocomplete_alter().
42 */
43function privatemsg_realname_privatemsg_sql_autocomplete_alter(&$fragments, $search, $names) {
44 // Create necessary LEFT JOINs. LEFT because some users might not have a realname for any reason.
45 $fragments['inner_join'][] = 'LEFT JOIN {realname} r ON (r.uid = u.uid)';
46
47 // Either select users where the profile field name and value matches or the username.
48 // This does replace the default where.
49 if (variable_get('privatemsg_realname_search_username', TRUE)) {
50 $fragments['where'][1] = "(r.realname LIKE '%s' OR u.name LIKE '%s')";
51 array_unshift($fragments['query_args']['where'], $search . '%%');
52 }
53 else {
54 $fragments['where'][1] = "r.realname LIKE '%s'";
55 }
56
57 if (!empty($names)) {
58 // Exclude already existing realnames, but explicitly allow NULL.
59 // r.realname is left joined and cann be NULL => NULL NOT IN (...) => NOT (NULL) => NULL.
60 $fragments['where'][] = "r.realname NOT IN (". db_placeholders($names, 'text') .") OR r.realname IS NULL";
61 $fragments['query_args']['where'] = array_merge($fragments['query_args']['where'], array_values($names));
62 }
63}
64
65/**
66 * Implements hook_privatemsg_recipient_info_alter().
67 */
68function privatemsg_realname_privatemsg_recipient_type_info_alter(&$recipients) {
69 // Override format callback.
70 $recipients['user']['format'] = 'privatemsg_realname_username';
71}
72
73/**
74 * Used to theme and display user recipients.
75 *
76 * Wrapper for theme_username() with a few additional options.
77 */
78function theme_privatemsg_realname_username($recipient, $options) {
79 if (!isset($recipient->uid)) {
80 $recipient->uid = $recipient->recipient;
81 }
82 if (!empty($options['plain'])) {
83 $name = theme('username', $recipient, array('plain' => TRUE));
84 if (!empty($options['unique'])) {
85 $identifier = variable_get('privatemsg_realname_unique_identifier', 'name');
86 $name .= ' [user: ' . $recipient->$identifier . ']';
87 }
88 return $name;
89 }
90 else {
91 return theme('username', $recipient);
92 }
93}
94
95/**
96 * Implements hook_privatemsg_sql_participants_alter().
97 */
98function privatemsg_realname_privatemsg_sql_participants_alter(&$fragments, $thread_id) {
99 // Select the realname.
100 $fragments['select'][] = 'r.realname';
6fb29165
SG
101
102 // Join realname table.
103 $fragments['inner_join'][] = 'LEFT JOIN {realname} r ON (r.uid = u.uid)';
0d5261bb
SG
104
105 // Add realname to GROUP BY.
106 $fragments['group_by'][] = 'r.realname';
6fb29165
SG
107}
108
109/**
110 * Implements hook_form_FORM_ID_alter().
111 */
112function privatemsg_realname_form_privatemsg_admin_settings_alter(&$form, &$form_state) {
113 $form['realname'] = array(
114 '#type' => 'fieldset',
115 '#title' => t('Realname integration'),
116 '#collapsed' => TRUE,
117 '#collapsible' => TRUE,
118 '#weight' => 25,
119 );
120
121 $form['realname']['privatemsg_realname_unique_identifier'] = array(
122 '#type' => 'radios',
123 '#title' => t('Field to use as a unique identifier'),
124 '#description' => t('Real names are often not unique. Choose which field should be used as a unique identifier when sending private messages.'),
125 '#default_value' => variable_get('privatemsg_realname_unique_identifier', 'name'),
126 '#options' => array(
127 'name' => t('Username'),
128 'uid' => t('UID'),
129 'mail' => t('E-mail'),
130 ),
131 );
132
133 $form['realname']['privatemsg_realname_search_username'] = array(
134 '#type' => 'checkbox',
135 '#title' => t('Search usernames for autocomplete suggestions'),
136 '#default_value' => variable_get('privatemsg_realname_search_username', TRUE),
137 );
138}