Index: email_registration.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/email_registration/email_registration.module,v retrieving revision 1.5.2.11 diff -u -p -r1.5.2.11 email_registration.module --- email_registration.module 14 Jul 2009 00:45:00 -0000 1.5.2.11 +++ email_registration.module 6 Nov 2009 22:32:18 -0000 @@ -22,15 +22,7 @@ function email_registration_user($op, &$ $names = array_filter($names); if (empty($names)) { - // Default implementation of name generation - $namenew = preg_replace('/@.*$/', '', $edit['mail']); - // if username generated from email record already exists, append underscore and number eg:(chris_123) - if (db_result(db_query("SELECT count(*) FROM {users} WHERE uid <> %d AND LOWER(name) = LOWER('%s')", $account->uid, $namenew)) > 0) { - // find the next number available to append to the name - $sql = "SELECT SUBSTRING_INDEX(name,'_',-1) FROM {users} WHERE name REGEXP '%s' ORDER BY CAST(SUBSTRING_INDEX(name,'_',-1) AS UNSIGNED) DESC LIMIT 1"; - $nameidx = db_result(db_query($sql, '^'. $namenew .'_[0-9]+$')); - $namenew .= '_'. ($nameidx + 1); - } + $namenew = email_registration_email_to_name($edit['mail'], $account->uid); } else { // One would expect a single implementation of the hook, but if there @@ -56,6 +48,30 @@ function email_registration_user($op, &$ } /** + * Helper function to convert an email address into a username. + * + * @param $email + * E-mail address string to convert. + * @param $uid + * User ID of the current user (if any) + * + * @return + * The username to use for the given e-mail address. By default just takes + * the user portion of the email (everything before the '@') but if there + * are collisions, append an underscore and a number (e.g. "chris_123"). + */ +function email_registration_email_to_name($email, $uid = 0) { + $username = preg_replace('/@.*$/', '', $email); + if (db_result(db_query("SELECT count(*) FROM {users} WHERE uid <> %d AND LOWER(name) = LOWER('%s')", $uid, $username)) > 0) { + // find the next number available to append to the name + $sql = "SELECT SUBSTRING_INDEX(name,'_',-1) FROM {users} WHERE name REGEXP '%s' ORDER BY CAST(SUBSTRING_INDEX(name,'_',-1) AS UNSIGNED) DESC LIMIT 1"; + $nameidx = db_result(db_query($sql, '^'. $username .'_[0-9]+$')); + $username .= '_'. ($nameidx + 1); + } + return $username; +} + +/** * Implementation of hook_form_alter(). * */