Commit 18117832 authored by David Lesieur's avatar David Lesieur

Getting ready for more specialization by other modules - part 3.

parent 34b6304e
......@@ -40,7 +40,7 @@ function cck_facets_faceted_search_collect(&$facets, $domain, $env_id, $filter,
$value = strtr($matches[3][$index], '""', '"');
if (isset($field_name) && isset($value) && isset($fields[$field_name]) && (!isset($filter) || isset($filter['cck'][$field_name]))) {
// Create an active facet with the value found in the search text.
if ($active_path = cck_facets_create_category($fields[$field_name], $value, 'user')) {
if ($active_path = cck_facets_create_category($fields[$field_name], $value)) {
$facets[] = cck_facets_create_facet($fields[$field_name], $active_path);
}
}
......@@ -54,12 +54,9 @@ function cck_facets_faceted_search_collect(&$facets, $domain, $env_id, $filter,
case 'node':
$fields = content_fields();
foreach ($fields as $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']})) {
foreach ($arg->{$field['field_name']} as $item) {
$active_path = cck_facets_create_category($field, $item[$column_name], 'db');
$active_path = cck_facets_create_category($field, $item);
$facets[] = cck_facets_create_facet($field, $active_path);
}
}
......@@ -109,11 +106,10 @@ function cck_facets_create_facet_default($field, $active_path) {
* Name of the field corresponding the facet containing the category to
* create.
* @param $value
* Value of the field, to be assigned to the category.
* @param $source
* Either 'db' when the value comes directly from the database, or 'user' when
* it was specified by the user. This is only useful for special cases where a
* facet cannot use the same value format as CCK's native database format.
* Value of the field, to be assigned to the category. When this is an array,
* the value is coming from the database. When this is a string, the value is
* used-specified and could require further parsing or validation before
* creating any category.
* @param $count
* Optional. The number of nodes associated to this category within the
* current search.
......@@ -126,7 +122,7 @@ function cck_facets_create_facet_default($field, $active_path) {
* Single category object (if $path was TRUE), an array of categories
* otherwise, or NULL if the category could not be created.
*/
function cck_facets_create_category($field, $value, $source, $count = NULL, $full_active_path = TRUE) {
function cck_facets_create_category($field, $value, $count = NULL, $full_active_path = TRUE) {
static $factories = NULL;
if (!isset($factories)) {
$factories = module_invoke_all('cck_facets_factory', 'category');
......@@ -138,7 +134,7 @@ function cck_facets_create_category($field, $value, $source, $count = NULL, $ful
// No specialized factory found for the given field type.
$factory = 'cck_facets_create_category_default';
}
return $factory($field, $value, $source, $count, $full_active_path);
return $factory($field, $value, $count, $full_active_path);
}
/**
......@@ -146,11 +142,16 @@ function cck_facets_create_category($field, $value, $source, $count = NULL, $ful
*
* @see cck_facets_create_category
*/
function cck_facets_create_category_default($field, $value, $source, $count, $full_active_path) {
if ($source == 'user') {
// Basic validation.
$db_info = _cck_facets_db_info($field);
$column = reset($db_info['columns']); // Get the first database column. Other columns are ignored.
function cck_facets_create_category_default($field, $value, $count, $full_active_path) {
$db_info = _cck_facets_db_info($field);
$column = reset($db_info['columns']); // Get the first database column. Other columns are ignored.
if (is_array($value)) {
// Value is coming from the database.
$key = key($db_info['columns']);
$value = $value[$key];
}
else {
// Perform basic validation on user-specified value.
if (!is_numeric($value) && in_array($column['type'], array('int', 'mediumint', 'tinyint', 'bigint', 'float'))) {
return FALSE;
}
......@@ -289,11 +290,12 @@ class cck_facet extends faceted_search_facet {
$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'];
$key = key($db_info['columns']);
$categories = array();
while ($result = db_fetch_object($results)) {
// Add a new category.
$categories[] = cck_facets_create_category($this->_field, $result->{$field_alias}, 'db', $result->count, FALSE);
$categories[] = cck_facets_create_category($this->_field, array($key => $result->{$field_alias}), $result->count, FALSE);
}
return $categories;
}
......@@ -336,8 +338,8 @@ class cck_facet_category extends faceted_search_category {
function get_label($html = FALSE) {
$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));
$key = key($db_info['columns']); // Get the key required by the formatter.
$label = content_format($this->_field['field_name'], array($key => $this->_value));
// Note: The label is already filtered by the CCK formatter and does not
// need to be checked here.
......
......@@ -26,7 +26,20 @@ function reference_facets_cck_facets_factory($type) {
/**
* Create category for the specified field value.
*/
function reference_facets_create_category($field, $value, $source, $count = NULL, $category_path = TRUE) {
function reference_facets_create_category($field, $value, $count = NULL, $category_path = TRUE) {
$db_info = _cck_facets_db_info($field);
$column = reset($db_info['columns']); // Get the first database column. Other columns are ignored.
if (is_array($value)) {
// Value is coming from the database.
$key = key($db_info['columns']);
$value = $value[$key];
}
else {
// Perform basic validation on user-specified value.
if (!is_numeric($value)) {
return FALSE;
}
}
$category = new reference_facet_category($field, $value, $count);
return $category_path ? array($category) : $category;
}
......
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