You are here

fapi_table.module in Subform 5

File

fapi_table.module
View source
<?php

/**
 * @file
 */
function fapi_table_elements() {
  $type['table'] = array(
    '#border' => 1,
    '#width' => "100%",
    '#height' => "100%",
    'rows' => array(),
  );
  return $type;
}

/**
 * Format a group of form items.
 *
 * @param $element
 *   An associative array containing the properties of the element.
 *   Properties used: attributes, title, value, description, children, collapsible, collapsed
 * @return
 *   A themed HTML string representing the form item group.
 */
function theme_fapi_table($table_element) {
  $types_lookup = array(
    'boolean' => 'alphanumeric',
    'integer' => 'numeric',
    'double' => 'numeric',
    'string' => 'alphanumeric',
    'array' => 'alphanumeric',
    'object' => 'alphanumeric',
    'resource' => 'alphanumeric',
    'NULL' => 'alphanumeric',
  );
  drupal_add_js(drupal_get_path('module', 'subform') . '/table.js');
  $table_contents = '';
  foreach ($table_element['rows'] as $row_id => $row) {
    $header = $table_element['#header'] && $row_id == 0;
    if (is_string($row_id) && substr($row_id, 0, 1) == '#') {
      continue;
    }
    if ($header) {
      $table_contents .= '<thead><tr>';
    }
    else {
      $table_contents .= "<tr class=" . ($row_id % 2 ? "'even'" : "'odd'") . ">";
    }
    foreach ($row as $cell_id => $cell) {
      if (is_string($cell_id) && substr($cell_id, 0, 1) == "#") {
        continue;
      }
      if ($header) {
        $classes = '';
        if ($cell['#table-sortable']) {
          $classes .= ' table-sortable:' . $types_lookup[$cell['#table-sortable']];
        }
        if ($cell['#table-filterable']) {
          $classes .= ' table-filterable';
        }
        $table_contents .= "<th class=\"{$classes}\">";
      }
      else {
        $table_contents .= '<td>';
      }
      if (isset($cell['#children'])) {
        $table_contents .= $cell['#children'];
      }
      if ($header) {
        $table_contents .= "</th>";
      }
      else {
        $table_contents .= "</td>";
      }
    }
    if ($header) {
      $table_contents .= "</tr></thead>";
    }
    else {
      $table_contents .= "</tr>";
    }
  }
  if (!isset($table_element['#attributes']['border']) && isset($table_element['#border'])) {
    $table_element['#attributes']['border'] = $table_element['#border'];
  }
  if (!isset($table_element['#attributes']['width']) && isset($table_element['#width'])) {
    $table_element['#attributes']['width'] = $table_element['#width'];
  }
  if (!isset($table_element['#attributes']['height']) && isset($table_element['#height'])) {
    $table_element['#attributes']['height'] = $table_element['#height'];
  }
  $table_element['#attributes']['class'] .= " table-autosort table-autofilter";

  // $table_element['#attributes']['class'] .= "table-autosort";
  $attributes = drupal_attributes($table_element['#attributes']);
  return "<table {$attributes} >{$table_contents}</table>\n";
}

Functions

Namesort descending Description
fapi_table_elements @file
theme_fapi_table Format a group of form items.