You are here

email_registration.module in Email Registration 5

File

email_registration.module
View source
<?php

/**
 * Implementation of hook_user().
 *
 */
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;
}

/**
 * Implementation of hook_form_alter().
 *
 */
function email_registration_form_alter($form_id, &$form) {
  switch ($form_id) {
    case 'user_edit':
      if (!variable_get('email_registration_eq_email', FALSE)) {
        break;
      }
    case 'user_register':
      if ($form['_account']['#value']->uid != 1) {
        if (!isset($form['name']) && isset($form['account'])) {
          $form['account']['name']['#type'] = 'hidden';
          $form['account']['name']['#value'] = user_password();
          $form['account']['mail']['#title'] = t('E-mail');
        }
        else {
          $form['name']['#type'] = 'hidden';
          $form['name']['#value'] = user_password();
          $form['mail']['#title'] = t('E-mail');
        }
      }
      break;
    case 'user_pass':
      $form['name']['#title'] = t('E-mail');
      $form['name']['#description'] = t('Enter your e-mail address. You\'ll be sent a new password immediately.');
      break;
    case 'user_login':
      $form['name']['#title'] = t('E-mail');
      $form['name']['#description'] = t('Enter your e-mail address.');
      $form['pass']['#description'] = t('Enter the password that accompanies your e-mail.');
      $form['#validate'] = array(
        'email_registration_user_login_validate' => array(),
      ) + ($form['#validate'] ? $form['#validate'] : array());
      break;
    case 'user_login_block':
      $form['name']['#title'] = t('E-mail');
      $form['#validate'] = array(
        'email_registration_user_login_validate' => array(),
      ) + ($form['#validate'] ? $form['#validate'] : array());
      break;
  }
}

/** 
 * function email_registration_settings() {
 *  return system_settings_form($form);
 * }
 */

/**
 * Custom validation function for user login form.
 * Allows users to authenticate by email only, which is our preferred method.
 *
 */
function email_registration_user_login_validate($form_id, $form_values, $form) {
  if (isset($form_values['name'])) {
    if ($name = db_result(db_query("SELECT name FROM {users} WHERE LOWER(mail) = LOWER('%s')", $form_values['name']))) {
      form_set_value($form['name'], $name);
    }
  }
}

/**
* Implementation of hook_menu().
*/
function email_registration_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/user/email_registration',
      'title' => t("Email Registration"),
      'description' => t('Change the way email_registration.module works'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'email_registration_admin',
      ),
    );
  }
  return $items;
}

/**
* The administration form
*/
function email_registration_admin() {
  $form['eq_email'] = array(
    '#type' => 'checkbox',
    '#title' => t('Make username equal to email'),
    '#description' => t("If This is checked, then the username field in the database will be the same as the user\ns email address. Otherwise, email_registration module will use its default username generation settings."),
    '#default_value' => variable_get('email_registration_eq_email', FALSE),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
    '#weight' => 10,
  );
  return $form;
}
function email_registration_admin_submit($form_id, $form_values) {
  variable_set('email_registration_eq_email', $form_values['eq_email']);
  drupal_set_message(t('Your options have been saved.'));
}

Functions

Namesort descending Description
email_registration_admin The administration form
email_registration_admin_submit
email_registration_form_alter Implementation of hook_form_alter().
email_registration_menu Implementation of hook_menu().
email_registration_user Implementation of hook_user().
email_registration_user_login_validate Custom validation function for user login form. Allows users to authenticate by email only, which is our preferred method.