You are here

function theme_diff_table in Diff 7.2

Same name and namespace in other branches
  1. 5.2 diff.module \theme_diff_table()
  2. 6.2 diff.theme.inc \theme_diff_table()
  3. 6 diff.module \theme_diff_table()

Return a themed table. This is a modified version of theme_table, adding colgroup tag and col tag options.

Parameters

$header: An array containing the table headers. Each element of the array can be either a localized string or an associative array with the following keys:

  • "data": The localized title of the table column.
  • "field": The database field represented in the table column (required if user is to be able to sort on this column).
  • "sort": A default sort order for this column ("asc" or "desc").
  • Any HTML attributes, such as "colspan", to apply to the column header cell.

$rows: An array of table rows. Every row is an array of cells, or an associative array with the following keys:

  • "data": an array of cells
  • Any HTML attributes, such as "class", to apply to the table row.

Each cell can be either a string or an associative array with the following keys:

  • "data": The string to display in the table cell.
  • "header": Indicates this cell is a header.
  • Any HTML attributes, such as "colspan", to apply to the table cell.

Here's an example for $rows: @verbatim $rows = array( // Simple row array( 'Cell 1', 'Cell 2', 'Cell 3' ), // Row with attributes on the row and some of its cells. array( 'data' => array('Cell 1', array('data' => 'Cell 2', 'colspan' => 2)), 'class' => 'funky' ) ); @endverbatim

$attributes: An array of HTML attributes to apply to the table tag.

$caption: A localized string to use for the <caption> tag.

$cols: An array of table colum groups. Every column group is an array of columns, or an associative array with the following keys:

  • "data": an array of cells
  • Any HTML attributes, such as "class", to apply to the table column group.

Each column can be either an empty array or associative array with the following keys:

  • Any HTML attributes, such as "class", to apply to the table column group.

Here's an example for $cols: @verbatim $cols = array( // Simple colgroup. array(), // Simple colgroup with attributes. array( 'data' => array(), 'colspan' => 2, 'style' => 'color: green;', ), // Simple colgroup with one col. array( array(), ), // Colgroup with attributes on the colgroup and some of its cols. array( 'data' => array(array('class' => 'diff-marker'), array('colspan' => 2)), 'class' => 'funky', ), ); @endverbatim

The HTML will look as follows: @verbatim <table> <!-- Simple colgroup. --> <colgroup />

<!-- Simple colgroup with attributes. --> <colgroup colspan="2" style="color: green;" />

<!-- Simple colgroup with one col. --> <colgroup> <col /> </colgroup>

<!-- Colgroup with attributes on the colgroup and some of its cols. --> <colgroup class="funky"> <col class="diff-marker" /> <col colspan="2" /> </colgroup> ... </table> @endverbatim

Return value

An HTML string representing the table.

2 theme calls to theme_diff_table()
diff_diffs_show in ./diff.pages.inc
Create output string for a comparison of 'node' between versions 'old_vid' and 'new_vid'.
diff_node_form_build_preview_changes in ./diff.module
Callback if 'View changes' is pressed.

File

./diff.theme.inc, line 154
Themeable function callbacks for diff.module.

Code

function theme_diff_table($vars) {
  $header = isset($vars['header']) ? $vars['header'] : array();
  $rows = isset($vars['rows']) ? $vars['rows'] : array();
  $attributes = isset($vars['attributes']) ? $vars['attributes'] : array();
  $caption = isset($vars['caption']) ? $vars['caption'] : '';
  $cols = isset($vars['cols']) ? $vars['cols'] : array();
  $output = '<table' . drupal_attributes($attributes) . ">\n";
  if (isset($caption)) {
    $output .= '<caption>' . $caption . "</caption>\n";
  }

  // Format the table columns:
  if (count($cols)) {
    foreach ($cols as $number => $col) {
      $attributes = array();

      // Check if we're dealing with a simple or complex column
      if (isset($col['data'])) {
        foreach ($col as $key => $value) {
          if ($key == 'data') {
            $cells = $value;
          }
          else {
            $attributes[$key] = $value;
          }
        }
      }
      else {
        $cells = $col;
      }

      // Build colgroup
      if (is_array($cells) && count($cells)) {
        $output .= ' <colgroup' . drupal_attributes($attributes) . '>';
        $i = 0;
        foreach ($cells as $cell) {
          $output .= ' <col' . drupal_attributes($cell) . ' />';
        }
        $output .= " </colgroup>\n";
      }
      else {
        $output .= ' <colgroup' . drupal_attributes($attributes) . " />\n";
      }
    }
  }

  // Format the table header:
  if (count($header)) {
    $ts = tablesort_init($header);
    $output .= ' <thead><tr>';
    foreach ($header as $cell) {
      $cell = tablesort_header($cell, $header, $ts);
      $output .= _theme_table_cell($cell, TRUE);
    }
    $output .= " </tr></thead>\n";
  }

  // Format the table rows:
  $output .= "<tbody>\n";
  if (count($rows)) {
    $flip = array(
      'even' => 'odd',
      'odd' => 'even',
    );
    $class = 'even';
    foreach ($rows as $number => $row) {
      $attributes = array();

      // Check if we're dealing with a simple or complex row
      if (isset($row['data'])) {
        foreach ($row as $key => $value) {
          if ($key == 'data') {
            $cells = $value;
          }
          else {
            $attributes[$key] = $value;
          }
        }
      }
      else {
        $cells = $row;
      }

      // Add odd/even class
      $class = $flip[$class];
      if (isset($attributes['class'])) {
        $attributes['class'] .= ' ' . $class;
      }
      else {
        $attributes['class'] = $class;
      }

      // Build row
      $output .= ' <tr' . drupal_attributes($attributes) . '>';
      $i = 0;
      foreach ($cells as $cell) {
        $cell = tablesort_cell($cell, $header, $ts, $i++);
        $output .= _theme_table_cell($cell);
      }
      $output .= " </tr>\n";
    }
  }
  $output .= "</tbody></table>\n";
  return $output;
}