| 1 |
<?php
|
| 2 |
// $Id: biblio_type_facet.module,v 1.6 2008/04/14 17:37:16 davidlesieur Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Provides facets based on Biblio types.
|
| 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 biblio_type_facet_faceted_search_collect(&$facets, $domain, $env_id, $selection, $arg = NULL) {
|
| 15 |
switch ($domain) {
|
| 16 |
case 'facets':
|
| 17 |
// If the biblio type facet is allowed.
|
| 18 |
if (!isset($selection) || isset($selection['biblio_type'][1])) {
|
| 19 |
$facets[] = new biblio_type_facet();
|
| 20 |
}
|
| 21 |
break;
|
| 22 |
|
| 23 |
case 'text':
|
| 24 |
// If the biblio type facet is allowed.
|
| 25 |
if (!isset($selection) || isset($selection['biblio_type'][1])) {
|
| 26 |
// Scan the search text for a 'biblio_type:tid' token, and extract a
|
| 27 |
// facet from it.
|
| 28 |
if ($tid = search_query_extract($arg, 'biblio_type')) {
|
| 29 |
if (is_numeric($tid) && $tid > 0) {
|
| 30 |
if ($type = biblio_type_facet_types($tid)) {
|
| 31 |
// Create a facet with the biblio type found in the search text as
|
| 32 |
// the active category.
|
| 33 |
$facets[] = new biblio_type_facet($type->tid, $type->name);
|
| 34 |
}
|
| 35 |
}
|
| 36 |
// Remove the parsed token from the search text.
|
| 37 |
$arg = search_query_insert($arg, 'biblio_type');
|
| 38 |
}
|
| 39 |
}
|
| 40 |
return $arg;
|
| 41 |
|
| 42 |
case 'node':
|
| 43 |
// If the biblio type facet is allowed.
|
| 44 |
if ((!isset($selection) || isset($selection['biblio_type'][1])) && $arg->type == 'biblio' && isset($arg->biblio_type)) {
|
| 45 |
if ($type = biblio_type_facet_types($arg->biblio_type)) {
|
| 46 |
// Create a facet with the node's biblio type as the active category.
|
| 47 |
$facets[] = new biblio_type_facet($type->tid, $type->name);
|
| 48 |
}
|
| 49 |
}
|
| 50 |
break;
|
| 51 |
}
|
| 52 |
}
|
| 53 |
|
| 54 |
/**
|
| 55 |
* A facet for Biblio types.
|
| 56 |
*
|
| 57 |
* @see biblio_type_category
|
| 58 |
*/
|
| 59 |
class biblio_type_facet extends faceted_search_facet {
|
| 60 |
|
| 61 |
/**
|
| 62 |
* Constructor. Optionally assigns the active type of the facet.
|
| 63 |
*/
|
| 64 |
function biblio_type_facet($tid = 0, $name = '') {
|
| 65 |
$active_path = array();
|
| 66 |
if ($tid > 0 && !empty($name)) {
|
| 67 |
$active_path[] = new biblio_type_category($tid, $name);
|
| 68 |
}
|
| 69 |
parent::faceted_search_facet('biblio_type', $active_path);
|
| 70 |
}
|
| 71 |
|
| 72 |
/**
|
| 73 |
* Return the id of this facet.
|
| 74 |
*/
|
| 75 |
function get_id() {
|
| 76 |
return 1; // Only one facet of this class.
|
| 77 |
}
|
| 78 |
|
| 79 |
/**
|
| 80 |
* Return the label of this facet.
|
| 81 |
*/
|
| 82 |
function get_label() {
|
| 83 |
return t('Publication type');
|
| 84 |
}
|
| 85 |
|
| 86 |
/**
|
| 87 |
* Returns the available sort options for this facet.
|
| 88 |
*/
|
| 89 |
function get_sort_options() {
|
| 90 |
$options = parent::get_sort_options();
|
| 91 |
$options['type'] = t('Publication type');
|
| 92 |
return $options;
|
| 93 |
}
|
| 94 |
|
| 95 |
/**
|
| 96 |
* Handler for the 'count' sort criteria.
|
| 97 |
*/
|
| 98 |
function build_sort_query_count(&$query) {
|
| 99 |
$query->add_orderby('count', 'DESC');
|
| 100 |
$query->add_orderby('biblio_types.weight', 'ASC');
|
| 101 |
$query->add_orderby('biblio_types.name', 'ASC');
|
| 102 |
}
|
| 103 |
|
| 104 |
/**
|
| 105 |
* Handler for the 'type' sort criteria.
|
| 106 |
*/
|
| 107 |
function build_sort_query_type(&$query) {
|
| 108 |
$query->add_orderby('biblio_types.weight', 'ASC');
|
| 109 |
$query->add_orderby('biblio_types.name', 'ASC');
|
| 110 |
}
|
| 111 |
|
| 112 |
/**
|
| 113 |
* Return the search text for this facet, taking into account this facet's
|
| 114 |
* active path.
|
| 115 |
*/
|
| 116 |
function get_text() {
|
| 117 |
if ($category = $this->get_active_category()) {
|
| 118 |
return $category->_tid;
|
| 119 |
}
|
| 120 |
return '';
|
| 121 |
}
|
| 122 |
|
| 123 |
/**
|
| 124 |
* Updates a query for retrieving the root categories of this facet and their
|
| 125 |
* associated nodes within the current search results.
|
| 126 |
*
|
| 127 |
* @param $query
|
| 128 |
* The query object to update.
|
| 129 |
*
|
| 130 |
* @return
|
| 131 |
* FALSE if this facet can't have root categories.
|
| 132 |
*/
|
| 133 |
function build_root_categories_query(&$query) {
|
| 134 |
$query->add_table('biblio', 'vid', 'n', 'vid');
|
| 135 |
$query->add_table('biblio_types', 'tid', 'biblio', 'biblio_type');
|
| 136 |
$query->add_field('biblio_types', 'tid');
|
| 137 |
$query->add_field('biblio_types', 'name');
|
| 138 |
$query->add_where("n.type = 'biblio'");
|
| 139 |
$query->add_where('biblio_types.visible = 1');
|
| 140 |
$query->add_groupby('biblio_types_tid');
|
| 141 |
return TRUE;
|
| 142 |
}
|
| 143 |
|
| 144 |
/**
|
| 145 |
* This factory method creates categories given query results that include the
|
| 146 |
* fields selected in get_root_categories_query() or get_subcategories_query().
|
| 147 |
*
|
| 148 |
* @param $results
|
| 149 |
* $results A database query result resource.
|
| 150 |
*
|
| 151 |
* @return
|
| 152 |
* Array of categories.
|
| 153 |
*/
|
| 154 |
function build_categories($results) {
|
| 155 |
$categories = array();
|
| 156 |
while ($result = db_fetch_object($results)) {
|
| 157 |
$categories[] = new biblio_type_category($result->biblio_types_tid, $result->biblio_types_name, $result->count);
|
| 158 |
}
|
| 159 |
return $categories;
|
| 160 |
}
|
| 161 |
}
|
| 162 |
|
| 163 |
/**
|
| 164 |
* A facet category for Biblio types.
|
| 165 |
*
|
| 166 |
* @see biblio_type_facet
|
| 167 |
*/
|
| 168 |
class biblio_type_category extends faceted_search_category {
|
| 169 |
var $_tid = 0;
|
| 170 |
var $_name = '';
|
| 171 |
|
| 172 |
/**
|
| 173 |
* Constructor.
|
| 174 |
*/
|
| 175 |
function biblio_type_category($tid, $name, $count = NULL) {
|
| 176 |
parent::faceted_search_category($count);
|
| 177 |
$this->_tid = $tid;
|
| 178 |
$this->_name = $name;
|
| 179 |
}
|
| 180 |
|
| 181 |
/**
|
| 182 |
* Return the label of this category.
|
| 183 |
*/
|
| 184 |
function get_label() {
|
| 185 |
return $this->_name;
|
| 186 |
}
|
| 187 |
|
| 188 |
/**
|
| 189 |
* Updates a query for selecting nodes matching this category.
|
| 190 |
*
|
| 191 |
* @param $query
|
| 192 |
* The query object to update.
|
| 193 |
*/
|
| 194 |
function build_results_query(&$query) {
|
| 195 |
$query->add_table('biblio', 'vid', 'n', 'vid');
|
| 196 |
$query->add_where('biblio.biblio_type = %d', $this->_tid);
|
| 197 |
}
|
| 198 |
}
|
| 199 |
|
| 200 |
// --------------------------------------------------------------------------
|
| 201 |
// Internal stuff
|
| 202 |
|
| 203 |
/**
|
| 204 |
* Retrieves a specified Biblio type, or all visible Biblio types.
|
| 205 |
*
|
| 206 |
* @param $tid
|
| 207 |
* Optional. The id of the Biblio type to retrieve. If omitted, all
|
| 208 |
* visible Biblio types are returned.
|
| 209 |
*
|
| 210 |
* @return
|
| 211 |
* Type object if a specific $tid was requested, or an array of type objects
|
| 212 |
* otherwise.
|
| 213 |
*/
|
| 214 |
function biblio_type_facet_types($tid = 0) {
|
| 215 |
static $types = NULL;
|
| 216 |
if (!isset($types)) {
|
| 217 |
$types = array();
|
| 218 |
$results = db_query('SELECT tid, name, weight FROM {biblio_types} WHERE tid > 0 AND visible = 1 ORDER BY weight, name', $tid);
|
| 219 |
while ($type = db_fetch_object($results)) {
|
| 220 |
$types[$type->tid] = $type;
|
| 221 |
}
|
| 222 |
}
|
| 223 |
if ($tid == 0) {
|
| 224 |
return $types;
|
| 225 |
}
|
| 226 |
else {
|
| 227 |
return $types[$tid];
|
| 228 |
}
|
| 229 |
}
|
| 230 |
|