domain_prefix_form

Definition

domain_prefix_form($domain_id, $arguments = array())
domain_prefix/domain_prefix.module, line 325

Description

The table prefixing page for a domain.

Parameters

$domain_id The domain_id taken from {domain}.

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

Code

function domain_prefix_form($domain_id, $arguments = array()) {
  // We must be on the root domain!
  $default = domain_default();
  domain_goto($default);
  $domain = domain_lookup($domain_id);
  drupal_set_title(t('Table prefixing for !domain', array('!domain' => $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 = unserialize(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) && !$_POST) {
    drupal_set_message(t('There are no default settings configured.'));
  }

  // Get the stored table data for this domain.
  $tables = domain_prefix_lookup($domain_id);
  $submit = t('Update domain tables');
  if (empty($tables)) {
    if (!$_POST && !$form_values['execute'] && !$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();
    foreach ($tables as $table) {
      $table_options[$table['tablename']] = $table;
      $settings[$table['tablename']] = $table['status'];
    }
    $table_options = array_merge($default, $table_options);
  }
  // Sort them by module
  uasort($table_options, '_domain_prefix_sort');
  // All tables are prefixed as 'domain_#_'
  $prefix = domain_prefix_string($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 = $info['module'];
      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 INNER JOIN {domain} d ON dp.domain_id = d.domain_id WHERE dp.tablename = '%s' AND dp.status > 1", $table);
      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' => $source_defaults['_source_'. $table],
      );
    }
  }
  $form['#theme'] = 'domain_prefix_configure_form';
  $form['prefix_theme'] = array('#type' => 'value', '#value' => $delete_flag);
  $form['domain_id'] = array('#type' => 'value', '#value' => $domain_id);
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => $submit
  );
  return $form;
}