You are here

tablesorter.module in Tablesorter 7

Same filename and directory in other branches
  1. 8 tablesorter.module
  2. 7.2 tablesorter.module
  3. 3.0.x tablesorter.module

Tablesorter.

File

tablesorter.module
View source
<?php

/**
 * @file
 * Tablesorter.
 */

/**
 * Implements hook_help().
 */
function tablesorter_help($path, $arg) {
  switch ($path) {
    case 'admin/help#tablesorter':
      $help = '<p>' . t('Ensure that you have downloaded tablesorter from <a href="@url">@url</a> and have extracted the css and js files from the dist folder into @path. The path should be @path@library.', array(
        '@url' => 'https://github.com/Mottie/tablesorter/archive/master.tar.gz',
        '@path' => libraries_get_path('tablesorter'),
        '@library' => '/js/jquery.tablesorter.combined.min.js',
      )) . '</p>';
      $help .= '<p>' . t('In views, tablesorter can be used by selecting the "tablesorter" display style.') . '</p>';
      $help .= '<p>' . t('Tablesorter can be added to specific tables by calling tablesorter_attach($TABLE_ARRAY).') . '</p>';
      $help .= '<p>' . t('Tablesorter can be added on specific pages through its <a href="@url">configuration page</a>.', array(
        '@url' => '/admin/config/user-interface/tablesorter',
      )) . '</p>';
      return $help;
      break;
  }
}

/**
 * Implements hook_libraries_info().
 */
function tablesorter_libraries_info() {
  $libraries['tablesorter'] = array(
    'name' => 'Tablesorter',
    'vendor url' => 'https://mottie.github.io/tablesorter/docs/index.html',
    'download url' => 'https://github.com/Mottie/tablesorter/archive/master.tar.gz',
    'version arguments' => array(
      'file' => 'js/jquery.tablesorter.combined.js',
      'pattern' => '/version : \'([\\d\\.]+)\'/',
      'lines' => 40,
    ),
    'files' => array(
      'js' => array(
        'dist/js/jquery.tablesorter.combined.min.js',
        'dist/js/extras/jquery.metadata.min.js',
        'dist/js/extras/jquery.tablesorter.pager.min.js',
      ),
    ),
  );

  // Add the theme if one is selected.
  $theme = variable_get('tablesorter_theme');
  if ($theme) {
    $libraries['tablesorter']['files']['css'][] = "css/theme.{$theme}.min.css";
  }
  return $libraries;
}

/**
 * Implements hook_menu().
 */
function tablesorter_menu() {
  $items = array();
  $items['admin/config/user-interface/tablesorter'] = array(
    'title' => 'Tablesorter',
    'description' => 'Configuration for Tablesorter',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'tablesorter_form',
    ),
    'access arguments' => array(
      'access tablesorter content',
    ),
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * Implements hook_permission().
 */
function tablesorter_permission() {
  return array(
    'access tablesorter content' => array(
      'title' => t('Access tablesorter for Customize.'),
    ),
  );
}

/**
 * Configuration form.
 */
function tablesorter_form($form, &$form_state) {

  // Tablesorter loading options.
  $page_options = array(
    'page_enable' => t('Load on all pages of site.'),
    'page_disable' => t('Load on the listed pages.'),
  );
  $form['tablesorter_page_init_action'] = array(
    '#type' => 'radios',
    '#options' => $page_options,
    '#title' => t('Enable tablesorter on specific pages'),
    '#default_value' => variable_get('tablesorter_page_init_action', 'page_enable'),
  );
  $form['tablesorter_page_list'] = array(
    '#type' => 'textarea',
    '#title' => t('Pages'),
    '#description' => t('List one page per line as Drupal paths.  The * character is a wildcard.  Example paths are "node/add/page" and "node/add/*".  Use &lt;front&gt; to match the front page.'),
    '#default_value' => variable_get('tablesorter_page_list', ''),
    '#states' => array(
      'invisible' => array(
        ':input[name="tablesorter_page_init_action"]' => array(
          'value' => 'page_enable',
        ),
      ),
    ),
  );

  // Add themes.
  // Get theme options from the directory.
  $options = array();
  $path = libraries_get_path('tablesorter');
  $themes = scandir($path . '/css');
  if ($themes) {
    foreach ($themes as $theme) {
      $theme_array = explode('.', $theme);
      if ($theme_array[0] == 'theme' && isset($theme_array[2]) && $theme_array[2] == 'min') {
        $options[$theme_array[1]] = ucfirst($theme_array[1]);
      }
    }
  }
  $form['tablesorter_theme'] = array(
    '#type' => 'select',
    '#title' => t('Select Theme'),
    '#options' => $options,
    '#default_value' => variable_get('tablesorter_theme'),
    '#description' => t('Set the theme for header.'),
    '#empty_option' => t('System Theme'),
  );

  // Add widgets.
  $widgets = array(
    'zebra' => t('Zebra striping'),
  );
  $form['tablesorter_widgets'] = array(
    '#type' => 'checkboxes',
    '#multiple' => 'multiple',
    '#options' => $widgets,
    '#title' => t('Add widgets'),
    '#default_value' => variable_get('tablesorter_widgets', ''),
  );

  // Options for the zebra widget.
  $form['tablesorter_zebra_odd_class'] = array(
    '#type' => 'textfield',
    '#title' => t('Odd row class'),
    '#description' => t("Select the class added to odd rows.  Defaults to 'odd'"),
    '#default_value' => variable_get('tablesorter_zebra_odd_class', 'odd'),
  );
  $form['tablesorter_zebra_even_class'] = array(
    '#type' => 'textfield',
    '#title' => t('Even row class'),
    '#description' => t("Select the class added to even rows.  Defaults to 'even'"),
    '#default_value' => variable_get('tablesorter_zebra_even_class', 'even'),
  );

  // Add a submit handler.
  $form['#submit'][] = '_tablesorter_form_submit';
  return system_settings_form($form);
}

/**
 * Submit handler for the tablesorter settings form.
 */
function _tablesorter_form_submit() {

  // Clear cache when the settings change to make sure the right css is loaded.
  cache_clear_all('tablesorter', 'cache_libraries');
}

/**
 * Implements hook_views_api().
 */
function tablesorter_views_api() {
  return array(
    'api' => 3.0,
    'path' => drupal_get_path('module', 'tablesorter') . '/views',
  );
}

/**
 * Preprocessor function for the tablesorter style.
 */
function template_preprocess_tablesorter_view(&$vars) {
  $view = $vars['view'];
  tablesorter_attach($vars);
  $result = $vars['result'] = $vars['rows'];
  $vars['rows'] = array();
  $vars['col_count'] = 0;
  $options = $view->style_plugin->options;
  $handler = $view->style_plugin;
  $fields =& $view->field;
  $columns = $handler
    ->sanitize_columns($options['columns'], $fields);

  // Fields must be rendered in order as of Views 2.3
  // so we will pre-render everything.
  $renders = $handler
    ->render_fields($result);
  foreach ($columns as $field => $column) {

    // Render the header labels.
    if ($field == $column && empty($fields[$field]->options['exclude'])) {
      $label = !empty($fields[$field]) ? check_plain($fields[$field]
        ->label()) : '';
      $vars['header'][$field] = $label;
      $vars['col_count']++;
    }

    // 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];
        if (!isset($vars['rows'][$num][$column])) {
          $vars['rows'][$num][$column] = '';
        }

        // Don't bother with separators and stuff if the field does not show up.
        if ($field_output === '') {
          continue;
        }

        // Place the field into the column, along with an optional separator.
        if ($vars['rows'][$num][$column] !== '') {
          if (!empty($options['info'][$column]['separator'])) {
            $vars['rows'][$num][$column] .= filter_xss_admin($options['info'][$column]['separator']);
          }
        }
        $vars['rows'][$num][$column] .= $field_output;
      }
    }
  }
  $count = 0;
  foreach ($vars['rows'] as $num => $row) {
    $vars['row_classes'][$num][] = $count++ % 2 == 0 ? 'odd' : 'even';
  }
  $vars['row_classes'][0][] = 'views-row-first';
  $vars['row_classes'][count($vars['row_classes']) - 1][] = 'views-row-last';
  $vars['class'] = 'tablesorter';
  if (!empty($options['sticky'])) {
    drupal_add_js('misc/tableheader.js');
    $vars['class'] .= " sticky-enabled";
  }
  $vars['class'] .= ' cols-' . count($vars['rows']);
  $vars['tvpager'] = $options['elements']['tablesorter_views_pager'];
}

/**
 * Helper function to configure tablesorter on a table's render array.
 *
 * This function returns by reference.
 *
 * @param array $table
 *   The table to add tablesorter to.
 */
function tablesorter_attach(array &$table) {

  // Make sure to only attach this once.
  if (!isset($table['#tablesorter_processed'])) {
    $table['#tablesorter_processed'] = TRUE;

    // Add a class and library to the table.
    $table['#attached']['libraries_load'][] = array(
      'tablesorter',
    );
    $table['#attributes']['class'][] = 'tablesorter';

    // Add this module's javascript.
    drupal_add_js(drupal_get_path('module', 'tablesorter') . '/tablesortervar.js');

    // Set the theme.
    $settings = array();
    $theme = variable_get('tablesorter_theme');
    if ($theme) {
      $settings['tablesorter']['theme'] = $theme;
    }

    // Add widgets.
    $widgets = variable_get('tablesorter_widgets');
    foreach ($widgets as $widget => $value) {
      if (!$value) {
        unset($widgets[$widget]);
      }
    }
    if ($widgets) {
      $settings['tablesorter']['widgets'] = array_keys($widgets);

      // Get stripe classes.
      if (in_array('zebra', $widgets)) {
        $settings['tablesorter']['zebra']['odd'] = variable_get('tablesorter_zebra_odd_class', 'odd');
        $settings['tablesorter']['zebra']['even'] = variable_get('tablesorter_zebra_even_class', 'even');
      }
    }

    // Add settings.
    drupal_add_js($settings, 'setting');

    // Allow other modules to make changes to sorted tables.
    drupal_alter(__FUNCTION__, $table);
  }
}

/**
 * Implements hook_preprocess_HOOK().
 *
 * Attaches the tablesorter library to tables based on the configuration form.
 */
function tablesorter_preprocess_table(&$variables) {

  // Check if all tables are supposed to be sorted.
  $attach = variable_get('tablesorter_page_init_action', 'page_enable') == 'page_enable';
  if (!$attach) {

    // Check if the current path is allowed to have sorted tables.
    $paths = variable_get('tablesorter_page_list') ?: '';
    $attach = drupal_match_path(drupal_get_path_alias(), $paths) || drupal_match_path(current_path(), $paths);
  }
  if ($attach) {
    tablesorter_attach($variables);
  }
}
function tablesorter_page_build() {
  $attach = variable_get('tablesorter_page_init_action', 'page_enable') == 'page_enable';
  if (!$attach) {

    // Check if the current path is allowed to have sorted tables.
    $paths = variable_get('tablesorter_page_list') ?: '';
    $attach = drupal_match_path(drupal_get_path_alias(), $paths) || drupal_match_path(current_path(), $paths);
  }
  if ($attach) {
    libraries_load('tablesorter');

    // Add this module's javascript.
    drupal_add_js(drupal_get_path('module', 'tablesorter') . '/tablesortervar.js');
    $settings = array();
    $theme = variable_get('tablesorter_theme');
    if ($theme) {
      $settings['tablesorter']['theme'] = $theme;
    }

    // Add widgets.
    $widgets = variable_get('tablesorter_widgets');
    foreach ($widgets as $widget => $value) {
      if (!$value) {
        unset($widgets[$widget]);
      }
    }
    if ($widgets) {
      $settings['tablesorter']['widgets'] = array_keys($widgets);

      // Get stripe classes.
      if (in_array('zebra', $widgets)) {
        $settings['tablesorter']['zebra']['odd'] = variable_get('tablesorter_zebra_odd_class', 'odd');
        $settings['tablesorter']['zebra']['even'] = variable_get('tablesorter_zebra_even_class', 'even');
      }
    }

    // Add settings.
    drupal_add_js($settings, 'setting');
  }
}

Functions

Namesort descending Description
tablesorter_attach Helper function to configure tablesorter on a table's render array.
tablesorter_form Configuration form.
tablesorter_help Implements hook_help().
tablesorter_libraries_info Implements hook_libraries_info().
tablesorter_menu Implements hook_menu().
tablesorter_page_build
tablesorter_permission Implements hook_permission().
tablesorter_preprocess_table Implements hook_preprocess_HOOK().
tablesorter_views_api Implements hook_views_api().
template_preprocess_tablesorter_view Preprocessor function for the tablesorter style.
_tablesorter_form_submit Submit handler for the tablesorter settings form.