function _og_massadd_createusername in Organic Groups Mass Add 7
Same name and namespace in other branches
- 6 og_massadd.module \_og_massadd_createusername()
Generate a preferred username
1 call to _og_massadd_createusername()
- _og_massadd_adduser in ./
og_massadd.module - Actually adding something Returns FALSE if the user couldn't be created, 0 if the user existed and 1 if it was created.
File
- ./
og_massadd.module, line 398 - The og_massadd module file
Code
function _og_massadd_createusername($email, $firstname = '', $lastname = '', $suggestion = '', $attempt = 0) {
/*
The following combinations are tried in order:
1: If one is provided, the suggested username
2. The first part of the email address (everything before the @)
3. If both firstname and lastname is provided, "F.L".
F is x number of letters from firstname, where x starts at 1 and goes up to the full length of firstname.
L is lastname.
4. If firstname is provided without lastname, firstname only.
5. If lastname is provided without firstname, lastname only.
6. If both firstname and lastname is provided, try adding numbers 1 and up to a name formatted as F.Lastname where F is the first letter of firstname.
7. Last (only performed if the previous combination didn't) add numbers 1 and up to the first part of the email address.
*/
$usernames = array();
$numbered = array();
// First, create the verbatim names to try
// #1 pri: suggested username
if (!empty($suggestion)) {
$usernames[] = $suggestion;
}
// #2 pri: first part of email address (always present)
preg_match('/^(.+)(?:@)/', $email, $matches);
if (!empty($matches[1])) {
$usernames[] = $matches[1];
}
else {
return FALSE;
// We DO need a valid mail address
}
// #3 pri: username on the form of [x number of letters from first name, starting from 1 and up].[lastname]
if (!empty($firstname) && !empty($lastname)) {
for ($i = 1; $i < drupal_strlen($firstname); $i++) {
$username = '';
if (!empty($firstname)) {
// Add firstname if present
$username .= drupal_substr($firstname, 0, $i);
}
if (!empty($firstname) && !empty($lastname)) {
// Add divider between first and last name
$username .= '.';
}
if (!empty($lastname)) {
// Add lastname
$username .= $lastname;
}
$usernames[] = $username;
}
}
// #4 pri: if no lastname, use firstname only
if (!empty($firstname) && empty($lastname)) {
$usernames[] = $firstname;
}
// #5 pri: if no firstname, use lastname only
if (empty($firstname) && !empty($lastname)) {
$usernames[] = $lastname;
}
// Create names to add numbers to as last resort. Different priorities than the verbatim names
// #1 pri: [first letter of first name].[lastname]
if (!empty($firstname) && !empty($lastname)) {
$numbered[] = drupal_substr($firstname, 0, 1) . '.' . $lastname;
}
// #2 pri: first part of email address (always present)
preg_match('/^(.+)(?:@)/', $email, $matches);
if (!empty($matches[1])) {
$numbered[] = $matches[1];
}
// If our attempt exists in the verbatim table, return it
if (isset($usernames[$attempt])) {
return $usernames[$attempt];
}
// Generate a numbered attempt using the first entry in the numbered table.
$attempt = $attempt - count($usernames);
return reset($numbered) . ($attempt + 1);
}