Commit 22f095ae authored by David Lesieur's avatar David Lesieur

Added hook to allow specialized facet classes per field type.

parent 05531bb5
......@@ -29,7 +29,7 @@ function cck_facets_faceted_search_collect(&$facets, $domain, $env_id, $filter,
$fields = content_fields();
foreach ($fields as $field) {
if (!isset($filter) || isset($filter['cck'][$field['field_name']])) {
$facets[] = new cck_facet($field['field_name']);
$facets[] = cck_facets_create_facet($field['field_name'], $field['type']);
}
}
break;
......@@ -53,8 +53,8 @@ function cck_facets_faceted_search_collect(&$facets, $domain, $env_id, $filter,
$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.
$active_category = new cck_facet_category($field_name, $field_value, content_format($fields[$field_name], array('value' => $field_value)));
$facets[] = new cck_facet($field_name, $active_category);
$active_path = cck_facets_create_category($field_name, $fields[$field_name]['type'], $field_value, content_format($fields[$field_name], array('value' => $field_value)));
$facets[] = cck_facets_create_facet($field_name, $fields[$field_name]['type'], $active_path);
}
}
}
......@@ -72,8 +72,8 @@ function cck_facets_faceted_search_collect(&$facets, $domain, $env_id, $filter,
$column = 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_category = new cck_facet_category($field['field_name'], $item[$column], content_format($field['field_name'], $item));
$facets[] = new cck_facet($field['field_name'], $active_category);
$active_path = cck_facets_create_category($field['field_name'], $field['type'], $item[$column], content_format($field['field_name'], $item));
$facets[] = cck_facets_create_facet($field['field_name'], $field['type'], $active_path);
}
}
}
......@@ -81,6 +81,73 @@ function cck_facets_faceted_search_collect(&$facets, $domain, $env_id, $filter,
}
}
/**
* Create a facet object for the specified field.
*
* @param $field_name
* Name of the field corresponding the facet to create.
* @param $field_type
* Type of the field corresponding the facet to create.
* @param $active_path
* Array representing the path leading to the active category, including the
* active category itself. Defaults to an empty array, meaning no active
* category in the facet.
* @return
* The newly created facet.
*/
function cck_facets_create_facet($field_name, $field_type, $active_path = array()) {
static $factories = array();
if (!isset($factories)) {
$factories = module_invoke_all('cck_facets_factory', 'facet');
}
if (isset($factories[$field_type])) {
$factory = $factories[$field_type];
return $factory($field_name, $field_type, $active_path);
}
else {
// No factory found for the given field type. Use the generic facet class.
return new cck_facet($field_name, $field_type, $active_path);
}
}
/**
* Create category, or a path of categories for the specified field value.
*
* @param $field_name
* Name of the field corresponding the facet containing the category to
* create.
* @param $field_type
* Type of the field corresponding the facet containing the category to
* create.
* @param $value
* Value of the field, to be assigned to the category.
* @param $formatted_value
* Formatted value of the field, to be used as the category's label.
* @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.
* @return
* Single category object (if $path was TRUE), or an array of categories otherwise.
*/
function cck_facets_create_category($field_name, $field_type, $value, $formatted_value, $count = NULL, $category_path = TRUE) {
static $factories = array();
if (!isset($factories)) {
$factories = module_invoke_all('cck_facets_factory', 'category');
}
if (isset($factories[$field_type])) {
$factory = $factories[$field_type];
return $factory($field_name, $category_path);
}
else {
// No factory found for the given field type. Use the generic category class.
$category = new cck_facet_category($field_name, $value, $formatted_value, $count);
return $category_path ? array($category) : $category;
}
}
/**
* A facet for CCK fields. Each facet represents a CCK field.
*
......@@ -93,7 +160,12 @@ class cck_facet extends faceted_search_facet {
var $_field_name = NULL;
/**
* Database column alias for retrieving values for this facet.
* Type of the field corresponding to this facet.
*/
var $_field_type = NULL;
/**
* Alias for retrieving values for this facet from faceted search's query results.
*/
var $_field_alias = NULL;
......@@ -103,20 +175,20 @@ class cck_facet extends faceted_search_facet {
var $_field_column = NULL;
/**
* Constructor. Optionally assigns the active category of the facet.
* Constructor.
*
* @param $field_name
* Name of the field corresponding to this facet.
*
* @param $active_category
* Optional. This facet's active category.
* @param $field_type
* Type of the field corresponding to this facet.
* @param $active_path
* Array representing the path leading to the active category, including the
* active category itself. Defaults to an empty array, meaning no active
* category.
*/
function cck_facet($field_name, $active_category = NULL) {
function cck_facet($field_name, $field_type, $active_path = array()) {
$this->_field_name = $field_name;
$active_path = array();
if (isset($active_category)) {
$active_path[] = $active_category;
}
$this->_field_type = $field_type;
parent::faceted_search_facet('cck', $active_path);
}
......@@ -197,7 +269,6 @@ class cck_facet extends faceted_search_facet {
*
* @param $query
* The query object to update.
*
* @return
* FALSE if this facet can't have root categories.
*/
......@@ -218,7 +289,6 @@ class cck_facet extends faceted_search_facet {
*
* @param $results
* $results A database query result resource.
*
* @return
* Array of categories.
*/
......@@ -227,7 +297,7 @@ class cck_facet extends faceted_search_facet {
while ($result = db_fetch_object($results)) {
// Add a new category.
$field_alias = $this->_field_alias;
$categories[] = new cck_facet_category($this->_field_name, $result->$field_alias, content_format($this->_field_name, array($this->_field_column => $result->$field_alias)), $result->count);
$categories[] = cck_facets_create_category($this->_field_name, $this->_field_type, $result->$field_alias, content_format($this->_field_name, array($this->_field_column => $result->$field_alias)), $result->count, FALSE);
}
return $categories;
}
......
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