You are here

function dba_table_overview in Database Administration 5

Same name and namespace in other branches
  1. 7 dba.admin.inc \dba_table_overview()
1 call to dba_table_overview()
dba_admin_tables_view in ./dba.module
Display the contents of the selected table.

File

./dba.module, line 663
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_table_overview($table) {
  $rows = array();
  $tables = dba_get_active_tables();
  $quantity = count($tables);
  if ($quantity == 1) {
    drupal_set_title(t('View table %table', array(
      '%table' => $table,
    )));
    if (user_access('dba administer database')) {
      $primary = dba_get_primary_key($table);
    }
    else {
      $primary = NULL;
    }
    $fields = dba_get_fields($table);
    foreach ($fields as $field) {
      $header[] = array(
        'data' => "{$field}",
        'field' => "{$field}",
      );
    }
    $sql = "SELECT * FROM {$table}";
    $sql .= tablesort_sql($header);
    $result = pager_query($sql, 20);
    if (!is_null($primary)) {
      $header[] = t('actions');
    }
    if (db_num_rows($result)) {
      while ($row = db_fetch_array($result)) {
        $line = array_map('check_plain', array_values($row));
        if (!is_null($primary)) {
          $id = "{$row[$primary]}";
          $id = str_replace('/', '__2F_', $id);
          $actions = '[' . l(t('edit'), "admin/build/database/table/{$table}/view/edit/{$primary}/{$id}") . ']';
          $actions .= ' [' . l(t('delete'), "admin/build/database/table/{$table}/view/delete/{$primary}/{$id}") . ']';
          $line[] = $actions;
        }
        $rows[] = $line;
        unset($line);
      }
      if ($pager = theme('pager', NULL, 20, 0)) {
        $rows[] = array(
          array(
            'data' => $pager,
            'colspan' => sizeof($fields),
          ),
        );
      }
      $output = theme('table', $header, $rows);
    }
    else {
      $output = t('The table is empty.');
    }
  }
  else {
    drupal_set_message(t('Unable to view more than one table at a time.'), 'error');
    $output .= dba_database_overview_form();
  }
  return $output;
}