View source
<?php
function email_registration_user($op, &$edit, &$account, $category = NULL) {
switch ($op) {
case 'validate':
global $form_values;
if ($form_values['_account']->uid != 1) {
if (variable_get('email_registration_eq_email', FALSE)) {
$namenew = $form_values['mail'];
}
else {
$namenew = preg_replace('/@.*$/', '', $form_values['mail']);
if (db_num_rows(db_query("SELECT uid FROM {users} WHERE uid != %d AND LOWER(name) = LOWER('%s')", $account->uid, $namenew)) > 0) {
$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);
}
}
$form_values['name'] = $namenew;
}
break;
}
return;
}
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_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);
}
}
}
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;
}
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.'));
}