realname_registration.module in Realname registration 6.2
Same filename and directory in other branches
For using real names during registration.
The realname_registration allows usernames to be generated based upon user fields.
File
realname_registration.moduleView source
<?php
/**
* @file
* For using real names during registration.
*
* The realname_registration allows usernames to be generated based upon
* user fields.
*/
/*
* Implementation of hook_enable().
*/
function realname_registration_enable() {
$p = module_exists('profile');
$c = module_exists('content_profile_registration');
// Check if atleast one of the dependent modules are enabled.
if ($p || $c) {
$success = t('Realname registration has been successfully enabled.');
$success .= ' ';
$success .= t('Please proceed to the <a href="../user/realname_registration">module configuration page</a>.');
drupal_set_message($success, 'status');
}
else {
$error = t('Realname registration requires that you enable the Profile module or Content profile registration.') . ' ';
$error .= t('Please enable one of the depedent modules before re-enabling Realname registration');
drupal_set_message($error, 'error');
module_disable(array(
0 => 'realname_registration',
));
}
}
/**
* Implementation of hook_help().
*/
function realname_registration_help($path, $arg) {
switch ($path) {
case 'admin/help#realname_registration':
$output .= '<h4>' . t('Summary') . '</h4>';
$output .= '<p>' . t('Usernames are made of a combination of the first name, middle name, and last name field. The values will be stored in your provided user fields.') . '</p>';
$output .= '<h4>' . t('Settings') . '</h4>';
$output .= '<dl>';
$output .= '<dt><strong>' . t('Using the core Profile module') . '</strong></dt>';
$output .= '<dd>' . t('(If you already have these fields, take note of the names and skip this step)') . '</dd>';
$output .= '<dd>' . t('Navigate to <a href="../user/profile">admin/user/profile</a> and create a text field that will hold the users first name.') . ' ';
$output .= t('While editing the field, ensure that the Required field option is checked, also to Display on the registration form.') . ' ';
$output .= t('Repeat this step for the last name field. The middle name field is optional, and does not need to be required. Be sure to take note of what you have named the new fields.') . '</dd>';
$output .= '<dd><a href="../user/profile">' . t('Manage user fields') . ' →</a></dd>';
if (module_exists('content_profile_registration')) {
$output .= '<dt><strong>' . t('Using the Content profile module') . '</strong></dt>';
$output .= '<dd>' . t('If you have Content profile installed, you will have additional fieldsets on the configuration page.') . ' ';
$output .= t('Create your field names so that the first and last names are required. The middle name field is always optional.') . ' ';
$output .= t('Ensure that your Content profile type is used during registration.') . ' ';
$output .= t('To use the fields in Realname registration, check the box saying use this module for storage and supply the machine name of the corresponding Content profile type.') . '</dd>';
$output .= '<dd><a href="../content/types">' . t('Manage Content types') . ' →</a></dd>';
}
$output .= '</dl>';
$output .= '<dl>';
$output .= '<dt><strong>' . t('Configure the Realname registration module') . '</strong></dt>';
$output .= '<dd>' . t('Now that you have first name, middle name, and last name fields associated with your users, you can map these fields in Realname registration.') . ' ';
$output .= t('Type the field names that you have created in the appropriate textfields, and select your other options and click Submit.') . '</dd>';
$output .= '<dd><a href="../user/realname_registration">' . t('Configure Realname registration') . ' →</a></dd>';
$output .= '</dl>';
$output .= '<p><strong>' . t('Project resources') . '</strong></p>';
$output .= '<ul>';
$output .= '<li><a href="http://drupal.org/project/realname_registration">' . t('Project home') . '</a></li>';
$output .= '<li><a href="http://drupal.org/node/1344864">' . t('Documentation') . '</a></li>';
$output .= '<li><a href="http://drupal.org/project/issues/realname_registration?status=All&categories=All">' . t('Issue tracker') . '</a></li>';
$output .= '</ul>';
return $output;
}
}
/**
* Implementation of hook_form_alter().
*/
function realname_registration_form_alter(&$form, $form_state, $form_id) {
if (!($form_id == 'user_register') || user_access('override realname registration')) {
return;
}
// First, we must hide the Username field from the registration form.
if (isset($form['account']) && is_array($form['account'])) {
$form['account']['name']['#type'] = 'hidden';
$form['account']['name']['#value'] = 'unset_username';
}
else {
$form['name']['#type'] = 'hidden';
$form['name']['#value'] = 'unset_username';
}
// We must ensure that the username will be accepted by Drupal core.
$form['#validate'][] = 'realname_registration_mandatory_validate';
// If regex validation is enabled, add the validation handler.
if (variable_get('realname_registration_use_validation', '')) {
$form['#validate'][] = 'realname_registration_validate';
}
}
/**
* Implementation of hook_user().
*/
function realname_registration_user($op, &$edit, &$account, $category = NULL) {
if ($op == 'validate') {
if (isset($edit['name']) && $edit['name'] == 'unset_username') {
if (variable_get('realname_registration_ucfirst', 0)) {
realname_registration_ucfirst($edit);
}
realname_registration_set_username($edit);
}
}
}
/**
* realname_registration_validate() is an optional field validator for
* for real names.
*
* @see user_register_form()
* @see user_register_submit()
*/
function realname_registration_validate($form, &$form_state) {
$c = realname_registration_load_settings();
// A first name may not contain symbols or numbers.
$fname = $c->fname->use_content_profile ? $form_state['content_profile_registration'][$c->fname->content_node]['node']->{$c->fname->field}[0]['value'] : $form_state['values'][$c->fname->field];
if (!preg_match("/^[-\\pL']*\$/u", $fname)) {
form_set_error($c->fname->field, t('First name may not contain symbols or numbers.'));
}
// A middle name may not contain symbols or numbers. This field is optional.
if ($c->mname->field) {
$mname = $c->mname->use_content_profile ? $form_state['content_profile_registration'][$c->mname->content_node]['node']->{$c->mname->field}[0]['value'] : $form_state['values'][$c->mname->field];
if (!preg_match("/^[-\\pL']*\$/u", $mname)) {
form_set_error($c->mname->field, t('Middle name may not contain symbols or numbers.'));
}
}
// A last name may not contain symbols or numbers.
$lname = $c->lname->use_content_profile ? $form_state['content_profile_registration'][$c->lname->content_node]['node']->{$c->lname->field}[0]['value'] : $form_state['values'][$c->lname->field];
if (!preg_match("/^[-\\pL']*\$/u", $lname)) {
form_set_error($c->lname->field, t('Last name may not contain symbols or numbers.'));
}
}
/**
* realname_registration_mandatory_validate() is a required field validator
* to ensure that field input will create a valid username with Realname
* registration username formats.
*
* @see user_register_form()
* @see user_register_submit()
*/
function realname_registration_mandatory_validate($form, &$form_state) {
$c = realname_registration_load_settings();
// A first name may not contain symbols or numbers.
$fname = $c->fname->use_content_profile ? $form_state['content_profile_registration'][$c->fname->content_node]['node']->{$c->fname->field}[0]['value'] : $form_state['values'][$c->fname->field];
if ($err = user_validate_name($fname)) {
form_set_error($c->fname->field, $err);
drupal_set_message(t('Please supply a valid first name.'), 'error');
}
// A middle name may not contain symbols or numbers. This field is optional.
if ($c->mname->field) {
$mname = $c->mname->use_content_profile ? $form_state['content_profile_registration'][$c->mname->content_node]['node']->{$c->mname->field}[0]['value'] : $form_state['values'][$c->mname->field];
if ($err = user_validate_name($mname)) {
form_set_error($c->mname->field, $err);
drupal_set_message(t('Please supply a valid middle name.'), 'error');
}
}
// A last name may not contain symbols or numbers.
$lname = $c->lname->use_content_profile ? $form_state['content_profile_registration'][$c->lname->content_node]['node']->{$c->lname->field}[0]['value'] : $form_state['values'][$c->lname->field];
if ($err = user_validate_name($lname)) {
form_set_error($c->lname->field, $err);
drupal_set_message(t('Please supply a valid last name.'), 'error');
}
}
/**
* Implementation of hook_menu().
*/
function realname_registration_menu() {
$items = array();
$items['admin/user/realname_registration'] = array(
'title' => t('Realname registration'),
'description' => t('Realname registration settings'),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'realname_registration_settings_form',
),
'access arguments' => array(
'administer realname registration',
),
'type' => MENU_NORMAL_ITEM,
'weight' => 10,
'file' => 'realname_registration.admin.inc',
);
return $items;
}
/**
* Build the username based on form values submitted by the user.
*
* Construct the usernames name adhering on the pre-defined formats set in
* Realname registrations administrative settings.
*
* @param &$edit
* The array of form values submitted by the user.
*
*/
function realname_registration_set_username(&$edit) {
$c = realname_registration_load_settings();
// Generate the username based on first name and last name fields.
switch ($c->username_format) {
case 0:
/*
* Create username based on first name and last name (e.g., John Smith).
*/
if (!$c->fname->use_content_profile) {
$username = $edit[$c->fname->field];
}
else {
$username = $edit[$c->fname->field][0]['value'];
}
$username .= ' ';
if (!$c->lname->use_content_profile) {
$username .= $edit[$c->lname->field];
}
else {
$username .= $edit[$c->lname->field][0]['value'];
}
break;
case 1:
/*
* Create username based on first initial and last name (e.g., JSmith).
*/
if (!$c->fname->use_content_profile) {
$username = drupal_substr($edit[$c->fname->field], 0, 1);
}
else {
$username = drupal_substr($edit[$c->fname->field][0]['value'], 0, 1);
}
if (!$c->lname->use_content_profile) {
$username .= $edit[$c->lname->field];
}
else {
$username .= $edit[$c->lname->field][0]['value'];
}
break;
case 2:
/*
* Create username based on first name, middle name, and last name
* (e.g., John Jacob Smith).
*/
if (!$c->fname->use_content_profile) {
$username = $edit[$c->fname->field];
}
else {
$username = $edit[$c->fname->field][0]['value'];
}
$username .= ' ';
if (!$c->mname->use_content_profile) {
if (!empty($edit[$c->mname->field])) {
$username .= $edit[$c->mname->field];
$username .= ' ';
}
}
else {
if (!empty($edit[$c->mname->field][0]['value'])) {
$username .= $edit[$c->mname->field][0]['value'];
$username .= ' ';
}
}
if (!$c->lname->use_content_profile) {
$username .= $edit[$c->lname->field];
}
else {
$username .= $edit[$c->lname->field][0]['value'];
}
break;
case 3:
/*
* Create username based on first name, middle initial, and last name
* (e.g., John J Smith).
*/
if (!$c->fname->use_content_profile) {
$username = $edit[$c->fname->field];
}
else {
$username = $edit[$c->fname->field][0]['value'];
}
$username .= ' ';
if (!$c->mname->use_content_profile) {
if (!empty($edit[$c->mname->field])) {
$username .= drupal_substr($edit[$c->mname->field], 0, 1);
$username .= ' ';
}
}
else {
if (!empty($edit[$c->mname->field][0]['value'])) {
$username .= drupal_substr($edit[$c->mname->field][0]['value'], 0, 1);
$username .= ' ';
}
}
if (!$c->lname->use_content_profile) {
$username .= $edit[$c->lname->field];
}
else {
$username .= $edit[$c->lname->field][0]['value'];
}
break;
case 4:
/*
* Create username based on first initial, middle initial, and last
* name (e.g., JJSmith).
*/
if (!$c->fname->use_content_profile) {
$username = drupal_substr($edit[$c->fname->field], 0, 1);
}
else {
$username = drupal_substr($edit[$c->fname->field][0]['value'], 0, 1);
}
if (!$c->mname->use_content_profile) {
if (!empty($edit[$c->mname->field])) {
$username .= drupal_substr($edit[$c->mname->field], 0, 1);
}
}
else {
if (!empty($edit[$c->mname->field][0]['value'])) {
$username .= drupal_substr($edit[$c->mname->field][0]['value'], 0, 1);
}
}
if (!$c->lname->use_content_profile) {
$username .= $edit[$c->lname->field];
}
else {
$username .= $edit[$c->lname->field][0]['value'];
}
break;
case 5:
/*
* Create username based on first name and last name without spaces
* (e.g., JohnSmith).
*/
if (!$c->fname->use_content_profile) {
$username = $edit[$c->fname->field];
}
else {
$username = $edit[$c->fname->field][0]['value'];
}
if (!$c->lname->use_content_profile) {
$username .= $edit[$c->lname->field];
}
else {
$username .= $edit[$c->lname->field][0]['value'];
}
break;
case 6:
/*
* Create username based on first name, middle name, and last name
* without spaces (e.g., JohnJacobSmith).
*/
if (!$c->fname->use_content_profile) {
$username = $edit[$c->fname->field];
}
else {
$username = $edit[$c->fname->field][0]['value'];
}
if (!$c->mname->use_content_profile) {
if (!empty($edit[$c->mname->field])) {
$username .= $edit[$c->mname->field];
}
}
else {
if (!empty($edit[$c->mname->field][0]['value'])) {
$username .= $edit[$c->mname->field][0]['value'];
}
}
if (!$c->lname->use_content_profile) {
$username .= $edit[$c->lname->field];
}
else {
$username .= $edit[$c->lname->field][0]['value'];
}
break;
case 7:
/*
* Create username based on first name, middle initial, and last name
* (e.g., John J Smith).
*/
if (!$c->fname->use_content_profile) {
$username = $edit[$c->fname->field];
}
else {
$username = $edit[$c->fname->field][0]['value'];
}
$username .= ' ';
if (!$c->mname->use_content_profile) {
if (!empty($edit[$c->mname->field])) {
$username .= drupal_substr($edit[$c->mname->field], 0, 1);
$username .= ' ';
}
}
else {
if (!empty($edit[$c->mname->field][0]['value'])) {
$username .= drupal_substr($edit[$c->mname->field][0]['value'], 0, 1);
$username .= ' ';
}
}
if (!$c->lname->use_content_profile) {
$username .= $edit[$c->lname->field];
}
else {
$username .= $edit[$c->lname->field][0]['value'];
}
break;
}
// Check if we should force the username to lowercase characters.
if ($c->username_tolower) {
$username = drupal_strtolower($username);
}
/*
* Check if the username already exists in the database.
* Otherwise append and increment a number on the end of the string.
*/
$original_username = $username;
while (db_result(db_query("SELECT COUNT(*) FROM {users} WHERE name = '%s';", $username))) {
$username = $original_username . ++$i;
}
$edit['name'] = $username;
}
/**
* Force uppercase characters in name fields.
*
* If settings demand, we must take the users input and ensure the first letter
* of each part the name is uppercase.
*
* @param &$edit
* The array of form values submitted by the user.
*
*/
function realname_registration_ucfirst(&$edit) {
$c = realname_registration_load_settings();
if (!$c->fname->use_content_profile) {
$edit[$c->fname->field] = drupal_ucfirst($edit[$c->fname->field]);
}
else {
$edit[$c->fname->field][0]['value'] = drupal_ucfirst($edit[$c->fname->field][0]['value']);
}
if (!empty($c->mname->field)) {
if (!$c->mname->use_content_profile) {
$edit[$c->mname->field] = drupal_ucfirst($edit[$c->mname->field]);
}
else {
$edit[$c->mname->field][0]['value'] = drupal_ucfirst($edit[$c->mname->field][0]['value']);
}
}
if (!$c->lname->use_content_profile) {
$edit[$c->lname->field] = drupal_ucfirst($edit[$c->lname->field]);
}
else {
$edit[$c->lname->field][0]['value'] = drupal_ucfirst($edit[$c->lname->field][0]['value']);
}
}
/**
* Load configuration and return an object.
*
* Grab all the system variables for Realname registration and return a
* neatly packed object with configuration settings.
*/
function realname_registration_load_settings() {
$configuration = (object) array(
// Set up the general settings.
'username_format' => variable_get('realname_registration_format', 0),
'username_tolower' => variable_get('realname_registration_tolower', 0),
// Set up the first name field settings.
'fname' => (object) array(
'field' => variable_get('realname_registration_firstname_field', ''),
'use_content_profile' => variable_get('realname_registration_use_content_profile_firstname_field', 0),
'content_node' => variable_get('realname_registration_content_profile_firstname_field_node', ''),
),
// Set up the middle name field settings.
'mname' => (object) array(
'field' => variable_get('realname_registration_middlename_field', ''),
'use_content_profile' => variable_get('realname_registration_use_content_profile_middlename_field', 0),
'content_node' => variable_get('realname_registration_content_profile_middlename_field_node', ''),
),
// Set up the last name field settings.
'lname' => (object) array(
'field' => variable_get('realname_registration_lastname_field', ''),
'use_content_profile' => variable_get('realname_registration_use_content_profile_lastname_field', 0),
'content_node' => variable_get('realname_registration_content_profile_lastname_field_node', ''),
),
);
return $configuration;
}
/**
* Implementation of hook_token_info().
*/
function realname_registration_token_list($type = 'all') {
if ($type == 'user' || $type == 'all') {
$tokens['user']['first-name'] = t("The first name of the user.");
$tokens['user']['middle-name'] = t("The middle name of the user.");
$tokens['user']['last-name'] = t("The last name of the user.");
$tokens['user']['first-initial'] = t("The first initial of the user.");
$tokens['user']['middle-initial'] = t("The middle initial of the user.");
$tokens['user']['last-initial'] = t("The last initial of the user.");
$tokens['user']['first-name-raw'] = t("The first name of the user.");
$tokens['user']['middle-name-raw'] = t("The middle name of the user.");
$tokens['user']['last-name-raw'] = t("The last name of the user.");
$tokens['user']['first-initial-raw'] = t("The first initial of the user.");
$tokens['user']['middle-initial-raw'] = t("The middle initial of the user.");
$tokens['user']['last-initial-raw'] = t("The last initial of the user.");
return $tokens;
}
}
/**
* Implementation of hook_tokens().
*/
function realname_registration_token_values($type, $object = NULL, $options = array()) {
$values = array();
switch ($type) {
case 'user':
if (!empty($object)) {
$account = $object;
}
else {
$account = user_load(array(
'uid' => $GLOBALS['user']->uid,
));
}
if ($account->uid) {
$c = realname_registration_load_settings();
$firstname = realname_registration_get_fname($account);
$middlename = realname_registration_get_mname($account);
$lastname = realname_registration_get_lname($account);
$values['first-name'] = !empty($firstname) ? check_plain($firstname) : '';
$values['last-name'] = !empty($lastname) ? check_plain($lastname) : '';
$values['middle-name'] = !empty($middlename) ? check_plain($middlename) : '';
$values['first-initial'] = !empty($firstname) ? check_plain(drupal_substr($firstname, 0, 1)) : '';
$values['middle-initial'] = !empty($middlename) ? check_plain(drupal_substr($middlename, 0, 1)) : '';
$values['last-initial'] = !empty($lastname) ? check_plain(drupal_substr($lastname, 0, 1)) : '';
$values['first-name-raw'] = !empty($firstname) ? $firstname : '';
$values['last-name-raw'] = !empty($lastname) ? $lastname : '';
$values['middle-name-raw'] = !empty($middlename) ? $middlename : '';
$values['first-initial-raw'] = !empty($firstname) ? drupal_substr($firstname, 0, 1) : '';
$values['middle-initial-raw'] = !empty($middlename) ? drupal_substr($middlename, 0, 1) : '';
$values['last-initial-raw'] = !empty($lastname) ? drupal_substr($lastname, 0, 1) : '';
}
break;
}
return $values;
}
/**
* Implementation of realname_registration_get_fname();
*
* Determine where the first name values are located and
* return the first name.
*
* @param &$account
* The user object on which the operation is being performed.
*/
function realname_registration_get_fname($account) {
$c = realname_registration_load_settings();
if (module_exists('content_profile_registration')) {
$content_profile_type_count = db_result(db_query("\n SELECT COUNT(DISTINCT type_name) FROM {content_node_field_instance}\n WHERE field_name = '%s'", $c->fname->field));
}
if (!$c->fname->use_content_profile) {
$firstname = db_result(db_query("\n SELECT profile_values.value\n FROM {profile_values}\n INNER JOIN {profile_fields}\n ON profile_values.fid = profile_fields.fid\n WHERE profile_fields.name = '%s'\n AND profile_values.uid = %d", $c->fname->field, $account->uid));
}
else {
if ($content_profile_type_count > 1) {
$firstname = db_result(db_query("\n SELECT %s\n FROM {node} AS n\n LEFT JOIN {content_%s} AS c\n ON n.nid = c.nid\n WHERE n.uid = %d\n AND n.type = '%s'\n LIMIT 0, 1", $c->fname->field . '_value', $c->fname->field, $account->uid, $c->fname->content_node));
}
else {
$firstname = db_result(db_query("\n SELECT %s\n FROM {content_type_%s} AS c\n LEFT JOIN {node} AS n\n ON c.nid = n.nid\n WHERE n.uid = %d\n LIMIT 0, 1", $c->fname->field . '_value', $c->fname->content_node, $account->uid));
}
}
return $firstname;
}
/**
* Implementation of realname_registration_get_mname();
*
* Determine where the middle name values are located and
* return the middle name.
*
* @param &$account
* The user object on which the operation is being performed.
*/
function realname_registration_get_mname($account) {
$c = realname_registration_load_settings();
if (module_exists('content_profile_registration')) {
$content_profile_type_count = db_result(db_query("\n SELECT COUNT(DISTINCT type_name) FROM {content_node_field_instance} \n WHERE field_name = '%s'", $c->mname->field));
}
if (!empty($c->mname->field)) {
if (!$c->mname->use_content_profile) {
$middlename = db_result(db_query("\n SELECT profile_values.value\n FROM {profile_values}\n INNER JOIN {profile_fields}\n ON profile_values.fid = profile_fields.fid\n WHERE profile_fields.name = '%s'\n AND profile_values.uid = %d", $c->mname->field, $account->uid));
}
else {
if ($content_profile_type_count > 1) {
$middlename = db_result(db_query("\n SELECT %s\n FROM {node} AS n\n LEFT JOIN {content_%s} AS c\n ON n.nid = c.nid\n WHERE n.uid = %d\n AND n.type = '%s'\n LIMIT 0, 1", $c->mname->field . '_value', $c->mname->field, $account->uid, $c->mname->content_node));
}
else {
$middlename = db_result(db_query("\n SELECT %s\n FROM {content_type_%s} AS c\n LEFT JOIN {node} AS n\n ON c.nid = n.nid\n WHERE n.uid = %d\n LIMIT 0, 1", $c->mname->field . '_value', $c->mname->content_node, $account->uid));
}
}
}
return $middlename;
}
/**
* Implementation of realname_registration_get_lname();
*
* Determine where the last name values are located and
* return the last name.
*
* @param &$account
* The user object on which the operation is being performed.
*/
function realname_registration_get_lname($account) {
$c = realname_registration_load_settings();
if (module_exists('content_profile_registration')) {
$content_profile_type_count = db_result(db_query("\n SELECT COUNT(DISTINCT type_name) FROM {content_node_field_instance} \n WHERE field_name = '%s'", $c->lname->field));
}
if (!$c->lname->use_content_profile) {
$lastname = db_result(db_query("\n SELECT profile_values.value\n FROM {profile_values}\n INNER JOIN {profile_fields}\n ON profile_values.fid = profile_fields.fid\n WHERE profile_fields.name = '%s'\n AND profile_values.uid = %d", $c->lname->field, $account->uid));
}
else {
if ($content_profile_type_count > 1) {
$lastname = db_result(db_query("\n SELECT %s\n FROM {node} AS n\n LEFT JOIN {content_%s} AS c\n ON n.nid = c.nid\n WHERE n.uid = %d\n AND n.type = '%s'\n LIMIT 0, 1", $c->lname->field . '_value', $c->lname->field, $account->uid, $c->lname->content_node));
}
else {
$lastname = db_result(db_query("\n SELECT %s\n FROM {content_type_%s} AS c\n LEFT JOIN {node} AS n\n ON c.nid = n.nid\n WHERE n.uid = %d\n LIMIT 0, 1", $c->lname->field . '_value', $c->lname->content_node, $account->uid));
}
}
return $lastname;
}
/**
* Implementation of hook_perm().
*/
function realname_registration_perm() {
return array(
'administer realname registration',
'override realname registration',
);
}