You are here

function template_preprocess_footable_view in FooTable 7.2

Same name and namespace in other branches
  1. 7 footable.module \template_preprocess_footable_view()

Display a view as a FooTable style.

See also

template_preprocess_views_view_table()

File

./footable.module, line 170
Provides Views integration for the jQuery FooTable plugin.

Code

function template_preprocess_footable_view(&$vars) {
  template_preprocess_views_view_table($vars);
  $view = $vars['view'];
  $result = $vars['result'] = $vars['rows'];
  $options = $view->style_plugin->options;

  /** @var views_plugin_style_table $handler */
  $handler = $view->style_plugin;
  $fields =& $view->field;
  $columns = $handler
    ->sanitize_columns($options['columns'], $fields);
  $footable = array();

  // Provide backwards compatibility with 7.x-1.x.
  if (!empty($options['footable']['hide']) && empty($options['footable']['breakpoint'])) {
    $options['footable']['breakpoint'] = $options['footable']['hide'];
  }

  // Set columns to be hidden by FooTable plugin.
  foreach ($columns as $field => $column) {

    /** @var views_handler_field $field_handler */
    $field_handler = $fields[$field];

    // Get column type.
    if (!empty($options['info'][$column]['type'])) {
      $vars['column_attributes'][$column]['data-type'] = $options['info'][$column]['type'];
    }
    elseif ($field_handler instanceof views_handler_field_date) {
      $vars['column_attributes'][$column]['data-type'] = 'date';
    }
    elseif (is_numeric($field_handler->last_render)) {
      $vars['column_attributes'][$column]['data-type'] = 'numeric';
    }
    elseif (drupal_strlen($field_handler->last_render) != drupal_strlen(strip_tags($field_handler->last_render))) {
      $vars['column_attributes'][$column]['data-type'] = 'html';
    }
    else {
      $vars['column_attributes'][$column]['data-type'] = 'text';
    }

    // Add breakpoints.
    if (!empty($options['footable']['breakpoint'][$column])) {
      if (!empty($options['footable']['breakpoint'][$column]['all'])) {
        $options['footable']['breakpoint'][$column] = array(
          'all' => 'all',
        );
      }
      $vars['column_attributes'][$column]['data-breakpoints'] = implode(' ', array_keys(array_filter($options['footable']['breakpoint'][$column])));
    }

    // Sorting.
    if (!empty($options['footable']['component']['sorting']['enabled'])) {
      $vars['header'][$field] = check_plain(!empty($field_handler) ? $field_handler
        ->label() : '');
      if (empty($options['info'][$field]['sortable']) || !$field_handler
        ->click_sortable()) {
        $vars['column_attributes'][$column]['data-sortable'] = FALSE;
      }
      else {
        if (!empty($options['default']) && $options['default'] == $field) {
          $vars['column_attributes'][$column]['data-sorted'] = TRUE;
          if (!empty($options['info'][$field]['default_sort_order'])) {
            $vars['column_attributes'][$column]['data-direction'] = strtoupper($options['info'][$field]['default_sort_order']);
          }
        }
      }
    }

    // Add data values.
    if (!empty($options['footable']['component']['filtering']['enabled']) || !empty($options['footable']['component']['sorting']['enabled'])) {
      $types = array(
        'html',
        'date',
      );
      foreach ($result as $num => $row) {
        if (in_array($vars['column_attributes'][$column]['data-type'], $types)) {
          $value = strip_tags($vars['rows'][$num][$column]);
          if (!empty($options['footable']['component']['filtering']['enabled'])) {
            $vars['field_attributes'][$field][$num]['data-filter-value'] = $value;
          }
          if (!empty($options['footable']['component']['sorting']['enabled'])) {
            $vars['field_attributes'][$field][$num]['data-sort-value'] = $value;
          }
        }
      }
    }
  }

  // Add FooTable classes and data attributes.
  $type = variable_get('footable_plugin_type', 'standalone');
  $vars['classes_array'][] = 'footable';
  if (empty($vars['attributes_array']['id'])) {
    $vars['attributes_array']['id'] = drupal_html_id('footable');
  }

  // Expand all rows.
  $footable['expandAll'] = !empty($options['footable']['expand_all']);

  // Expand first row.
  $footable['expandFirst'] = !empty($options['footable']['expand_first']);

  // Show header.
  $footable['showHeader'] = !empty($options['footable']['show_header']);

  // Expandable column.
  $footable['toggleColumn'] = $options['footable']['toggle_column'];

  // Icon expanded.
  $vars['classes_array'][] = 'fooicon-expanded-' . $options['footable']['icon_expanded'];

  // Icon collapsed.
  $vars['classes_array'][] = 'fooicon-collapsed-' . $options['footable']['icon_collapsed'];

  // Add Bootstrap configuration.
  if ($type == 'bootstrap') {
    if (!empty($options['footable']['bootstrap']['striped'])) {
      $vars['classes_array'][] = 'table-striped';
    }
    if (!empty($options['footable']['bootstrap']['bordered'])) {
      $vars['classes_array'][] = 'table-bordered';
    }
    if (!empty($options['footable']['bootstrap']['hover'])) {
      $vars['classes_array'][] = 'table-hover';
    }
    if (!empty($options['footable']['bootstrap']['condensed'])) {
      $vars['classes_array'][] = 'table-condensed';
    }
  }

  // Add components.
  // Filtering.
  if (!empty($options['footable']['component']['filtering']['enabled'])) {
    $footable['filtering'] = array(
      'enabled' => TRUE,
      'delay' => $options['footable']['component']['filtering']['delay'],
      'ignoreCase' => !empty($options['footable']['component']['filtering']['ignore_case']),
      'min' => $options['footable']['component']['filtering']['min'],
      'placeholder' => t($options['footable']['component']['filtering']['placeholder']),
      'position' => $options['footable']['component']['filtering']['position'],
      'space' => $options['footable']['component']['filtering']['space'],
    );
  }

  // Paging.
  if (!empty($options['footable']['component']['paging']['enabled'])) {
    $footable['paging'] = array(
      'enabled' => TRUE,
      'countFormat' => $options['footable']['component']['paging']['count_format'],
      'current' => $options['footable']['component']['paging']['current'],
      'limit' => $options['footable']['component']['paging']['limit'],
      'position' => $options['footable']['component']['paging']['position'],
      'size' => $options['footable']['component']['paging']['size'],
    );
  }

  // Sorting.
  if (!empty($options['footable']['component']['sorting']['enabled'])) {
    $footable['sorting'] = array(
      'enabled' => TRUE,
    );
  }

  // Add breakpoints.
  $footable['breakpoints'] = array();
  foreach (footable_breakpoint_load_multiple() as $breakpoint) {
    if (!empty($options['footable']['overwrite'][$breakpoint->machine_name])) {
      $breakpoint->breakpoint = $options['footable']['overwrite'][$breakpoint->machine_name];
    }
    $footable['breakpoints'][$breakpoint->machine_name] = (int) $breakpoint->breakpoint;
  }

  // Add FooTable plugin files and calling script.
  footable_libraries_load();
  drupal_add_js(array(
    'footable' => array(
      $vars['attributes_array']['id'] => $footable,
    ),
  ), array(
    'type' => 'setting',
  ));
  drupal_add_js(drupal_get_path('module', 'footable') . '/footable.js');
  drupal_add_css(drupal_get_path('module', 'footable') . '/css/footable_' . $type . '.css');
}