View source
<?php
function ajax_register_menu() {
$items = array();
$items['ajax_register/%/%'] = array(
'title' => 'Request new password',
'page callback' => 'ajax_register_ajax_page_callback',
'page arguments' => array(
1,
2,
),
'access callback' => 'ajax_register_ajax_page_access',
'access arguments' => array(
1,
2,
),
'type' => MENU_CALLBACK,
'file' => 'ajax_register.pages.inc',
);
return $items;
}
function ajax_register_form_alter(&$form, &$form_state, $form_id) {
switch ($form_id) {
case 'user_login_block':
$registration_allowed = variable_get('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL);
if ($registration_allowed) {
$user_register_link = array(
'#type' => 'link',
'#title' => t('Create new account'),
'#href' => 'ajax_register/register/nojs',
'#id' => 'ajax_link',
'#attributes' => array(
'class' => array(
'use-ajax',
),
'title' => t('Create a new user account.'),
),
);
}
$user_password_link = array(
'#type' => 'link',
'#title' => t('Request new password'),
'#href' => 'ajax_register/password/nojs',
'#id' => 'ajax_link',
'#attributes' => array(
'class' => array(
'use-ajax',
),
'title' => t('Request new password via e-mail.'),
),
);
$items[] = render($user_register_link);
$items[] = render($user_password_link);
$form['links'] = array(
'#theme' => 'item_list',
'#items' => $items,
);
case 'user_login':
$form['messages'] = array(
'#markup' => '<div id="ajax-register-login-messages"></div>',
);
$form['actions']['submit']['#ajax'] = array(
'callback' => 'ajax_register_login_ajax_callback',
);
break;
case 'user_pass':
$form['messages'] = array(
'#markup' => '<div id="ajax-register-pass-messages"></div>',
);
$user_login_link = array(
'#type' => 'link',
'#title' => t('Back to login'),
'#href' => 'ajax_register/login/nojs',
'#id' => 'ajax_link',
'#attributes' => array(
'class' => array(
'use-ajax',
),
'title' => t('Back to login form.'),
),
);
$items[] = render($user_login_link);
$form['links'] = array(
'#theme' => 'item_list',
'#items' => $items,
);
$form['actions']['submit']['#ajax'] = array(
'callback' => 'ajax_register_pass_ajax_callback',
);
$form['#after_build'][] = 'ajax_register_user_pass_after_build';
break;
case 'user_register_form':
$form['messages'] = array(
'#markup' => '<div id="ajax-register-reg-messages"></div>',
);
$form['actions']['submit']['#ajax'] = array(
'callback' => 'ajax_register_register_ajax_callback',
);
break;
}
}
function ajax_register_user_pass_after_build($form, &$form_state) {
module_load_include('pages.inc', 'user');
return $form;
}
function ajax_register_register_ajax_callback() {
$commands = array();
if (!form_get_errors()) {
$commands[] = ajax_command_replace('#user-register-form', theme('status_messages'));
}
else {
$commands[] = ajax_command_html('#ajax-register-reg-messages', theme('status_messages'));
}
return array(
'#type' => 'ajax',
'#commands' => $commands,
);
}
function ajax_register_pass_ajax_callback() {
$commands = array();
if (!form_get_errors()) {
$commands[] = ajax_command_replace('#user-pass', theme('status_messages'));
}
else {
$commands[] = ajax_command_html('#ajax-register-pass-messages', theme('status_messages'));
}
return array(
'#type' => 'ajax',
'#commands' => $commands,
);
}
function ajax_register_login_ajax_callback() {
$commands = array();
if (!form_get_errors()) {
drupal_set_message(t('Login successful. You are now being redirected.'));
ctools_include('ajax');
ctools_add_js('ajax-responder');
$commands[] = ctools_ajax_command_reload();
}
$commands[] = ajax_command_html('#ajax-register-login-messages', theme('status_messages'));
return array(
'#type' => 'ajax',
'#commands' => $commands,
);
}
function ajax_register_ajax_page_access($form, $type) {
$forms = array(
'login',
'register',
'password',
);
$types = array(
'ajax',
'nojs',
);
if (in_array($form, $forms) && in_array($type, $types) && user_is_anonymous()) {
return TRUE;
}
return FALSE;
}