| 1 |
<?php
|
| 2 |
// $Id: realname.module,v 1.3 2008/06/04 14:15:13 nancyw Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* The RealName module allows the admin to choose fields from the user profile
|
| 7 |
* that will be used to add a "realname" element (method) to a user object.
|
| 8 |
* Hook_user is used to automatically add this to any user object that is loaded.
|
| 9 |
*
|
| 10 |
* @copyright Copyright (c) 2007-2008 Nancy Wichmann. All rights reserved.
|
| 11 |
*/
|
| 12 |
|
| 13 |
//********************************************************************
|
| 14 |
//* Drupal Hooks
|
| 15 |
//********************************************************************/
|
| 16 |
|
| 17 |
/**
|
| 18 |
* Implementation of hook_help().
|
| 19 |
*/
|
| 20 |
function realname_help($path, $args = null) {
|
| 21 |
switch ($path) {
|
| 22 |
case 'admin/user/realname':
|
| 23 |
return t('This page displays the status of and settings for the RealName module.');
|
| 24 |
|
| 25 |
case 'admin/help#realname':
|
| 26 |
return t('This simple module adds a "realname" element (method) to a user object when that object is loaded.');
|
| 27 |
}
|
| 28 |
}
|
| 29 |
|
| 30 |
/**
|
| 31 |
* Implementation of hook_menu().
|
| 32 |
*/
|
| 33 |
function realname_menu($may_cache) {
|
| 34 |
global $user;
|
| 35 |
$items = array();
|
| 36 |
|
| 37 |
if ($may_cache) {
|
| 38 |
$items[] = array(
|
| 39 |
'path' => 'admin/user/realname',
|
| 40 |
'title' => t('RealName'),
|
| 41 |
'description' => t("Configure which fields are used to create a user's RealName."),
|
| 42 |
'access' => user_access('administer users'),
|
| 43 |
'callback' => 'drupal_get_form',
|
| 44 |
'callback arguments' => array('realname_admin_settings'),
|
| 45 |
);
|
| 46 |
}
|
| 47 |
else {
|
| 48 |
// If desired, load the theme override file.
|
| 49 |
if (variable_get('realname_theme', false)) {
|
| 50 |
include_once(drupal_get_path('module', 'realname') .'/realname_theme.inc');
|
| 51 |
}
|
| 52 |
}
|
| 53 |
return $items;
|
| 54 |
}
|
| 55 |
|
| 56 |
/**
|
| 57 |
* Implementation of hook_user().
|
| 58 |
*/
|
| 59 |
function realname_user($op, &$edit, &$account, $category = null) {
|
| 60 |
if ($op == 'load') {
|
| 61 |
$account->realname = realname_make_name($account);
|
| 62 |
}
|
| 63 |
}
|
| 64 |
|
| 65 |
/**
|
| 66 |
* Implementation of hook_nodeapi().
|
| 67 |
*/
|
| 68 |
function realname_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
|
| 69 |
if (!variable_get('realname_nodeapi', false)) {
|
| 70 |
return;
|
| 71 |
}
|
| 72 |
switch ($op) {
|
| 73 |
case 'load':
|
| 74 |
// Node is being loaded.
|
| 75 |
// Save the username that is already there.
|
| 76 |
$node->realname_save = $node->name;
|
| 77 |
$account = user_load(array('uid' => $node->uid));
|
| 78 |
$node->realname = $node->name = realname_make_name($account);
|
| 79 |
break;
|
| 80 |
|
| 81 |
case 'prepare':
|
| 82 |
// Node is about to be edited.
|
| 83 |
// Reset the username or save will fail.
|
| 84 |
if (isset($node->realname_save)) {
|
| 85 |
$node->name = $node->realname_save;
|
| 86 |
}
|
| 87 |
break;
|
| 88 |
}
|
| 89 |
}
|
| 90 |
|
| 91 |
//********************************************************************
|
| 92 |
//* Module Functions
|
| 93 |
//********************************************************************
|
| 94 |
|
| 95 |
/**
|
| 96 |
* Using selected fields, build the "real name" field in the object.
|
| 97 |
*
|
| 98 |
* @param
|
| 99 |
* $account - the user object to update.
|
| 100 |
*
|
| 101 |
* @return
|
| 102 |
* The constructed "real name" string.
|
| 103 |
*/
|
| 104 |
function realname_make_name(&$account) {
|
| 105 |
$fields = variable_get('realname_fields', array());
|
| 106 |
// $sep = variable_get('realname_sep', ' ');
|
| 107 |
$pattern = variable_get('realname_pattern', ' ');
|
| 108 |
|
| 109 |
// Has the profile been loaded?
|
| 110 |
if (!isset($account->{$fields[0]['name']})) {
|
| 111 |
profile_load_profile($account);
|
| 112 |
}
|
| 113 |
|
| 114 |
$stuff = array();
|
| 115 |
$i = 0;
|
| 116 |
foreach ($fields as $name => $weight) {
|
| 117 |
++$i;
|
| 118 |
if (isset($account->$name)) {
|
| 119 |
$stuff['%'. $i] = check_plain($account->$name);
|
| 120 |
}
|
| 121 |
else {
|
| 122 |
// If there is no value, remove the patterm piece, except the first.
|
| 123 |
$pattern = $i > 1 ? str_replace('%'. $i, null, $pattern) : $pattern;
|
| 124 |
}
|
| 125 |
}
|
| 126 |
|
| 127 |
// If no fields set, use username.
|
| 128 |
if (count($stuff) == 0) {
|
| 129 |
$stuff['%1'] = $account->name;
|
| 130 |
}
|
| 131 |
|
| 132 |
// TODO: Make a pattern, rather than hard separator.
|
| 133 |
$string = trim(strtr($pattern, $stuff));
|
| 134 |
return $string;
|
| 135 |
}
|
| 136 |
|
| 137 |
/**
|
| 138 |
* Provides array sorting function for uasort.
|
| 139 |
* @link http://us2.php.net/manual/en/function.uasort.php PHP Manual @end-link
|
| 140 |
*
|
| 141 |
* @param
|
| 142 |
* $a - the first array to be compared.
|
| 143 |
*
|
| 144 |
* @param
|
| 145 |
* $b - the second array to be compared.
|
| 146 |
*
|
| 147 |
* @return
|
| 148 |
* integer indicating ordering.
|
| 149 |
*/
|
| 150 |
function _realname_sort($a, $b) {
|
| 151 |
// Sort first by weight.
|
| 152 |
$ret = $a['weight'] - $b['weight'];
|
| 153 |
if ($ret == 0) {
|
| 154 |
// The two are equal, so use the title.
|
| 155 |
$ret = strcmp($a['title'], $b['title']);
|
| 156 |
}
|
| 157 |
|
| 158 |
return $ret;
|
| 159 |
}
|
| 160 |
|
| 161 |
/**
|
| 162 |
* Displays the admin settings form.
|
| 163 |
*/
|
| 164 |
function realname_admin_settings() {
|
| 165 |
$form = $fields = array();
|
| 166 |
$current = variable_get('realname_fields', array());
|
| 167 |
|
| 168 |
$result = db_query("SELECT * FROM {profile_fields} WHERE type='textfield'");
|
| 169 |
while ($field = db_fetch_array($result)) {
|
| 170 |
$name = $field['name'];
|
| 171 |
$selected = array_key_exists($name, $current);
|
| 172 |
$fields[$name] = array(
|
| 173 |
'title' => $field['title'],
|
| 174 |
'weight' => $selected ? $current[$name] : 0,
|
| 175 |
'selected' => $selected,
|
| 176 |
);
|
| 177 |
}
|
| 178 |
uasort($fields, '_realname_sort');
|
| 179 |
|
| 180 |
$form['start_table'] = array(
|
| 181 |
'#type' => 'markup',
|
| 182 |
'#value' => '<table><tr><th>Select</th><th>Field name</th><th>Weight</th></tr>',
|
| 183 |
);
|
| 184 |
|
| 185 |
$i = 0;
|
| 186 |
foreach ($fields as $f_name => $values) {
|
| 187 |
$form['field_select_'. $i] = array(
|
| 188 |
'#type' => 'checkbox',
|
| 189 |
'#default_value' => $values['selected'],
|
| 190 |
'#prefix' => '<tr><td align="center">',
|
| 191 |
'#suffix' => '</td>',
|
| 192 |
);
|
| 193 |
|
| 194 |
$form['field_name_'. $i] = array(
|
| 195 |
'#type' => 'hidden',
|
| 196 |
'#value' => $f_name,
|
| 197 |
);
|
| 198 |
|
| 199 |
$form['field_title_'. $i] = array(
|
| 200 |
'#type' => 'item',
|
| 201 |
'#value' => $values['title'],
|
| 202 |
'#prefix' => '<td>',
|
| 203 |
'#suffix' => '</td>',
|
| 204 |
);
|
| 205 |
|
| 206 |
$form['field_weight_'. $i] = array(
|
| 207 |
'#type' => 'weight',
|
| 208 |
'#delta' => 10,
|
| 209 |
'#default_value' => $values['weight'],
|
| 210 |
'#prefix' => '<td>',
|
| 211 |
'#suffix' => '</td></tr>',
|
| 212 |
);
|
| 213 |
|
| 214 |
++$i;
|
| 215 |
}
|
| 216 |
|
| 217 |
$form['end_table'] = array(
|
| 218 |
'#type' => 'markup',
|
| 219 |
'#value' => '</table>',
|
| 220 |
);
|
| 221 |
|
| 222 |
$form['realname_pattern'] = array(
|
| 223 |
'#type' => 'textfield',
|
| 224 |
'#field_prefix' => '<strong>'. t('Name Pattern') .'</strong> ',
|
| 225 |
'#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..'),
|
| 226 |
'#size' => 30,
|
| 227 |
'#default_value' => variable_get('realname_pattern', '%1'),
|
| 228 |
);
|
| 229 |
|
| 230 |
$form['realname_theme'] = array(
|
| 231 |
'#type' => 'checkbox',
|
| 232 |
'#title' => '<strong>'. t('Override username theme') .'</strong>',
|
| 233 |
'#description' => t('If this option is selected, the standard username theme function will be overriden to use the "Real name."'),
|
| 234 |
'#default_value' => variable_get('realname_theme', false),
|
| 235 |
);
|
| 236 |
|
| 237 |
$form['realname_nodeapi'] = array(
|
| 238 |
'#type' => 'checkbox',
|
| 239 |
'#title' => '<strong>'. t('Show realname in nodes') .'</strong>',
|
| 240 |
'#description' => t('If this option is selected, the "Real name" will be used on node displays.'),
|
| 241 |
'#default_value' => variable_get('realname_nodeapi', false),
|
| 242 |
);
|
| 243 |
|
| 244 |
$form['submit'] = array(
|
| 245 |
'#type' => 'submit',
|
| 246 |
'#value' => t('Save'),
|
| 247 |
);
|
| 248 |
|
| 249 |
return $form;
|
| 250 |
}
|
| 251 |
|
| 252 |
/**
|
| 253 |
* Form submit handler.
|
| 254 |
*/
|
| 255 |
function realname_admin_settings_submit($form_id, $form_values) {
|
| 256 |
$i = 0;
|
| 257 |
$fields = array();
|
| 258 |
// Run the form values to get all the fields they want.
|
| 259 |
while (isset($form_values['field_select_'. $i])) {
|
| 260 |
if ($form_values['field_select_'. $i]) {
|
| 261 |
$fields[] = array('title' => $form_values['field_name_'. $i], 'weight' => $form_values['field_weight_'. $i]);
|
| 262 |
}
|
| 263 |
++$i;
|
| 264 |
}
|
| 265 |
// A little hoop jumping to sort right.
|
| 266 |
uasort($fields, '_realname_sort');
|
| 267 |
$realname_fields = array();
|
| 268 |
foreach ($fields as $key => $values) {
|
| 269 |
$realname_fields[$values['title']] = $values['weight'];
|
| 270 |
}
|
| 271 |
|
| 272 |
// Okay, save the stuff.
|
| 273 |
variable_set('realname_fields', $realname_fields);
|
| 274 |
// variable_set('realname_sep', $form_values['realname_sep']);
|
| 275 |
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 |
}
|