realname.module 6.36 KB
Newer Older
1 2 3 4 5
<?php
// $Id$

/**
 * @file
6
 * The RealName module allows the admin to choose fields from the user profile
7 8 9
 * that will be used to add a "realname" element (method) to a user object.
 * Hook_user is used to automatically add this to any user object that is loaded.
 *
10
 * @copyright Copyright (c) 2007-2008 Nancy Wichmann.  All rights reserved.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
 */

//********************************************************************
//* Drupal Hooks
//********************************************************************/

/**
 * Implementation of hook_menu().
 */
function realname_menu($may_cache) {
  global $user;
  $items = array();

  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/user/realname',
      'title' => t('RealName'),
      'description' => t("Configure which fields are used to create a user's RealName."),
      'access' => user_access('administer users'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array('realname_admin_settings'),
      );
  }
34 35 36 37 38 39
  else {
    // If desired, load the theme override file.
    if (variable_get('realname_theme', false)) {
      include_once(drupal_get_path('module', 'realname') .'/realname_theme.inc');
    }
  }
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
  return $items;
}

/**
 * Implementation of hook_user().
 */
function realname_user($op, &$edit, &$account, $category = null) {
  if ($op == 'load') {
    $account->realname = realname_make_name($account);
  }
}

/**
 * Implementation of hook_help().
 */
function realname_help($path, $args = null) {
  switch ($path) {
    case 'admin/user/realname':
      return t('This page displays the status of and settings for the RealName module.');

    case 'admin/help#realname':
      return t('This simple module adds a "realname" element (method) to a user object when that object is loaded.');
  }
}

65

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
//********************************************************************
//* Module Functions
//********************************************************************

/**
 * Using selected fields, build the "real name" field in the object.
 *
 * @param
 *   $account - the user object to update.
 *
 * @return
 *   The constructed "real name" string.
 */
function realname_make_name(&$account) {
  $fields = variable_get('realname_fields', array());
  $sep = variable_get('realname_sep', ' ');

  // Has the profile been loaded?
  if (!isset($account->{$fields[0]['name']})) {
    profile_load_profile($account);
  }

  $stuff = array();
  foreach ($fields as $name => $weight) {
    if (isset($account->$name)) {
      $stuff[] = $account->$name;
    } 
  }

95 96 97 98 99
  // If no fields set, use username.
  if (count($stuff) == 0) {
    $stuff[] = $account->name;
  }

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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
  // TODO: Make a pattern, rather than hard separator.
  return implode($sep, $stuff);
}

/**
 * Provides array sorting function for uasort.
 *  @link http://us2.php.net/manual/en/function.uasort.php PHP Manual @end-link
 *
 * @param
 *   $a - the first array to be compared.
 *
 * @param
 *   $b - the second array to be compared.
 *
 * @return
 *   integer indicating ordering.
 */
function _realname_sort($a, $b) {
  // Sort first by weight.
  $ret = $a['weight'] - $b['weight'];
  if ($ret == 0) {
    // The two are equal, so use the title.
    $ret = strcmp($a['title'], $b['title']);
  }

  return $ret;   
}

/**
 * Displays the admin settings form.
 */
function realname_admin_settings() {
  $form = $fields = array();
  $current =   variable_get('realname_fields', array());

  $result = db_query("SELECT * FROM {profile_fields} WHERE type='textfield'");
  while ($field = db_fetch_array($result)) {
    $name = $field['name'];
    $selected = array_key_exists($name, $current);
    $fields[$name] = array(
      'title' => $field['title'],
      'weight' => $selected ? $current[$name] : 0,
      'selected' => $selected,
      );
  }
  uasort($fields, '_realname_sort');

  $form['start_table'] = array(
    '#type' => 'markup',
    '#value' => '<table><tr><th>Select</th><th>Field name</th><th>Weight</th></tr>',
    );

  $i = 0;
  foreach ($fields as $f_name => $values) {
    $form['field_select_'. $i] = array(
      '#type' => 'checkbox',
      '#default_value' => $values['selected'],
      '#prefix' => '<tr><td align="center">',
      '#suffix' => '</td>',
      );

    $form['field_name_'. $i] = array(
      '#type' => 'hidden',
      '#value' => $f_name,
      );

    $form['field_title_'. $i] = array(
      '#type' => 'item',
      '#value' => $values['title'],
      '#prefix' => '<td>',
      '#suffix' => '</td>',
      );

    $form['field_weight_'. $i] = array(
      '#type' => 'weight',
      '#delta' => 10,
      '#default_value' => $values['weight'],
      '#prefix' => '<td>',
      '#suffix' => '</td></tr>',
      );

    ++$i;
  }

  $form['end_table'] = array(
    '#type' => 'markup',
    '#value' => '</table>',
    );

  $form['realname_sep'] = array(
    '#type' => 'textfield',
    '#field_prefix' => '<strong>'. t('Separator') .'</strong> ',
    '#description' => t('This will be used between the fields to create the "Real name." The default is a single blank.'),
    '#size' => 10,
    '#default_value' => variable_get('realname_sep', ' '),
    );

197 198 199 200 201 202 203
  $form['realname_theme'] = array(
    '#type' => 'checkbox',
    '#title' => '<strong>'. t('Override username theme') .'</strong>',
    '#description' => t('If this option is selected, the standard username theme function will be overriden to use the "Real name."'),
    '#default_value' => variable_get('realname_theme', false),
    );

204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
    );

  return $form;
}

/**
 * Form submit handler.
 */
function realname_admin_settings_submit($form_id, $form_values) {
  $i = 0;
  $fields = array();
  // Run the form values to get all the fields they want.
  while (isset($form_values['field_select_'. $i])) {
    if ($form_values['field_select_'. $i]) {
      $fields[] = array('title' => $form_values['field_name_'. $i], 'weight' => $form_values['field_weight_'. $i]);
    }
    ++$i;
  }
  // A little hoop jumping to sort right.
  uasort($fields, '_realname_sort');
  $realname_fields = array();
  foreach ($fields as $key => $values) {
    $realname_fields[$values['title']] = $values['weight'];
  }

  // Okay, save the stuff.
  variable_set('realname_fields', $realname_fields);
  variable_set('realname_sep', $form_values['realname_sep']);
235 236 237
  variable_set('realname_theme', $form_values['realname_theme']);

  drupal_set_message(t('Configuration has been updated.'), 'status');
238
}