Created
October 6, 2011 16:17
-
-
Save mrryanjohnston/1267833 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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