Commit a4e8c62d authored by David Lesieur's avatar David Lesieur

Simplified URLs. Made some nice generalizations that also simplify the code...

Simplified URLs. Made some nice generalizations that also simplify the code and slightly improve efficiency.
parent 29428d9f
......@@ -25,10 +25,6 @@ class cck_facet extends faceted_search_facet {
/**
* Constructor.
*
* @param $key
* Key corresponding to this class of facet. This should be the same string
* as used to construct the facet from the search text in the module's
* implementation of hook_faceted_search_parse().
* @param $field
* The field corresponding to this facet.
* @param $active_path
......@@ -36,16 +32,16 @@ class cck_facet extends faceted_search_facet {
* active category itself. Defaults to an empty array, meaning no active
* category.
*/
function cck_facet($key, $field, $active_path = array()) {
function cck_facet($field, $active_path = array()) {
$this->_field = &$field;
parent::faceted_search_facet($key, $active_path);
parent::faceted_search_facet($field['field_name'], $active_path);
}
/**
* Return the id of this facet.
*/
function get_id() {
return $this->_field['field_name'];
return 1;
}
/**
......@@ -67,7 +63,7 @@ class cck_facet extends faceted_search_facet {
*/
function get_text() {
if ($category = $this->get_active_category()) {
return $this->_field['field_name'] .':'. $category->_value;
return $category->_value;
}
return '';
}
......@@ -217,37 +213,3 @@ class cck_facet_category extends faceted_search_category {
}
}
// TODO: Move this to faceted_search.inc, avoiding duplication in other modules (biblio_facets).
function _cck_facets_quoted_query_extract($keys, $option) {
// Based on search_query_extract, but keys terminate on a double-quote
// followed by a space rather than a single space.
if (preg_match('/(^| )'. $option .':(.*?")( |$)/i', $keys, $matches)) {
return $matches[2];
}
}
// TODO: Move this to faceted_search.inc, avoiding duplication in other modules (biblio_facets).
function _cck_facets_quoted_query_insert($keys, $option, $value = '') {
// Based on search_query_insert, but keys terminate on a double-quote followed
// by a space rather than a single space.
if (search_query_extract($keys, $option)) {
$keys = trim(preg_replace('/(^| )'. $option .':(.*?)(" |$)/i', '', $keys));
}
if ($value != '') {
$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
; $Id$
name = CCK Facets
description = "Provides basic functionality for modules that expose CCK fields as facets."
dependencies = content faceted_search cck_facets
package = Faceted Search
<?php
// $Id$
/**
* @file
* Provides an API for modules to expose facets based on CCK fields.
*/
/**
* Implementation of hook_faceted_search_collect().
*
* To ensure that all fields are not repeatedly iterated by every CCK facet
* module, this implementation of hook_faceted_search_collect() acts as the
* master and finds what module to call for each type of field.
*/
function cck_facets_faceted_search_collect(&$facets, $domain, $env_id, $filter, $arg = NULL) {
$fields = content_fields();
foreach ($fields as $field) {
if (!isset($filter) || isset($filter[$field['field_name']][1])) {
$hook = 'cck_facets_collect';
foreach (module_implements($hook) as $module) {
$function = $module .'_'. $hook;
if ($domain == 'text') {
$arg = $function($facets, $field, $domain, $env_id, $arg);
}
else {
$function($facets, $field, $domain, $env_id, $arg);
}
}
}
}
if ($domain == 'text') {
return $arg;
}
}
// TODO: Move this to faceted_search.inc, avoiding duplication in other modules (biblio_facets).
function _cck_facets_quoted_query_extract($keys, $option) {
// Based on search_query_extract, but keys terminate on a double-quote
// followed by a space rather than a single space.
if (preg_match('/(^| )'. $option .':(.*?")( |$)/i', $keys, $matches)) {
return $matches[2];
}
}
// TODO: Move this to faceted_search.inc, avoiding duplication in other modules (biblio_facets).
function _cck_facets_quoted_query_insert($keys, $option, $value = '') {
// Based on search_query_insert, but keys terminate on a double-quote followed
// by a space rather than a single space.
if (search_query_extract($keys, $option)) {
$keys = trim(preg_replace('/(^| )'. $option .':(.*?)(" |$)/i', '', $keys));
}
if ($value != '') {
$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
; $Id$
name = CCK Number Facets
name = Number Facets
description = "Provides facets based on CCK Number fields."
dependencies = content number faceted_search search
dependencies = content number faceted_search cck_facets search
package = Faceted Search
......@@ -9,56 +9,40 @@
require_once('./'. drupal_get_path('module', 'number_facets') .'/cck_facets.inc');
/**
* Implementation of hook_faceted_search_collect().
* Implementation of hook_cck_facets_collect().
*/
function number_facets_faceted_search_collect(&$facets, $domain, $env_id, $filter, $arg = NULL) {
switch ($domain) {
case 'all':
$fields = content_fields();
foreach ($fields as $field) {
if ((!isset($filter) || isset($filter['number'][$field['field_name']])) && ($field['type'] == 'number_integer' || $field['type'] == 'number_decimal')) {
$facets[] = new cck_facet('number', $field);
}
}
break;
function number_facets_cck_facets_collect(&$facets, $field, $domain, $env_id, $arg = NULL) {
if ($field['type'] == 'number_decimal' || $field['type'] == 'number_integer') {
switch ($domain) {
case 'all':
$facets[] = new cck_facet($field);
break;
case 'text':
// Scan the given search text for a 'number:{field_name}:{value}'
// token, and create facets from it.
if ($found_text = search_query_extract($arg, 'number')) {
// Extract separate facets. Using a regex rather than a simple
// explode(',',$found_text) because commas might be used in field values
// and we want to ignore those when splitting the text.
$matches = array();
if (preg_match_all('/((.+?):(.+?))(,| |$)/', $found_text, $matches)) {
$fields = content_fields();
foreach ($matches[1] as $index => $match) {
$field_name = $matches[2][$index];
$value = $matches[3][$index];
if (isset($field_name) && isset($value) && is_numeric($value) && isset($fields[$field_name]) && (!isset($filter) || isset($filter['number'][$field_name]))) {
// Create an active facet with the value found in the search text.
$category = new cck_facet_category($fields[$field_name], $value);
$facets[] = new cck_facet('number', $fields[$field_name], array($category));
}
case 'text':
// Scan the given search text for a '{field_name}:{value}'
// token, and create facets from it.
if ($number = search_query_extract($arg, $field['field_name'])) {
if (is_numeric($number)) {
// Create an active facet with the value found in the search text.
$category = new cck_facet_category($field, $number);
$facets[] = new cck_facet($field, array($category));
}
// Remove the parsed text
$arg = search_query_insert($arg, $field['field_name']);
}
// Remove the parsed text
$arg = search_query_insert($arg, 'number');
}
return $arg;
break;
case 'node':
$fields = content_fields();
foreach ($fields as $field) {
if ((!isset($filter) || isset($filter['number'][$field['field_name']])) && isset($arg->{$field['field_name']}) && is_array($arg->{$field['field_name']})) {
case 'node':
if (isset($arg->{$field['field_name']}) && is_array($arg->{$field['field_name']})) {
// Iterate through the field's multiple values.
foreach ($arg->{$field['field_name']} as $item) {
$category = new cck_facet_category($field, array_shift($item));
$facets[] = new cck_facet('number', $field, array($category));
$facets[] = new cck_facet($field, array($category));
}
}
}
break;
break;
}
}
return $arg;
}
; $Id$
name = CCK Reference Facets
name = Reference Facets
description = "Provides facets based on CCK Node Reference and User Reference fields."
dependencies = content faceted_search search
dependencies = content faceted_search cck_facets search
package = Faceted Search
......@@ -9,48 +9,31 @@
require_once('./'. drupal_get_path('module', 'reference_facets') .'/cck_facets.inc');
/**
* Implementation of hook_faceted_search_collect().
* Implementation of hook_cck_facets_collect().
*/
function reference_facets_faceted_search_collect(&$facets, $domain, $env_id, $filter, $arg = NULL) {
switch ($domain) {
case 'all':
$fields = content_fields();
foreach ($fields as $field) {
if ((!isset($filter) || isset($filter['reference'][$field['field_name']])) && ($field['type'] == 'nodereference' || $field['type'] == 'userreference')) {
$facets[] = new reference_facet($field);
}
}
break;
function reference_facets_cck_facets_collect(&$facets, $field, $domain, $env_id, $arg = NULL) {
if ($field['type'] == 'nodereference' || $field['type'] == 'userreference') {
switch ($domain) {
case 'all':
$facets[] = new reference_facet($field);
break;
case 'text':
// Scan the given search text for a 'reference:{field_name}:{value}'
// token, and create facets from it.
if ($found_text = search_query_extract($arg, 'reference')) {
// Extract separate facets. Using a regex rather than a simple
// explode(',',$found_text) because commas might be used in field values
// and we want to ignore those when splitting the text.
$matches = array();
if (preg_match_all('/((.+?):(.+?))(,| |$)/', $found_text, $matches)) {
$fields = content_fields();
foreach ($matches[1] as $index => $match) {
$field_name = $matches[2][$index];
$value = $matches[3][$index];
if (isset($field_name) && isset($value) && is_numeric($value) && $value > 0 && isset($fields[$field_name]) && (!isset($filter) || isset($filter['reference'][$field_name]))) {
// Create an active facet with the value found in the search text.
$category = new reference_facet_category($fields[$field_name], $value);
$facets[] = new reference_facet($fields[$field_name], array($category));
}
case 'text':
// Scan the given search text for a '{field_name}:{value}'
// token, and create facets from it.
if ($ref = search_query_extract($arg, $field['field_name'])) {
if (is_numeric($ref) && $ref > 0) {
// Create an active facet with the value found in the search text.
$category = new reference_facet_category($field, $ref);
$facets[] = new reference_facet($field, array($category));
}
// Remove the parsed text
$arg = search_query_insert($arg, $field['field_name']);
}
// Remove the parsed text
$arg = search_query_insert($arg, 'reference');
}
return $arg;
break;
case 'node':
$fields = content_fields();
foreach ($fields as $field) {
if ((!isset($filter) || isset($filter['reference'][$field['field_name']])) && isset($arg->{$field['field_name']}) && is_array($arg->{$field['field_name']})) {
case 'node':
if (isset($arg->{$field['field_name']}) && is_array($arg->{$field['field_name']})) {
// Iterate through the field's multiple values.
foreach ($arg->{$field['field_name']} as $item) {
if (($value = array_shift($item)) != 0) { // Don't show empty references.
......@@ -59,9 +42,10 @@ function reference_facets_faceted_search_collect(&$facets, $domain, $env_id, $fi
}
}
}
}
break;
break;
}
}
return $arg;
}
/**
......@@ -72,7 +56,7 @@ function reference_facets_faceted_search_collect(&$facets, $domain, $env_id, $fi
class reference_facet extends cck_facet {
function reference_facet($field, $active_path = array()) {
parent::cck_facet('reference', $field, $active_path);
parent::cck_facet($field, $active_path);
}
function build_root_categories_query(&$query) {
......
; $Id$
name = CCK Text Facets
name = Text Facets
description = "Provides facets based on CCK Text fields."
dependencies = content text faceted_search search
dependencies = content text faceted_search cck_facets search
package = Faceted Search
......@@ -9,57 +9,42 @@
require_once('./'. drupal_get_path('module', 'text_facets') .'/cck_facets.inc');
/**
* Implementation of hook_faceted_search_collect().
* Implementation of hook_cck_facets_collect().
*/
function text_facets_faceted_search_collect(&$facets, $domain, $env_id, $filter, $arg = NULL) {
switch ($domain) {
case 'all':
$fields = content_fields();
foreach ($fields as $field) {
if ((!isset($filter) || isset($filter['text'][$field['field_name']])) && $field['type'] == 'text') {
$facets[] = new text_facet($field);
}
}
break;
function text_facets_cck_facets_collect(&$facets, $field, $domain, $env_id, $arg = NULL) {
if ($field['type'] == 'text') {
switch ($domain) {
case 'all':
$facets[] = new text_facet($field);
break;
case 'text':
// Scan the given search text for a 'text:{field_name}:"{value}"'
// token, and create facets from it.
if ($found_text = _cck_facets_quoted_query_extract($arg, 'text')) {
// Extract separate facets. Using a regex rather than a simple
// explode(',',$found_text) because commas might be used in field values
// and we want to ignore those when splitting the text.
$matches = array();
if (preg_match_all('/((.+?):"(.+?)")(,| |$)/', $found_text, $matches)) {
$fields = content_fields();
foreach ($matches[1] as $index => $match) {
$field_name = $matches[2][$index];
$value = strtr($matches[3][$index], '""', '"');
if (isset($field_name) && isset($value) && isset($fields[$field_name]) && (!isset($filter) || isset($filter['text'][$field_name]))) {
// Create an active facet with the value found in the search text.
$category = new cck_facet_category($fields[$field_name], $value);
$facets[] = new text_facet($fields[$field_name], array($category));
}
case 'text':
// Scan the given search text for a '{field_name}:"{value}"'
// token, and create facets from it.
if ($found_text = _cck_facets_quoted_query_extract($arg, $field['field_name'])) {
$found_text = trim(strtr($found_text, '""', '"'), '"');
if (!empty($found_text)) {
// Create an active facet with the value found in the search text.
$category = new cck_facet_category($field, $found_text);
$facets[] = new text_facet($field, array($category));
}
// Remove the parsed text
$arg = _cck_facets_quoted_query_insert($arg, $field['field_name']);
}
// Remove the parsed text
$arg = _cck_facets_quoted_query_insert($arg, 'text');
}
return $arg;
break;
case 'node':
$fields = content_fields();
foreach ($fields as $field) {
if ((!isset($filter) || isset($filter['text'][$field['field_name']])) && isset($arg->{$field['field_name']}) && is_array($arg->{$field['field_name']})) {
case 'node':
if (isset($arg->{$field['field_name']}) && is_array($arg->{$field['field_name']})) {
// Iterate through the field's multiple values.
foreach ($arg->{$field['field_name']} as $item) {
$category = new cck_facet_category($field, array_shift($item));
$facets[] = new text_facet($field, array($category));
}
}
}
break;
break;
}
}
return $arg;
}
/**
......@@ -68,13 +53,13 @@ function text_facets_faceted_search_collect(&$facets, $domain, $env_id, $filter,
class text_facet extends cck_facet {
function text_facet($field, $active_path = array()) {
parent::cck_facet('text', $field, $active_path);
parent::cck_facet($field, $active_path);
}
function get_text() {
if ($category = $this->get_active_category()) {
// Enclose value in double-quotes and escape double-quotes within the value.
return $this->_field['field_name'] .':"'. strtr($category->_value, '"', '""') .'"';
return '"'. strtr($category->_value, '"', '""') .'"';
}
return '';
}
......
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