You are here

ajax_register.module in Ajax Login/Register 6

File

ajax_register.module
View source
<?php

/*
 * @file
 * This module provides a block with 2 links (Login, Register) for the anonymous user
 * When the user clicks on Login or register the requested form appears in a nice ajax popup
 * The login and register forms can validate using ajax and not redirect on error
 * On success the module redirects to the user page
 * When user is logged in, the block provides a Welcome !username message and a link to the user page
 * and a logout link.
 */

/*
 * Implementing hook_init
 */
function ajax_register_init() {

  //Declare the thickbox image path
  $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");
}

/*
 * Implementing hook_block
 */
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;
    }
  }
}

/*
 * Implementing hook_menu
 */
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;
}

/*
 * Implementing hook_form_alter
 */
function ajax_register_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'user_login') {

    //Set form to work with ajax
    $form['#ajax'] = array(
      'enabled' => TRUE,
    );
  }
  else {
    if ($form_id == 'user_register') {

      //Set form to work with ajax
      $form['#ajax'] = array(
        'enabled' => TRUE,
      );
    }
    else {
      if ($form_id == 'user_pass') {

        //Set form to work with ajax
        $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() {

  //prints the html of the form to the ajax url request
  $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() {

  //prints the html of the form to the ajax url request
  print drupal_get_form('user_register');
}