Projet

Général

Profil

Paste
Télécharger (4,69 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / privatemsg / privatemsg_realname / privatemsg_realname.module @ e3063c4a

1
<?php
2

    
3
/**
4
 * Implements hook_privatemsg_name_lookup().
5
 */
6
function privatemsg_realname_privatemsg_name_lookup($string) {
7

    
8
  // First, check the unique version.
9
  if (preg_match('/\[user:(.+)\]/', $string, $match)) {
10
    $account = user_load_multiple(array(), array(variable_get('privatemsg_realname_unique_identifier', 'name') => trim($match[1])));
11
    $account = reset($account);
12
    if ($account && $account->uid) {
13
      $account->type = 'user';
14
      $account->recipient = $account->uid;
15
      return array($account);
16
    }
17
  }
18

    
19
  // Then try to look it up with the real name.
20
  $result = db_query('SELECT r.uid FROM {realname} r WHERE r.realname = :realname', array(':realname' => $string));
21
  $accounts = array();
22
  foreach ($result as $row) {
23
    if ($account = user_load($row->uid)) {
24
      $account->type = 'user';
25
      $account->recipient = $account->uid;
26
      $accounts[] = $account;
27
    }
28
  }
29
  return $accounts;
30
}
31

    
32
/**
33
 * Implements hook_theme().
34
 */
35
function privatemsg_realname_theme() {
36
  return array(
37
    'privatemsg_realname_username'  => array(
38
      'variables' => array('recipient' => NULL, 'options' => array()),
39
    ),
40
  );
41
}
42

    
43
/**
44
 * Implements hook_query_privatemsg_autocomplete_alter().
45
 */
46
function privatemsg_realname_query_privatemsg_autocomplete_alter(SelectQueryInterface $query) {
47
  $search = $query->getMetaData('arg_1');
48
  $names = $query->getMetaData('arg_2');
49

    
50
  // LEFT JOIN realname table, some users might not have a realname stored in
51
  // there.
52
  $alias = $query->leftJoin('realname', 'r', '%alias.uid = u.uid');
53

    
54
  // Either select users where the profile field name and value matches or the username.
55
  // This does replace the default where.
56

    
57
  $conditions = &$query->conditions();
58
  foreach ($conditions as $key => $condition) {
59
    // Figure out which is the condition that checks the username.
60
    if (isset($condition['field']) && is_string($condition['field']) && $condition['field'] == 'u.name') {
61
      // Update existing condition.
62
      if (variable_get('privatemsg_realname_search_username', TRUE)) {
63
        $condition['field'] = db_or()
64
          ->condition('r.realname', $search . '%', 'LIKE')
65
          ->condition('u.name', $search . '%', 'LIKE');
66
        //$condition['operator'] = NULL;
67
        $condition['value'] = array();
68
      }
69
      else {
70
        $condition['field'] = 'r.realname';
71
      }
72
      $conditions[$key] = $condition;
73
    }
74
  }
75

    
76
  if (!empty($names)) {
77
    // Exclude already existing realnames, but explicitly allow NULL.
78
    // r.realname is left joined and can be NULL => NULL NOT IN (...) => NOT (NULL) => NULL.
79
    $query->condition(db_or()
80
      ->condition($alias . '.realname', $names, 'NOT IN')
81
      ->isNull($alias . '.realname')
82
    );
83
  }
84
}
85

    
86
/**
87
 * Implements hook_privatemsg_recipient_info_alter().
88
 */
89
function privatemsg_realname_privatemsg_recipient_type_info_alter(&$recipients) {
90
  // Override format callback.
91
  $recipients['user']['format'] = 'privatemsg_realname_username';
92
}
93

    
94
/**
95
 * Used to theme and display user recipients.
96
 *
97
 * Wrapper for theme_username() with a few additional options.
98
 */
99
function theme_privatemsg_realname_username($variables) {
100
  $recipient = $variables['recipient'];
101
  $options = $variables['options'];
102
  if (!isset($recipient->uid)) {
103
    $recipient->uid = $recipient->recipient;
104
  }
105
  if (!empty($options['plain'])) {
106
    $name = strip_tags(format_username($recipient));
107
    if (!empty($options['unique'])) {
108
      $identifier = variable_get('privatemsg_realname_unique_identifier', 'name');
109
      $name .= ' [user: ' . $recipient->$identifier . ']';
110
    }
111
    return $name;
112
  }
113
  else {
114
    return theme('username', array('account' => $recipient));
115
  }
116
}
117

    
118
/**
119
 * Implements hook_form_FORM_ID_alter().
120
 */
121
function privatemsg_realname_form_privatemsg_admin_settings_alter(&$form, &$form_state) {
122
  $form['realname'] = array(
123
    '#type' => 'fieldset',
124
    '#title' => t('Realname integration'),
125
    '#collapsed' => TRUE,
126
    '#collapsible' => TRUE,
127
    '#weight' => 25,
128
    '#group' => 'settings',
129
  );
130

    
131
  $form['realname']['privatemsg_realname_unique_identifier'] = array(
132
    '#type' => 'radios',
133
    '#title' => t('Field to use as a unique identifier'),
134
    '#description' => t('Real names are often not unique. Choose which field should be used as a unique identifier when sending private messages.'),
135
    '#default_value' => variable_get('privatemsg_realname_unique_identifier', 'name'),
136
    '#options' => array(
137
      'name' => t('Username'),
138
      'uid' => t('UID'),
139
      'mail' => t('E-mail'),
140
    ),
141
  );
142

    
143
  $form['realname']['privatemsg_realname_search_username'] = array(
144
    '#type' => 'checkbox',
145
    '#title' => t('Search usernames for autocomplete suggestions'),
146
    '#default_value' => variable_get('privatemsg_realname_search_username', TRUE),
147
  );
148
}