| 1 |
<?php
|
| 2 |
// $Id: field_keyword_filter.module,v 1.4 2009/01/04 23:00:01 davidlesieur Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Allows users to perform keyword searches restricted by field.
|
| 7 |
*/
|
| 8 |
|
| 9 |
require_once('./'. drupal_get_path('module', 'faceted_search') .'/faceted_search.inc');
|
| 10 |
|
| 11 |
/**
|
| 12 |
* Implementation of hook_faceted_search_collect().
|
| 13 |
*/
|
| 14 |
function field_keyword_filter_faceted_search_collect(&$filters, $domain, $env, $selection, $arg = NULL) {
|
| 15 |
switch ($domain) {
|
| 16 |
case 'keyword filters':
|
| 17 |
$fields = field_indexer_load_fields(TRUE);
|
| 18 |
foreach ($fields as $field) {
|
| 19 |
$filter_key = field_indexer_type($field['fiid']);
|
| 20 |
// If the field's corresponding filter is allowed.
|
| 21 |
if (!isset($selection) || isset($selection[$filter_key]['keyword'])) {
|
| 22 |
$filters[] = new faceted_search_keyword_filter($filter_key, $field['label']);
|
| 23 |
}
|
| 24 |
}
|
| 25 |
break;
|
| 26 |
|
| 27 |
case 'text':
|
| 28 |
$fields = field_indexer_load_fields(TRUE);
|
| 29 |
foreach ($fields as $field) {
|
| 30 |
$filter_key = field_indexer_type($field['fiid']);
|
| 31 |
// If the field's corresponding filter is allowed.
|
| 32 |
if (!isset($selection) || isset($selection[$filter_key]['keyword'])) {
|
| 33 |
// Scan the given search text for a '{key}:"{value}"' token, and create
|
| 34 |
// filters from it.
|
| 35 |
if (!is_null($found_text = faceted_search_quoted_query_extract($arg, $filter_key))) {
|
| 36 |
$keys = faceted_search_parse_keywords($found_text);
|
| 37 |
|
| 38 |
// Create the filters.
|
| 39 |
foreach ($keys['positive'] as $keyword) {
|
| 40 |
if (is_array($keyword)) {
|
| 41 |
$filter = new field_keyword_filter($filter_key, $field['label'], new faceted_search_keyword_or_category($keyword));
|
| 42 |
}
|
| 43 |
elseif (strpos($keyword, ' ')) {
|
| 44 |
$filter = new field_keyword_filter($filter_key, $field['label'], new faceted_search_keyword_phrase_category($keyword));
|
| 45 |
}
|
| 46 |
else {
|
| 47 |
$filter = new field_keyword_filter($filter_key, $field['label'], new faceted_search_keyword_and_category($keyword));
|
| 48 |
}
|
| 49 |
$filters[] = $filter;
|
| 50 |
}
|
| 51 |
foreach ($keys['negative'] as $keyword) {
|
| 52 |
$filter = new field_keyword_filter($filter_key, $field['label'], new faceted_search_keyword_not_category($keyword));
|
| 53 |
$filters[] = $filter;
|
| 54 |
}
|
| 55 |
// Remove the parsed text
|
| 56 |
$arg = faceted_search_quoted_query_insert($arg, $filter_key);
|
| 57 |
}
|
| 58 |
}
|
| 59 |
}
|
| 60 |
return $arg;
|
| 61 |
}
|
| 62 |
}
|
| 63 |
|
| 64 |
/**
|
| 65 |
* A filter for restricting a keyword search to a specific field.
|
| 66 |
*/
|
| 67 |
class field_keyword_filter extends faceted_search_keyword_filter {
|
| 68 |
/**
|
| 69 |
* Constructor.
|
| 70 |
*
|
| 71 |
* @param $type
|
| 72 |
* Type of the search index entries corresponding to the field.
|
| 73 |
* @param $label
|
| 74 |
* Label of the field.
|
| 75 |
* @param $category
|
| 76 |
* Active category of the field.
|
| 77 |
*/
|
| 78 |
function field_keyword_filter($type, $label, $category = NULL) {
|
| 79 |
parent::faceted_search_keyword_filter($type, $label, $category);
|
| 80 |
}
|
| 81 |
|
| 82 |
/**
|
| 83 |
* Return the search text corresponding to this filter.
|
| 84 |
*/
|
| 85 |
function get_text() {
|
| 86 |
if ($category = $this->get_active_category()) {
|
| 87 |
// Quote and escape the value.
|
| 88 |
return '"'. faceted_search_quoted_query_escape(parent::get_text()) .'"';
|
| 89 |
}
|
| 90 |
return '';
|
| 91 |
}
|
| 92 |
|
| 93 |
}
|