You are here

contextual_range_filter.module in Views Contextual Range Filter 7

Same filename and directory in other branches
  1. 8 contextual_range_filter.module

contextual_range_filter.module

Adds to Views an option to contextually filter by range. For instance, if you have a View with a float field (e.g., Price or Distance) and have added this field as the first contextual filter, then you can filter your View page like so:

http://yoursite.com/yourview/100--199.99

Integer, float, string and list types are supported. The OR ('+') operator is supported. The negate operator ("Exclude" tick box) is supported. "Glossary mode" (match on first N characters of a string) is supported.

File

contextual_range_filter.module
View source
<?php

/**
 * @file
 * contextual_range_filter.module
 *
 * Adds to Views an option to contextually filter by range.
 * For instance, if you have a View with a float field (e.g., Price or Distance)
 * and have added this field as the first contextual filter, then you can filter
 * your View page like so:
 *
 *   http://yoursite.com/yourview/100--199.99
 *
 * Integer, float, string and list types are supported.
 * The OR ('+') operator is supported.
 * The negate operator ("Exclude" tick box) is supported.
 * "Glossary mode" (match on first N characters of a string) is supported.
 */

// Same as used for dates.
define('CONTEXTUAL_RANGE_FILTER_SEPARATOR1', '--');

// Alternative delimiter.
define('CONTEXTUAL_RANGE_FILTER_SEPARATOR2', ':');

/**
 * Implements hook_help().
 */
function contextual_range_filter_help($path, $arg) {
  $help1 = t('You must first add contextual filters in the advanced section of the Views UI before they appear on this <a href="@config_page">configuration</a> page.<br/>When defined contextual range filters are employed like normal contextual filters, i.e. by apppending "arguments" to the URL. You separate "from" and "to" values by a double hyphen, e.g., <em>http://yoursite.com/yourview/50--149.95</em><br/>You may omit the from or to values to create open-ended ranges, e.g., <em>http://yoursite.com/yourview/--149.95</em> or <em>http://yoursite.com/yourview/50--</em><br/>All filter ranges are inclusive of from and to values.<br/>Instead of "<strong>--</strong>" you may use a colon "<strong>:</strong>"', array(
    '@config_page' => url('admin/config/content/contextual-range-filter'),
  ));
  switch ($path) {
    case 'admin/config/content/contextual-range-filter':
      return $help1;
    case 'admin/help#contextual_range_filter':
      $extra1 = t('Configuration and usage instructions with examples can be found in the <a target="readme" href="@README">README</a> file.', array(
        '@README' => url(drupal_get_path('module', 'contextual_range_filter') . '/README.txt'),
      ));
      $extra2 = t('Known issues and solutions may be found on the <a target="project-page" href="@contextual_range_filter">Views Contextual Range Filter</a> project page.', array(
        '@contextual_range_filter' => url('http://drupal.org/project/contextual_range_filter'),
      ));
      return $help1 . '<p>' . $extra1 . '</p><p>' . $extra2 . '</p>';
  }
}

/**
 * Split a filter range string into an array containing "from" and "to" values.
 *
 * @param string $range
 *   format "from--to", "from--" or "--to".
 *   A single value is also allowed. A single colon is accepted instead of --
 *
 * @return array
 *   array of length 2, the 2nd value equals FALSE when no separator was found
 */
function contextual_range_filter_split($range) {

  // Defensive programming to make sure we have a string.
  if (is_array($range)) {
    $range = reset($range);
  }
  $range = trim($range);
  $from_to = explode(CONTEXTUAL_RANGE_FILTER_SEPARATOR1, $range);
  if (count($from_to) < 2) {
    $from_to = explode(CONTEXTUAL_RANGE_FILTER_SEPARATOR2, $range);
  }
  return count($from_to) == 1 ? array(
    reset($from_to),
    FALSE,
  ) : $from_to;
}

/**
 * Return values of a list range as an array.
 *
 * @param string $range
 *   in format parseable by contextual_range_filter()
 * @param array $allowed_values
 *   the allowed values
 *
 * @return array|bool
 *   array of keys into $allowed_values array, or FALSE when range 'from'
 *   was not found
 */
function contextual_range_filter_explode_list_range($range, $allowed_values) {
  list($from, $to) = contextual_range_filter_split($range);
  $from = strtolower($from);
  if ($to === FALSE) {
    $to = $from;
  }
  else {
    $to = strtolower($to);
  }
  foreach ($allowed_values as $key => $value) {
    $value = strtolower($value);
    if (empty($from) || $key == $from || $value == $from) {

      // Found the from value, start collecting keys.
      $keys = array();
    }
    if (isset($keys)) {
      $keys[] = $key;
    }
    if (!empty($to) && ($key == $to || $value == $to)) {
      return $keys;
    }
  }
  return isset($keys) ? $keys : FALSE;
}

/**
 * Implements hook_menu().
 */
function contextual_range_filter_menu() {
  $items['admin/config/content/contextual-range-filter'] = array(
    'title' => 'Views Contextual Range Filters',
    'description' => 'Select which contextual filters need to be converted to contextual <em>range</em> filters.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'contextual_range_filter_config_form',
    ),
    'access arguments' => array(
      'administer contextual range filters',
    ),
    'file' => 'contextual_range_filter.admin.inc',
  );
  return $items;
}

/**
 * Implements hook_permission().
 */
function contextual_range_filter_permission() {
  return array(
    'administer contextual range filters' => array(
      'title' => t('Administer contextual range filters'),
      'description' => t('Allow access the Contextual Range Filter configuration page.'),
    ),
  );
}

/**
 * Implements hook_views_api().
 */
function contextual_range_filter_views_api() {
  return array(
    'api' => views_api_version(),
    'path' => drupal_get_path('module', 'contextual_range_filter') . '/views',
  );
}

Functions

Namesort descending Description
contextual_range_filter_explode_list_range Return values of a list range as an array.
contextual_range_filter_help Implements hook_help().
contextual_range_filter_menu Implements hook_menu().
contextual_range_filter_permission Implements hook_permission().
contextual_range_filter_split Split a filter range string into an array containing "from" and "to" values.
contextual_range_filter_views_api Implements hook_views_api().

Constants