Commit cb95f7fd authored by David Lesieur's avatar David Lesieur

Improved handling of database information.

parent c9a4862f
......@@ -40,7 +40,7 @@ function cck_facets_faceted_search_collect(&$facets, $domain, $env_id, $filter,
$field_value = strtr($matches[3][$index], '""', '"');
if (isset($field_name) && isset($field_value) && isset($fields[$field_name]) && (!isset($filter) || isset($filter['cck'][$field_name]))) {
// Validate the value.
$db_info = content_database_info($fields[$field_name]);
$db_info = _cck_facets_db_info($fields[$field_name]);
$column = reset($db_info['columns']); // Get the first database column. Other columns are ignored.
if (is_numeric($field_value) || !in_array($column['type'], array('int', 'mediumint', 'tinyint', 'bigint', 'float'))) {
// Create an active facet with the value found in the search text.
......@@ -58,7 +58,7 @@ function cck_facets_faceted_search_collect(&$facets, $domain, $env_id, $filter,
case 'node':
$fields = content_fields();
foreach ($fields as $field) {
$db_info = content_database_info($field);
$db_info = _cck_facets_db_info($field);
reset($db_info['columns']); // Go the first database column. Other columns are ignored!
$column_name = key($db_info['columns']); // Get the column name (required for the formatter).
if ((!isset($filter) || isset($filter['cck'][$field['field_name']])) && isset($arg->{$field['field_name']}) && is_array($arg->{$field['field_name']})) {
......@@ -143,11 +143,6 @@ class cck_facet extends faceted_search_facet {
*/
var $_field = NULL;
/**
* Alias for retrieving values for this facet from faceted search's query results.
*/
var $_field_alias = NULL;
/**
* Constructor.
*
......@@ -209,15 +204,19 @@ class cck_facet extends faceted_search_facet {
* Handler for the 'count' sort criteria.
*/
function build_sort_query_count(&$query) {
$db_info = _cck_facets_db_info($this->_field);
$column = reset($db_info['columns']); // Get the first database column. Other columns are ignored!
$query->add_orderby('count', 'DESC');
$query->add_orderby($this->_field_alias, 'ASC');
$query->add_orderby($db_info['table'] .'_'. $column['column'], 'ASC');
}
/**
* Handler for the 'field' sort criteria.
*/
function build_sort_query_field(&$query) {
$query->add_orderby($this->_field_alias, 'ASC');
$db_info = _cck_facets_db_info($this->_field);
$column = reset($db_info['columns']); // Get the first database column. Other columns are ignored!
$query->add_orderby($db_info['table'] .'_'. $column['column'], 'ASC');
}
/**
......@@ -242,10 +241,10 @@ class cck_facet extends faceted_search_facet {
* FALSE if this facet can't have root categories.
*/
function build_root_categories_query(&$query) {
$db_info = content_database_info($this->_field);
$db_info = _cck_facets_db_info($this->_field);
$query->add_table($db_info['table'], 'vid', 'n', 'vid');
$column = reset($db_info['columns']); // Get the first database column. Other columns are ignored!
$this->_field_alias = $query->add_field($db_info['table'], $column['column']);
$query->add_field($db_info['table'], $column['column']);
$query->add_groupby($db_info['table'] .'.'. $column['column']);
return TRUE;
}
......@@ -260,10 +259,14 @@ class cck_facet extends faceted_search_facet {
* Array of categories.
*/
function build_categories($results) {
$db_info = _cck_facets_db_info($this->_field);
$column = reset($db_info['columns']); // Get the first database column. Other columns are ignored!
$field_alias = $db_info['table'] .'_'. $column['column'];
$categories = array();
while ($result = db_fetch_object($results)) {
// Add a new category.
$categories[] = cck_facets_create_category($this->_field, $result->{$this->_field_alias}, $result->count, FALSE);
$categories[] = cck_facets_create_category($this->_field, $result->{$field_alias}, $result->count, FALSE);
}
return $categories;
}
......@@ -304,7 +307,7 @@ class cck_facet_category extends faceted_search_category {
* implementor is responsible to ensure adequate security filtering.
*/
function get_label($html = FALSE) {
$db_info = content_database_info($this->_field);
$db_info = _cck_facets_db_info($this->_field);
reset($db_info['columns']); // Get the first database column. Other columns are ignored!
$column_name = key($db_info['columns']); // Get the column name (required for the formatter).
$label = content_format($this->_field['field_name'], array($column_name => $this->_value));
......@@ -321,7 +324,7 @@ class cck_facet_category extends faceted_search_category {
* The query object to update.
*/
function build_results_query(&$query) {
$db_info = content_database_info($this->_field);
$db_info = _cck_facets_db_info($this->_field);
// Since the table might be joined multiple times in the query, we use the
// field's name in the table alias to create a unique table reference.
$query->add_table($db_info['table'], 'vid', 'n', 'vid', $db_info['table'] .'_'. $this->_field['field_name']);
......@@ -365,3 +368,15 @@ function _cck_facets_quoted_query_insert($keys, $option, $value = '') {
return $keys;
}
/**
* Return database storage information for the specified field.
*
* This is needed often, so results are cached.
*/
function _cck_facets_db_info($field) {
static $db_info = NULL;
if (!isset($db_info[$field['field_name']])) {
$db_info[$field['field_name']] = content_database_info($field);
}
return $db_info[$field['field_name']];
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment