View source
<?php
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,
),
);
}
function views_raw_sql_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'views_raw_sql'),
);
}
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;
}
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] = '';
}
$tokens['!' . $count] = isset($view->args[$count - 1]) ? strip_tags(decode_entities($view->args[$count - 1])) : '';
}
$tokens += views_raw_sql_get_query_tokens_recursive($_GET);
foreach ($view->display_handler
->get_handlers('field') as $current_field_id => $field_handler) {
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;
}
function views_raw_sql_get_field_sql($field_handler, $query_alias) {
if (empty($field_handler->query->fields)) {
$field_handler->view
->_build('field');
}
$query_fields = $field_handler->query->fields;
if (isset($query_fields[$query_alias]['field'])) {
$query_field = $query_fields[$query_alias];
$snippet = $query_field['field'];
if (!empty($query_field['table'])) {
$snippet = $query_field['table'] . '.' . $snippet;
}
return $snippet;
}
}
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)) {
$child_parent_keys = $parent_keys;
$child_parent_keys[] = $param;
$child_tokens = views_raw_sql_get_query_tokens_recursive($val, $child_parent_keys);
$tokens += $child_tokens;
}
else {
$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.');
}