You are here

fancy_login.module in Fancy Login 7.3

Holds hooks for the Fancy Login module.

File

fancy_login.module
View source
<?php

/**
 * @file
 * Holds hooks for the Fancy Login module.
 */

/**
 * Implements hook_menu().
 */
function fancy_login_menu() {
  $menu['admin/config/people/fancy_login'] = array(
    'title' => 'Fancy Login',
    'description' => 'Settings for Fancy Login Page',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'fancy_login_settings',
    ),
    'access arguments' => array(
      'Administer fancy login',
    ),
    'file' => 'includes/fancy_login.pages.inc',
  );
  $menu['fancy_login/ajax/%'] = array(
    'title' => '',
    'page callback' => 'fancy_login_ajax_callback',
    'page arguments' => array(
      2,
    ),
    'access callback' => 'user_is_anonymous',
    'file' => 'includes/fancy_login.pages.inc',
    'type' => MENU_CALLBACK,
  );
  return $menu;
}

/**
 * Implments hook_permission().
 */
function fancy_login_permission() {
  return array(
    'Administer fancy login' => array(
      'title' => t('Administer fancy login'),
      'description' => t('Allows users to change the settings for the Fancy Login module'),
    ),
  );
}

/**
 * Implements hook_forms().
 *
 * Aliases the following list of forms so that we can
 * alter them in hook_form_alter() for use in the Fancy
 * Login modal, while leaving them unaltered anywhere
 * else they may exist on the site.
 *
 * Aliases
 *   form: user_login_block()
 *   alias: fancy_login_user_login_block()
 *
 *   form: user_pass()
 *   alias: fancy_login_user_pass()
 *
 *   form: user_register_form()
 *   alias: fancy_login_user_register_form()
 */
function fancy_login_forms($form_id, $args) {
  $forms = array();

  // Alias the user_login_block() form.
  if ($form_id == 'fancy_login_user_login_block') {
    $forms['fancy_login_user_login_block'] = array(
      'callback' => 'user_login_block',
    );
  }
  elseif ($form_id == 'fancy_login_user_pass') {
    $forms['fancy_login_user_pass'] = array(
      'callback' => 'user_pass',
    );
  }
  elseif ($form_id == 'fancy_login_user_register_form') {
    $forms['fancy_login_user_register_form'] = array(
      'callback' => 'user_register_form',
    );
  }
  return $forms;
}

/**
 * Implements hook_page_build().
 *
 * Adds login form and page dimmer to the footer of the page.
 */
function fancy_login_page_build(&$page) {
  if (user_is_anonymous()) {
    $form = drupal_get_form('user_login_block');
    $path = drupal_get_path('module', 'fancy_login');
    $login_path = drupal_get_path_alias('user/login');
    $fl_settings = array(
      'screenFadeColor' => variable_get('screen_fade_color', 'white'),
      'screenFadeZIndex' => variable_get('screen_fade_z_index', '10'),
      'loginBoxBackgroundColor' => variable_get('login_box_background_color', 'white'),
      'loginBoxTextColor' => variable_get('login_box_text_color', 'black'),
      'loginBoxBorderColor' => variable_get('login_box_border_color', 'black'),
      'loginBoxBorderWidth' => variable_get('login_box_border_width', '3px'),
      'loginBoxBorderStyle' => variable_get('login_box_border_style', 'solid'),
      'loginPath' => $login_path,
      'dimFadeSpeed' => (int) variable_get('fancy_login_dim_fade_speed', 500),
      'boxFadeSpeed' => (int) variable_get('fancy_login_box_fade_speed', 1000),
      'hideObjects' => variable_get('fancy_login_hide_objects', 0),
      'disableRegistration' => variable_get('fancy_login_disable_registration', 0),
    );
    $page['page_bottom']['fancy_login'] = array(
      '#prefix' => '<div id="fancy_login_dim_screen"></div><div id="fancy_login_login_box">',
      '#suffix' => '</div>',
      'form_wrapper' => array(
        '#prefix' => '<div id="fancy_login_form_contents"><a href="#" id="fancy_login_close_button">X</a>',
        '#suffix' => '</div>',
        'form' => drupal_get_form('fancy_login_user_login_block'),
      ),
      '#attached' => array(
        'js' => array(
          array(
            'type' => 'file',
            'data' => $path . '/js/fancy_login.js',
          ),
          array(
            'type' => 'setting',
            'data' => array(
              'fancyLogin' => $fl_settings,
            ),
          ),
        ),
        'css' => array(
          array(
            'type' => 'file',
            'data' => $path . '/css/fancy_login.css',
          ),
        ),
      ),
    );
  }
}

/**
 * Implements hook_form_alter().
 */
function fancy_login_form_alter(&$form, &$form_state, $form_id) {
  global $base_url;
  if ($form_id == 'fancy_login_user_login_block') {

    // Invoke all instances of hook_form_user_login_block_alter()
    foreach (module_implements('form_user_login_block_alter') as $module) {
      $function = $module . '_form_user_login_block_alter';
      $function($form, $form_state);
    }

    // Invoke all instances of hook_form_alter() with a $form_id of
    // 'user_login_block'.
    $id = 'user_login_block';
    foreach (module_implements('form_alter') as $module) {
      $function = $module . '_form_alter';
      $function($form, $form_state, $id);
    }

    // If the SSL icon is to be shown on the form, insert it into the form in
    // the relevant location.
    $icon_position = variable_get('fancy_login_icon_position', 0);
    if ($icon_position && variable_get('fancy_login_https', 0)) {
      $icon = theme('ssl_icon', array(
        'base_url' => $base_url,
      ));
      $form['ssl_logo'] = array(
        '#markup' => $icon,
      );
      if ($icon_position == 1) {
        $form['ssl_logo']['#weight'] = -100;
        $form['#attributes'] = array(
          'class' => array(
            'ssl_icon_above',
          ),
        );
      }
      elseif ($icon_position == 2) {
        $form['ssl_logo']['#weight'] = 100;
        $form['#attributes'] = array(
          'class' => array(
            'ssl_icon_below',
          ),
        );
      }
    }

    // Store the current path and set it before all other validation so that
    // any modules (such as Login Destination) that depend on the current
    // path will respond properly.
    $form['current_path'] = array(
      '#type' => 'value',
      '#value' => current_path(),
    );
    array_unshift($form['#validate'], 'fancy_login_insert_current_path');

    // Add a wrapper for our #ajax callback.
    if (!isset($form['#prefix'])) {
      $form['#prefix'] = '';
    }
    $form['#prefix'] = '<div id="fancy_login_user_login_block_wrapper">';
    if (!isset($form['#suffix'])) {
      $form['#suffix'] = '';
    }
    $form['#suffix'] = '</div>';

    // If Fancy Login is set to use https, change the protocol of the form
    // action if necessary.
    if (variable_get('fancy_login_https', 0) && strpos($base_url, 'https:') !== 0) {
      if (strpos($form['#action'], 'https') !== 0) {
        if (strpos($form['#action'], 'http') === 0) {
          $form['#action'] = preg_replace('/^http:/', 'https:', $form['#action']);
        }
        elseif (strpos($form['#action'], '//') === 0) {
          $form['#action'] = 'https:' . $form['#action'];
        }
        else {
          $form['#action'] = preg_replace('/^http:/', 'https:', $base_url) . $form['#action'];
        }
      }
    }
    else {

      // Set the submit button of the forum to submit with #ajax.
      $form['actions']['submit']['#ajax'] = array(
        'wrapper' => 'fancy_login_user_login_block_wrapper',
        'callback' => 'fancy_login_user_login_block_ajax_callback',
      );

      // The #ajax on submit buttons loaded using AJAX will not work with the
      // current form key if that form has previously been loaded, as the system
      // thinks that the #ajax has already been applied to the given submit
      // button and therefore skips it. This next step ensures that the submit
      // button has a unique key every time the button is loaded, so that the
      // system does not think the #ajax is already applied.
      $form['actions']['submit_' . REQUEST_TIME] = $form['actions']['submit'];
      $form['actions']['submit']['#access'] = FALSE;
    }
  }
  elseif ($form_id == 'fancy_login_user_pass') {
    form_load_include($form_state, 'inc', 'user', 'user.pages');

    // Invoke all instances of hook_form_user_pass_alter()
    foreach (module_implements('form_user_pass_alter') as $module) {
      $function = $module . '_form_user_pass_alter';
      $function($form, $form_state);
    }

    // Invoke all instances of hook_form_alter() with a $form_id of 'user_pass'.
    $id = 'user_pass';
    foreach (module_implements('form_alter') as $module) {
      $function = $module . '_form_alter';
      $function($form, $form_state, $id);
    }

    // Add a wrapper for our #ajax callback.
    if (!isset($form['#prefix'])) {
      $form['#prefix'] = '';
    }
    $form['#prefix'] = '<div id="fancy_login_user_pass_block_wrapper">';
    if (!isset($form['#suffix'])) {
      $form['#suffix'] = '';
    }
    $form['#suffix'] = '</div>';

    // Store the current path and set it before all other validation so that
    // any modules that depend on the current path will respond properly.
    $form['current_path'] = array(
      '#type' => 'value',
      '#value' => current_path(),
    );
    array_unshift($form['#validate'], 'fancy_login_insert_current_path');

    // Add links to be used in our Fancy Login block, allowing the states to be
    // changed between login, register, and recover password.
    $items = array();
    $items[] = l(t('Sign in'), 'user/login', array(
      'attributes' => array(
        'title' => t('Log in to !site_name.', array(
          '!site_name' => variable_get('site_name', t('this site')),
        )),
      ),
    ));
    if (variable_get('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL)) {
      $items[] = l(t('Create new account'), 'user/register', array(
        'attributes' => array(
          'title' => t('Create a new user account.'),
        ),
      ));
    }
    $form['links'] = array(
      '#theme' => 'item_list',
      '#items' => $items,
    );

    // If Fancy Login is set to use https, change the protocol of the form
    // action if necessary.
    if (variable_get('fancy_login_https', 0) && strpos($base_url, 'https:') !== 0) {
      if (strpos($form['#action'], 'https') !== 0) {
        if (strpos($form['#action'], 'http') === 0) {
          $form['#action'] = preg_replace('/^http:/', 'https:', $form['#action']);
        }
        elseif (strpos($form['#action'], '//') === 0) {
          $form['#action'] = 'https:' . $form['#action'];
        }
        else {
          $form['#action'] = preg_replace('/^http:/', 'https:', $base_url) . $form['#action'];
        }
      }
    }
    else {

      // Set the submit button of the forum to submit with #ajax.
      $form['actions']['submit']['#ajax'] = array(
        'wrapper' => 'fancy_login_user_pass_block_wrapper',
        'callback' => 'fancy_login_user_pass_ajax_callback',
      );

      // The #ajax on submit buttons loaded using AJAX will not work with the
      // current form key if that form has previously been loaded, as the system
      // thinks that the #ajax has already been applied to the given submit
      // button and therefore skips it. This next step ensures that the submit
      // button has a unique key every time the button is loaded, so that the
      // system does not think the #ajax is already applied.
      $form['actions']['submit_' . REQUEST_TIME] = $form['actions']['submit'];
      $form['actions']['submit']['#access'] = FALSE;
    }
  }
  elseif ($form_id == 'fancy_login_user_register_form') {

    // Invoke all instances of hook_form_user_register_form_alter()
    foreach (module_implements('form_user_register_form_alter') as $module) {
      $function = $module . '_form_user_register_form_alter';
      $function($form, $form_state);
    }

    // Invoke all instances of hook_form_alter() with a $form_id of
    // 'user_register_form'.
    $id = 'user_register_form';
    foreach (module_implements('form_alter') as $module) {
      $function = $module . '_form_alter';
      $function($form, $form_state, $id);
    }

    // Add a wrapper for our #ajax callback.
    if (!isset($form['#prefix'])) {
      $form['#prefix'] = '';
    }
    $form['#prefix'] = '<div id="fancy_login_user_register_form_block_wrapper">';
    if (!isset($form['#suffix'])) {
      $form['#suffix'] = '';
    }
    $form['#suffix'] = '</div>';

    // Store the current path and set it before all other validation so that
    // any modules that depend on the current path will respond properly.
    $form['current_path'] = array(
      '#type' => 'value',
      '#value' => current_path(),
    );
    array_unshift($form['#validate'], 'fancy_login_insert_current_path');

    // Add links to be used in our Fancy Login block, allowing the states to be
    // changed between login, register, and recover password.
    $items = array();
    $items[] = l(t('Sign in'), 'user/login', array(
      'attributes' => array(
        'title' => t('Log in to !site_name.', array(
          '!site_name' => variable_get('site_name', t('this site')),
        )),
      ),
    ));
    $items[] = l(t('Request new password'), 'user/password', array(
      'attributes' => array(
        'title' => t('Request new password via e-mail.'),
      ),
    ));
    $form['links'] = array(
      '#theme' => 'item_list',
      '#items' => $items,
    );

    // If Fancy Login is set to use https, change the protocol of the form
    // action if necessary.
    if (variable_get('fancy_login_https', 0) && strpos($base_url, 'https:') !== 0) {
      if (strpos($form['#action'], 'https') !== 0) {
        if (strpos($form['#action'], 'http') === 0) {
          $form['#action'] = preg_replace('/^http:/', 'https:', $form['#action']);
        }
        elseif (strpos($form['#action'], '//') === 0) {
          $form['#action'] = 'https:' . $form['#action'];
        }
        else {
          $form['#action'] = preg_replace('/^http:/', 'https:', $base_url) . $form['#action'];
        }
      }
    }
    else {

      // Set the submit button of the forum to submit with #ajax.
      $form['actions']['submit']['#ajax'] = array(
        'wrapper' => 'fancy_login_user_register_form_block_wrapper',
        'callback' => 'fancy_login_user_register_form_ajax_callback',
      );

      // The #ajax on submit buttons loaded using AJAX will not work with the
      // current form key if that form has previously been loaded, as the system
      // thinks that the #ajax has already been applied to the given submit
      // button and therefore skips it. This next step ensures that the submit
      // button has a unique key every time the button is loaded, so that the
      // system does not think the #ajax is already applied.
      $form['actions']['submit_' . REQUEST_TIME] = $form['actions']['submit'];
      $form['actions']['submit']['#access'] = FALSE;
    }
  }
}

/**
 * Insert current path in to $_GET['q'].
 *
 * This ensures that any modules that depend upon it will have the correct path
 * to work with.
 */
function fancy_login_insert_current_path($form, &$form_state) {
  $_GET['q'] = $form_state['values']['current_path'];
}

/**
 * Ajax callback function for fancy_login_user_login_block submit button.
 */
function fancy_login_user_login_block_ajax_callback($form, &$form_state) {
  global $destination;
  $commands = array();
  $form = render($form);
  $message_data = drupal_get_messages(NULL, FALSE);

  // Check to see if there were any errors with the form submission.
  if (!isset($message_data['error']) || !count($message_data['error'])) {
    if (variable_get('fancy_login_no_redirect', 0)) {
      drupal_set_message(t('You have been successfully logged in. Please wait while the page is refreshed.'));
      $commands[] = array(
        'command' => 'fancyLoginRefreshPage',
        'closePopup' => TRUE,
      );
    }
    else {
      drupal_set_message(t('You have been successfully logged in. Please wait while you are redirected.'));
      $dest = $destination && is_array($destination) && isset($destination[0]) ? $destination[0] : $form_state['redirect'];
      $commands[] = array(
        'command' => 'fancyLoginRedirect',
        'closePopup' => TRUE,
        'destination' => url($dest),
      );
    }
  }
  $messages = theme('status_messages');
  $commands[] = ajax_command_replace(NULL, $messages . $form);
  return array(
    '#type' => 'ajax',
    '#commands' => $commands,
  );
}

/**
 * Ajax callback function for fancy_login_user_pass submit button.
 */
function fancy_login_user_pass_ajax_callback($form, &$form_state) {
  $commands = array();
  $form = render($form);
  $message_data = drupal_get_messages(NULL, FALSE);

  // Check to see if there were any errors with the form submission.
  if (!count($message_data['error'])) {
    $commands[] = array(
      'command' => 'fancyLoginClosePopup',
    );
  }
  $messages = theme('status_messages');
  $scripts = drupal_add_js();
  if (!empty($scripts['settings'])) {
    $settings = '<script type="text/javascript">jQuery.extend(Drupal.settings, ';
    $settings .= drupal_json_encode(call_user_func_array('array_merge_recursive', $scripts['settings']['data']));
    $settings .= ');</script>';
  }
  $commands[] = ajax_command_replace(NULL, $messages . $form . $settings);
  return array(
    '#type' => 'ajax',
    '#commands' => $commands,
  );
}

/**
 * Ajax callback function for fancy_login_user_register_form submit button.
 */
function fancy_login_user_register_form_ajax_callback($form, &$form_state) {
  $commands = array();
  $form = render($form);
  $message_data = drupal_get_messages(NULL, FALSE);

  // Check to see if there were any errors with the form submission.
  if (!isset($message_data['error']) || !count($message_data['error'])) {
    $commands[] = array(
      'command' => 'fancyLoginClosePopup',
    );
  }
  $messages = theme('status_messages');
  $scripts = drupal_add_js();
  if (!empty($scripts['settings'])) {
    $settings = '<script type="text/javascript">jQuery.extend(Drupal.settings, ';
    $settings .= drupal_json_encode(call_user_func_array('array_merge_recursive', $scripts['settings']['data']));
    $settings .= ');</script>';
  }
  $commands[] = ajax_command_replace(NULL, $messages . $form . $settings);
  return array(
    '#type' => 'ajax',
    '#commands' => $commands,
  );
}

/**
 * Implements hook_block_info().
 */
function fancy_login_block_info() {
  $blocks['fancy_login_login_block'] = array(
    'info' => t('Fancy Login Link'),
    'cache' => DRUPAL_CACHE_PER_ROLE,
  );
  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function fancy_login_block_view($delta = '') {
  if ($delta == 'fancy_login_login_block') {
    if (user_is_anonymous()) {
      $block['subject'] = t('Login');
      $block['content'] = array(
        'link' => array(
          '#markup' => l(variable_get('fancy_login_login_block_default_text', 'Login'), 'user/login'),
          '#prefix' => '<div id="fancy_login_login_link_wrapper">',
          '#suffix' => '</div>',
        ),
      );
      return $block;
    }
  }
}

/**
 * Implements hook_theme().
 */
function fancy_login_theme() {
  $path = drupal_get_path('module', 'fancy_login');
  return array(
    'ssl_icon' => array(
      'arguments' => array(
        'base_url' => NULL,
      ),
      'path' => $path . '/templates',
      'template' => 'ssl-icon',
    ),
  );
}

/**
 * Implements hook_variable_group_info().
 */
function fancy_login_variable_group_info() {
  $groups['fancy_login'] = array(
    'title' => t('Fancy Login'),
    'description' => t('Variables related to the fancy login module'),
    'path' => array(
      'admin/config/people/fancy_login',
    ),
  );
  return $groups;
}

/**
 * Implements hook_variable_info().
 *
 * @link http://api.drupalhelp.net/api/variable/variable.api.php/function/hook_variable_info/7
 */
function fancy_login_variable_info($options) {
  $variable['fancy_login_login_block_default_text'] = array(
    'title' => t('Login block default text'),
    'type' => 'string',
    'group' => 'fancy_login',
  );
  return $variable;
}

Functions

Namesort descending Description
fancy_login_block_info Implements hook_block_info().
fancy_login_block_view Implements hook_block_view().
fancy_login_forms Implements hook_forms().
fancy_login_form_alter Implements hook_form_alter().
fancy_login_insert_current_path Insert current path in to $_GET['q'].
fancy_login_menu Implements hook_menu().
fancy_login_page_build Implements hook_page_build().
fancy_login_permission Implments hook_permission().
fancy_login_theme Implements hook_theme().
fancy_login_user_login_block_ajax_callback Ajax callback function for fancy_login_user_login_block submit button.
fancy_login_user_pass_ajax_callback Ajax callback function for fancy_login_user_pass submit button.
fancy_login_user_register_form_ajax_callback Ajax callback function for fancy_login_user_register_form submit button.
fancy_login_variable_group_info Implements hook_variable_group_info().
fancy_login_variable_info Implements hook_variable_info().