text_facets.module 2.03 KB
Newer Older
1 2 3 4 5
<?php
// $Id$

/**
 * @file
David Lesieur's avatar
David Lesieur committed
6
 * Exposes CCK Text fields as facets.
7 8 9 10 11
 */

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

/**
12
 * Implementation of hook_cck_facets_collect().
13
 */
14 15 16 17 18 19
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;
20

21 22 23 24 25 26 27 28 29
      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));
30
          }
31 32
          // Remove the parsed text
          $arg = _cck_facets_quoted_query_insert($arg, $field['field_name']);
33
        }
34
        break;
35

36 37
      case 'node':
        if (isset($arg->{$field['field_name']}) && is_array($arg->{$field['field_name']})) {
38 39
          // Iterate through the field's multiple values.
          foreach ($arg->{$field['field_name']} as $item) {
40 41 42 43 44
            $value = array_shift($item);
            if ($value != '') {
              $category = new cck_facet_category($field, $value);
              $facets[] = new text_facet($field, array($category));
            }
45 46
          }
        }
47 48
        break;
    }
49
  }
50
  return $arg;
51 52 53 54 55 56 57 58
}

/**
 * A facet for CCK Text fields.
 */
class text_facet extends cck_facet {
  
  function text_facet($field, $active_path = array()) {
59
    parent::cck_facet($field, $active_path);
60 61 62 63 64
  }

  function get_text() {
    if ($category = $this->get_active_category()) {
      // Enclose value in double-quotes and escape double-quotes within the value.
65
      return '"'. strtr($category->_value, '"', '""') .'"';
66 67 68 69
    }
    return '';
  }
}