Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function yourthemename_tablefield($variables) {
- $header = $variables['header'];
- $rows = $variables['rows'];
- $attributes = $variables['attributes'];
- $caption = $variables['caption'];
- $colgroups = $variables['colgroups'];
- $header_orientation = $variables['header_orientation'];
- $sticky = $variables['sticky'];
- $no_striping = !$variables['striping'];
- $empty = $variables['empty'];
- if ($variables['sortable']) {
- drupal_add_css(drupal_get_path('module', 'tablefield') . '/css/tablefield_sort.css');
- $tooltip = ' title="' . t('Sort column') . '"';
- }
- else {
- $tooltip = NULL;
- }
- drupal_add_css(drupal_get_path('module', 'tablefield') . '/css/tablefield_sort.css'); $empty = $variables['empty'];
- // If the only header is the first column make the first row a normal row.
- if (!isset($rows['row_0']) && $header_orientation == 'Vertical') {
- $rows['row_0'] = $header;
- ksort($rows);
- $header = array();
- }
- // Add sticky headers, if applicable.
- if (count($header) && $sticky) {
- drupal_add_js('misc/tableheader.js');
- // Add 'sticky-enabled' class to the table to identify it for JS.
- // This is needed to target tables constructed by this function.
- $attributes['class'][] = 'sticky-enabled';
- }
- $output = '<table' . drupal_attributes($attributes) . ">\n";
- if (isset($caption)) {
- $output .= '<caption>' . $caption . "</caption>\n";
- }
- // Format the table columns:
- if (count($colgroups)) {
- foreach ($colgroups as $number => $colgroup) {
- $attributes = array();
- // Check if we're dealing with a simple or complex column.
- if (isset($colgroup['data'])) {
- foreach ($colgroup as $key => $value) {
- if ($key == 'data') {
- $cols = $value;
- }
- else {
- $attributes[$key] = $value;
- }
- }
- }
- else {
- $cols = $colgroup;
- }
- // Build colgroup.
- if (is_array($cols) && count($cols)) {
- $output .= ' <colgroup' . drupal_attributes($attributes) . '>';
- $i = 0;
- foreach ($cols as $col) {
- $output .= ' <col' . drupal_attributes($col) . ' />';
- }
- $output .= " </colgroup>\n";
- }
- else {
- $output .= ' <colgroup' . drupal_attributes($attributes) . " />\n";
- }
- }
- }
- // Add the 'empty' row message if available.
- if (!count($rows) && $empty) {
- $header_count = 0;
- foreach ($header as $header_cell) {
- if (is_array($header_cell)) {
- $header_count += isset($header_cell['colspan']) ? $header_cell['colspan'] : 1;
- }
- else {
- $header_count++;
- }
- }
- $rows[] = array(
- array(
- 'data' => $empty,
- 'colspan' => $header_count,
- 'class' => array('empty', 'message'),
- ),
- );
- }
- // Add rowspan and colspan based on empty cells
- $previous_row = false;
- foreach ($rows as $row => $columns) {
- foreach ($columns as $col => $cell) {
- preg_match("/(?:#colspan#)/", $cell['data'], $match);
- // Is #colspan# in the data?
- if(!empty($match)) {
- $keys = array_keys($rows[$row]);
- $search = array_search($col, $keys);
- $prev = $keys[intval($search) - 1];
- $colspan = isset($rows[$row][$prev]['colspan']) ? $rows[$row][$prev]['colspan'] : 1;
- $rows[$row][$prev]['colspan'] = $colspan + 1;
- unset($rows[$row][$col]);
- }
- preg_match("/(?:#rowspan#)/", $cell['data'], $match);
- // Is #rowpsan# in the data?
- if(!empty($match)) {
- if($previous_row !== false) {
- $keys = array_keys($rows[$row]);
- $search = array_search($col,$keys);
- $col = $keys[intval($search)];
- $prev_row = $previous_row;
- while (true) {
- if(isset($rows[$prev_row][$col])) {
- $rowspan = isset($rows[$prev_row][$col]['rowspan']) ? $rows[$prev_row][$col]['rowspan'] : 1;
- $rows[$prev_row][$col]['rowspan'] = $rowspan + 1;
- break;
- }
- $prev_row--;
- }
- unset($rows[$row][$col]);
- }
- }
- }
- $previous_row = $row;
- }
- // Format the table header:
- if (count($header)) {
- $ts = tablesort_init($header);
- // HTML requires that the thead tag has tr tags in it followed by tbody
- // tags. Using ternary operator to check and see if we have any rows.
- $output .= (count($rows) ? ' <thead' . $tooltip . '><tr>' : ' <tr>');
- foreach ($header as $cell) {
- $cell = tablesort_header($cell, $header, $ts);
- $output .= _theme_table_cell($cell, TRUE);
- }
- // Using ternary operator to close the tags based on whether or not there
- // are rows.
- $output .= (count($rows) ? " </tr></thead>\n" : "</tr>\n");
- }
- else {
- $ts = array();
- }
- // Format the table rows:
- if (count($rows)) {
- $output .= "<tbody>\n";
- $flip = array('even' => 'odd', 'odd' => 'even');
- $class = 'even';
- foreach ($rows as $number => $row) {
- // Check if we're dealing with a simple or complex row.
- if (isset($row['data'])) {
- $cells = $row['data'];
- $no_striping = isset($row['no_striping']) ? $row['no_striping'] : FALSE;
- // Set the attributes array and exclude 'data' and 'no_striping'.
- $attributes = $row;
- unset($attributes['data']);
- unset($attributes['no_striping']);
- }
- else {
- $cells = $row;
- $attributes = array();
- }
- if (count($cells)) {
- // Add odd/even class.
- if (!$no_striping) {
- $class = $flip[$class];
- $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, ($header_orientation === 'Both' || $header_orientation === 'Vertical') && $i == 1);
- $i++;
- }
- $output .= " </tr>\n";
- }
- }
- $output .= "</tbody>\n";
- }
- $output .= "</table>\n";
- return $output;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement