You are here

username_check.module in Username originality AJAX check 5

Same filename and directory in other branches
  1. 8 username_check.module
  2. 6 username_check.module
  3. 7 username_check.module

Checks user name availability on registration page.

File

username_check.module
View source
<?php

/**
 * @file
 * Checks user name availability on registration page.
 */

/**
 * Implementation of hook_menu().
 */
function username_check_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'username_check/isunique',
      'type' => MENU_CALLBACK,
      'callback' => 'username_check_callback',
      'access' => user_access('access content'),
    );
    $items[] = array(
      'path' => 'admin/settings/username_check',
      'type' => MENU_NORMAL_ITEM,
      'title' => t('Username check'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'username_check_settings',
      ),
      'access' => user_access('administer site configuration'),
    );
  }
  return $items;
}

/**
 * Implementation of hook_form_alter().
 */
function username_check_form_alter($form_id, &$form) {
  if ('user_register' == $form_id) {
    $mode = variable_get('username_check_mode', 'manual');
    _username_check_load_resources($mode);
    if (isset($form['account']) && $form['account']['#type'] == 'fieldset') {
      $form_group =& $form['account'];
    }
    else {
      $form_group =& $form;
    }
    if ($mode == 'manual') {
      $form_group['name']['#weight'] = -5;
      $form_group['name']['#prefix'] = '<div id="username-check-wrapper">';
      $form_group['name']['#suffix'] = '</div>';
      $form_group['username_check_button'] = array(
        '#type' => 'button',
        '#button_type' => 'button',
        // Really not supported in D5.14 and fixed in JS :'(
        '#value' => t('Check username'),
        '#prefix' => '<div id="username-check-message" class="username-check-message"></div>',
        '#weight' => -4,
      );
    }
    elseif ($mode == 'auto') {
      $module_path = drupal_get_path('module', 'username_check');
      $form_group['name']['#prefix'] = '<div id="username-check-wrapper">';
      $form_group['name']['#suffix'] = '</div><div id="username-check-message"></div><div id="username-check-informer" class="username-check-informer">&nbsp;</div>';
    }
  }
}

/**
 * Load username_check resources.
 */
function _username_check_load_resources($mode) {
  $module_path = drupal_get_path('module', 'username_check');
  drupal_add_css($module_path . '/username_check.css');
  drupal_add_js($module_path . "/username_check_{$mode}.js");
  drupal_add_js(array(
    'usernameCheck' => array(
      'ajaxUrl' => url('username_check/isunique', NULL, NULL, TRUE),
      'msgWait' => t('Checking username availability ...'),
      'delay' => variable_get('username_check_delay', 1),
    ),
  ), 'setting');
}

/**
 * Main AJAX function: originality check menu callback.
 */
function username_check_callback() {
  $output = array();
  $username = $_GET['username'];
  $ret = user_validate_name($username);
  if ($ret) {
    $output['allowed'] = FALSE;
    $output['msg'] = $ret;
  }
  else {
    $ret = drupal_is_denied('user', $username);
    if ($ret) {
      $output['allowed'] = FALSE;
      $output['msg'] = t('The username %username is not allowed.', array(
        '%username' => $username,
      ));
    }
    else {
      $username = check_plain($username);
      $ret = _username_check_is_user_exists($username);
      if ($ret) {
        $output['allowed'] = FALSE;
        $output['msg'] = t('The name %username is already taken.', array(
          '%username' => $username,
        ));
      }
      else {
        $output['allowed'] = TRUE;
        $output['msg'] = t('The username %username is available.', array(
          '%username' => $username,
        ));
      }
    }
  }
  drupal_page_header();
  print drupal_to_js($output);
  exit;
}

/**
 * Query user table to check if such username is already exists.
 */
function _username_check_is_user_exists($username) {
  return db_result(db_query("SELECT COUNT(u.name) FROM {users} u WHERE u.name = '%s'", $username));
}

/**
 * Menu callback; displays the username_check module settings page.
 */
function username_check_settings() {
  $form = array();
  $form['username_check_mode'] = array(
    '#type' => 'radios',
    '#title' => t('Checking mode'),
    '#options' => array(
      'manual' => t('Manual - executes upon clicking a button'),
      'auto' => t('Automatic - executes when user leaves username field or upon timer'),
    ),
    '#default_value' => variable_get('username_check_mode', 'manual'),
  );
  $form['username_check_delay'] = array(
    '#type' => 'textfield',
    '#title' => t('Timer threshold'),
    '#description' => t('Threshold in seconds (ex: 0.5, 1). Only for automatic mode.'),
    '#default_value' => variable_get('username_check_delay', 1),
  );
  return system_settings_form($form);
}

Functions

Namesort descending Description
username_check_callback Main AJAX function: originality check menu callback.
username_check_form_alter Implementation of hook_form_alter().
username_check_menu Implementation of hook_menu().
username_check_settings Menu callback; displays the username_check module settings page.
_username_check_is_user_exists Query user table to check if such username is already exists.
_username_check_load_resources Load username_check resources.