Commit 298b7a65 authored by David Lesieur's avatar David Lesieur

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

parent cb95f7fd
......@@ -37,14 +37,14 @@ function cck_facets_faceted_search_collect(&$facets, $domain, $env_id, $filter,
$fields = content_fields();
foreach ($matches[1] as $index => $match) {
$field_name = $matches[2][$index];
$field_value = strtr($matches[3][$index], '""', '"');
if (isset($field_name) && isset($field_value) && isset($fields[$field_name]) && (!isset($filter) || isset($filter['cck'][$field_name]))) {
$value = strtr($matches[3][$index], '""', '"');
if (isset($field_name) && isset($value) && isset($fields[$field_name]) && (!isset($filter) || isset($filter['cck'][$field_name]))) {
// Validate the value.
$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'))) {
if (is_numeric($value) || !in_array($column['type'], array('int', 'mediumint', 'tinyint', 'bigint', 'float'))) {
// Create an active facet with the value found in the search text.
$active_path = cck_facets_create_category($fields[$field_name], $field_value);
$active_path = cck_facets_create_category($fields[$field_name], $value, 'user');
$facets[] = cck_facets_create_facet($fields[$field_name], $active_path);
}
}
......@@ -63,7 +63,7 @@ function cck_facets_faceted_search_collect(&$facets, $domain, $env_id, $filter,
$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]);
$active_path = cck_facets_create_category($field, $item[$column_name], 'db');
$facets[] = cck_facets_create_facet($field, $active_path);
}
}
......@@ -91,12 +91,19 @@ function cck_facets_create_facet($field, $active_path = array()) {
}
if (isset($factories[$field['type']])) {
$factory = $factories[$field['type']];
return $factory($field, $active_path);
}
else {
// No factory found for the given field type. Use the generic facet class.
return new cck_facet($field, $active_path);
// No specialized factory found for the given field type.
$factory = 'cck_facets_create_facet_default';
}
return $factory($field, $active_path);
}
/**
* Default factory for creating a facet.
*/
function cck_facets_create_facet_default($field, $active_path) {
return new cck_facet($field, $active_path);
}
/**
......@@ -107,29 +114,45 @@ function cck_facets_create_facet($field, $active_path = array()) {
* 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.
* @param $count
* Optional. The number of nodes associated to this category within the
* current search.
* @param $category_path
* Optional. When TRUE (which is the default), create the full path of
* categories corresponding to the field value.
* @param $full_active_path
* Optional. For some field types (such as dates), a single value might be
* represented by a path of categories (e.g. yyyy > yyyy-mm > yyyy-mm-dd).
* When $full_active_path is TRUE, the full path corresponding to $value is to
* be returned, otherwise only the "leaf" category gets returned.
* @return
* Single category object (if $path was TRUE), or an array of categories otherwise.
* 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, $count = NULL, $category_path = TRUE) {
function cck_facets_create_category($field, $value, $source, $count = NULL, $full_active_path = TRUE) {
static $factories = NULL;
if (!isset($factories)) {
$factories = module_invoke_all('cck_facets_factory', 'category');
}
if (isset($factories[$field['type']])) {
$factory = $factories[$field['type']];
return $factory($field, $value, $count, $category_path);
}
else {
// No factory found for the given field type. Use the generic category class.
$category = new cck_facet_category($field, $value, $count);
return $category_path ? array($category) : $category;
// No specialized factory found for the given field type.
$factory = 'cck_facets_create_category_default';
}
return $factory($field, $value, $source, $count, $full_active_path);
}
/**
* Default factory for creating a category.
*
* @see cck_facets_create_category
*/
function cck_facets_create_category_default($field, $value, $source, $count, $full_active_path) {
$category = new cck_facet_category($field, $value, $count);
return $full_active_path ? array($category) : $category;
}
/**
......@@ -266,7 +289,7 @@ class cck_facet extends faceted_search_facet {
$categories = array();
while ($result = db_fetch_object($results)) {
// Add a new category.
$categories[] = cck_facets_create_category($this->_field, $result->{$field_alias}, $result->count, FALSE);
$categories[] = cck_facets_create_category($this->_field, $result->{$field_alias}, 'db', $result->count, FALSE);
}
return $categories;
}
......
......@@ -26,7 +26,7 @@ function reference_facets_cck_facets_factory($type) {
/**
* Create category for the specified field value.
*/
function reference_facets_create_category($field, $value, $count = NULL, $category_path = TRUE) {
function reference_facets_create_category($field, $value, $source, $count = NULL, $category_path = TRUE) {
$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