text_facets.module 2.85 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
<?php
// $Id$

/**
 * @file
 * Provides facets based on CCK Text fields.
 */

require_once('./'. drupal_get_path('module', 'text_facets') .'/cck_facets.inc');

/**
 * Implementation of hook_faceted_search_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;

    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));
            }
          }
        }
        // Remove the parsed text
        $arg = _cck_facets_quoted_query_insert($arg, 'text');
      }
      return $arg;

    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']})) {
          // 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;
  }
}

/**
 * A facet for CCK Text fields.
 */
class text_facet extends cck_facet {
  
  function text_facet($field, $active_path = array()) {
    parent::cck_facet('text', $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 '';
  }
}