You are here

function email_registration_user in Email Registration 5

Same name and namespace in other branches
  1. 6 email_registration.module \email_registration_user()

Implementation of hook_user().

File

./email_registration.module, line 8

Code

function email_registration_user($op, &$edit, &$account, $category = NULL) {
  switch ($op) {
    case 'validate':
      global $form_values;

      // skips validation for super-admin (uid=1)
      if ($form_values['_account']->uid != 1) {

        // if the flag is TRUE, then just set $namenew to the email and move on
        if (variable_get('email_registration_eq_email', FALSE)) {
          $namenew = $form_values['mail'];
        }
        else {
          $namenew = preg_replace('/@.*$/', '', $form_values['mail']);

          // if username generated from email record already exists, append underscore and number eg:(chris_123)
          if (db_num_rows(db_query("SELECT uid 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_[0-9]+\$' ORDER BY CAST(SUBSTRING_INDEX(name,'_',-1) AS UNSIGNED) DESC LIMIT 1";
            $nameidx = db_result(db_query($sql, $namenew));
            $namenew .= '_' . ($nameidx + 1);
          }
        }

        // replace with generated username
        $form_values['name'] = $namenew;
      }
      break;
  }
  return;
}