You are here

function domain_prefix_form in Domain Access 6.2

Same name and namespace in other branches
  1. 5 domain_prefix/domain_prefix.module \domain_prefix_form()

The table prefixing page for a domain.

Parameters

$domain: The domain array provided by domain_lookup().

$arguments: An array of additional hidden key/value pairs to pass to the form. Used by child modules to control behaviors.

2 string references to 'domain_prefix_form'
domain_prefix_domainupdate in domain_prefix/domain_prefix.module
Implement hook_domainupdate().
domain_prefix_menu in domain_prefix/domain_prefix.module
Implement hook_menu()

File

domain_prefix/domain_prefix.admin.inc, line 216
Admin page functions for selective table prefixing for use with Domain Access.

Code

function domain_prefix_form($form_state, $domain, $arguments = array()) {

  // If an invalid request was made, reject it.
  if ($domain == -1) {
    return drupal_access_denied();
  }

  // We must use the settings from the root domain.
  $default = domain_default();
  domain_set_domain($default['domain_id'], TRUE);
  drupal_set_title(t('Table prefixing for !domain', array(
    '!domain' => check_plain($domain['sitename']),
  )));

  // Remove the disallowed tables.
  $disallow = domain_prefix_disallow();

  // Get the default table set and settings.
  $default = domain_prefix_get_tables();

  // Get the current settings.
  $info = variable_get('domain_prefix', NULL);
  $settings = $info['settings'];
  $source_defaults = $info['sources'];

  // Check the defaults against those saved for this domain.
  $result = db_query("SELECT tablename, source FROM {domain_prefix} WHERE domain_id = %d", $domain['domain_id']);
  while ($sourcedata = db_fetch_array($result)) {
    $source_defaults['_source_' . $sourcedata['tablename']] = $sourcedata['source'];
  }

  // Get the root source table data.
  $root = domain_default();
  if (empty($settings) && empty($_POST)) {
    drupal_set_message(t('There are no default settings configured.'));
  }

  // Get the stored table data for this domain.
  $tables = domain_prefix_lookup($domain['domain_id']);
  $submit = t('Update domain tables');
  if (empty($tables)) {
    if (empty($_POST) && empty($form_state['values']['execute']) && empty($arguments['user_submitted'])) {
      drupal_set_message(t('The table creation sequence has not run for this domain.'));
    }
    $submit = t('Generate domain tables');
    $table_options = $default;
  }
  else {
    $table_options = array();
    $settings = array();

    // Process the existing tables.
    foreach ($tables as $name => $table) {
      if (array_key_exists($name, $default)) {
        $table_options[$table['tablename']] = $table;
        $settings[$table['tablename']] = $table['status'];
      }
    }

    // Check to see if new tables have been added.
    foreach ($default as $name => $table) {
      if (!array_key_exists($name, $table_options)) {
        $table_options[$name] = $table;
      }
    }
  }

  // Sort the options by module.
  uasort($table_options, '_domain_prefix_sort');

  // All tables are prefixed as 'domain_#_'
  $prefix = domain_prefix_string($domain['domain_id']);

  // Generate the form.
  $form = array();

  // The $arguments arrray allows other modules to pass values to change the bahavior
  // of submit and validate functions.
  if (!empty($arguments)) {
    $form['domain_arguments'] = array(
      '#type' => 'value',
      '#value' => $arguments,
    );
  }
  $delete_flag = 0;

  // Flag for the theme function delete column
  $last = '';

  // Flag for module groupings.
  foreach ($table_options as $table => $info) {
    if (!in_array($table, $disallow)) {
      if (empty($settings[$table])) {
        $settings[$table] = DOMAIN_PREFIX_IGNORE;
      }
      $options = array();
      $options[DOMAIN_PREFIX_IGNORE] = t('ignore');
      $options[DOMAIN_PREFIX_CREATE] = t('create');
      $options[DOMAIN_PREFIX_COPY] = t('copy');
      if ($settings[$table] > 0) {
        $exists = domain_prefix_table_exists($prefix, $table);
        if ($exists > 0) {
          $options[DOMAIN_PREFIX_UPDATE] = t('update');
          $options[DOMAIN_PREFIX_DROP] = t('drop');
          $delete_flag++;
        }
      }
      $module = domain_prefix_get_name($info);
      if ($last != $module) {
        $last = $module;
      }
      else {
        $module = '';
      }
      $form['domain_prefix'][$table] = array(
        '#type' => 'radios',
        '#title' => $table,
        '#options' => $options,
        '#default_value' => $settings[$table],
        '#description' => $module,
      );

      // Get the table copying options for this entry.
      // Can't pass a zero through FormAPI select.
      $sources = array();
      $sources[0] = $root['sitename'];

      // Check to see if other table prefixes have been created.
      $result = db_query("SELECT dp.domain_id, d.sitename FROM {domain_prefix} dp\n        INNER JOIN {domain} d ON dp.domain_id = d.domain_id\n        WHERE dp.tablename = '%s' AND dp.status > %d", $table, 1);
      while ($data = db_fetch_array($result)) {

        // Cannot copy onto itself.
        if ($data['domain_id'] != $domain['domain_id']) {
          $sources[$data['domain_id']] = $data['sitename'];
        }
      }
      $form['domain_source']['_source_' . $table] = array(
        '#type' => 'select',
        '#title' => '',
        '#options' => $sources,
        '#default_value' => isset($source_defaults['_source_' . $table]) ? $source_defaults['_source_' . $table] : DOMAIN_PREFIX_IGNORE,
      );
    }
  }
  $form['#theme'] = 'domain_prefix_configure_form';
  $form['table_data'] = array(
    '#type' => 'value',
    '#value' => $default,
  );
  $form['prefix_theme'] = array(
    '#type' => 'value',
    '#value' => $delete_flag,
  );
  $form['domain_id'] = array(
    '#type' => 'value',
    '#value' => $domain['domain_id'],
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => $submit,
  );

  // Reset the active domain.
  domain_reset_domain(TRUE);
  return $form;
}