/[drupal]/contributions/modules/cck_facets/reference_facets.module
ViewVC logotype

Contents of /contributions/modules/cck_facets/reference_facets.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.11 - (show annotations) (download) (as text)
Sun Jan 4 20:06:55 2009 UTC (10 months, 2 weeks ago) by davidlesieur
Branch: MAIN
CVS Tags: HEAD
Changes since 1.10: +2 -2 lines
File MIME type: text/x-php
Updated for API changes in Faceted Search.
1 <?php
2 // $Id: reference_facets.module,v 1.10 2008/09/03 23:06:52 davidlesieur Exp $
3
4 /**
5 * @file
6 * Exposes CCK Node Reference and User Reference fields as facets.
7 */
8
9 module_load_include('inc', 'cck_facets');
10
11 /**
12 * Implementation of hook_cck_facets_collect().
13 */
14 function reference_facets_cck_facets_collect(&$facets, $field, $domain, $env, $arg = NULL) {
15 if ($field['type'] == 'nodereference' || $field['type'] == 'userreference') {
16 switch ($domain) {
17 case 'facets':
18 $facets[] = new reference_facet($field);
19 break;
20
21 case 'text':
22 // Scan the given search text for a '{field_name}:{value}'
23 // token, and create facets from it.
24 if ($ref = search_query_extract($arg, $field['field_name'])) {
25 if (is_numeric($ref) && $ref > 0) {
26 // Create an active facet with the value found in the search text.
27 $category = new reference_facet_category($field, $ref);
28 $facets[] = new reference_facet($field, array($category));
29 }
30 // Remove the parsed text
31 $arg = search_query_insert($arg, $field['field_name']);
32 }
33 break;
34
35 case 'node':
36 if (isset($arg->{$field['field_name']}) && is_array($arg->{$field['field_name']})) {
37 // Iterate through the field's multiple values.
38 foreach ($arg->{$field['field_name']} as $item) {
39 if (($value = array_shift($item)) != 0) { // Don't show empty references.
40 $category = new reference_facet_category($field, $value);
41 $facets[] = new reference_facet($field, array($category));
42 }
43 }
44 }
45 break;
46 }
47 }
48 return $arg;
49 }
50
51 /**
52 * A facet for CCK Node Reference and User Reference fields.
53 *
54 * @see reference_facet_category
55 */
56 class reference_facet extends cck_facet {
57
58 function reference_facet($field, $active_path = array()) {
59 parent::cck_facet($field, $active_path);
60 }
61
62 function build_root_categories_query(&$query) {
63 $db_info = _cck_facets_db_info($this->_field);
64 $query->add_table($db_info['table'], 'vid', 'n', 'vid');
65 $main_column = array_shift($db_info['columns']);
66 $query->add_field($db_info['table'], $main_column['column'], $this->_field['field_name']);
67 $query->add_where($db_info['table'] .'.'. $main_column['column'] .' != 0'); // No empty references.
68 $query->add_groupby($this->_field['field_name']);
69 return TRUE;
70 }
71
72 function build_categories($results) {
73 $categories = array();
74 while ($result = db_fetch_object($results)) {
75 $categories[] = new reference_facet_category($this->_field, $result->{$this->_field['field_name']}, $result->count);
76 }
77 return $categories;
78 }
79 }
80
81
82 /**
83 * A facet category for CCK Node Reference and User Reference fields.
84 *
85 * @see reference_facet
86 */
87 class reference_facet_category extends cck_facet_category {
88
89 function reference_facet_category($field, $value, $count = NULL) {
90 parent::cck_facet_category($field, $value, $count);
91 }
92
93 function get_label($html = FALSE) {
94 $db_info = _cck_facets_db_info($this->_field);
95
96 // Get the label using the 'plain' formatter.
97 $label = content_format($this->_field['field_name'], array(key($db_info['columns']) => $this->_value), 'plain');
98
99 // Note: The label is already filtered by the CCK formatter and does not
100 // need to be filtered here.
101 return $html ? $label : strip_tags($label);
102 }
103 }

  ViewVC Help
Powered by ViewVC 1.1.2