You are here

domain_prefix.module in Domain Access 6.2

Same filename and directory in other branches
  1. 5 domain_prefix/domain_prefix.module

Interface for selective table prefixing for use with Domain Access.

File

domain_prefix/domain_prefix.module
View source
<?php

/**
 * @defgroup domain_prefix Domain Prefix: dynamic table prefixing
 *
 * Allows for the dynamic table prefixing of selected database tables.
 */

/**
 * @file
 * Interface for selective table prefixing for use with Domain Access.
 *
 * @ingroup domain_prefix
 */

/**
 * Constant definitions for the various actions.
 */
define('DOMAIN_PREFIX_IGNORE', 1);
define('DOMAIN_PREFIX_CREATE', 2);
define('DOMAIN_PREFIX_COPY', 4);
define('DOMAIN_PREFIX_DROP', 8);
define('DOMAIN_PREFIX_UPDATE', 16);

/**
 * Implementats hook_domain_bootstrap_full().
 */
function domain_prefix_domain_bootstrap_full($domain) {

  // We must make sure that we can reset the $db_prefix to that
  // provided by settings.php, which is available the first time this
  // function is called.
  static $original_prefix;
  global $db_prefix;
  if (!isset($original_prefix)) {
    $original_prefix = $db_prefix;
  }

  // Now load the dynamic tables.
  $tables = array();
  $prefix = 'domain_' . $domain['domain_id'] . '_';
  $result = db_query("SELECT tablename FROM {domain_prefix} WHERE domain_id = %d AND status > %d", $domain['domain_id'], 1);
  while ($data = db_fetch_array($result)) {
    $tables[] = $data['tablename'];
  }
  if (!empty($tables)) {
    $new_prefix = array();

    // There might be global prefixing; if so, prepend the global.
    if (is_string($db_prefix)) {
      $new_prefix['default'] = $db_prefix;
      $prefix = $db_prefix . $prefix;
    }
    foreach ($tables as $table) {
      $new_prefix[$table] = $prefix;
    }

    // Keep existing $db_prefix values as they were defined in settings.php,
    // in case they're not overwritten in $new_prefix.
    // See http://drupal.org/node/632950 for background.
    if (is_array($db_prefix)) {
      $db_prefix = array_merge($db_prefix, $new_prefix);
    }
    else {
      $db_prefix = $new_prefix;
    }
  }
  else {
    $db_prefix = $original_prefix;
  }

  // This function may already be loaded, see http://drupal.org/node/929540.
  if (function_exists('domain_prefix_domainpath')) {
    return;
  }

  // Check for prefixes of the url_alias table, and if present add hook_domainprefix().
  $count = db_result(db_query("SELECT 1 FROM {domain_prefix} WHERE status > 1 AND tablename = 'url_alias'"));
  if ($count > 0) {
    include 'domain_prefix.path.inc';
  }
}

/**
 * Implement hook_menu()
 */
function domain_prefix_menu() {
  $items = array();
  $items['admin/build/domain/prefix'] = array(
    'title' => 'Table prefixing',
    'type' => MENU_LOCAL_TASK,
    'access arguments' => array(
      'administer table prefixing',
    ),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'domain_prefix_configure_form',
    ),
    'file' => 'domain_prefix.admin.inc',
  );
  $items['admin/build/domain/prefix/%domain'] = array(
    'title' => 'Domain prefix settings',
    'access arguments' => array(
      'administer table prefixing',
    ),
    'type' => MENU_CALLBACK,
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'domain_prefix_form',
      4,
    ),
    'file' => 'domain_prefix.admin.inc',
  );
  $items['domain_prefix_update'] = array(
    'title' => 'Domain prefix update',
    'access arguments' => array(
      'administer table prefixing',
    ),
    'type' => MENU_CALLBACK,
    'page callback' => 'domain_prefix_update',
    'file' => 'domain_prefix.admin.inc',
  );
  return $items;
}

/**
 * Implement hook_perm
 */
function domain_prefix_perm() {
  return array(
    'administer table prefixing',
  );
}

/**
 * Implement hook_theme()
 */
function domain_prefix_theme() {
  $themes = array(
    'domain_prefix_configure_form' => array(
      'arguments' => array(
        'form' => array(),
      ),
    ),
  );
  return $themes;
}

/**
 * Implement hook_enable().
 *
 * Register the domain_prefix with the domain module so it's loaded during domain
 * bootstrap and can implement domain_bootstrap hooks.
 */
function domain_prefix_enable() {
  domain_bootstrap_register();
}

/**
 * Implement hook_disable().
 */
function domain_prefix_disable() {
  domain_bootstrap_unregister('domain_prefix');
}

/**
 * Uniform prefix string
 *
 * @param $domain_id
 *  The domain_id taken from {domain}.
 */
function domain_prefix_string($domain_id) {
  return 'domain_' . $domain_id . '_';
}

/**
 * Check for existing table prefixing.
 */
function domain_prefix_get_prefix() {
  global $db_prefix;

  // Check for existing table prefixing.
  $prefix = NULL;
  if (!empty($db_prefix)) {
    if (is_array($db_prefix)) {
      $prefix = $db_prefix['default'];
    }
    else {
      $prefix = $db_prefix;
    }
  }
  return $prefix;
}

/**
 * Does a table exist -- we use this to bypass Drupal's default table prefixing check.
 *
 * @param $prefix
 *   The table prefix used with this domain. Optional.
 * @param $table
 *   The name of the table being acted upon.
 */
function domain_prefix_table_exists($prefix, $table) {
  return db_table_exists(db_escape_table($prefix . $table));
}

/**
 * Implement hook_domainlinks()
 *
 * @param $domain
 *  The currently active $domain array, provided by domain_lookup().
 */
function domain_prefix_domainlinks($domain) {
  if (user_access('administer table prefixing') && $domain['domain_id'] > 0) {
    $links[] = array(
      'title' => t('tables'),
      'path' => 'admin/build/domain/prefix/' . $domain['domain_id'],
    );
    return $links;
  }
  return FALSE;
}

/**
 * Implement hook_domainupdate().
 */
function domain_prefix_domainupdate($op, $domain, $form_state = array()) {

  // Include the extra functions.
  module_load_include('inc', 'domain_prefix', 'domain_prefix.admin');
  switch ($op) {
    case 'create':
      $rule = variable_get('domain_prefix_options', 1);
      if ($rule) {

        // Get the current settings.
        $settings = variable_get('domain_prefix', NULL);
        if (!empty($settings)) {
          $settings['domain_id'] = $domain['domain_id'];
          $form_state['values']['op'] = t('Generate domain tables');
          $form_state['values']['execute'] = TRUE;
          drupal_execute('domain_prefix_form', $form_state, $domain, $settings);
        }
      }
      break;
    case 'delete':
      domain_prefix_drop_records($domain['domain_id']);
      break;
  }
}

Related topics

Functions

Namesort descending Description
domain_prefix_disable Implement hook_disable().
domain_prefix_domainlinks Implement hook_domainlinks()
domain_prefix_domainupdate Implement hook_domainupdate().
domain_prefix_domain_bootstrap_full Implementats hook_domain_bootstrap_full().
domain_prefix_enable Implement hook_enable().
domain_prefix_get_prefix Check for existing table prefixing.
domain_prefix_menu Implement hook_menu()
domain_prefix_perm Implement hook_perm
domain_prefix_string Uniform prefix string
domain_prefix_table_exists Does a table exist -- we use this to bypass Drupal's default table prefixing check.
domain_prefix_theme Implement hook_theme()

Constants