| 1 |
<?php
|
| 2 |
// $Id: cck_facets.inc,v 1.3 2008/02/28 18:26:03 davidlesieur Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Provides an API for modules to expose facets based on CCK fields.
|
| 7 |
*/
|
| 8 |
|
| 9 |
require_once('./'. drupal_get_path('module', 'faceted_search') .'/faceted_search.inc');
|
| 10 |
|
| 11 |
/**
|
| 12 |
* A base class for facets for CCK fields.
|
| 13 |
*
|
| 14 |
* @see cck_facet_category
|
| 15 |
*/
|
| 16 |
class cck_facet extends faceted_search_facet {
|
| 17 |
/**
|
| 18 |
* The field corresponding to this facet.
|
| 19 |
*/
|
| 20 |
var $_field = NULL;
|
| 21 |
|
| 22 |
/**
|
| 23 |
* Constructor.
|
| 24 |
*
|
| 25 |
* @param $field
|
| 26 |
* The field corresponding to this facet.
|
| 27 |
* @param $active_path
|
| 28 |
* Array representing the path leading to the active category, including the
|
| 29 |
* active category itself. Defaults to an empty array, meaning no active
|
| 30 |
* category.
|
| 31 |
*/
|
| 32 |
function cck_facet($field, $active_path = array()) {
|
| 33 |
$this->_field = &$field;
|
| 34 |
parent::faceted_search_facet($field['field_name'], $active_path);
|
| 35 |
}
|
| 36 |
|
| 37 |
/**
|
| 38 |
* Return the id of this facet.
|
| 39 |
*/
|
| 40 |
function get_id() {
|
| 41 |
return 1;
|
| 42 |
}
|
| 43 |
|
| 44 |
/**
|
| 45 |
* Return the label of this facet. The implementor is responsible to ensure
|
| 46 |
* adequate security filtering.
|
| 47 |
*/
|
| 48 |
function get_label() {
|
| 49 |
if (isset($this->_field['widget']['label'])) {
|
| 50 |
return check_plain($this->_field['widget']['label']);
|
| 51 |
}
|
| 52 |
else {
|
| 53 |
return check_plain($this->_field['field_name']);
|
| 54 |
}
|
| 55 |
}
|
| 56 |
|
| 57 |
/**
|
| 58 |
* Return the search text for this facet, taking into account this facet's
|
| 59 |
* active path.
|
| 60 |
*/
|
| 61 |
function get_text() {
|
| 62 |
if ($category = $this->get_active_category()) {
|
| 63 |
return $category->_value;
|
| 64 |
}
|
| 65 |
return '';
|
| 66 |
}
|
| 67 |
|
| 68 |
/**
|
| 69 |
* Return the field name to help site administrators identify fields.
|
| 70 |
*/
|
| 71 |
function get_help() {
|
| 72 |
return $this->_field['type_name'];
|
| 73 |
}
|
| 74 |
|
| 75 |
/**
|
| 76 |
* Returns the available sort options for this facet.
|
| 77 |
*/
|
| 78 |
function get_sort_options() {
|
| 79 |
$options = parent::get_sort_options();
|
| 80 |
if (isset($this->_field['widget']['label'])) {
|
| 81 |
$label = $this->_field['widget']['label'];
|
| 82 |
}
|
| 83 |
else {
|
| 84 |
$label = $this->_field['field_name'];
|
| 85 |
}
|
| 86 |
$options['field'] = check_plain($label);
|
| 87 |
return $options;
|
| 88 |
}
|
| 89 |
|
| 90 |
/**
|
| 91 |
* Handler for the 'count' sort criteria.
|
| 92 |
*/
|
| 93 |
function build_sort_query_count(&$query) {
|
| 94 |
$query->add_orderby('count', 'DESC');
|
| 95 |
$query->add_orderby($this->_field['field_name'], 'ASC');
|
| 96 |
}
|
| 97 |
|
| 98 |
/**
|
| 99 |
* Handler for the 'field' sort criteria.
|
| 100 |
*/
|
| 101 |
function build_sort_query_field(&$query) {
|
| 102 |
$query->add_orderby($this->_field['field_name'], 'ASC');
|
| 103 |
}
|
| 104 |
|
| 105 |
/**
|
| 106 |
* Updates a query for retrieving the root categories of this facet and their
|
| 107 |
* associated nodes within the current search results.
|
| 108 |
*
|
| 109 |
* @param $query
|
| 110 |
* The query object to update.
|
| 111 |
* @return
|
| 112 |
* FALSE if this facet can't have root categories.
|
| 113 |
*/
|
| 114 |
function build_root_categories_query(&$query) {
|
| 115 |
$db_info = _cck_facets_db_info($this->_field);
|
| 116 |
$query->add_table($db_info['table'], 'vid', 'n', 'vid');
|
| 117 |
$main_column = array_shift($db_info['columns']);
|
| 118 |
$query->add_field($db_info['table'], $main_column['column'], $this->_field['field_name']);
|
| 119 |
if ($main_column['type'] == 'longtext' || $main_column['type'] == 'varchar') {
|
| 120 |
// Reject empty text.
|
| 121 |
$query->add_where($db_info['table'] .'.'. $main_column['column'] ." != ''");
|
| 122 |
}
|
| 123 |
else {
|
| 124 |
// Reject null value.
|
| 125 |
$query->add_where($db_info['table'] .'.'. $main_column['column'] ." IS NOT NULL");
|
| 126 |
}
|
| 127 |
$query->add_groupby($this->_field['field_name']);
|
| 128 |
return TRUE;
|
| 129 |
}
|
| 130 |
|
| 131 |
/**
|
| 132 |
* This factory method creates categories given query results that include the
|
| 133 |
* fields selected in get_root_categories_query() or get_subcategories_query().
|
| 134 |
*
|
| 135 |
* @param $results
|
| 136 |
* $results A database query result resource.
|
| 137 |
* @return
|
| 138 |
* Array of categories.
|
| 139 |
*/
|
| 140 |
function build_categories($results) {
|
| 141 |
$categories = array();
|
| 142 |
while ($result = db_fetch_object($results)) {
|
| 143 |
$categories[] = new cck_facet_category($this->_field, $result->{$this->_field['field_name']}, $result->count);
|
| 144 |
}
|
| 145 |
return $categories;
|
| 146 |
}
|
| 147 |
}
|
| 148 |
|
| 149 |
/**
|
| 150 |
* A base class for facet categories for CCK fields.
|
| 151 |
*
|
| 152 |
* @see cck_facet
|
| 153 |
*/
|
| 154 |
class cck_facet_category extends faceted_search_category {
|
| 155 |
/**
|
| 156 |
* The field corresponding to this category's parent facet.
|
| 157 |
*/
|
| 158 |
var $_field = NULL;
|
| 159 |
|
| 160 |
/**
|
| 161 |
* Value corresponding to this category.
|
| 162 |
*/
|
| 163 |
var $_value = NULL;
|
| 164 |
|
| 165 |
/**
|
| 166 |
* Constructor.
|
| 167 |
*/
|
| 168 |
function cck_facet_category($field, $value, $count = NULL) {
|
| 169 |
parent::faceted_search_category($count);
|
| 170 |
$this->_field = &$field;
|
| 171 |
$this->_value = $value;
|
| 172 |
}
|
| 173 |
|
| 174 |
/**
|
| 175 |
* Return the label of this category.
|
| 176 |
*
|
| 177 |
* @param $html
|
| 178 |
* TRUE when HTML is allowed in the label, FALSE otherwise. Checking this
|
| 179 |
* flag allows implementors to provide a rich-text label if desired, and an
|
| 180 |
* alternate plain text version for cases where HTML cannot be used. The
|
| 181 |
* implementor is responsible to ensure adequate security filtering.
|
| 182 |
*/
|
| 183 |
function get_label($html = FALSE) {
|
| 184 |
$db_info = _cck_facets_db_info($this->_field);
|
| 185 |
$label = content_format($this->_field['field_name'], array(key($db_info['columns']) => $this->_value));
|
| 186 |
|
| 187 |
// Note: The label is already filtered by the CCK formatter and does not
|
| 188 |
// need to be filtered here.
|
| 189 |
return $html ? $label : strip_tags($label);
|
| 190 |
}
|
| 191 |
|
| 192 |
/**
|
| 193 |
* Updates a query for selecting nodes matching this category.
|
| 194 |
*
|
| 195 |
* @param $query
|
| 196 |
* The query object to update.
|
| 197 |
*/
|
| 198 |
function build_results_query(&$query) {
|
| 199 |
$db_info = _cck_facets_db_info($this->_field);
|
| 200 |
$main_column = array_shift($db_info['columns']);
|
| 201 |
// Since the same table might be joined multiple times in the query, we use
|
| 202 |
// the field's name as the table alias to create a unique table reference.
|
| 203 |
$table = $query->add_table($db_info['table'], 'vid', 'n', 'vid', $this->_field['field_name']);
|
| 204 |
switch($main_column['type']) {
|
| 205 |
case 'int':
|
| 206 |
case 'mediumint':
|
| 207 |
case 'tinyint':
|
| 208 |
case 'bigint':
|
| 209 |
$placeholder = '%d';
|
| 210 |
break;
|
| 211 |
case 'float':
|
| 212 |
$placeholder = '%f';
|
| 213 |
break;
|
| 214 |
default:
|
| 215 |
$placeholder = "'%s'";
|
| 216 |
}
|
| 217 |
$query->add_where($table .'.'. $main_column['column'] .' = '. $placeholder, $this->_value);
|
| 218 |
}
|
| 219 |
}
|
| 220 |
|