privatemsg_realname.module

Tracking 7.x-2.x branch
  1. drupal
    1. 7 contributions/privatemsg/privatemsg_realname/privatemsg_realname.module

Functions & methods

NameDescription
privatemsg_realname_form_privatemsg_admin_settings_alterImplements hook_form_FORM_ID_alter().
privatemsg_realname_privatemsg_name_lookupImplements hook_privatemsg_name_lookup().
privatemsg_realname_privatemsg_recipient_type_info_alterImplements hook_privatemsg_recipient_info_alter().
privatemsg_realname_query_privatemsg_autocomplete_alterImplements hook_query_privatemsg_autocomplete_alter().
privatemsg_realname_themeImplements hook_theme().
theme_privatemsg_realname_usernameUsed to theme and display user recipients.

File

View source
  1. <?php
  2. /**
  3. * Implements hook_privatemsg_name_lookup().
  4. */
  5. function privatemsg_realname_privatemsg_name_lookup($string) {
  6. // First, check the unique version.
  7. if (preg_match('/\[user:(.+)\]/', $string, $match)) {
  8. $account = user_load_multiple(array(), array(variable_get('privatemsg_realname_unique_identifier', 'name') => trim($match[1])));
  9. $account = reset($account);
  10. if ($account && $account->uid) {
  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 = :realname', array(':realname' => $string));
  18. $accounts = array();
  19. foreach ($result as $row) {
  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. * Implements hook_theme().
  30. */
  31. function privatemsg_realname_theme() {
  32. return array(
  33. 'privatemsg_realname_username' => array(
  34. 'variables' => array('recipient' => NULL, 'options' => array()),
  35. ),
  36. );
  37. }
  38. /**
  39. * Implements hook_query_privatemsg_autocomplete_alter().
  40. */
  41. function privatemsg_realname_query_privatemsg_autocomplete_alter(SelectQueryInterface $query) {
  42. $search = $query->getMetaData('arg_1');
  43. $names = $query->getMetaData('arg_2');
  44. // LEFT JOIN realname table, some users might not have a realname stored in
  45. // there.
  46. $alias = $query->leftJoin('realname', 'r', '%alias.uid = u.uid');
  47. // Either select users where the profile field name and value matches or the username.
  48. // This does replace the default where.
  49. $conditions = &$query->conditions();
  50. foreach ($conditions as $key => $condition) {
  51. // Figure out which is the condition that checks the username.
  52. if ($condition['field'] == 'u.name') {
  53. // Update existing condition.
  54. if (variable_get('privatemsg_realname_search_username', TRUE)) {
  55. $condition['field'] = db_or()
  56. ->condition('r.realname', $search . '%', 'LIKE')
  57. ->condition('u.name', $search . '%', 'LIKE');
  58. //$condition['operator'] = NULL;
  59. $condition['value'] = array();
  60. }
  61. else {
  62. $condition['field'] = 'r.realname';
  63. }
  64. $conditions[$key] = $condition;
  65. }
  66. }
  67. if (!empty($names)) {
  68. // Exclude already existing realnames, but explicitly allow NULL.
  69. // r.realname is left joined and can be NULL => NULL NOT IN (...) => NOT (NULL) => NULL.
  70. $query->condition(db_or()
  71. ->condition($alias . '.realname', $names, 'NOT IN')
  72. ->isNull($alias . '.realname')
  73. );
  74. }
  75. }
  76. /**
  77. * Implements hook_privatemsg_recipient_info_alter().
  78. */
  79. function privatemsg_realname_privatemsg_recipient_type_info_alter(&$recipients) {
  80. // Override format callback.
  81. $recipients['user']['format'] = 'privatemsg_realname_username';
  82. }
  83. /**
  84. * Used to theme and display user recipients.
  85. *
  86. * Wrapper for theme_username() with a few additional options.
  87. */
  88. function theme_privatemsg_realname_username($variables) {
  89. $recipient = $variables['recipient'];
  90. $options = $variables['options'];
  91. if (!isset($recipient->uid)) {
  92. $recipient->uid = $recipient->recipient;
  93. }
  94. if (!empty($options['plain'])) {
  95. $name = check_plain(format_username($recipient));
  96. if (!empty($options['unique'])) {
  97. $identifier = variable_get('privatemsg_realname_unique_identifier', 'name');
  98. $name .= ' [user: ' . $recipient->$identifier . ']';
  99. }
  100. return $name;
  101. }
  102. else {
  103. return theme('username', array('account' => $recipient));
  104. }
  105. }
  106. /**
  107. * Implements hook_form_FORM_ID_alter().
  108. */
  109. function privatemsg_realname_form_privatemsg_admin_settings_alter(&$form, &$form_state) {
  110. $form['realname'] = array(
  111. '#type' => 'fieldset',
  112. '#title' => t('Realname integration'),
  113. '#collapsed' => TRUE,
  114. '#collapsible' => TRUE,
  115. '#weight' => 25,
  116. '#group' => 'settings',
  117. );
  118. $form['realname']['privatemsg_realname_unique_identifier'] = array(
  119. '#type' => 'radios',
  120. '#title' => t('Field to use as a unique identifier'),
  121. '#description' => t('Real names are often not unique. Choose which field should be used as a unique identifier when sending private messages.'),
  122. '#default_value' => variable_get('privatemsg_realname_unique_identifier', 'name'),
  123. '#options' => array(
  124. 'name' => t('Username'),
  125. 'uid' => t('UID'),
  126. 'mail' => t('E-mail'),
  127. ),
  128. );
  129. $form['realname']['privatemsg_realname_search_username'] = array(
  130. '#type' => 'checkbox',
  131. '#title' => t('Search usernames for autocomplete suggestions'),
  132. '#default_value' => variable_get('privatemsg_realname_search_username', TRUE),
  133. );
  134. }