realname.module 7.82 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
 */

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

17
18
19
20
21
22
23
24
25
26
27
28
29
/**
 * 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.');
  }
}

30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
 * 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'),
      );
  }
47
48
49
50
51
52
  else {
    // If desired, load the theme override file.
    if (variable_get('realname_theme', false)) {
      include_once(drupal_get_path('module', 'realname') .'/realname_theme.inc');
    }
  }
53
54
55
56
57
58
59
60
61
62
63
64
65
  return $items;
}

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

/**
66
 * Implementation of hook_nodeapi().
67
 */
68
69
70
71
72
73
74
75
76
77
function realname_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  if (!variable_get('realname_nodeapi', false)) {
    return;
  }
  switch ($op) {
    case 'load':
      // Node is being loaded.
      // Save the username that is already there.
      $node->realname_save = $node->name;
      $account = user_load(array('uid' => $node->uid));
78
      $node->realname = $node->name = realname_make_name($account);
79
80
81
82
83
84
      break;

    case 'prepare':
      // Node is about to be edited.
      // Reset the username or save will fail.
      if (isset($node->realname_save)) {
85
        $node->name = $node->realname_save;
86
87
      }
      break;
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
  }
}

//********************************************************************
//* 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());
106
107
//  $sep = variable_get('realname_sep', ' ');
  $pattern = variable_get('realname_pattern', ' ');
108
109
110
111
112
113
114

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

  $stuff = array();
115
  $i = 0;
116
  foreach ($fields as $name => $weight) {
117
    ++$i;
118
    if (isset($account->$name)) {
119
120
121
122
123
124
      $stuff['%'. $i] = check_plain($account->$name);
    }
    else {
      // If there is no value, remove the patterm piece, except the first.
      $pattern = $i > 1 ? str_replace('%'. $i, null, $pattern) : $pattern;
    }
125
126
  }

127
128
  // If no fields set, use username.
  if (count($stuff) == 0) {
129
    $stuff['%1'] = $account->name;
130
131
  }

132
  // TODO: Make a pattern, rather than hard separator.
133
134
  $string = trim(strtr($pattern, $stuff));
  return $string;
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
}

/**
 * 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>',
    );

222
  $form['realname_pattern'] = array(
223
    '#type' => 'textfield',
224
225
226
227
    '#field_prefix' => '<strong>'. t('Name Pattern') .'</strong> ',
    '#description' => t('The determines how the fields will be used to create the "Real name." Use "%1" to refer to the first field, "%2" to refer to the second, etc..'),
    '#size' => 30,
    '#default_value' => variable_get('realname_pattern', '%1'),
228
229
    );

230
231
232
233
234
235
236
  $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),
    );

237
238
239
240
241
242
243
  $form['realname_nodeapi'] = array(
    '#type' => 'checkbox',
    '#title' => '<strong>'. t('Show realname in nodes') .'</strong>',
    '#description' => t('If this option is selected, the "Real name" will be used on node displays.'),
    '#default_value' => variable_get('realname_nodeapi', false),
    );

244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
  $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);
274
275
//  variable_set('realname_sep', $form_values['realname_sep']);
  variable_set('realname_pattern', $form_values['realname_pattern']);
276
  variable_set('realname_theme', $form_values['realname_theme']);
277
  variable_set('realname_nodeapi', $form_values['realname_nodeapi']);
278
279

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