<?php

/**
 * @file
 * Install, update and uninstall functions for the AJAX REGISTER module.
 */

/**
 * Implements hook_install().
 */
function ajax_register_install() {
  //Make sure we're heavy so our form_alter hook gets executed last.
  db_update('system')
    ->fields(array('weight' => 1000))
    ->condition('name', 'ajax_register')
    ->execute();
}

/**
 * Implements hook_uninstall().
 */
function ajax_register_uninstall() {
  db_query("DELETE FROM {variable} WHERE name LIKE 'ajax_register_%'");
  cache_clear_all('variables', 'cache_bootstrap');
}

/**
 * Make sure we're heavy so our form_alter hook gets executed last.
 */
function ajax_register_update_7406() {
  db_update('system')
    ->fields(array('weight' => 1000))
    ->condition('name', 'ajax_register')
    ->execute();
}

/**
 * Rename existing redirect variables and create new redirect behavior variable from redirect url.
 */
function ajax_register_update_7407() {
  // Login and register redirect behaviors should initially refresh the page.
  // This has always been the default functionality provided by the module.
  $login_behavior = 'refresh';
  $register_behavior = 'refresh';
  // Set the password redirect to none, as this was the default behavior.
  $password_behavior = 'none';
  // Load the existing variables and then delete them.
  $login_redirect_url = variable_get('ajax_register_login_form_redirect', '');
  variable_del('ajax_register_login_form_redirect');
  if (!empty($login_redirect_url)) {
    $login_behavior = 'custom';
  }
  $register_redirect_url = variable_get('ajax_register_register_form_redirect', '');
  variable_del('ajax_register_register_form_redirect');
  if (!empty($register_redirect_url)) {
    $register_behavior = 'custom';
  }
  elseif ($register_redirect_url == '<noredirect>') {
    $register_behavior = 'none';
    $register_redirect_url = '';
  }
  // Save the new variables.
  variable_set('ajax_register_login_redirect_behavior', $login_behavior);
  variable_set('ajax_register_register_redirect_behavior', $register_behavior);
  variable_set('ajax_register_password_redirect_behavior', $password_behavior);
  // Only set the new custom URL redirects if they're not actually empty.
  if (!empty($login_redirect_url)) {
    variable_set('ajax_register_login_redirect_url', $login_redirect_url);
  }
  if (!empty($register_redirect_url)) {
    variable_set('ajax_register_register_redirect_url', $register_redirect_url);
  }
}
