You are here

function dba_view_table in Database Administration 7

1 call to dba_view_table()
dba_view_tables in ./dba.admin.inc
View selected tables.

File

./dba.admin.inc, line 174

Code

function dba_view_table($table) {
  static $pager_element = 0;
  $unprefixed_table = dba_strip_prefix($table);
  $fields = dba_get_fields($unprefixed_table);
  $header = array();
  $options = array();
  if (count($fields)) {
    $count = 0;
    foreach ($fields as $field) {
      $header[$field] = array(
        'data' => "{$field}",
        'field' => "{$field}",
      );
      if (!$count++) {

        // set default sort column as first field
        $header[$field]['sort'] = 'desc';
      }
    }

    // SELECT * FROM $table with pager and tablesort
    $conn = db_set_active(variable_get('dba_active_database', 'default'));
    $query = db_select($unprefixed_table)
      ->fields($unprefixed_table)
      ->extend('PagerDefault')
      ->extend('TableSort')
      ->element($pager_element);
    $rows = $query
      ->limit(25)
      ->orderByHeader($header)
      ->execute();
    db_set_active($conn);
    $count = 0;
    while ($row = $rows
      ->fetchAssoc()) {
      foreach ($fields as $field) {
        $options[$count][$field] = check_plain($row[$field]);
      }
      $count++;
    }
  }
  $form[$table]['rows'] = array(
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $options,
    '#empty' => t('The !table table is empty.', array(
      '!table' => $table,
    )),
  );
  $form[$table]['pager'] = array(
    '#markup' => theme('pager', array(
      'tags' => NULL,
      'element' => $pager_element,
    )),
  );
  $pager_element++;
  return $form;
}