realname.module 4.19 KB
Newer Older
1
<?php
2 3 4 5 6

// @todo Alter the username autocomplete
// @todo Allow users to login with their real name
// @todo Disable the username field
// @todo Allow users to override their real name
7 8

/**
9
 * Implements hook_permission().
10
 */
11 12 13 14 15 16
function realname_permission() {
  $permissions['administer realname'] = array(
    'title' => t('Administer Real Name configuration.'),
  );
  return $permissions;
}
17

18
/**
19
 * Implements hook_menu().
20
 */
21 22 23 24 25 26 27 28
function realname_menu() {
  $items['admin/config/people/realname'] = array(
    'title' => 'Real name',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('realname_settings_form'),
    'access arguments' => array('administer realname'),
    'file' => 'realname.admin.inc',
  );
29

30
  return $items;
31 32
}

33
/**
34
 * Implements hook_username_alter().
35
 */
36 37
function realname_username_alter(&$name, stdClass $account) {
  static $in_username_alter = FALSE;
38

39 40
  if (isset($account->realname)) {
    $name = $account->realname;
41
  }
42 43 44 45 46 47
  elseif (!$in_username_alter) {
    // Because realname tokens may execute format_username() we need to prevent
    // recursion from happening.
    $in_username_alter = TRUE;
    if ($realname = realname_load($account)) {
      $name = $realname;
48
    }
49
    $in_username_alter = FALSE;
50
  }
51 52 53
}

/**
54
 * Implements hook_user_load().
55
 */
56 57 58 59
function realname_user_load(array $accounts) {
  $realnames = realname_load_multiple($accounts);
  foreach ($realnames as $uid => $realname) {
    $accounts[$uid]->realname = $realname;
60 61 62 63
  }
}

/**
64
 * Implements hook_user_update().
65
 */
66 67 68
function realname_user_update(array &$edit, stdClass $account, $category) {
  // Since user data may have changed, delete the existing realname.
  realname_delete($account->uid);
69 70
}

71 72 73 74 75 76
/**
 * Implements hook_user_delete().
 */
function realname_user_delete(stdClass $account) {
  realname_delete($account->uid);
}
77 78

/**
79
 * Loads a real name.
80
 *
81 82
 * @param $uid
 *   A user account object.
83
 * @return
84
 *   The user's generated real name.
85
 */
86 87 88 89
function realname_load(stdClass $account) {
  $realnames = realname_load_multiple(array($account->uid => $account));
  return reset($realnames);
}
90

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
/**
 * Loads multiple real names.
 *
 * @param $accounts
 *   An array of user account objects keyed by user ID.
 * @return
 *   An array of real names keyed by user ID.
 */
function realname_load_multiple(array $accounts) {
  $realnames = &drupal_static(__FUNCTION__, array());

  if ($new_accounts = array_diff_key($accounts, $realnames)) {
    // Attempt to fetch realnames from the database first.
    $realnames += db_query("SELECT uid, realname FROM {realname} WHERE uid IN (:uids)", array(':uids' => array_keys($new_accounts)))->fetchAllKeyed();

    // For each account that was not present in the database, generate its
    // real name.
    foreach ($new_accounts as $uid => $account) {
      if (!isset($realnames[$uid]) || !drupal_strlen($realnames[$uid])) {
        // Get the default pattern and allow other modules to alter it.
        $pattern = variable_get('realname_pattern', '[user:name-raw]');
        drupal_alter('realname_pattern', $pattern, $account);

        // Perform token replacement on the real name pattern.
        $realname = token_replace($pattern, array('user' => $account), array('clear' => TRUE));
        $realname = trim($realname);
        module_invoke_all('realname_load', $realname, $account);

        // Save to the database and the static cache.
        db_merge('realname')
          ->key(array('uid' => $uid))
          ->fields(array(
            'realname' => $realname,
            'created' => REQUEST_TIME,
          ))
          ->execute();
        $realnames[$uid] = $realname;
      }
129
    }
130

131 132
  }

133
  return array_intersect_key($realnames, $accounts);
134 135 136
}

/**
137
 * Delete a real name.
138
 *
139 140
 * @param $uid
 *   A user ID.
141
 */
142 143
function realname_delete($uid) {
  return realname_delete_multiple(array($uid));
144 145 146
}

/**
147 148 149 150
 * Delete multiple real names.
 *
 * @param $uids
 *   An array of user IDs.
151
 */
152 153 154
function realname_delete_multiple(array $uids) {
  db_delete('realname')->condition('uid', $uids, 'IN')->execute();
  drupal_static_reset('realname_load_multiple');
155 156 157
}

/**
158
 * Delete all real names.
159
 */
160 161 162
function realname_delete_all() {
  db_delete('realname')->execute();
  drupal_static_reset('realname_load_multiple');
163
}