You are here

views_raw_sql.module in Views Raw SQL 7

Same filename and directory in other branches
  1. 6 views_raw_sql.module

File

views_raw_sql.module
View source
<?php

/**
 * Implements hook_permission().
 */
function views_raw_sql_permission() {
  return array(
    'edit views raw sql' => array(
      'title' => t('Add or edit Views raw SQL fields and sorts'),
      'restrict access' => TRUE,
    ),
  );
}

/**
 * Implements hook_views_api().
 */
function views_raw_sql_views_api() {
  return array(
    'api' => 2,
    'path' => drupal_get_path('module', 'views_raw_sql'),
  );
}

// views_raw_sql_views_api

/**
 * Implements hook_views_data().
 */
function views_raw_sql_views_data() {
  $data = array();
  $data['views_raw_sql'] = array(
    'table' => array(
      'group' => t('Raw SQL'),
      'join' => array(
        '#global' => array(),
      ),
    ),
  );
  $data['views_raw_sql']['raw_field'] = array(
    'title' => t('Raw field'),
    'help' => t('Provide raw SQL field expression.'),
    'field' => array(
      'click sortable' => TRUE,
      'handler' => 'views_handler_field_views_raw_sql',
    ),
  );
  $data['views_raw_sql']['raw_sort'] = array(
    'title' => t('Raw sort'),
    'help' => t('Provide raw SQL sort exporession.'),
    'sort' => array(
      'handler' => 'views_handler_sort_views_raw_sql',
    ),
  );
  $data['views_raw_sql']['raw_filter'] = array(
    'title' => t('Raw filter'),
    'help' => t('Provide raw SQL filter exporession.'),
    'filter' => array(
      'handler' => 'views_handler_filter_views_raw_sql',
    ),
  );
  return $data;
}

// views_raw_sql_views_data

/**
 * Get query tokens. Nearly straight copy (without self tokens) from \views_handler_field::get_render_tokens().
 *
 * @todo We might want to cache parts of this.
 *
 * @param $view
 * @param $field_id
 *   When this is called from a field, searching after current field id is useless.
 * @return array
 */
function views_raw_sql_get_query_tokens($view, $field_id = NULL) {
  $tokens = array();
  if (!empty($view->build_info['substitutions'])) {
    $tokens = $view->build_info['substitutions'];
  }
  $count = 0;
  foreach ($view->display_handler
    ->get_handlers('argument') as $arg => $display_handler) {
    $token = '%' . ++$count;
    if (!isset($tokens[$token])) {
      $tokens[$token] = '';
    }

    // Use strip tags as there should never be HTML in the path.
    // However, we need to preserve special characters like " that
    // were removed by check_plain().
    $tokens['!' . $count] = isset($view->args[$count - 1]) ? strip_tags(decode_entities($view->args[$count - 1])) : '';
  }

  // Get flattened set of tokens for any array depth in $_GET parameters.
  $tokens += views_raw_sql_get_query_tokens_recursive($_GET);

  // Now add replacements for our fields.
  foreach ($view->display_handler
    ->get_handlers('field') as $current_field_id => $field_handler) {

    // We only use fields up to (and NOT including) this one.
    if ($current_field_id == $field_id) {
      break;
    }
    $tokens["[{$current_field_id}]"] = views_raw_sql_get_field_sql($field_handler, $field_handler->field_alias);
    if (property_exists($field_handler, 'field_info')) {
      $field_name = $field_handler->field_info['field_name'];
      foreach ($field_handler->field_info['columns'] as $column_name => $column_info) {
        $full_column_name = $field_name . '_' . $column_name;
        if (isset($field_handler->aliases[$full_column_name])) {
          $tokens["[{$current_field_id}:{$column_name}]"] = views_raw_sql_get_field_sql($field_handler, $field_handler->aliases[$full_column_name]);
        }
      }
    }
  }
  return $tokens;
}

/**
 * Get sql from field. We did this before in views_fieldoperations.
 *
 * @param $field_handler
 * @param $query_alias
 * @return string
 */
function views_raw_sql_get_field_sql($field_handler, $query_alias) {

  // Oh this is hacky, but we need  it...
  if (empty($field_handler->query->fields)) {
    $field_handler->view
      ->_build('field');
  }
  $query_fields = $field_handler->query->fields;
  if (isset($query_fields[$query_alias]['field'])) {

    // We must copy the SQL expression, because aliases can neither be used in
    // select, where, having, orderby. (At least not portably.)
    $query_field = $query_fields[$query_alias];
    $snippet = $query_field['field'];
    if (!empty($query_field['table'])) {
      $snippet = $query_field['table'] . '.' . $snippet;
    }
    return $snippet;
  }
}

/**
 * Get recursive tokens from $_GET value. Straight copy from \views_handler_field::get_token_values_recursive().
 */
function views_raw_sql_get_query_tokens_recursive(array $array, array $parent_keys = array()) {
  $tokens = array();
  foreach ($array as $param => $val) {
    if (is_array($val)) {

      // Copy parent_keys array, so we don't afect other elements of this iteration.
      $child_parent_keys = $parent_keys;
      $child_parent_keys[] = $param;

      // Get the child tokens.
      $child_tokens = views_raw_sql_get_query_tokens_recursive($val, $child_parent_keys);

      // Add them to the current tokens array.
      $tokens += $child_tokens;
    }
    else {

      // Create a token key based on array element structure.
      $token_string = !empty($parent_keys) ? implode('_', $parent_keys) . '_' . $param : $param;
      $tokens['%' . $token_string] = strip_tags(decode_entities($val));
    }
  }
  return $tokens;
}
function views_raw_sql_tokens_help() {
  return t('You can use placeholders like in field rewrite. If you use the views_fielddata module, you can also use tokens like [field:column], as due to views internals [field] only queries the entity ID.');
}

Functions

Namesort descending Description
views_raw_sql_get_field_sql Get sql from field. We did this before in views_fieldoperations.
views_raw_sql_get_query_tokens Get query tokens. Nearly straight copy (without self tokens) from \views_handler_field::get_render_tokens().
views_raw_sql_get_query_tokens_recursive Get recursive tokens from $_GET value. Straight copy from \views_handler_field::get_token_values_recursive().
views_raw_sql_permission Implements hook_permission().
views_raw_sql_tokens_help
views_raw_sql_views_api Implements hook_views_api().
views_raw_sql_views_data Implements hook_views_data().