Skip to content

Instantly share code, notes, and snippets.

@mrryanjohnston
Created October 6, 2011 16:17
Show Gist options
  • Save mrryanjohnston/1267833 to your computer and use it in GitHub Desktop.
Save mrryanjohnston/1267833 to your computer and use it in GitHub Desktop.
<?php
function services_sso_server_helper_email_registration_name($edit, $account) {
//If it's an ons.org email, these won't be set!
$first_name = $account->field_profile_fname['und'][0]['value'];
$last_name = $account->field_profile_lname['und'][0]['value'];
//If this hook_username_generation returns false, then use the defaul method to generate a new name
if (!($new_name = module_invoke_all('username_generation', $account, $edit))) {
$new_name = str_ireplace('-', '', substr(drupal_html_class($first_name), 0, 1) . drupal_html_class($last_name));
}
// if username generated from this name combination already exists, append underscore and number eg:(cjames_123)
if ((bool) db_query("SELECT 1 FROM {users} WHERE uid <> :uid AND LOWER(name) = LOWER(:new_name)", array(':uid' => $account->uid, ':new_name' => $new_name))->fetchField()) {
$name_idx = db_query_range("SELECT SUBSTRING_INDEX(name,'_',-1) FROM {users} WHERE name REGEXP :search ORDER BY CAST(SUBSTRING_INDEX(name,'_',-1) AS UNSIGNED) DESC", 0, 1, array(':search' => '^' . $new_name . '_[0-9]+$'))->fetchField();
$new_name .= '_' . ($name_idx + 1);
}
return $new_name;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment