You are here

function template_preprocess_datatables_view in DataTables 6

Same name and namespace in other branches
  1. 7.2 datatables.module \template_preprocess_datatables_view()
  2. 7 datatables.module \template_preprocess_datatables_view()

Display a view as a DataTable style.

File

./datatables.module, line 148
Provides integration of the jQuery DataTables plugin

Code

function template_preprocess_datatables_view(&$vars) {
  $view = $vars['view'];

  // We need the raw data for this grouping, which is passed in as $vars['rows'].
  // However, the template also needs to use for the rendered fields.  We
  // therefore swap the raw data out to a new variable and reset $vars['rows']
  // so that it can get rebuilt.
  $result = $vars['rows'];
  $vars['rows'] = array();
  $options = $view->style_plugin->options;
  $handler = $view->style_plugin;
  $fields =& $view->field;
  $columns = $handler
    ->sanitize_columns($options['columns'], $fields);
  $active = !empty($handler->active) ? $handler->active : '';
  $order = !empty($handler->order) ? $handler->order : 'asc';

  // Fields must be rendered in order as of Views 2.3, so we will pre-render
  // everything.
  $renders = array();
  $keys = array_keys($view->field);
  foreach ($result as $count => $row) {
    foreach ($keys as $id) {
      $renders[$count][$id] = $view->field[$id]
        ->theme($row);
    }
  }
  $position = 0;
  foreach ($columns as $field => $column) {
    $column_options = NULL;
    switch ($options['hidden_columns'][$field]) {
      case 'expandable':
        $datatable_options['bExpandable'] = TRUE;

      // ... and fall through, since expandable columns are also hidden
      case 'hidden':

        // Hidden or expandable columns get the bVisible init option set to false
        $column_options['bVisible'] = FALSE;
    }

    // render the header labels
    if ($field == $column && empty($fields[$field]->options['exclude'])) {
      $vars['header'][$field] = check_plain(!empty($fields[$field]) ? $fields[$field]
        ->label() : '');
      if (empty($options['info'][$field]['sortable']) || !$fields[$field]
        ->click_sortable()) {
        $column_options['bSortable'] = FALSE;
      }
      else {

        // Attempt to autodetect the type of field in order to handle sorting correctly.
        if (strlen($fields[$field]->last_render) != strlen(strip_tags($fields[$field]->last_render))) {
          $column_options['sType'] = 'html';
        }
        elseif (is_numeric($fields[$field]->last_render)) {
          $column_options['sType'] = 'numeric';
        }
        elseif ($fields[$field] instanceof views_handler_field_date) {
          $column_options['sType'] = 'date';
        }
        $column_options['bSortable'] = TRUE;
      }
      $datatable_options['aoColumns'][] = $column_options;

      // Save header columns for use in expandable hidden rows.
      $datatable_options['aoColumnHeaders'][] = $vars['header'][$field];
    }

    // Set default sort order
    if ($options['default'] == $field) {
      $datatable_options['aaSorting'] = array(
        array(
          $position,
          $options['order'],
        ),
      );
    }

    // Create a second variable so we can easily find what fields we have and what the
    // CSS classes should be.
    $vars['fields'][$field] = views_css_safe($field);
    if ($active == $field) {
      $vars['fields'][$field] .= ' active';
    }

    // Render each field into its appropriate column.
    foreach ($result as $num => $row) {
      if (!empty($fields[$field]) && empty($fields[$field]->options['exclude'])) {
        $field_output = $renders[$num][$field];

        // Don't bother with separators and stuff if the field does not show up.
        if (empty($field_output) && !empty($vars['rows'][$num][$column])) {
          continue;
        }

        // Place the field into the column, along with an optional separator.
        if (!empty($vars['rows'][$num][$column])) {
          if (!empty($options['info'][$column]['separator'])) {
            $vars['rows'][$num][$column] .= filter_xss_admin($options['info'][$column]['separator']);
          }
        }
        else {
          $vars['rows'][$num][$column] = '';
        }
        $vars['rows'][$num][$column] .= $field_output;
      }
    }
    $position++;
  }

  // Enable table info display, if necessary.
  $datatable_options['bInfo'] = $options['elements']['table_info'];
  $datatable_options['bFilter'] = $options['elements']['search_box'];
  $datatable_options['bStateSave'] = $options['elements']['save_state'];
  $datatable_options['bLengthChange'] = $options['pages']['length_change'];
  $datatable_options['iDisplayLength'] = (int) $options['pages']['display_length'];

  // Enable ThemeRoller support, if necessary.
  if ($options['layout']['themeroller']) {
    $datatable_options['bJQueryUI'] = TRUE;
  }

  // Pass the sDOM parameter, if one is specified.
  if ($options['layout']['sdom']) {
    $datatable_options['sDom'] = $options['layout']['sdom'];
  }
  $datatable_options['bAutoWidth'] = $options['layout']['autowidth'];

  // Enable full_numbers pagination if selected
  switch ($options['pages']['pagination_style']) {
    case 'full_numbers':
      $datatable_options['sPaginationType'] = 'full_numbers';
      break;
    case 'no_pagination':
      $datatable_options['bPaginate'] = FALSE;
      break;
    default:

      // Do nothing. No parameters need to be sent for the default (two-button) style
      break;
  }
  foreach ($vars['rows'] as $num => $row) {
    $vars['row_classes'][$num][] = $num % 2 == 0 ? 'odd' : 'even';
  }
  $vars['class'] = 'views-table';
  $vars['id'] = _datatables_get_id();
  drupal_add_css(drupal_get_path('module', 'datatables') . '/dataTables/media/css/demo_table.css');
  drupal_add_js(drupal_get_path('module', 'datatables') . '/dataTables/media/js/jquery.dataTables.js');
  drupal_add_js(drupal_get_path('module', 'datatables') . '/js/datatables.js');
  drupal_add_js(array(
    'datatables' => array(
      '#' . $vars['id'] => $datatable_options,
    ),
  ), 'setting');
}