View source
<?php
function ajax_register_init() {
$path = 'var tb_pathToImage ="' . drupal_get_path('module', 'ajax_register') . '/images/loading.gif";';
drupal_add_js($path, 'inline');
drupal_add_js(drupal_get_path('module', 'ajax_register') . "/thickbox.js");
drupal_add_css(drupal_get_path('module', 'ajax_register') . "/thickbox.css");
}
function ajax_register_block($op = 'list', $delta = 0, $edit = array()) {
if ($op == 'list') {
$blocks[0] = array(
'info' => t('Ajax login/register'),
);
return $blocks;
}
if ($op == 'view') {
if ($delta == 0) {
$block['subject'] = t('Ajax Login/Register');
$block['content'] = ajax_get_login_block_content();
return $block;
}
}
}
function ajax_register_menu() {
$items = array();
$items['ajax_register/login'] = array(
'title' => t('Ajax register login'),
'page callback' => 'ajax_register_get_ajax_login_form',
'access arguments' => array(
'access content',
),
'type' => MENU_CALLBACK,
);
$items['ajax_register/register'] = array(
'title' => t('Ajax register sign up'),
'page callback' => 'ajax_register_get_ajax_register_form',
'access arguments' => array(
'access content',
),
'type' => MENU_CALLBACK,
);
return $items;
}
function ajax_register_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'user_login') {
$form['#ajax'] = array(
'enabled' => TRUE,
);
}
else {
if ($form_id == 'user_register') {
$form['#ajax'] = array(
'enabled' => TRUE,
);
}
else {
if ($form_id == 'user_pass') {
$form['#ajax'] = array(
'enabled' => TRUE,
);
}
}
}
}
function ajax_get_login_block_content() {
global $user;
$user_uri = ajax_register_get_my_account_link();
$login_uri = ajax_register_get_login_link();
$register_uri = ajax_register_get_register_link();
$logout_uri = ajax_register_get_logout_link();
if (user_is_anonymous()) {
$block_content = "<div id='ajax_register_block_anonymous'>" . $login_uri . "<br/>" . $register_uri . "</div>";
return $block_content;
}
else {
$block_content = "<div id='ajax_register_block_authenticated'><p>" . t("Welcome: ") . "<span class='ajax_register_user_name'>" . $user->name . "</span></p>" . $user_uri . "<br/>" . $logout_uri . "</div>";
return $block_content;
}
}
function ajax_register_get_register_link($attributes = array(), $query = array()) {
$attributes['class'] = $attributes['class'] . ' thickbox';
$register_uri = l(t('Register'), 'ajax_register/register', array(
'attributes' => $attributes,
'query' => $query,
));
return $register_uri;
}
function ajax_register_get_login_link($attributes = array(), $query = array()) {
$attributes['class'] = $attributes['class'] . ' thickbox';
$login_uri = l(t('Login'), 'ajax_register/login', array(
'attributes' => $attributes,
'query' => $query,
));
return $login_uri;
}
function ajax_register_get_my_account_link($attributes = array()) {
global $user;
$user_uri = l(t('My Account'), 'user/' . $user->uid, array(
'attributes' => $attributes,
));
return $user_uri;
}
function ajax_register_get_logout_link($attributes = array()) {
$logout_uri = l(t('Logout'), 'logout', array(
'attributes' => $attributes,
));
return $logout_uri;
}
function ajax_register_get_ajax_login_form() {
$login = drupal_get_form('user_login');
$pass = drupal_get_form('user_pass');
$forgot_title = t('Forgot Your Password');
print $login . '<br /><div><h3>' . $forgot_title . '</h3></div>' . $pass;
}
function ajax_register_get_ajax_register_form() {
print drupal_get_form('user_register');
}