You are here

function dba_check_table_form_pre_render in Database Administration 5

1 string reference to 'dba_check_table_form_pre_render'
dba_check_tables_form in ./dba.module

File

./dba.module, line 967
Allows administrators direct access to their Drupal database. Written by Jeremy Andrews <jeremy@kerneltrap.org>, June 2004. PostgreSQL functionality provided by AAM <aam@ugpl.de> Major security audit, porting, and maintenance by Derek…

Code

function dba_check_table_form_pre_render($form_id, &$form) {
  if (form_get_errors()) {

    // If there's a validation error (e.g. #token is wrong), return
    // immediately since we can't trust $form.
    return;
  }
  $action = isset($_POST['op']) ? $_POST['op'] : 'check';
  $type = $form['check_options']['check_type']['#value'];
  $tables = explode(',', $form['check_tables']['tables']['#value']);
  if ($action == t('Repair')) {
    drupal_set_title(t('Performing table repair.'));
    $result = dba_repair_tables($tables);
  }
  else {
    drupal_set_title(t('Performing %type table check', array(
      '%type' => check_plain($type),
    )));
    $result = dba_check_tables($tables, $type);
  }

  // Construct the output of the operation as a table, and see if any
  // tables need to be repaired.
  $repair = array();
  $header = array(
    t('Table'),
    t('Operation'),
    t('Message type'),
    t('Message text'),
  );
  while ($row = db_fetch_object($result)) {
    $rows[] = (array) $row;
    if ($row->Msg_type == 'status') {
      $status = $row->Msg_text;
      if ($status != 'OK' && $status != 'Table is already up to date') {

        // An error message will result if we use the database name when trying
        // to repair a table and the database has '-' in the name, so to be
        // safe we strip off the database name.
        $repair_table = explode('.', $row->Table);
        $repair[] = $repair_table[1];
      }
    }
  }
  $output .= theme('table', $header, $rows);
  if ($repair) {
    $output .= '<h3>' . t('One or more tables need repairs.') . '</h3>';
    $to_repair = 1;
  }
  else {
    $output .= '<h3>' . t('No repairs are required.') . '</h3>';
    $to_repair = 0;
  }
  $form['check_results'] = array(
    '#type' => 'fieldset',
    '#title' => t('Result'),
    '#weight' => -5,
  );
  $form['check_results']['result'] = array(
    '#type' => 'markup',
    '#value' => $output,
  );

  // Since we just added these form elements, but we're already at the
  // pre_render stage, we need to manually invoke form_builder() so that
  // these elements are rendered in the final form show to the user.
  $form['check_results'] = form_builder('dba_check_tables', $form['check_results']);
  if (user_access('dba administer database')) {
    $repair_option = variable_get('dba_repair', 0);
    if ($repair_option == 0 && $to_repair || $repair_option == 1) {
      $form['check_options']['repair'] = array(
        '#type' => 'submit',
        '#value' => t('Repair'),
        '#weight' => 20,
      );
      if (!$repair_option) {
        $form['check_options']['repair_tables'] = array(
          '#type' => 'hidden',
          '#value' => implode(',', $repair_tables),
        );
      }
    }
  }

  // Now that we know what type of check we used, we can set the default.
  $form['check_options']['check_type']['#default_value'] = $type;

  // Rebuild the form elements we've modified here, so they're
  // displayed on when the page is rendered.
  $form['check_options'] = form_builder('dba_check_tables', $form['check_options']);
}